refactor: simplify input handling in game loop by removing redundant code
This commit is contained in:
parent
99e6ce9699
commit
1e6d809382
1 changed files with 8 additions and 9 deletions
|
|
@ -24,8 +24,6 @@ func CheckMatchTerminate(lobbyState *MatchState, logger *runtime.Logger) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const GRACE_WINDOW_TICKS = 15
|
|
||||||
|
|
||||||
func RespondToInput(lobbyState *MatchState, messages []runtime.MatchData, logger *runtime.Logger, tick int64, dispatcher *runtime.MatchDispatcher) {
|
func RespondToInput(lobbyState *MatchState, messages []runtime.MatchData, logger *runtime.Logger, tick int64, dispatcher *runtime.MatchDispatcher) {
|
||||||
for _, msg := range messages {
|
for _, msg := range messages {
|
||||||
_, exists := lobbyState.presences[msg.GetSessionId()]
|
_, exists := lobbyState.presences[msg.GetSessionId()]
|
||||||
|
|
@ -34,10 +32,16 @@ func RespondToInput(lobbyState *MatchState, messages []runtime.MatchData, logger
|
||||||
continue
|
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
|
// 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
|
// 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
|
// Apply the input to the player's state
|
||||||
lobbyState.presences[msg.GetSessionId()].stageState.BoundsCheckedMove(update.X, update.Y)
|
lobbyState.presences[msg.GetSessionId()].stageState.BoundsCheckedMove(update.X, update.Y)
|
||||||
// Check if the player survives after applying the input
|
// 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)
|
lobbyState.presences[msg.GetSessionId()].stageState.BoundsCheckedMove(update.X, update.Y)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Reference in a new issue