refactor player movement and move physics calculation to static methods

This commit is contained in:
2025-08-22 13:34:16 +10:00
parent 3e308d7c4c
commit 116928d505

View File

@@ -23,15 +23,64 @@ static var instance: Player
@export var _debug_charge_len: float = 30
@export var _debug_charge_width: float = 3
var _direction: float = 0
var _is_charging_jump: bool = false
var _jump_charge_frames: float = 0
var _charge_strength: float = 0
var _direction: float = 0
var _jump_released: bool = false
var _freemove_enabled: bool = false
@onready var _saved_state: Vector2 = global_position
static func process_movement(
new_velocity: Vector2,
delta: float,
on_floor: bool,
direction: float,
is_charging_jump: bool,
charge_strength: float,
jump_released: bool,
move_speed: float,
jump_speed: float,
jump_force: float,
fall_speed: float,
fall_acceleration: float,
) -> Vector2:
# falling
new_velocity.y = (
move_toward(new_velocity.y, fall_speed, delta * fall_acceleration)
if not on_floor
else 0.0
)
# moving
if on_floor:
new_velocity.x = direction * move_speed if not is_charging_jump else 0.0
# jump charge release
if jump_released:
new_velocity.y = -jump_force * charge_strength
new_velocity.x = direction * jump_speed
return new_velocity
static func process_collision(
new_velocity: Vector2,
slide_velocity: Vector2,
on_ceiling: bool,
on_wall: bool,
wall_bounce_velocity_loss: float,
) -> Vector2:
if not on_ceiling:
slide_velocity.x *= wall_bounce_velocity_loss
if on_wall:
slide_velocity.x = -new_velocity.x * wall_bounce_velocity_loss
return slide_velocity
func _ready() -> void:
instance = self
Debugger.add_event("collided")
@@ -57,73 +106,56 @@ func _physics_process(delta: float) -> void:
move_and_slide()
return
var new_velocity := velocity
var is_on_floor_prev := is_on_floor()
var is_on_ceiling_prev := is_on_ceiling()
var is_on_wall_prev := is_on_wall()
# falling
new_velocity.y = (
move_toward(new_velocity.y, _fall_speed, delta * _fall_acceleration)
if not is_on_floor()
else 0.0
_gather_input(delta, is_on_floor_prev)
var new_velocity := process_movement(
velocity,
delta,
is_on_floor_prev,
_direction,
_is_charging_jump,
_charge_strength,
_jump_released,
_move_speed,
_jump_speed,
_jump_force,
_fall_speed,
_fall_acceleration,
)
# movement
_direction = signf(Input.get_axis("move_left", "move_right"))
if is_on_floor():
new_velocity.x = _direction * _move_speed if not _is_charging_jump else 0.0
# jump charge start
if Input.is_action_just_pressed("jump") and is_on_floor():
_is_charging_jump = true
_jump_charge_frames = 0
if _is_charging_jump:
_jump_charge_frames += delta * Engine.physics_ticks_per_second
_charge_strength = (
minf(_jump_charge_frames, _jump_full_charge_frames)
/ _jump_full_charge_frames
)
_trajectory.global_position = global_position
_trajectory.charge_strength = _charge_strength
_trajectory.flip = _direction < 0
_trajectory.visible = _direction != 0.0
# jump charge release
if (
_is_charging_jump
and is_on_floor()
and (
Input.is_action_just_released("jump")
or _jump_charge_frames >= _jump_full_charge_frames
)
):
new_velocity.y = -_jump_force * _charge_strength
new_velocity.x = _direction * _jump_speed
_is_charging_jump = false
_jump_charge_frames = 0
if _jump_released:
jumped.emit(_charge_strength, new_velocity)
# apply velocity
velocity = new_velocity
var collision_occured := move_and_slide()
# bounce off
# collisions
if collision_occured:
if not is_on_floor_prev and is_on_floor():
_trajectory.visible = false
collided.emit(SIDE_BOTTOM, new_velocity)
if not is_on_ceiling_prev and is_on_ceiling():
velocity.x *= _wall_bounce_velocity_loss
collided.emit(SIDE_TOP, new_velocity)
if (
var on_floor := not is_on_floor_prev and is_on_floor()
var on_ceiling := not is_on_ceiling_prev and is_on_ceiling()
var on_wall := (
not is_on_wall_prev
and is_on_wall()
and not is_on_floor()
and not is_zero_approx(new_velocity.x)
):
velocity.x = -new_velocity.x * _wall_bounce_velocity_loss
)
velocity = process_collision(
new_velocity,
velocity,
on_ceiling,
on_wall,
_wall_bounce_velocity_loss,
)
if on_floor:
_trajectory.visible = false
collided.emit(SIDE_BOTTOM, new_velocity)
if on_ceiling:
collided.emit(SIDE_TOP, new_velocity)
if on_wall:
collided.emit(SIDE_LEFT if velocity.x > 0 else SIDE_RIGHT, new_velocity)
queue_redraw()
@@ -169,3 +201,37 @@ func _draw() -> void:
_debug_charge_color,
_debug_charge_width
)
func _gather_input(delta: float, on_floor: bool) -> void:
_direction = signf(Input.get_axis("move_left", "move_right"))
# jump charge start
if Input.is_action_just_pressed("jump") and on_floor:
_is_charging_jump = true
_jump_charge_frames = 0
# jump charging
if _is_charging_jump:
_jump_charge_frames += delta * Engine.physics_ticks_per_second
_charge_strength = (
minf(_jump_charge_frames, _jump_full_charge_frames)
/ _jump_full_charge_frames
)
_trajectory.global_position = global_position
_trajectory.charge_strength = _charge_strength
_trajectory.flip = _direction < 0
_trajectory.visible = _direction != 0.0
# jump charge release
_jump_released = (
_is_charging_jump
and on_floor
and (
Input.is_action_just_released("jump")
or _jump_charge_frames >= _jump_full_charge_frames
)
)
if _jump_released:
_is_charging_jump = false
_jump_charge_frames = 0