player health and damage by enemies
authorEduardo <[email protected]>
Thu, 9 May 2024 14:18:11 +0000 (16:18 +0200)
committerEduardo <[email protected]>
Thu, 9 May 2024 14:18:11 +0000 (16:18 +0200)
components/base_enemy/base_enemy.gd
components/player/Player.tscn
components/player/player.gd
project.godot
scenes/base_level/base_level.gd

index b39fa54f1e21cbee30fa8da3ef1a114a3d97178c..38f60da2ef7c4510dfef3a5c1ff4233a91f13252 100644 (file)
@@ -10,6 +10,7 @@ signal enemy_died
 func _physics_process(_delta):
        velocity = position.direction_to(player.position) * SPEED
        move_and_slide()
+       detect_collision()
 
 
 func take_damage(amount: float):
@@ -22,3 +23,10 @@ func die():
        # TODO: animation to die
        emit_signal("enemy_died")
        queue_free()
+
+func detect_collision():
+       var collision = get_last_slide_collision()
+       if collision:
+               var collider = collision.get_collider()
+               if collider.name.to_lower().contains("player"):
+                       collider.take_damage(1)
index d82a0b5a9b2c1cf49c8b141c05351febc55c742b..81becf4d31392adc554f8885afbb5e7ea76165f9 100644 (file)
@@ -5,6 +5,7 @@
 [sub_resource type="CircleShape2D" id="CircleShape2D_oxutk"]
 
 [node name="Player" type="CharacterBody2D"]
+collision_mask = 3
 motion_mode = 1
 script = ExtResource("1_twtc0")
 
@@ -15,3 +16,13 @@ polygon = PackedVector2Array(-0.267257, -4.94762, -0.367478, 0, -0.267257, 4.947
 
 [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
 shape = SubResource("CircleShape2D_oxutk")
+
+[node name="LabelHealth" type="Label" parent="."]
+offset_top = -23.0
+offset_right = 21.0
+text = "10"
+horizontal_alignment = 1
+
+[node name="InvulTimer" type="Timer" parent="."]
+wait_time = 0.8
+one_shot = true
index 34443ef4c09505690c8a75a015d7455a851ff2b3..d2ec62044f2e5d910981d7978d8c485b022624d9 100644 (file)
@@ -2,9 +2,16 @@ extends CharacterBody2D
 
 
 const SPEED = 300.0
+const MAX_HEALTH = 10
+var health = 10
 var fire_timer: Timer
 var nearest_enemy: Node2D
 @export var Enemies: Node
+@onready var label_health: Label = $LabelHealth
+@onready var invul_timer: Timer = $InvulTimer
+
+signal player_dead
+
 
 func _init():
        fire_timer = Timer.new()
@@ -15,6 +22,10 @@ func _init():
        add_child(fire_timer)
 
 
+func _ready():
+       label_health.text = str(MAX_HEALTH)
+
+
 func _process(delta):
        var nearest_node: Node2D
 
@@ -57,3 +68,13 @@ func on_fire_timer_timeout():
                bullet_instance.set_target(nearest_enemy)
 
 
+func take_damage(amount: int):
+       if invul_timer.is_stopped():
+               invul_timer.start()
+               
+               health -= amount
+               label_health.text = str(health)
+               
+               if health <= 0:
+                       get_tree().paused = true
+                       emit_signal("player_dead")
index 88b84ad25cfc15d2dc93083c774c484ccafd2922..658a9dc1877416007bd4d92b351aa0bf38212d64 100644 (file)
@@ -88,6 +88,7 @@ ui_down={
 
 [layer_names]
 
+2d_physics/layer_1="Player"
 2d_physics/layer_2="Enemy Layer"
 2d_physics/layer_3="Bullets"
 2d_physics/layer_10="KillZone"
index fd2bbee4337f0329710cbfdb05cf8771c08b4bb4..7ba92151d4f4bd63640e33750a193164674b0f3b 100644 (file)
@@ -1,16 +1,5 @@
 extends Node2D
 
-
-# Called when the node enters the scene tree for the first time.
-func _ready():
-       pass # Replace with function body.
-
-
-# Called every frame. 'delta' is the elapsed time since the previous frame.
-func _process(delta):
-       pass
-
-
 # to remove stray bullets mainly
 func _on_kill_zone_area_entered(area: Area2D):
        area.queue_free()