extends CharacterBody2D

const SPEED = 400.0
const ACCEL = 7.0

const JUMP_VELOCITY = -450.0

const MAX_JUMPS = 2
var jumps = MAX_JUMPS

const FALL_SPEED = -JUMP_VELOCITY * 1.5
var falling = false

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

var direction = 0

var addedveloc = 0

var wallKayote = 0

func launch(veloc):
	addedveloc = veloc
	velocity.x = veloc

func _physics_process(delta):
	direction = round(Input.get_axis("left", "right"))
	
	wallKayote -= delta
	
	if not is_on_floor():
		var lowGrav = velocity.y < 0 and Input.is_action_pressed("jump")
		
		velocity.y += gravity * delta / gen.boolToNumb(lowGrav, 1)
	else:
		jumps = MAX_JUMPS
		falling = false
	
	if is_on_wall_only() and direction:
		wallKayote = 0.225
	
	print(-get_wall_normal().x, direction)
	
	if Input.is_action_just_pressed("jump"):
		if wallKayote > 0:
			velocity.y = clamp(velocity.y + JUMP_VELOCITY, -INF, JUMP_VELOCITY)
			launch(get_wall_normal().x * SPEED)
		elif jumps > 0:
			velocity.y = clamp(velocity.y + JUMP_VELOCITY, -INF, JUMP_VELOCITY)
			jumps -= 1
			falling = false
	
	if Input.is_action_just_pressed("down") and not falling:
		falling = true
		velocity.y = clamp(velocity.y + FALL_SPEED, FALL_SPEED, INF)
	
	if is_on_wall_only():
		velocity.y = lerpf(velocity.y, clamp(velocity.y, JUMP_VELOCITY, 100), delta * 10)
	
	var finalSpeed = clamp(abs(velocity.x), SPEED, SPEED * 2) - abs(addedveloc)
	
	finalSpeed = clamp(lerp(finalSpeed, SPEED, delta * 10), SPEED, INF)
	#print(finalSpeed)
	
	velocity.x = lerp(velocity.x, direction * finalSpeed, delta * ACCEL)
	
	#if abs(velocity.x) < abs(addedveloc):
		#print("boiyoing")
	velocity.x += addedveloc * delta * 10
	
	addedveloc = lerpf(addedveloc, 0, delta * 5)

	move_and_slide()
