class_name Flyer
extends CharacterBody2D
# constants
## Maximum angle in degrees that the flyer can tilt.
@export var max_tilt = 45
## Maximum acceleration of the flyer in vertical directions. Measured in pixels per second.
@export var vertical_accelleration = 5
## Maximum acceleration of the flyer in horizontal directions. Measured in pixels per second.
@export var horizontal_accelleration = 15
## Default deceleration of the flyer in vertical directions when no input is given. Measured in pixels per second.
@export var vertical_decelleration = 8
## Default deceleration of the flyer in horizontal directions when no input is given. Measured in pixels per second.
@export var horizontal_decelleration = 20
## Maximum speed of the flyer in vertical directions. Measured in pixels per second.
@export var vertical_max_speed = 300
## Maximum speed of the flyer in horizontal directions. Measured in pixels per second.
@export var horizontal_max_speed = 300
@export var max_hp = 100
@export var weapons: Array[Weapon]
@export var friendly = false
#@export var panAudio = true


# gameplay variables
var tilt
var speed
var hp
var directionInput

func _ready() -> void:
	hp = max_hp
	max_tilt = deg_to_rad(max_tilt)
	directionInput = Vector2(0,0)

func _physics_process(delta: float) -> void:
	# convert control input to movement
	var verticalIn = directionInput.y
	var horizontalIn = directionInput.x
	if verticalIn:
		if (verticalIn / velocity.y) < 0:
			velocity.y = move_toward(velocity.y, vertical_max_speed, verticalIn * (vertical_decelleration))
		else:
			velocity.y = move_toward(velocity.y, sign(verticalIn) * vertical_max_speed, sign(verticalIn) * verticalIn * vertical_accelleration)
	else:
		velocity.y = move_toward(velocity.y, 0, vertical_decelleration)
	if horizontalIn:
		if (horizontalIn / velocity.x) < 0:
			velocity.x = move_toward(velocity.x, horizontal_max_speed, horizontalIn * (horizontal_decelleration))
		else:
			velocity.x = move_toward(velocity.x, sign(horizontalIn) * horizontal_max_speed, sign(horizontalIn) * horizontalIn * horizontal_accelleration)
	else:
		velocity.x = move_toward(velocity.x, 0, horizontal_decelleration)
	
	#print_debug("current velocity: ", velocity, ", current input: ", Vector2(horizontalIn, verticalIn), ", input signs: ", Vector2(sign(horizontalIn), sign(verticalIn)))
	move_and_collide(velocity * delta)
	
	# tilt the flyer according to vertical movement.
	tilt = velocity.y / vertical_max_speed * max_tilt
	rotation = tilt

func shoot(index):
	#print_debug("trying to shoot weapon ", index, " out of ", )
	if index < weapons.size():
		weapons[index].shoot()

func deal_damage(amount):
	hp -= amount
	if hp <= 0:
		die()

func die():
	return
	#TODO: implement death
