refactor: Separate collision check and death timer logic in game loop
This commit is contained in:
parent
d71b7ae89d
commit
9598e44c94
2 changed files with 23 additions and 14 deletions
|
|
@ -43,12 +43,14 @@ func RespondToInput(lobbyState *MatchState, messages []runtime.MatchData, logger
|
||||||
|
|
||||||
// Check if the input is within the grace window
|
// Check if the input is within the grace window
|
||||||
if update.Tick < tick && tick-update.Tick <= GRACE_WINDOW_TICKS {
|
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++ {
|
for t := update.Tick; t < tick; t++ {
|
||||||
if lobbyState.presences[msg.GetSessionId()].stageState.CheckDeathState(t) == PLAYER_ALIVE {
|
if lobbyState.presences[msg.GetSessionId()].stageState.CheckCollisionState(t) == PLAYER_DEAD {
|
||||||
// Set a flag to cancel death
|
playerSurvives = false
|
||||||
lobbyState.presences[msg.GetSessionId()].stageState.cancelDeath = true
|
// Set a flag to cancel death if the player survives all ticks
|
||||||
break
|
if playerSurvives {
|
||||||
|
lobbyState.presences[msg.GetSessionId()].stageState.cancelDeath = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -64,29 +64,36 @@ func (s *PlayerStageState) DeleteBulletsBeyondKillBoundary(tick int64) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PlayerStageState) CheckDeathState(tick int64) int {
|
func (s *PlayerStageState) UpdateDeathTimer() {
|
||||||
// If the player is dead. Decrement the death timer
|
// If the player is dead, decrement the death timer
|
||||||
if s.deathTimer >= 0 {
|
if s.deathTimer >= 0 {
|
||||||
s.deathTimer -= 1
|
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.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.grazeCol.UpdatePos(STAGE_WIDTH*0.5, STAGE_HEIGHT-STAGE_HEIGHT*0.1)
|
||||||
s.updatePlayerPos = true
|
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 {
|
if slices.ContainsFunc(s.bullets, func(b *ffi.Bullet) bool {
|
||||||
return b.CollidesWith(s.hitCol, tick)
|
return b.CollidesWith(s.hitCol, tick)
|
||||||
}) {
|
}) {
|
||||||
s.deathTimer = PLAYER_DEATH_TIMER_MAX
|
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
|
|
||||||
return b.CollidesWith(s.grazeCol, tick)
|
|
||||||
}) {
|
|
||||||
s.graze += GRAZE_ADDITION_MULTIPLIER
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return s.deathTimer
|
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 PLAYER_ALIVE
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PlayerStageState) AddBullet(b *ffi.Bullet) {
|
func (s *PlayerStageState) AddBullet(b *ffi.Bullet) {
|
||||||
|
|
|
||||||
Reference in a new issue