class_name Player
extends CharacterBody2D

signal move

#nodes
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
@onready var hit_animation_player: AnimationPlayer = $HitAnimationPlayer
@onready var camera_center: Marker2D = $Markers/CameraCenter
@onready var center: Marker2D = $Markers/Center

#State Vars
const  GRAVITY: int = 700
@export var speed: float = 1300
@export var maxSpeed: float = 300
@export var maxAirSpeed: float = 350
@export var friction: int = 3000
@export var airFriction: int = 1400
@export var jumpHight: float = -300
@export var maxJumpCount: int = 2
@export var dashSpeed: float = 600
@export var wallPushBack: float = 200
var canDash: bool = true

var deathEffect: PackedScene = preload("uid://dm4oirxl6bpnn")
var camreaCenterPosition: Vector2
var swordHitBoxXPos: float
var dashTime: float = 0.1

func _ready() -> void:
	#connects the signal from scene manager to on spawn
	#sets the things to make player movement better
	set_floor_constant_speed_enabled(true)
	floor_snap_length = 2

@warning_ignore("unused_parameter")
func _physics_process(delta: float) -> void:
	#calls move and slide so the state scripts don't have to cause that causes bugs
	move_and_slide()
	#print(velocity.x)
	#emits move purly for jump state cause that is dependent on exact move and slide in the code
	move.emit()
	
	get_dash()
	
	#kills the player when they get too low
	#if global_position.y > 200:
		#player_death()

#sets the players pos to the spawn pos so the player statrs on the correct door to the level
func on_spawn(spawnPosition: Vector2):
	global_position = spawnPosition

func player_death() -> void:
	Util.add_effect_to_self(self, deathEffect)
	queue_free()

func _on_hurt_box_area_entered(_area: Area2D) -> void:
	player_death()
	GameManager.reset_level()

func hurt(enemy: Node2D) -> void:
	PlayerManager.decress_health(enemy.damage)
	hit_animation_player.play("hit")
	
	if PlayerManager.currenthealth == 0:
		player_death()

func get_dash() -> void:
	if is_on_floor():
		await get_tree().create_timer(dashTime).timeout
		if is_on_floor():
			canDash = true
	if is_on_wall():
		canDash = true
