SlimePlatformer/Characters/player.gd

63 lines
2.1 KiB
GDScript3
Raw Normal View History

2026-10-14 03:50:16 -05:00
extends CharacterBody2D
const MOVE_SPEED = 200.0
const CLIMB_SPEED = -150.0
const JUMP_VELOCITY = -250.0
2026-10-18 22:14:18 -05:00
# Time window (in seconds) on when to auto-jump when pressing jump key
# before landing on the ground.
const TOTAL_JUMP_BUFFER_S = 0.1
var jumpBufferTimeLeft = 0.0
2026-10-14 03:50:16 -05:00
const WALL_SLIDE_MODIFIER = 0.1
const ROTATE_TO_WALL_SPEED = 25 # How quickly to rotate the sprite when clinging to a wall.
# Temp for now, need to use aniamtedspite2d later on
@onready var sprite = $Sprite2D
func _process(delta: float) -> void:
# Update sprite to be on the wall
if not is_on_floor() and is_on_wall():
sprite.rotation = lerp_angle(sprite.rotation, PI/2 * get_wall_normal().x, ROTATE_TO_WALL_SPEED * delta)
else:
sprite.rotation = lerp_angle(sprite.rotation, 0.0, ROTATE_TO_WALL_SPEED * delta)
func _physics_process(delta: float) -> void:
# Get the input direction and handle the movement/deceleration.
var horizontalInput := Input.get_axis("moveLeft", "moveRight")
var verticalInput := Input.get_axis("moveDown", "moveUp")
# Quick flip sprite thing.
if horizontalInput: sprite.flip_h = (horizontalInput < 0)
# If you're pressing against the wall, stick to it.
# If you're just touching the wall, slowly ooze down it.
# If you're not touching anything, fall normally.
if is_on_wall() and horizontalInput != 0:
velocity.y = 0.0
elif is_on_wall():
velocity.y += get_gravity().y * WALL_SLIDE_MODIFIER * delta
elif not is_on_floor():
velocity.y += get_gravity().y * delta
2026-10-18 22:14:18 -05:00
# Jumping! And also jump buffering.
if Input.is_action_just_pressed("jump") and not (is_on_floor() or is_on_wall()):
jumpBufferTimeLeft = TOTAL_JUMP_BUFFER_S
print("start jump buffer")
if ((Input.is_action_just_pressed("jump") or jumpBufferTimeLeft > 0.0)
and is_on_floor()):
2026-10-14 03:50:16 -05:00
velocity.y = JUMP_VELOCITY
2026-10-18 22:14:18 -05:00
jumpBufferTimeLeft = 0.0
if jumpBufferTimeLeft > 0.0:
jumpBufferTimeLeft -= delta
2026-10-14 03:50:16 -05:00
# Quick setup for oozing up walls
if verticalInput > 0 and is_on_wall():
velocity.y = CLIMB_SPEED
if horizontalInput: velocity.x = horizontalInput * MOVE_SPEED
else: velocity.x = move_toward(velocity.x, 0, MOVE_SPEED)
move_and_slide()