SlimePlatformer/Characters/player.gd

59 lines
2.2 KiB
GDScript3
Raw Normal View History

2026-10-14 03:50:16 -05:00
extends CharacterBody2D
const MOVE_SPEED = 150.0
2026-10-14 03:50:16 -05:00
const CLIMB_SPEED = -150.0
const JUMP_VELOCITY = -250.0
2026-10-18 22:14:18 -05:00
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.
const CEILING_STICK_SPEED = -20.0 # Small upward push that keeps the slime pressed against the ceiling.
2026-10-14 03:50:16 -05:00
# Temp for now, need to use aniamtedspite2d later on
@onready var sprite = $Sprite2D
# is_on_ceiling() is only true on frames where we actually bumped the ceiling,
# so we remember that we're stuck to it here.
var isClingingToCeiling := false
2026-10-14 03:50:16 -05:00
func _process(delta: float) -> void:
# Update sprite to be on the ceiling or wall
if isClingingToCeiling:
sprite.rotation = lerp_angle(sprite.rotation, PI, ROTATE_TO_WALL_SPEED * delta)
elif not is_on_floor() and is_on_wall():
2026-10-14 03:50:16 -05:00
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)
# Pressing down lets go of the ceiling. Zeroing velocity.y means we start
# falling right away, instead of drifting up and sticking again.
if isClingingToCeiling and verticalInput < 0:
isClingingToCeiling = false
2026-10-14 03:50:16 -05:00
velocity.y = 0.0
# If on the ceiling, stay pressed against it.
# If touching the wall, stick to it or move up/down.
# Else, fall normally if not on floor.
if isClingingToCeiling:
velocity.y = CEILING_STICK_SPEED
2026-10-14 03:50:16 -05:00
elif is_on_wall():
velocity.y = CLIMB_SPEED * verticalInput
2026-10-14 03:50:16 -05:00
elif not is_on_floor():
velocity.y += get_gravity().y * delta
if horizontalInput: velocity.x = horizontalInput * MOVE_SPEED
else: velocity.x = move_toward(velocity.x, 0, MOVE_SPEED)
move_and_slide()
# Touching the ceiling sticks us to it. Crawling off the ceiling's edge
# means we're not touching it anymore, so we start falling.
isClingingToCeiling = is_on_ceiling()