This commit is contained in:
Marek Le
2026-03-29 22:58:23 +02:00
parent b9ed399d34
commit 80a65fa555
19 changed files with 217 additions and 58 deletions

View File

@@ -1,13 +1,14 @@
extends Node
const SPEED := 3.0
const LEASH_RANGE := 15.0
const ATTACK_RANGE := 2.0
const REGEN_FAST := 0.10
const REGEN_SLOW := 0.01
@onready var enemy: CharacterBody3D = get_parent()
@onready var nav_agent: NavigationAgent3D = get_parent().get_node("NavigationAgent3D")
func _physics_process(_delta: float) -> void:
func _physics_process(delta: float) -> void:
match enemy.state:
enemy.State.IDLE:
enemy.velocity.x = 0
@@ -15,17 +16,12 @@ func _physics_process(_delta: float) -> void:
enemy.State.CHASE:
_chase()
enemy.State.RETURN:
_return_to_spawn()
_return_to_spawn(delta)
func _chase() -> void:
if not is_instance_valid(enemy.target):
enemy.state = enemy.State.RETURN
return
var dist_to_spawn := enemy.global_position.distance_to(enemy.spawn_position)
if dist_to_spawn > LEASH_RANGE:
enemy.state = enemy.State.RETURN
enemy.target = null
return
var dist_to_target := enemy.global_position.distance_to(enemy.target.global_position)
if dist_to_target <= ATTACK_RANGE:
enemy.state = enemy.State.ATTACK
@@ -37,7 +33,7 @@ func _chase() -> void:
enemy.velocity.x = direction.x * SPEED
enemy.velocity.z = direction.z * SPEED
func _return_to_spawn() -> void:
func _return_to_spawn(delta: float) -> void:
var dist := enemy.global_position.distance_to(enemy.spawn_position)
if dist < 1.0:
enemy.state = enemy.State.IDLE
@@ -50,3 +46,13 @@ func _return_to_spawn() -> void:
direction.y = 0
enemy.velocity.x = direction.x * SPEED
enemy.velocity.z = direction.z * SPEED
_regenerate(delta)
func _regenerate(delta: float) -> void:
var health: Node = enemy.get_node("Health")
if health.current_health < health.max_health:
var rate: float = REGEN_FAST if health.current_health < health.max_health else REGEN_SLOW
if health.current_health >= health.max_health * 0.99:
rate = REGEN_SLOW
health.current_health = min(health.current_health + health.max_health * rate * delta, health.max_health)
EventBus.health_changed.emit(enemy, health.current_health, health.max_health)