extends CharacterBody2D


const JUMP_VELOCITY = -230.0
signal gameover

@onready var alive = true
var state = "main-menu"

func _ready() -> void:
	%Skin.change_to(0)
	$EnteConstantSprite/EnteColormask.modulate = Color.from_hsv(0.166666667,1,1)
	
func die():
	print("die")
	gameover.emit()
	alive = false

func start():
	global_position = Vector2(21,91)
	velocity = Vector2(0,0)
	alive = true
	
func _physics_process(delta: float) -> void:
	if not is_on_floor():
		velocity += get_gravity() * delta

	if Input.is_action_pressed("jump") and is_on_floor() and alive and $"/root/Global".gamerunning:
		velocity.y = JUMP_VELOCITY
		$Jump.play()
	move_and_slide()

func _process(_delta: float) -> void:
	if global_position.x < 20 or global_position.y > 100:
		if alive:
			die()
			
	if $"/root/Global".gamerunning:
		state = "game"

	if state == "main-menu"	:
		$AnimationPlayer.play("idle")
	elif state == "game":
		if not is_on_floor():
			$AnimationPlayer.play("fly")
		elif not alive:
			$AnimationPlayer.play("die")
		else:
			$AnimationPlayer.play("walk")


func _color_slider_changed(value) -> void:
	$EnteConstantSprite/EnteColormask.modulate.h = value*0.01


func _on_ente_sprite_frame_changed() -> void:
	$EnteConstantSprite/EnteColormask.frame = $EnteConstantSprite.frame
	$EnteConstantSprite/Skin.frame = $EnteConstantSprite.frame

func get_duck_color():
	return $EnteConstantSprite/EnteColormask.modulate.h
