fix: wire movement controller before state machine; harden machine lookup

This commit is contained in:
2026-06-03 15:14:10 -04:00
parent 24a8651a6d
commit c0a90df690
2 changed files with 43 additions and 24 deletions
+5 -8
View File
@@ -65,6 +65,11 @@ func _build_player() -> void:
shape.shape.height = 1.8 shape.shape.height = 1.8
player.add_child(shape) player.add_child(shape)
# Movement controller MUST be attached first, before machine/children
var mover_script = load("res://movement/player_movement_controller.gd")
if mover_script:
player.set_script(mover_script)
# State machine # State machine
var sm := Node.new() var sm := Node.new()
sm.name = "MovementStateMachine" sm.name = "MovementStateMachine"
@@ -91,14 +96,6 @@ func _build_player() -> void:
if st_script: if st_script:
st.set_script(st_script) st.set_script(st_script)
# Movement controller
var mover_script = load("res://movement/player_movement_controller.gd")
if mover_script:
player.set_script(mover_script)
# set_script does not auto-call _ready(); do it manually
if player.has_method("_ready"):
player._ready()
# Camera # Camera
var camera := Camera3D.new() var camera := Camera3D.new()
camera.name = "Camera3D" camera.name = "Camera3D"
+34 -12
View File
@@ -12,6 +12,21 @@ signal chain_updated(count: int, bonus: float)
var machine: MovementStateMachine var machine: MovementStateMachine
var jump_buffered: bool = false var jump_buffered: bool = false
var _ready_ran: bool = false
# ── Helpers ───────────────────────────────────────────────────────────────────
func _ensure_machine() -> MovementStateMachine:
if typeof(_machine) != TYPE_NIL and is_instance_valid(_machine):
return _machine as MovementStateMachine
var candidates := [
get_node("MovementStateMachine") if has_node("MovementStateMachine") else null,
_machine if is_instance_valid(_machine) and _machine.has_method("register_chain_mechanic") else null,
]
for c in candidates:
if c and (c is MovementStateMachine or (c.has_method("switch_to") and c.has_method("register_chain_mechanic"))):
_machine = c
return _machine
return null
# ── Input references ───────────────────────────────────────────────────────── # ── Input references ─────────────────────────────────────────────────────────
func _unhandled_input(event: InputEvent) -> void: func _unhandled_input(event: InputEvent) -> void:
@@ -31,22 +46,29 @@ func _unhandled_input(event: InputEvent) -> void:
func _ready() -> void: func _ready() -> void:
_machine.player = self if _ready_ran:
_machine.params = params return
_ready_ran = true
var node := _ensure_machine()
if node:
var sm := node
sm.player = self
sm.params = params
# Mirror signal up # Mirror signal up
_machine.movement_event.connect(func(ev, data): sm.movement_event.connect(func(ev, data):
if ev == "chain_updated": if ev == "chain_updated":
chain_updated.emit(data.count, data.bonus) chain_updated.emit(data.count, data.bonus)
) )
func _process(_delta: float) -> void: func _process(_delta: float) -> void:
var fsm_node = _machine.get_node("MovementStateMachine") var node := _ensure_machine()
if fsm_node and fsm_node.has_method("switch_to"): if not node:
_machine.input_jump_just_pressed = Input.is_action_just_pressed("jump") and not _machine.input_jump_pressed return
_machine.input_jump_pressed = Input.is_action_pressed("jump") # Keep one consistent source of truth for inputs
_machine.input_sprint = Input.is_action_pressed("sprint") node.input_jump_just_pressed = Input.is_action_just_pressed("jump") and not node.input_jump_pressed
_machine.input_crouch = Input.is_action_pressed("crouch") node.input_jump_pressed = Input.is_action_pressed("jump")
_machine.input_dash = Input.is_action_just_pressed("dash") and not _machine.input_dash node.input_sprint = Input.is_action_pressed("sprint")
var input_v := Input.get_vector("move_left", "move_right", "move_forward", "move_back") node.input_crouch = Input.is_action_pressed("crouch")
_machine.input_dir = input_v node.input_dash = Input.is_action_just_pressed("dash") and not node.input_dash
node.input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_back")