From 7f4bc01dbe50dd29858bbc6f9803671166fed725 Mon Sep 17 00:00:00 2001 From: OddlyTimbot Date: Tue, 1 Oct 2024 20:16:45 -0400 Subject: [PATCH] instructions for player death --- README.md | 78 ++++++++++++++++++++- augustgamecourse/scripts/game_controller.gd | 10 +-- 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b3e65c4..410fa41 100644 --- a/README.md +++ b/README.md @@ -1 +1,77 @@ -Please update with readme \ No newline at end of file +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. \ No newline at end of file diff --git a/augustgamecourse/scripts/game_controller.gd b/augustgamecourse/scripts/game_controller.gd index 00fc698..290de5b 100644 --- a/augustgamecourse/scripts/game_controller.gd +++ b/augustgamecourse/scripts/game_controller.gd @@ -18,11 +18,11 @@ func resetPlayer(): player.health = player.max_health 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() + #is the player dead? + if player.health <= 0: + #kill him + playerDeath.emit()