2D multiplayer platformer using Godot Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

57 lines
1.7 KiB

class_name PlayerMovement
extends Node
# TODO: Implement "low jumps" activated by releasing the jump button early.
@export var jump_early_time := 0.2
@export var jump_coyote_time := 0.2
@export var movement_speed := 160.0
@export var jump_velocity := 240.0
@export var gravity := 480.0
@export var acceleration := 0.25
@export var friction_ground := 0.2
@export var friction_air := 0.05
@onready var player: Player = get_parent()
var time_since_jump_pressed := INF
var time_since_on_floor := INF
func _physics_process(delta: float) -> void:
_apply_movement(delta)
player.velocity.y += gravity * delta # Apply gravity.
player.move_and_slide() # Actually move the player.
func _apply_movement(delta: float) -> void:
if not player.network.is_local: return
var move_dir := 0.0
var jump_pressed := false
# TODO: Check if escape menu is open?
if player.health.is_alive:
move_dir = Input.get_axis("move_left", "move_right")
jump_pressed = Input.is_action_just_pressed("move_jump")
# Handle jumping.
if jump_pressed: time_since_jump_pressed = 0
if player.is_on_floor(): time_since_on_floor = 0
if time_since_jump_pressed <= jump_early_time and time_since_on_floor <= jump_coyote_time:
player.velocity.y -= jump_velocity
time_since_jump_pressed = INF
time_since_on_floor = INF
time_since_jump_pressed += delta
time_since_on_floor += delta
if move_dir:
# If player is trying to move, accelerate them.
player.velocity.x = lerpf(player.velocity.x, move_dir * movement_speed, acceleration)
else:
# If player is not moving, apply friction.
var friction := friction_ground if player.is_on_floor() else friction_air
player.velocity.x = lerpf(player.velocity.x, 0, friction)