extends AnimatedSprite2D

@onready var player := $".." as Player


# Called when the node enters the scene tree for the first time.
func _ready():
	play("default")

func _process(_delta):
	if player.direction and player.state not in Player.STATES_IMMOBILE:
		flip_h = player.direction < 0

func _on_player_state_change(old:  Player.States, new:  Player.States) -> void:
	match new:
		Player.States.IDLE:
			play("default")
		Player.States.RUN_START:
			play("run_start")
		Player.States.RUN:
			play("run")
		Player.States.ROLL:
			play("roll")
		Player.States.JUMP_START:
			play("jump_start")
		Player.States.JUMP:
			play("jump")
		Player.States.FALL:
			play("fall")
		Player.States.LAND:
			play("land")
		Player.States.DAMAGE:
			play("damage")
		Player.States.RAGDOLL:
			play("knockdown")
		Player.States.BOUNCE:
			play("knockdown")
			if player.is_on_floor():
				frame = 4
		Player.States.LAY:
			play("lay")
		Player.States.POUND:
			play("pound")
			if old == Player.States.POUND_FALL:
				frame = 4
		Player.States.POUND_FALL:
			play("pound_fall")
		Player.States.KICK:
			play("kick")

func _on_animation_finished():
	match animation:
		"run_start":
			player.state = Player.States.RUN
		"jump_start":
			player.state = Player.States.JUMP
		"land", "roll", "damage", "pound", "kick":
			player.state = Player.States.IDLE

func _on_frame_changed() -> void:
	match animation:
		"pound" when frame == 3 and !player.is_on_floor():
			player.state = Player.States.POUND_FALL
		"pound" when frame == 5:
			player.audio_player.stream = player.POUND_SOUND
			player.audio_player.play()
			
			var pound_bullet = player.POUND_BULLET.instantiate()
			pound_bullet.position = player.position
			pound_bullet.flip_h = !flip_h
			player.add_sibling(pound_bullet)
