extends CharacterBody2D

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


# 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 move(motion: Vector3, mod: float) -> 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 handle_damage(dmg: int, src: Node) -> void:
	if owner:
		owner.handle_damage(dmg, src)

func duplicate_init() -> void:
	var sight = $Sight
	remove_child(sight)
	sight.queue_free()
