Compare commits
No commits in common. "a6fc6acc12bc10ca7830bf08ac85a3cac8cb23c6" and "5bd5a35d881534a08f05df173bc0f599a819ec04" have entirely different histories.
a6fc6acc12
...
5bd5a35d88
78
README.md
78
README.md
@ -1,77 +1 @@
|
|||||||
Week 5 Challenge - Player Death Animation
|
Please update with readme
|
||||||
|
|
||||||
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.
|
|
@ -18,11 +18,10 @@ func resetPlayer():
|
|||||||
player.health = player.max_health
|
player.health = player.max_health
|
||||||
|
|
||||||
func playerDamage():
|
func playerDamage():
|
||||||
#one can not kill what is dead....
|
player.health -= 20
|
||||||
if player.health >0:
|
print("Player health is : "+str(player.health) )
|
||||||
player.health -= 20
|
#is the player dead?
|
||||||
#is the player dead?
|
if player.health <= 0:
|
||||||
if player.health <= 0:
|
#kill him
|
||||||
#kill him
|
playerDeath.emit()
|
||||||
playerDeath.emit()
|
|
||||||
|
|
||||||
|
@ -6,8 +6,6 @@ const JUMP_VELOCITY = -400.0
|
|||||||
const PUSH_FORCE = 700
|
const PUSH_FORCE = 700
|
||||||
|
|
||||||
var faceLeft = false
|
var faceLeft = false
|
||||||
var living = true
|
|
||||||
|
|
||||||
# can i push right or left
|
# can i push right or left
|
||||||
var pushLeftEnabled = false
|
var pushLeftEnabled = false
|
||||||
var pushRightEnabled = false
|
var pushRightEnabled = false
|
||||||
@ -23,82 +21,87 @@ var bullet = preload("res://scenes/bullet.tscn")
|
|||||||
|
|
||||||
func killPlayer():
|
func killPlayer():
|
||||||
print("kill the player")
|
print("kill the player")
|
||||||
if living:
|
#play death animation
|
||||||
living = false
|
playerSprite.play("death")
|
||||||
#play death animation
|
|
||||||
playerSprite.play("death")
|
|
||||||
|
|
||||||
func _physics_process(delta: float) -> void:
|
func _physics_process(delta: float) -> void:
|
||||||
if living:
|
# Add the gravity.
|
||||||
# Add the gravity.
|
if not is_on_floor():
|
||||||
if not is_on_floor():
|
velocity += get_gravity() * delta
|
||||||
velocity += get_gravity() * delta
|
|
||||||
|
|
||||||
# Handle jump.
|
# Handle jump.
|
||||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||||
velocity.y = JUMP_VELOCITY
|
velocity.y = JUMP_VELOCITY
|
||||||
|
|
||||||
|
if Input.is_action_just_pressed("Shove") && pushLeftEnabled && faceLeft:
|
||||||
|
print("shove a box on the left")
|
||||||
|
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 1.5)
|
||||||
|
pushLeftEnabled = false
|
||||||
|
|
||||||
if Input.is_action_just_pressed("Shove") && pushLeftEnabled && faceLeft:
|
if Input.is_action_just_pressed("Shove") && pushRightEnabled && not faceLeft:
|
||||||
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 1.5)
|
print("shove a box on the right")
|
||||||
pushLeftEnabled = false
|
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 1.5)
|
||||||
|
pushRightEnabled=false
|
||||||
if Input.is_action_just_pressed("Shove") && pushRightEnabled && not faceLeft:
|
|
||||||
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 1.5)
|
if Input.is_action_just_pressed("shoot"):
|
||||||
pushRightEnabled=false
|
|
||||||
|
|
||||||
if Input.is_action_just_pressed("shoot"):
|
|
||||||
if faceLeft:
|
|
||||||
# mybullet.setSpeed(-700)
|
|
||||||
#mybullet.transform = marker_left.global_transform
|
|
||||||
%SceneManager.placeBullet(-700, marker_left.global_transform)
|
|
||||||
else:
|
|
||||||
#mybullet.transform = marker_right.global_transform
|
|
||||||
%SceneManager.placeBullet(700, marker_right.global_transform)
|
|
||||||
|
|
||||||
# Get the input direction and handle the movement/deceleration.
|
|
||||||
# As good practice, you should replace UI actions with custom gameplay actions.
|
|
||||||
var direction := Input.get_axis("ui_left", "ui_right")
|
|
||||||
if direction <0:
|
|
||||||
faceLeft = true
|
|
||||||
playerSprite.flip_h = true
|
|
||||||
if direction >0:
|
|
||||||
faceLeft = false
|
|
||||||
playerSprite.flip_h = false
|
|
||||||
|
|
||||||
if direction:
|
|
||||||
velocity.x = direction * SPEED
|
|
||||||
else:
|
|
||||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
||||||
|
|
||||||
if is_on_floor():
|
if faceLeft:
|
||||||
if direction == 0:
|
print("shoot left")
|
||||||
playerSprite.play("idle")
|
# mybullet.setSpeed(-700)
|
||||||
else:
|
#mybullet.transform = marker_left.global_transform
|
||||||
playerSprite.play("run")
|
%SceneManager.placeBullet(-700, marker_left.global_transform)
|
||||||
else:
|
else:
|
||||||
playerSprite.play("jump")
|
print("shoot right")
|
||||||
|
#mybullet.transform = marker_right.global_transform
|
||||||
move_and_slide()
|
%SceneManager.placeBullet(700, marker_right.global_transform)
|
||||||
for i in get_slide_collision_count():
|
|
||||||
var c = get_slide_collision(i)
|
# Get the input direction and handle the movement/deceleration.
|
||||||
if c.get_collider() is RigidBody2D:
|
# As good practice, you should replace UI actions with custom gameplay actions.
|
||||||
c.get_collider().apply_central_impulse(-c.get_normal()* 100)
|
var direction := Input.get_axis("ui_left", "ui_right")
|
||||||
|
if direction <0:
|
||||||
|
faceLeft = true
|
||||||
|
playerSprite.flip_h = true
|
||||||
|
if direction >0:
|
||||||
|
faceLeft = false
|
||||||
|
playerSprite.flip_h = false
|
||||||
|
|
||||||
if left_ray.is_colliding():
|
if direction:
|
||||||
var collider = left_ray.get_collider()
|
velocity.x = direction * SPEED
|
||||||
if collider is Node:
|
else:
|
||||||
if collider.is_in_group("pushables"):
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||||
pushLeftEnabled = true
|
if is_on_floor():
|
||||||
pushTarget = collider
|
if direction == 0:
|
||||||
|
playerSprite.play("idle")
|
||||||
else:
|
else:
|
||||||
#do something else
|
playerSprite.play("run")
|
||||||
pushLeftEnabled = false
|
else:
|
||||||
|
playerSprite.play("jump")
|
||||||
|
|
||||||
if right_ray.is_colliding():
|
move_and_slide()
|
||||||
var collider = right_ray.get_collider()
|
for i in get_slide_collision_count():
|
||||||
if collider is Node:
|
var c = get_slide_collision(i)
|
||||||
if collider.is_in_group("pushables"):
|
if c.get_collider() is RigidBody2D:
|
||||||
pushRightEnabled = true
|
c.get_collider().apply_central_impulse(-c.get_normal()* 100)
|
||||||
pushTarget = collider
|
|
||||||
else:
|
if left_ray.is_colliding():
|
||||||
pushRightEnabled = false
|
print("left ray collision")
|
||||||
|
var collider = left_ray.get_collider()
|
||||||
|
if collider is Node:
|
||||||
|
if collider.is_in_group("pushables"):
|
||||||
|
pushLeftEnabled = true
|
||||||
|
pushTarget = collider
|
||||||
|
else:
|
||||||
|
#do something else
|
||||||
|
pushLeftEnabled = false
|
||||||
|
|
||||||
|
if right_ray.is_colliding():
|
||||||
|
print("right ray collision")
|
||||||
|
var collider = right_ray.get_collider()
|
||||||
|
if collider is Node:
|
||||||
|
if collider.is_in_group("pushables"):
|
||||||
|
pushRightEnabled = true
|
||||||
|
pushTarget = collider
|
||||||
|
else:
|
||||||
|
pushRightEnabled = false
|
||||||
|
@ -64,10 +64,9 @@ func crateFactory():
|
|||||||
return myCrate
|
return myCrate
|
||||||
|
|
||||||
func killPlayer():
|
func killPlayer():
|
||||||
# tell player to die - starts death animation
|
print("kill the player now")
|
||||||
player.killPlayer()
|
player.killPlayer()
|
||||||
if timer.is_stopped():
|
timer.start(3)
|
||||||
timer.start(4)
|
|
||||||
|
|
||||||
|
|
||||||
func resetWorld():
|
func resetWorld():
|
||||||
|
Loading…
Reference in New Issue
Block a user