GrappleTest/Player/BasicCharacter/CharacterController.gd

98 lines
2.4 KiB
GDScript3
Raw Normal View History

2024-02-22 00:10:13 -06:00
extends CharacterBody2D
const SPEED = 400.0
const ACCEL = 7.0
2024-02-26 08:28:11 -06:00
var direction = 0
var addedveloc = 0
2024-02-22 00:10:13 -06:00
2024-02-24 12:12:57 -06:00
const JUMP_VELOCITY = -450.0
const MAX_JUMPS = 2
var jumps = MAX_JUMPS
2024-02-27 11:27:54 -06:00
2024-02-26 08:28:11 -06:00
var wallKayote = 0
2024-02-24 12:12:57 -06:00
2024-02-27 11:27:54 -06:00
const FALL_SPEED = -JUMP_VELOCITY
2024-02-22 00:10:13 -06:00
var falling = false
2024-02-26 08:28:11 -06:00
var floorTime = 0
2024-02-24 12:12:57 -06:00
2024-02-26 08:28:11 -06:00
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
2024-02-24 12:12:57 -06:00
2024-02-26 08:28:11 -06:00
func isWallSliding():
return is_on_wall_only() and direction
2024-02-24 12:12:57 -06:00
2024-02-27 11:27:54 -06:00
func getGravMulti():
var axis = (gen.boolToNumb(Input.is_action_pressed("up"), 1))
if velocity.y < 0:
return axis
return 1
2024-02-24 12:12:57 -06:00
func launch(veloc):
addedveloc = veloc
velocity.x = veloc
2024-02-22 00:10:13 -06:00
2024-02-26 08:28:11 -06:00
signal Jumped
2024-02-22 00:10:13 -06:00
func _physics_process(delta):
2024-02-27 11:27:54 -06:00
2024-02-24 12:12:57 -06:00
direction = round(Input.get_axis("left", "right"))
wallKayote -= delta
2024-02-22 00:10:13 -06:00
if not is_on_floor():
2024-02-27 11:27:54 -06:00
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()
2024-02-26 08:28:11 -06:00
floorTime = 0
2024-02-22 00:10:13 -06:00
else:
2024-02-26 08:28:11 -06:00
floorTime += delta
2024-02-24 12:12:57 -06:00
jumps = MAX_JUMPS
2024-02-22 00:10:13 -06:00
falling = false
2024-02-24 12:12:57 -06:00
2024-02-26 08:28:11 -06:00
if isWallSliding():
wallKayote = 0.2
falling = false
2024-02-24 12:12:57 -06:00
2024-02-27 11:27:54 -06:00
if Input.is_action_just_pressed("respawn") or position.y > 5000:
2024-02-26 08:28:11 -06:00
position = Vector2.ZERO
velocity = Vector2.ZERO
2024-02-24 12:12:57 -06:00
if Input.is_action_just_pressed("jump"):
2024-02-26 08:28:11 -06:00
if wallKayote > 0 and not is_on_floor():
wallKayote /= 2
if Input.is_action_pressed("down"):
2024-02-27 11:27:54 -06:00
launch(get_wall_normal().x * SPEED * 2)
velocity.y = JUMP_VELOCITY / 1.8
2024-02-26 08:28:11 -06:00
else:
launch(get_wall_normal().x * SPEED * 1.5)
velocity.y = clamp(velocity.y + JUMP_VELOCITY, -INF, JUMP_VELOCITY)
2024-02-24 12:12:57 -06:00
elif jumps > 0:
2024-02-26 08:28:11 -06:00
Jumped.emit()
2024-02-24 12:12:57 -06:00
velocity.y = clamp(velocity.y + JUMP_VELOCITY, -INF, JUMP_VELOCITY)
jumps -= 1
falling = false
2024-02-26 08:28:11 -06:00
if Input.is_action_just_pressed("down") and not falling and not isWallSliding():
2024-02-22 00:10:13 -06:00
falling = true
2024-02-24 12:12:57 -06:00
velocity.y = clamp(velocity.y + FALL_SPEED, FALL_SPEED, INF)
2024-02-26 08:28:11 -06:00
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)
2024-02-24 12:12:57 -06:00
2024-02-26 08:28:11 -06:00
var finalSpeed = clamp(abs(velocity.x), SPEED, INF)
if floorTime > 0.05:
finalSpeed = clamp(lerp(finalSpeed, SPEED, delta * 20), SPEED, INF)
2024-02-24 12:12:57 -06:00
velocity.x = lerp(velocity.x, direction * finalSpeed, delta * ACCEL)
2024-02-26 08:28:11 -06:00
if abs(velocity.x) < abs(addedveloc):
addedveloc = lerp(addedveloc, velocity.x, ACCEL * 2 * delta)
velocity.x += addedveloc * delta * 15
2024-02-24 12:12:57 -06:00
addedveloc = lerpf(addedveloc, 0, delta * 5)
2024-02-22 00:10:13 -06:00
move_and_slide()