Add initial logic to handle player collisions

This commit is contained in:
James Skemp 2023-10-10 19:48:26 -05:00
parent 2746e77c0c
commit 9fe331f651
2 changed files with 20 additions and 1 deletions

View File

@ -1,12 +1,15 @@
extends Area2D
signal hit
@export var speed = 400
var screen_size
# Called when the node enters the scene tree for the first time.
func _ready():
screen_size = get_viewport_rect().size
# Hide the player initially.
hide()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
@ -41,3 +44,17 @@ func _process(delta):
elif velocity.y != 0:
$AnimatedSprite2D.animation = "up"
$AnimatedSprite2D.flip_v = velocity.y > 0
func _on_body_entered(body):
# Hide player when hit.
hide()
hit.emit()
# Defer as you can't change physics properties on a physics callback.
# This tells Godot to wait until it's safe to disable it.
$CollisionShape2D.set_deferred("disabled", true)
func start(pos):
position = pos
show()
$CollisionShape2D.disabled = false

View File

@ -46,3 +46,5 @@ animation = &"walk"
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CapsuleShape2D_hn6ot")
[connection signal="body_entered" from="." to="." method="_on_body_entered"]