89 lines
2.3 KiB
GDScript
89 lines
2.3 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 * 1.5
|
|
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 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():
|
|
var lowGrav = velocity.y < 0 and (Input.is_action_pressed("up") or Input.is_action_pressed("jump"))
|
|
velocity.y += gravity * delta / gen.boolToNumb(lowGrav, 1)
|
|
|
|
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 > 500:
|
|
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.5)
|
|
velocity.y = JUMP_VELOCITY / 2
|
|
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()
|