Week 5 Challenge - Player Death Animation First, we need to make a slight update to the logic in the game controller. ## Game Controller This change ensures that our signal to kill the player can ONLY happen once, by checking that the health is above zero before issuing the signal. ``` func playerDamage(): #one can not kill what is dead.... if player.health >0: player.health -= 20 #is the player dead? if player.health <= 0: #kill him playerDeath.emit() ``` ## Scene Manager In the scene_manager, we have created a connection that listens for the game controller issuing the player death command: `GameController.playerDeath.connect(killPlayer)` We must change the `killPlayer` function. It will tell the player to die, but will then start a timer rather than immediatly reloading the scene. ``` func killPlayer(): # tell player to die - starts death animation player.killPlayer() if timer.is_stopped(): timer.start(4) ``` Notice that we only start the timer *if it is not already running*. Otherwise if the scene_manager happens to hear more `playerDeath` signals from the GameController, it will keep restarting the timer. The `resetWorld` function is connected to the `timeout` event of the timer. It looks like this: ``` func resetWorld(): print("screne manager resetting world") GameController.resetPlayer() get_tree().reload_current_scene() ``` ## Player Lastly, the player code needs to ensure that the user can not do anything when their death animation is playing. A simple way to do this is to create a boolean that simply keeps track of whether they are alive or not. `var living = true` We can then update the `killPlayer` function to check if the player is currently living and update the variable before calling the death animation. ``` func killPlayer(): print("kill the player") if living: living = false #play death animation playerSprite.play("death") ``` With the `living` variable set to false, we simply check the variable before doing *anything* in the `_physics_process` function. ``` func _physics_process(delta: float) -> void: if living: # Add the gravity. if not is_on_floor(): velocity += get_gravity() * delta .......and so forth.... ``` When the timer ends and the scene reloads, all of these variables will be reset and the loop begins again.