class_name Enemy extends Unit @export var player: Player @export var chasing_range := 30.0 @export var attack_range := 3.0 @export var attacks := ["Unarmed_Melee_Attack_Punch_A", "Unarmed_Melee_Attack_Punch_B", "Unarmed_Melee_Attack_Kick"] @onready var nav_agent: NavigationAgent3D = $NavigationAgent3D func _ready() -> void: health = maximum_health stamina = maximum_stamina state_changed.connect(_on_state_changed) func _physics_process(_delta: float) -> void: if state == States.dead or player == null: return if player.state == States.dead: state = States.idle return var distance: float = global_position.distance_to(player.global_position) if distance > chasing_range: state = States.idle elif distance <= chasing_range and distance >= attack_range: state = States.chasing elif distance <= attack_range: state = States.attacking nav_agent.set_target_position(player.position) if nav_agent.is_navigation_finished() or nav_agent.is_target_reached(): return move_and_slide() func _on_state_changed(_old_state: States, new_state: States) -> void: #if new_state == _old_state: #return name_changed.emit(unit_name, States.keys()[new_state]) print("Enemy changed state from " + str(_old_state) + " to " + str(new_state)) match new_state: #States.idle: # TODO: Random movement #_on_velocity_computed(Vector3.ZERO) States.chasing: print("Player position: ", player.position) var next_path_position: Vector3 = nav_agent.get_next_path_position() print("Next path position: ", next_path_position) print("Global position: ", global_position) model.look_at(next_path_position) var direction = global_position.direction_to(next_path_position) velocity = Vector3(direction.x, 0, direction.z).normalized() * speed var vy: float = direction.y velocity.y = 0 var vl: Vector3 = velocity * model.transform.basis var iwr := Vector2(vl.x, -vl.z) / speed anim_tree.set("parameters/IWR/blend_position", iwr) velocity.y = vy print("velocity = ", velocity) States.attacking: if enough_stamina_available(attack_cost): use_stamina(attack_cost) if attacks.size() > 0: anim_state.travel(attacks.pick_random()) States.dead: player.gold += 100