Private
Public Access
1
0

refactor client prediction logic and add tolerance levels; implement restartGame function to reset game state without a full reload
All checks were successful
Dynamic Branch Deploy / build-and-deploy (push) Successful in 8m27s

This commit is contained in:
Sebastian Unterschütz
2026-01-28 12:01:15 +01:00
parent bbe8007b20
commit af08c13255
3 changed files with 50 additions and 15 deletions

View File

@@ -128,25 +128,42 @@ func (g *Game) ReconcileWithServer(serverState game.PlayerState) {
g.correctionX = diffX
g.correctionY = diffY
// Bei sehr kleinen Abweichungen (<2px): Sofort korrigieren um Drift zu vermeiden
if dist < 4.0 { // 2px threshold
// === NEUE KORREKTUR-LOGIK MIT TOLERANZ ===
const (
// Toleranzen für Client-Abweichungen
smallTolerance = 4.0 // 2px - sofort korrigieren
normalTolerance = 225.0 // 15px - akzeptable Abweichung, sanfte Korrektur
largeTolerance = 900.0 // 30px - Warnschwelle, nur noch Monitoring
hugeTolerance = 10000.0 // 100px - kritisch (Teleport/Respawn)
)
if dist < smallTolerance {
// Bei sehr kleinen Abweichungen (<2px): Sofort korrigieren um Drift zu vermeiden
g.predictedX = replayX
g.predictedY = replayY
} else if dist > 100*100 {
// Bei sehr großen Abweichungen (>100px): Sofort korrigieren (Teleport/Respawn)
g.predictedX = replayX
g.predictedY = replayY
g.correctionCount++
} else if dist > 1.0 {
// Bei normalen Abweichungen: Sanfte Interpolation
// Bei 20 TPS: Aggressivere Interpolation
interpFactor := 0.5 // 50% pro Tick
if dist > 50*50 {
interpFactor = 0.8 // 80% bei großen Abweichungen
}
} else if dist < normalTolerance {
// Bei kleinen Abweichungen (2-15px): Sanfte Korrektur, Client hat etwas Spielraum
interpFactor := 0.3 // Nur 30% Korrektur - mehr Client-Freiheit
g.predictedX += diffX * interpFactor
g.predictedY += diffY * interpFactor
g.correctionCount++
} else if dist < largeTolerance {
// Bei mittleren Abweichungen (15-30px): Stärkere Korrektur
interpFactor := 0.5 // 50% Korrektur
g.predictedX += diffX * interpFactor
g.predictedY += diffY * interpFactor
g.correctionCount++
} else if dist < hugeTolerance {
// Bei großen Abweichungen (30-100px): Nur noch Monitoring, KEINE Korrektur mehr
// Der Unterschied ist zu groß geworden - Client-Position wird akzeptiert
// Server überwacht weiterhin, greift aber nicht mehr ein
// (Dies verhindert "Ruckeln" bei starker Latenz)
} else {
// Bei kritischen Abweichungen (>100px): Sofort korrigieren (Teleport/Respawn/Desync)
g.predictedX = replayX
g.predictedY = replayY
g.correctionCount++
}
// Velocity und Ground Status vom Server übernehmen

View File

@@ -817,6 +817,23 @@ function deleteHighscoreCode(index) {
loadMyCodes(); // Reload display
}
// Restart game without reload
function restartGame() {
console.log('🔄 Restarting game...');
// Reset game state
gameStarted = false;
gameStarting = false;
// Return to main menu
setUIState(UIState.MENU);
// Re-enable start buttons
enableStartButtons();
console.log('✅ Game restarted - ready to play again');
}
// Export functions for WASM to call
window.showMenu = showMenu;
window.hideMenu = hideMenu;
@@ -824,6 +841,7 @@ window.updateLeaderboard = updateLeaderboard;
window.showGameOver = showGameOver;
window.onGameStarted = onGameStarted;
window.saveHighscoreCode = saveHighscoreCode;
window.restartGame = restartGame;
// Initialize on load
initWASM();

View File

@@ -277,7 +277,7 @@
Lade Leaderboard...
</div>
<button class="big-btn" onclick="location.reload()" style="background: #ff4444; margin-top: 20px;">NOCHMAL SPIELEN</button>
<button class="big-btn" onclick="restartGame()" style="background: #ff4444; margin-top: 20px;">ERNEUT VERSUCHEN</button>
</div>
</div>