GrappleTest/Player/BasicCharacter/CharacterController.gd

74 lines
1.7 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-24 12:12:57 -06:00
const JUMP_VELOCITY = -450.0
2024-02-22 00:10:13 -06:00
2024-02-24 12:12:57 -06:00
const MAX_JUMPS = 2
var jumps = MAX_JUMPS
const FALL_SPEED = -JUMP_VELOCITY * 1.5
2024-02-22 00:10:13 -06:00
var falling = false
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
2024-02-24 12:12:57 -06:00
var direction = 0
var addedveloc = 0
var wallKayote = 0
func launch(veloc):
addedveloc = veloc
velocity.x = veloc
2024-02-22 00:10:13 -06:00
func _physics_process(delta):
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():
var lowGrav = velocity.y < 0 and Input.is_action_pressed("jump")
velocity.y += gravity * delta / gen.boolToNumb(lowGrav, 1)
else:
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
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
2024-02-22 00:10:13 -06:00
if Input.is_action_just_pressed("down") and not falling:
falling = true
2024-02-24 12:12:57 -06:00
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
2024-02-22 00:10:13 -06:00
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()