WASD/โโโโ walk ยท hold shift run ยท hold space or mouse = aim the light (walking only โ you cannot run and aim; that is the design) ยท the same FSM documented in the next tab
0 ยท the shape of the thing
One node owns movement; each state is its own script file. States never touch the player's internals directly โ they receive a reference and call its public API. Swapping, adding, or debugging a state means touching exactly one file. This is the architecture the demo tab is actually running (ported to JS, same names).
res:// โโ player/ โ โโ player.tscn CharacterBody2D + AnimatedSprite2D + flashlight (PointLight2D) โ โโ player.gd owns velocity, input facade, animation dispatch โ โโ states/ โ โโ state.gd abstract base โ enter/exit/physics_update โ โโ state_machine.gd the switchboard, a plain Node โ โโ idle.gd โ โโ walk.gd โ โโ run.gd โ โโ walk_aim.gd walking with the flashlight raised
1 ยท state.gd โ the contract
# state.gd ยท every state extends this and overrides what it needs class_name State extends Node var player: CharacterBody2D var machine: StateMachine func enter() -> void: pass func exit() -> void: pass func physics_update(delta: float) -> void: pass
2 ยท state_machine.gd โ the switchboard
# state_machine.gd ยท child states register themselves by node name class_name StateMachine extends Node @export var initial: State var current: State var states: Dictionary = {} func _ready() -> void: for child in get_children(): if child is State: states[child.name.to_lower()] = child child.machine = self child.player = owner current = initial current.enter() func transition(to: String) -> void: if states[to] == current: return current.exit() current = states[to] current.enter() func _physics_process(delta: float) -> void: current.physics_update(delta)
3 ยท the four states
# idle.gd โ the resting heartbeat; every other state falls back here extends State func enter() -> void: player.play_anim("idle") func physics_update(_d: float) -> void: player.velocity = player.velocity.move_toward(Vector2.ZERO, player.FRICTION) player.move_and_slide() if player.move_input() != Vector2.ZERO: machine.transition("run" if player.wants_run() else "walk")
# walk.gd โ WALK_SPEED 60 px/s ยท quiet footsteps (noise radius small) extends State func enter() -> void: player.play_anim("walk") player.noise_radius = 40.0 func physics_update(_d: float) -> void: var dir := player.move_input() player.velocity = dir * player.WALK_SPEED player.move_and_slide() if dir == Vector2.ZERO: machine.transition("idle") elif player.wants_aim(): machine.transition("walk_aim") elif player.wants_run(): machine.transition("run")
# run.gd โ RUN_SPEED 140 px/s ยท LOUD (noise radius wide, monsters hear) # design rule: you cannot aim while running โ fear needs a cost. extends State func enter() -> void: player.play_anim("run") player.noise_radius = 160.0 func physics_update(_d: float) -> void: var dir := player.move_input() player.velocity = dir * player.RUN_SPEED player.move_and_slide() if dir == Vector2.ZERO: machine.transition("idle") elif not player.wants_run(): machine.transition("walk") # wants_aim() is deliberately ignored here โ drop to walk first
# walk_aim.gd โ AIM_SPEED 35 px/s ยท flashlight on, facing locked to aim extends State func enter() -> void: player.play_anim("walk_aim") player.flashlight.enabled = true func exit() -> void: player.flashlight.enabled = false func physics_update(_d: float) -> void: var dir := player.move_input() player.velocity = dir * player.AIM_SPEED player.face_toward(player.aim_point()) # strafe: body โ gaze player.move_and_slide() if not player.wants_aim(): machine.transition("walk" if dir != Vector2.ZERO else "idle")
4 ยท player.gd โ the facade the states speak to
# player.gd ยท states call THESE, never raw Input โ swap gamepad/AI later class_name Player extends CharacterBody2D const WALK_SPEED := 60.0 const RUN_SPEED := 140.0 const AIM_SPEED := 35.0 const FRICTION := 8.0 @onready var flashlight: PointLight2D = $Flashlight var noise_radius := 0.0 # monsters query this โ movement IS stealth func move_input() -> Vector2: return Input.get_vector("left","right","up","down") func wants_run() -> bool: return Input.is_action_pressed("run") func wants_aim() -> bool: return Input.is_action_pressed("aim") func play_anim(n: String) -> void: $AnimatedSprite2D.play(n)
5 ยท why this shape, for horror specifically
Noise radius lives on the player and is SET BY STATES โ movement choice is the stealth system, no extra code. The run state refusing to aim creates the panic decision the genre runs on. Adding "hide", "injured limp", or "possessed" later = one new file under states/, zero edits elsewhere. The demo tab runs this exact FSM in JS with the same state names, transitions, and the run-can't-aim rule โ kick it around to feel the tuning before you write a line of GDScript.