extends Player

export var score := 100


func _ready() -> void:
	set_physics_process(false)


# TODO adapt to groups
func _on_StompDetector_body_entered(body: Node) -> void:
	if body.global_position.y > get_node("StompDetector").global_position.y:
		return
	get_node("EnemyBody").disabled = true
	die()

func _physics_process(delta: float) -> void:
	velocity.y += _gravity * delta
	velocity.x = 80 * player_on_floor_direction()
	velocity.y = move_and_slide(velocity, FLOOR_NORMAL).y

# TODO Detects player over gaps
func player_on_floor_direction():
	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

func die() -> void:
	queue_free()
	PlayerData.score += score

func _on_EnemySkin_area_entered(area:Area2D) -> void:
	if area.is_in_group("harmful"):
		get_node("EnemyBody").disabled = true
		die()
