Archived
1
0
Fork 0

refactor: Separate collision check and death timer logic in game loop

This commit is contained in:
Sebastian Benjamin (aider) 2025-06-03 18:41:50 -07:00
parent d71b7ae89d
commit 9598e44c94
2 changed files with 23 additions and 14 deletions

View file

@ -43,12 +43,14 @@ func RespondToInput(lobbyState *MatchState, messages []runtime.MatchData, logger
// Check if the input is within the grace window
if update.Tick < tick && tick-update.Tick <= GRACE_WINDOW_TICKS {
// Check the player's death state for all subsequent ticks
// Check the player's collision state for all subsequent ticks
playerSurvives := true
for t := update.Tick; t < tick; t++ {
if lobbyState.presences[msg.GetSessionId()].stageState.CheckDeathState(t) == PLAYER_ALIVE {
// Set a flag to cancel death
if lobbyState.presences[msg.GetSessionId()].stageState.CheckCollisionState(t) == PLAYER_DEAD {
playerSurvives = false
// Set a flag to cancel death if the player survives all ticks
if playerSurvives {
lobbyState.presences[msg.GetSessionId()].stageState.cancelDeath = true
break
}
}
}

View file

@ -64,29 +64,36 @@ func (s *PlayerStageState) DeleteBulletsBeyondKillBoundary(tick int64) {
}
func (s *PlayerStageState) CheckDeathState(tick int64) int {
// If the player is dead. Decrement the death timer
func (s *PlayerStageState) UpdateDeathTimer() {
// If the player is dead, decrement the death timer
if s.deathTimer >= 0 {
s.deathTimer -= 1
}
if s.deathTimer == PLAYER_DEATH_RESET { // If the player's death timer has run out, reset them. 0 is a special deathTimer tick that indicates reset to the clients.
if s.deathTimer == PLAYER_DEATH_RESET {
s.hitCol.UpdatePos(STAGE_WIDTH*0.5, STAGE_HEIGHT-STAGE_HEIGHT*0.1)
s.grazeCol.UpdatePos(STAGE_WIDTH*0.5, STAGE_HEIGHT-STAGE_HEIGHT*0.1)
s.updatePlayerPos = true
} else if s.deathTimer == PLAYER_ALIVE { // If the player is alive, check if the player collided with a bullet and kill them if so
} else if s.deathTimer == PLAYER_ALIVE {
if slices.ContainsFunc(s.bullets, func(b *ffi.Bullet) bool {
return b.CollidesWith(s.hitCol, tick)
}) {
s.deathTimer = PLAYER_DEATH_TIMER_MAX
} else if slices.ContainsFunc(s.bullets, func(b *ffi.Bullet) bool { // Otherwise, check the graze col and increment the graze and score
}
}
}
func (s *PlayerStageState) CheckCollisionState(tick int64) int {
if slices.ContainsFunc(s.bullets, func(b *ffi.Bullet) bool {
return b.CollidesWith(s.hitCol, tick)
}) {
return PLAYER_DEAD
} else if slices.ContainsFunc(s.bullets, func(b *ffi.Bullet) bool {
return b.CollidesWith(s.grazeCol, tick)
}) {
s.graze += GRAZE_ADDITION_MULTIPLIER
}
}
return s.deathTimer
return PLAYER_ALIVE
}
func (s *PlayerStageState) AddBullet(b *ffi.Bullet) {