feat: implement player movement controller and state machine with dash, slide, and wall-run mechanics
This commit is contained in:
@@ -7,39 +7,54 @@ var params: MovementParams:
|
||||
|
||||
var elapsed: float = 0.0
|
||||
var direction: Vector3 = Vector3.ZERO
|
||||
var dash_cooldown_timer: float = 0.0
|
||||
var _exit_speed: float = 0.0
|
||||
|
||||
# Cooldown tracked across dash instances
|
||||
static var _last_dash_time: float = -999.0
|
||||
|
||||
|
||||
func enter(_data: Dictionary = {}) -> void:
|
||||
# Check cooldown
|
||||
var now := Time.get_ticks_msec() / 1000.0
|
||||
if now - _last_dash_time < params.dash_cooldown:
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
elapsed = 0.0
|
||||
dash_cooldown_timer = params.dash_cooldown
|
||||
var input_v := machine.input_dir
|
||||
var wish_dir := Vector3(input_v.x, 0.0, -input_v.y)
|
||||
if wish_dir.length_squared() > 0.0:
|
||||
direction = wish_dir.normalized()
|
||||
_last_dash_time = now
|
||||
|
||||
# Direction: use wish direction if available, else player forward
|
||||
var wish := machine.wish_dir_world
|
||||
if wish.length_squared() > 0.01:
|
||||
direction = wish.normalized()
|
||||
else:
|
||||
direction = -machine.player.global_transform.basis.z
|
||||
direction.y = 0.0
|
||||
direction = direction.normalized()
|
||||
direction = (machine.player.global_transform.basis * Vector3(direction.x, 0.0, direction.y)).normalized()
|
||||
|
||||
machine.register_chain_mechanic("dash")
|
||||
machine.player.velocity = direction * machine.get_effective_speed(params.dash_speed)
|
||||
_exit_speed = machine.get_effective_speed(params.dash_speed)
|
||||
machine.player.velocity = direction * _exit_speed
|
||||
machine.on_ground = false
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
# Preserve dash velocity on exit (don't cut speed abruptly)
|
||||
pass
|
||||
|
||||
|
||||
func update(delta: float) -> void:
|
||||
elapsed += delta
|
||||
dash_cooldown_timer -= delta
|
||||
if elapsed > params.dash_duration:
|
||||
# Transition to air with preserved velocity (slight decay)
|
||||
machine.player.velocity = direction * _exit_speed * 0.85
|
||||
machine.switch_to("air")
|
||||
return
|
||||
# Skip gravity, maintain dash velocity
|
||||
machine.player.velocity = direction * machine.get_effective_speed(params.dash_speed)
|
||||
|
||||
# Maintain dash velocity — no gravity, no friction
|
||||
machine.player.velocity = direction * _exit_speed
|
||||
machine.player.move_and_slide()
|
||||
|
||||
if machine.player.is_on_floor():
|
||||
machine.on_ground = true
|
||||
machine.switch_to("ground")
|
||||
|
||||
Reference in New Issue
Block a user