This commit is contained in:
Lilith Schier 2026-05-01 19:50:57 +02:00
commit f389e4e807
186 changed files with 10853 additions and 0 deletions

179
player/player.gd Normal file
View file

@ -0,0 +1,179 @@
extends CharacterBody2D
class_name Player
@export_custom(PROPERTY_HINT_NONE, "suffix:s")
var max_input_buffer_time := 0.15
@export_custom(PROPERTY_HINT_NONE, "suffix:s⁻¹")
var grounded_speed_decay := 0.1
@export_custom(PROPERTY_HINT_NONE, "suffix:s⁻¹")
var grounded_speed_stop_decay := 0.005
@export_custom(PROPERTY_HINT_NONE, "suffix:px/s")
var walk_max_speed := 130.0
@export_custom(PROPERTY_HINT_NONE, "suffix:px/s²")
var walk_accel := 600.0
@export_custom(PROPERTY_HINT_NONE, "suffix:px/s²")
var air_accel := 200.0
@export_custom(PROPERTY_HINT_NONE, "suffix:s")
var jump_ascent_max_time := 0.2
@export_custom(PROPERTY_HINT_NONE, "suffix:px/s")
var jump_ascent_speed := 200.0
@export_custom(PROPERTY_HINT_NONE, "suffix:s")
var late_jump_time_limit := 0.15
@export_custom(PROPERTY_HINT_NONE, "suffix:px/s")
var slash_speed_boost := 90.0
var _max_input_buffer_time_usec:
get():
return max_input_buffer_time * 1_000_000
var _jump_ascent_remaining_time := 0.0
var _is_jumping := false
var _jump_allowed_time := 0.0
var _current_direction := Vector2.ZERO
var _is_facing_right := true
var _input_buffer: Array[BufferedInput] = []
class BufferedInput:
var direction: Vector2
var event: InputEvent
var time: int
func _ready() -> void:
%Body.play()
func _unhandled_input(event: InputEvent) -> void:
if event.is_echo():
return
if (event.is_action(&"move_left")
or event.is_action(&"move_right")
or event.is_action(&"move_up")
or event.is_action(&"move_down") ):
_current_direction = Vector2(
Input.get_axis(&"move_left", &"move_right"),
Input.get_axis(&"move_up", &"move_down"),
)
if _current_direction.x < 0:
_is_facing_right = false
elif _current_direction.x > 0:
_is_facing_right = true
if event.is_action(&"jump") or event.is_action(&"attack"):
var buffered_input := BufferedInput.new()
buffered_input.direction = _current_direction
buffered_input.event = event
buffered_input.time = Time.get_ticks_usec()
_input_buffer.push_back(buffered_input)
handle_buffered_inputs()
func _physics_process(delta: float) -> void:
# Walking on the floor
if is_on_floor():
if sign(velocity.x) != sign(_current_direction.x):
velocity.x *= grounded_speed_stop_decay ** delta
else:
velocity.x *= grounded_speed_decay ** delta
var target_vel_x := _current_direction.x * walk_max_speed
var is_walking := target_vel_x != 0
if not is_walking or abs(velocity.x) < walk_max_speed:
velocity.x = move_toward(
velocity.x,
target_vel_x,
walk_accel * delta,
)
# Air control
else:
velocity.x += _current_direction.x * air_accel * delta
# Move up while holding jump
if _jump_ascent_remaining_time > 0:
velocity.y = -jump_ascent_speed
_jump_ascent_remaining_time -= delta
if _jump_ascent_remaining_time < 0:
velocity += get_gravity() * -_jump_ascent_remaining_time
elif not is_on_floor():
velocity += get_gravity() * delta
# Drag while in air
if not is_on_floor() and _jump_ascent_remaining_time <= 0:
var drag_force := -0.01 * velocity * velocity.abs()
velocity += drag_force * delta
move_and_slide()
_jump_allowed_time = max(0, _jump_allowed_time - delta)
if is_on_floor():
_jump_allowed_time = late_jump_time_limit
if is_on_ceiling():
_is_jumping = false
_jump_ascent_remaining_time = 0
if _is_holding_wall():
velocity.y = min(5.0, velocity.y)
_is_facing_right = sign(get_wall_normal().x) > 0
func _is_holding_wall() -> bool:
return (not is_on_floor()
and is_on_wall()
and sign(get_wall_normal().x) != sign(_current_direction.x)
and _current_direction.x != 0 )
func _process(_delta: float) -> void:
handle_buffered_inputs()
%Body.scale.x = 1 if _is_facing_right else -1
if _is_holding_wall():
%Body.play(&"wall_grab")
elif is_on_floor() and _current_direction.x != 0.0:
%Body.play(&"walk")
else:
%Body.play(&"idle")
func handle_buffered_inputs() -> void:
var input_found := false
var current_time := Time.get_ticks_usec()
# Clear expired input buffer items
while (not _input_buffer.is_empty()
and _input_buffer[0].time + _max_input_buffer_time_usec < current_time ):
_input_buffer.pop_front()
# Check new commands
for i in _input_buffer.size():
var input := _input_buffer[i]
if input.event.is_action(&"jump"):
# Grounded jump
if (input.event.is_pressed()
and _jump_allowed_time > 0
and (not is_on_floor() or input.direction.y <= 0) ):
input_found = true
_jump_allowed_time = 0
_jump_ascent_remaining_time = jump_ascent_max_time
_is_jumping = true
elif (input.event.is_pressed()
and is_on_wall() ):
input_found = true
_jump_allowed_time = 0
velocity += get_wall_normal() * 200.0
velocity.y = min(0, velocity.y)
# Jump cuts
elif input.event.is_released() and _is_jumping:
_is_jumping = false
_jump_ascent_remaining_time = 0
# Jump cut only once, if we're still moving up
if velocity.y < 0:
velocity.y *= 0.1
elif input.event.is_action_pressed(&"attack"):
input_found = true
velocity.x += (1 if _is_facing_right else -1) * slash_speed_boost
$AnimationPlayer.play("slash")
if input_found:
_input_buffer = _input_buffer.slice(i + 1)
break