func _physics_process(_delta):
velocity = position.direction_to(player.position) * SPEED
move_and_slide()
+ detect_collision()
func take_damage(amount: float):
# 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)
[sub_resource type="CircleShape2D" id="CircleShape2D_oxutk"]
[node name="Player" type="CharacterBody2D"]
+collision_mask = 3
motion_mode = 1
script = ExtResource("1_twtc0")
[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
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()
add_child(fire_timer)
+func _ready():
+ label_health.text = str(MAX_HEALTH)
+
+
func _process(delta):
var nearest_node: Node2D
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")
[layer_names]
+2d_physics/layer_1="Player"
2d_physics/layer_2="Enemy Layer"
2d_physics/layer_3="Bullets"
2d_physics/layer_10="KillZone"
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()