Files
colorvania/boxboy/boxboy.gd
2026-01-20 10:36:32 +11:00

61 lines
1.4 KiB
GDScript

extends CharacterBody2D
@onready var _sprite = $AnimatedSprite2D
const MOVE_FORCE: int = 300
const JUMP_FORCE: int = 800
const GRAVITY_UP: int = 15
const GRAVITY_DOWN: int = 30
const MAX_MOVE_SPEED: int = 300
const MAX_FALL_SPEED: int = 500
#boilerplate
func _ready():
_sprite.play("idle", 1)
#movement
func _physics_process(_delta) -> void:
#input, jumps
if is_on_floor() and Input.is_action_just_pressed("input_jump"):
velocity.y -= JUMP_FORCE
#fall faster than you rise
if is_airborne_rising() and Input.is_action_pressed("input_jump"):
velocity.y += GRAVITY_UP
else:
velocity.y += GRAVITY_DOWN
if is_airborne_falling() and velocity.y > MAX_FALL_SPEED:
velocity.y = MAX_FALL_SPEED
#input, walking
var move_dir = Input.get_axis("input_left", "input_right")
#move with a maximum value
if move_dir:
velocity.x += MOVE_FORCE * move_dir
if abs(velocity.x) > MAX_MOVE_SPEED:
velocity.x = MAX_MOVE_SPEED * sign(velocity.x)
#no input, slow down
elif velocity.x != 0:
velocity.x = log(abs(velocity.x)) * sign(velocity.x)
print(velocity)
#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 is_airborne() -> bool: return !is_on_floor()
func is_airborne_rising() -> bool: return !is_on_floor() and velocity.y < 0
func is_airborne_falling() -> bool: return !is_on_floor() and velocity.y >= 0