SlimePlatformer/Characters/player.gd

48 lines
1.6 KiB
GDScript
Executable file

extends CharacterBody2D
const MOVE_SPEED = 200.0
const CLIMB_SPEED = -150.0
const JUMP_VELOCITY = -250.0
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
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# 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()