GrappleTest/Player/BasicCharacter/CharacterController.gd
2024-02-27 11:27:54 -06:00

98 lines
2.4 KiB
GDScript

extends CharacterBody2D
const SPEED = 400.0
const ACCEL = 7.0
var direction = 0
var addedveloc = 0
const JUMP_VELOCITY = -450.0
const MAX_JUMPS = 2
var jumps = MAX_JUMPS
var wallKayote = 0
const FALL_SPEED = -JUMP_VELOCITY
var falling = false
var floorTime = 0
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func isWallSliding():
return is_on_wall_only() and direction
func getGravMulti():
var axis = (gen.boolToNumb(Input.is_action_pressed("up"), 1))
if velocity.y < 0:
return axis
return 1
func launch(veloc):
addedveloc = veloc
velocity.x = veloc
signal Jumped
func _physics_process(delta):
direction = round(Input.get_axis("left", "right"))
wallKayote -= delta
if not is_on_floor():
if velocity.y < 1500:
var lowGrav = velocity.y < 0 and (Input.is_action_pressed("up") or Input.is_action_pressed("jump"))
velocity.y += gravity * delta / getGravMulti()
floorTime = 0
else:
floorTime += delta
jumps = MAX_JUMPS
falling = false
if isWallSliding():
wallKayote = 0.2
falling = false
if Input.is_action_just_pressed("respawn") or position.y > 5000:
position = Vector2.ZERO
velocity = Vector2.ZERO
if Input.is_action_just_pressed("jump"):
if wallKayote > 0 and not is_on_floor():
wallKayote /= 2
if Input.is_action_pressed("down"):
launch(get_wall_normal().x * SPEED * 2)
velocity.y = JUMP_VELOCITY / 1.8
else:
launch(get_wall_normal().x * SPEED * 1.5)
velocity.y = clamp(velocity.y + JUMP_VELOCITY, -INF, JUMP_VELOCITY)
elif jumps > 0:
Jumped.emit()
velocity.y = clamp(velocity.y + JUMP_VELOCITY, -INF, JUMP_VELOCITY)
jumps -= 1
falling = false
if Input.is_action_just_pressed("down") and not falling and not isWallSliding():
falling = true
velocity.y = clamp(velocity.y + FALL_SPEED, FALL_SPEED, INF)
if isWallSliding():
var upDown = clamp(Input.get_axis("up", "down"), -0.5, 1)
var holdMulti = (upDown * 2) + 1
velocity.y = lerpf(velocity.y, clamp(velocity.y, JUMP_VELOCITY, 100 * holdMulti), delta * 10)
var finalSpeed = clamp(abs(velocity.x), SPEED, INF)
if floorTime > 0.05:
finalSpeed = clamp(lerp(finalSpeed, SPEED, delta * 20), SPEED, INF)
velocity.x = lerp(velocity.x, direction * finalSpeed, delta * ACCEL)
if abs(velocity.x) < abs(addedveloc):
addedveloc = lerp(addedveloc, velocity.x, ACCEL * 2 * delta)
velocity.x += addedveloc * delta * 15
addedveloc = lerpf(addedveloc, 0, delta * 5)
move_and_slide()