TowerDefense/Enemies/enemy.gd
2025-09-03 15:05:23 +02:00

79 lines
1.7 KiB
GDScript

extends CharacterBody3D
class_name Enemy
signal died
@onready var death_vfx_packed : PackedScene = preload("res://VFX/death_particles.tscn")
@onready var sad_vfx_packed : PackedScene = preload("res://VFX/sad_particles.tscn")
@export var speed : int = 1
@export var base_attack_damage : int = 1
@export var max_life : int = 5
@export var money : int = 5
var health : int = 0 :
set(value):
health = value
if health >= max_life and is_alive:
death()
@onready var Path : PathFollow3D = get_parent()
@onready var healthBar : ProgressBar = $HealthBar3D/SubViewport/HealthBar2D
var attack_target : Node3D :
set(value):
attack_target = value
if value:
movement_stop = true
else:
movement_stop = false
var is_alive : bool = true
var movement_stop : bool = false
func _ready() -> void:
healthBar.value = health
healthBar.max_value = max_life
func _physics_process(delta: float) -> void:
if !is_alive:
return
if not movement_stop:
Path.set_progress(Path.get_progress() + speed * delta)
if attack_target:
attack()
func take_damage(damage : int) -> void:
health += damage
healthBar.visible = true
healthBar.value = health
func attack():
if $AttackCooldown.is_stopped():
$AttackCooldown.start()
attack_target.take_damage(base_attack_damage)
func death() -> void:
is_alive = false
died.emit()
EventBus.money_received.emit(money)
$Sprite3D.visible = false
var death_vfx : CPUParticles3D = death_vfx_packed.instantiate()
get_tree().current_scene.add_child(death_vfx)
death_vfx.global_position = global_position
Path.queue_free()
func _on_attack_range_body_entered(body: Node3D) -> void:
if body is TheCube:
attack_target = body
func _on_attack_range_body_exited(body: Node3D) -> void:
if body is TheCube:
attack_target = null