From 1e6d809382daebfe258c241c281c612415539644 Mon Sep 17 00:00:00 2001 From: Sebastian Benjamin Date: Tue, 3 Jun 2025 18:23:42 -0700 Subject: [PATCH] refactor: simplify input handling in game loop by removing redundant code --- server/game-modes/battle-royale/game-loop.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/server/game-modes/battle-royale/game-loop.go b/server/game-modes/battle-royale/game-loop.go index 2c24119..2a16eb0 100644 --- a/server/game-modes/battle-royale/game-loop.go +++ b/server/game-modes/battle-royale/game-loop.go @@ -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) }