Quellcode durchsuchen

Add collision detection allowing player to crash

DricomDragon vor 5 Jahren
Ursprung
Commit
d712690ac1
2 geänderte Dateien mit 11 neuen und 1 gelöschten Zeilen
  1. 9 0
      Player.gd
  2. 2 1
      Player.tscn

+ 9 - 0
Player.gd

@@ -11,6 +11,7 @@ var rayRight
 # Misc
 var cell_size
 var cell_half_size
+var alive = true
 
 # Enum
 const DIR_UP = 0
@@ -48,6 +49,9 @@ func _ready():
 	rayRight = $RayRight
 
 func _physics_process(delta):
+	
+	if !alive:
+		return
 
 	if Input.is_action_just_pressed("ui_left"):
 		dire_delta = -1
@@ -86,6 +90,11 @@ func _physics_process(delta):
 func _on_tween_completed(o, k):
 	is_moving = false
 
+func _on_crash(b):
+	alive = false
+	grid.set_cell(posix - dirx, posiy - diry, 1)
+	grid.set_cell(posix, posiy, 2)
+
 func turn(dir:int):
 	dirx = 0
 	diry = 0

+ 2 - 1
Player.tscn

@@ -16,7 +16,7 @@ __meta__ = {
 "_edit_lock_": true
 }
 
-[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
+[node name="HitBox" type="CollisionShape2D" parent="."]
 shape = SubResource( 1 )
 __meta__ = {
 "_edit_lock_": true
@@ -52,3 +52,4 @@ __meta__ = {
 [node name="Tween" type="Tween" parent="."]
 script = ExtResource( 3 )
 duration = 0.25
+[connection signal="body_entered" from="." to="." method="_on_crash"]