extends Node3D
class_name Move

@onready var character = (get_owner() as Character)
@onready var status = (%Status as Status)
@onready var buff = (%Buff as Buff)

var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var velocity_cache : Vector3

func _process(delta):
	if Global.is_level_loading_contorl:return
	if status.is_pause:return
	var is_hit_floor = update_on_floor(delta)
	update_move(delta)
	update_speed_y(delta)
	character.move_and_slide()
	status.velocity_change = character.velocity.abs() - velocity_cache
	velocity_cache = character.velocity.abs()
	if character.velocity:
		SignalManager.character_pos_changed.emit(character.id(),character.pos())
		SignalManager.character_ui_pos_changed.emit(character.id(),character.ui_pos())
		SignalManager.character_status_changed.emit(status.id,"pos2d",character.pos2D())
	if is_hit_floor:
		SignalManager.character_hit_floor.emit(character.id(),character.pos())
	update_deformation(delta)

func update_on_floor(delta) -> bool:
	var is_on_floor = character.is_on_floor()
	var is_hit = not status.is_on_floor and is_on_floor
	status.is_on_floor = is_on_floor
	if status.is_on_floor and not status.is_jumping:
		status.is_jumped = false
	return is_hit
	
func update_speed_y(delta):
	var has_y_speed = status.skill_float_speed != 0 or status.hit_up_speed != 0
	var is_on_stair = character.is_on_stair()
	if has_y_speed:
		character.velocity.y = status.skill_float_speed + status.hit_up_speed
	else:
		if is_on_stair:
			character.velocity.y = status.cfg.move.speed * 0.75
		else:
			if not status.is_on_floor:
				character.velocity.y -= gravity * delta * status.cfg.move.gravity_scale
	status.speed_y = character.velocity.y

func update_move(delta):
	var move_velocity = status.move_dir.normalized() * (status.cfg.move.speed * (1 + status.speed_up_rate))
	var skill_velocity = status.skill_dir * status.skill_move_speed
	var hit_back_velocity = status.hit_back_dir * status.hit_back_speed
	move_velocity += skill_velocity + hit_back_velocity
	
	if move_velocity.length() == 0:
		move_velocity = Vector2(character.velocity.x,character.velocity.z);
		if status.is_on_floor:
			move_velocity = Util.vector_reduce(move_velocity,Setting.drag_ground*delta)
		else:
			move_velocity = Util.vector_reduce(move_velocity,Setting.drag_air*delta)
	else:
		move_velocity.x = move_velocity.x
		move_velocity.y = move_velocity.y
		
	character.velocity.x = move_velocity.x
	character.velocity.z = move_velocity.y
		
	status.speed_xz = Vector2(character.velocity.x,character.velocity.z).length()
	if status.is_free_turn and status.move_dir.x != 0:
		status.is_right = status.move_dir.x > 0

func update_deformation(delta):
	var velocity_change_x = status.velocity_change.x
	var velocity_change_y = status.velocity_change.y + status.velocity_change.z
	var dir_x = 1 if velocity_change_x > 1 or velocity_change_y < -1 else 0
	var dir_y = 1 if velocity_change_y > 1 or velocity_change_x < -1 else 0
	if dir_x or dir_y:
		status.deformation_dir=Vector2(dir_x,dir_y)
		character.add_buff("deformation",0.2)
	
func jump():
	character.velocity.y = status.cfg.move.jump_velocity
	status.is_jumped = true
	status.trigger_jump = true
	buff.add_buff("jumping",0.1)
