extends AbstractPredator2D

# TODO: attacking logic + behaviour
# TODO: movement is buged (seems to not move/teleport somewhat
# TODO: mirroring (thought, extracct that to general function/resource?

@onready var sprite = $AnimatedSprite2D
@onready var fsm = $StateMachine

@export var speed = 0.8
var desired_rotation: float = self.rotation

func _ready() -> void:
	health = maxHealth
	sprite.play("Healthy")
	

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void: 
	# smoothly rotate
	if self.rotation != self.desired_rotation: 
		self.rotation = lerp_angle(self.rotation, self.desired_rotation, clampf(4 * delta, 0, 1))

func _physics_process(delta: float) -> void:
	pass

# FIXME: (also goes for prey) this is framerate dependent
func move(motion: Vector3, mod: float = 1.0) -> void:
	move_and_collide(Vector2(motion.x, motion.y).normalized() * self.speed * mod)  # Moves along the given vector
	self.desired_rotation = atan2(motion.y, motion.x)	
		
	# Apply boundary to new position
	position = GameManager.get_boundaried_position(position)


func _on_sight_body_entered(body: Node2D) -> void:
	if body.is_in_group("prey") or (health < maxHealth and body.is_in_group("player")):
		fsm.transition_to_next_state(fsm.States.HUNTING, {"target": body})
