Files
colorvania/BoxBoy/BoxBoy.gd

92 lines
2.1 KiB
GDScript

class_name BoxBoy extends CharacterBody2D
@onready var _gameplayController = $/root/Scene/GameplayController
@onready var _sprite = $AnimatedSprite2D
const MOVE_FORCE: int = 150
const JUMP_FORCE: int = 380 #about 2 tiles
const GRAVITY_RISING: int = 15
const GRAVITY_FALLING: int = 30
#bouncy platform
const BOUNCE_FORCE: int = -500 #about 4 tiles
var just_bounced: bool = false #allow max bounce height (i.e. ignore jump input)
#limits
const MAX_MOVE_SPEED: int = 200 #about 6 tiles when airborne
const MAX_FALL_SPEED: int = 800
#game-feel
var buffer_grounded: int = 0
var buffer_jumping: int = 0
#boilerplate
func _ready():
_sprite.play("idle", 1)
#movement
func _physics_process(_delta) -> void:
#coyote time
if is_on_floor():
buffer_grounded = 6
else:
buffer_grounded -= 1
#jump buffering
if Input.is_action_just_pressed("input_jump"):
buffer_jumping = 6
else:
buffer_jumping -= 1
#process coyote and jump buffers
if buffer_grounded > 0 and buffer_jumping > 0 and just_bounced == false:
velocity.y = -JUMP_FORCE
buffer_grounded = 0
buffer_jumping = 0
#normally, fall faster than you rise
elif velocity.y < 0 and (Input.is_action_pressed("input_jump") or just_bounced):
velocity.y += GRAVITY_RISING
else:
velocity.y += GRAVITY_FALLING
just_bounced = false
#sideways movement
var move_dir = Input.get_axis("input_left", "input_right")
if move_dir:
_sprite.flip_h = move_dir < 0 #fancy HD 4K graphics
velocity.x += MOVE_FORCE * move_dir
#no input, slow down
elif velocity.x != 0:
velocity.x = log(abs(velocity.x)) * sign(velocity.x)
#terminal velocity (in all directions)
if velocity.y > MAX_FALL_SPEED:
velocity.y = MAX_FALL_SPEED
if abs(velocity.x) > MAX_MOVE_SPEED:
velocity.x = MAX_MOVE_SPEED * sign(velocity.x)
if _gameplayController.godmode and velocity.y > 0:
velocity.y = 0
#do the thing
move_and_slide()
#animation stuff
func _on_animation_finished() -> void:
if randf() < 0.2:
_sprite.play("idle_glance", 2)
else:
_sprite.play("idle", 2)
#utils
func apply_bounce() -> void:
velocity.y = BOUNCE_FORCE
just_bounced = true
buffer_grounded = 0
buffer_jumping = 0