extends Player

export var score := 100

onready var left_src = $SlopeRaycastLeft
onready var right_src = $SlopeRaycastRight
onready var left_wrc = $WallRaycastLeft
onready var right_wrc = $WallRaycastRight

var time = 0
var snap = Vector2.DOWN * 24

func _ready() -> void:
	velocity.x = -120


# 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:
	# rotation
	var movement = max(0,sign(sin(time*15)))
	if(left_src.is_colliding() && right_src.is_colliding() && !left_wrc.is_colliding() && !right_wrc.is_colliding()):
		pass
	elif(left_wrc.is_colliding() || (!right_src.is_colliding() && left_src.is_colliding())):
		rotation += delta * 7 * movement
	else:
		rotation += sign(velocity.x) * delta * 7 * movement
	
	# velocity
	var v = Vector2(velocity.x * movement, 0)
	time += delta
	move_and_slide_with_snap(v.rotated(rotation), snap.rotated(rotation), FLOOR_NORMAL, false, 4, PI)


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

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