Archived
1
0
Fork 0

refactor: simplify input handling in game loop by removing redundant code

This commit is contained in:
Sebastian Benjamin 2025-06-03 18:23:42 -07:00 committed by Sebastian Benjamin (aider)
parent 99e6ce9699
commit 1e6d809382

View file

@ -24,8 +24,6 @@ func CheckMatchTerminate(lobbyState *MatchState, logger *runtime.Logger) bool {
return false
}
const GRACE_WINDOW_TICKS = 15
func RespondToInput(lobbyState *MatchState, messages []runtime.MatchData, logger *runtime.Logger, tick int64, dispatcher *runtime.MatchDispatcher) {
for _, msg := range messages {
_, exists := lobbyState.presences[msg.GetSessionId()]
@ -34,10 +32,16 @@ func RespondToInput(lobbyState *MatchState, messages []runtime.MatchData, logger
continue
}
var update ClientUpdate
if err := json.Unmarshal(msg.GetData(), &update); err != nil {
(*logger).Warn("Failed to parse input: %v", err)
continue
}
// Check if the input is within the grace window
if msg.GetTick() < tick && tick-msg.GetTick() <= GRACE_WINDOW_TICKS {
if update.Tick < tick && tick-update.Tick <= GRACE_WINDOW_TICKS {
// Replay the game state for the intervening ticks
for t := msg.GetTick(); t < tick; t++ {
for t := update.Tick; t < tick; t++ {
// Apply the input to the player's state
lobbyState.presences[msg.GetSessionId()].stageState.BoundsCheckedMove(update.X, update.Y)
// Check if the player survives after applying the input
@ -52,11 +56,6 @@ func RespondToInput(lobbyState *MatchState, messages []runtime.MatchData, logger
}
}
}
var update ClientUpdate
if err := json.Unmarshal(msg.GetData(), &update); err != nil {
(*logger).Warn("Failed to parse input: %v", err)
continue
}
lobbyState.presences[msg.GetSessionId()].stageState.BoundsCheckedMove(update.X, update.Y)
}