extends Enemy
const PhysicsFunc = preload("res://src/Utilities/Physic/PhysicsFunc.gd")

export var invincible := false
export var speed := 80
export var acceleration := 80

func _ready() -> void:
	$StompDetector.monitoring = !invincible


# TODO Only moves when on screen
func _physics_process(delta: float) -> void:
	velocity.y += _gravity * delta
	var player_direction := player_on_floor_direction()
	if(player_direction != 0):
		velocity.x = PhysicsFunc.two_step_euler(velocity.x, acceleration * player_direction,
		mass, delta)
		velocity.x =  clamp(velocity.x, -speed, speed)
	else:
		velocity.x = PhysicsFunc.two_step_euler(velocity.x, acceleration * -sign(velocity.x),
		mass, delta)


	velocity.y = move_and_slide(velocity, FLOOR_NORMAL).y

# TODO Detects player over gaps
func player_on_floor_direction() -> float:
	for raycast in $LedgeDetectorRays.get_children():
		if raycast.is_colliding():
			var collider = raycast.get_collider()
			if collider.is_in_group("player"):
				return sign(collider.position.x - self.position.x)
	return 0.0
