feat: implement player movement controller and state machine with dash, slide, and wall-run mechanics

This commit is contained in:
DottsGit
2026-06-03 21:55:10 -04:00
parent 5726aa1043
commit da277e9d35
14 changed files with 1092 additions and 329 deletions
+366 -50
View File
@@ -1,24 +1,38 @@
extends Node3D
class_name TestLevelBuilder
## Builds the test level ONLY from code — no .tscn SubResource references.
## One-click playtest: attach to TestLevel root, F5.
## Builds a full test environment from code — parkour geometry, lighting, player, HUD.
## Attach to TestLevel root, press F5.
var _speed_label: Label
var _state_label: Label
var _chain_label: Label
var _player: CharacterBody3D
func _ready() -> void:
build()
func build() -> void:
_build_input_map()
_build_environment()
_build_floor()
_build_walls()
_build_walls_arena()
_build_ramps()
_build_platforms()
_build_wall_run_corridor()
_build_speed_corridor()
_build_lighting()
_build_player()
_build_ui()
_build_hud()
func _box_static(pos: Vector3, size: Vector3, color: Color) -> StaticBody3D:
# ── Utility ───────────────────────────────────────────────────────────────────
func _box_static(pos: Vector3, size: Vector3, color: Color, node_name: String = "") -> StaticBody3D:
var body := StaticBody3D.new()
body.name = "Static_" + str(pos)
body.name = node_name if not node_name.is_empty() else "Static_%s" % str(pos)
body.position = pos
add_child(body)
var shape := CollisionShape3D.new()
@@ -30,11 +44,20 @@ func _box_static(pos: Vector3, size: Vector3, color: Color) -> StaticBody3D:
mesh.mesh.size = size
var mat := StandardMaterial3D.new()
mat.albedo_color = color
mat.roughness = 0.8
mesh.mesh.surface_set_material(0, mat)
body.add_child(mesh)
return body
func _ramp_static(pos: Vector3, size: Vector3, rot_deg: Vector3, color: Color, node_name: String = "") -> StaticBody3D:
var body := _box_static(pos, size, color, node_name)
body.rotation_degrees = rot_deg
return body
# ── Input Map ─────────────────────────────────────────────────────────────────
func _build_input_map() -> void:
var bindings := {
"jump": [KEY_SPACE],
@@ -56,56 +79,212 @@ func _build_input_map() -> void:
if not InputMap.has_action(action):
InputMap.add_action(action)
InputMap.action_erase_events(action)
var events: Array[InputEvent] = []
for code in bindings[action]:
if code in [KEY_SPACE, KEY_SHIFT, KEY_CTRL, KEY_R, KEY_F, KEY_E, KEY_W, KEY_S, KEY_A, KEY_D]:
var ev := InputEventKey.new()
ev.keycode = code
events.append(ev)
InputMap.action_add_event(action, ev)
else:
var ev := InputEventMouseButton.new()
ev.button_index = code
events.append(ev)
for ev in events:
InputMap.action_add_event(action, ev)
InputMap.action_add_event(action, ev)
# ── Environment ───────────────────────────────────────────────────────────────
func _build_environment() -> void:
var env := WorldEnvironment.new()
env.name = "WorldEnvironment"
var environment := Environment.new()
environment.background_mode = Environment.BG_SKY
var sky := Sky.new()
var sky_mat := ProceduralSkyMaterial.new()
sky_mat.sky_top_color = Color(0.15, 0.2, 0.35)
sky_mat.sky_horizon_color = Color(0.45, 0.5, 0.65)
sky_mat.ground_bottom_color = Color(0.1, 0.08, 0.06)
sky_mat.ground_horizon_color = Color(0.35, 0.3, 0.25)
sky.sky_material = sky_mat
environment.sky = sky
environment.ambient_light_source = Environment.AMBIENT_SOURCE_SKY
environment.ambient_light_energy = 0.4
environment.tonemap_mode = 2 # Filmic
environment.glow_enabled = true
environment.glow_intensity = 0.3
environment.glow_bloom = 0.1
environment.fog_enabled = true
environment.fog_light_color = Color(0.5, 0.55, 0.65)
environment.fog_density = 0.002
env.environment = environment
add_child(env)
# ── Floor ─────────────────────────────────────────────────────────────────────
func _build_floor() -> void:
_box_static(Vector3(0, 0.0, 0), Vector3(30, 1.0, 30), Color(0.35, 0.25, 0.15))
# Main arena floor (80x80)
_box_static(Vector3(0, -0.5, 0), Vector3(80, 1.0, 80), Color(0.22, 0.22, 0.25), "Floor_Main")
# Grid lines on floor for spatial reference
for i in range(-40, 41, 5):
_box_static(Vector3(float(i), 0.01, 0), Vector3(0.05, 0.02, 80), Color(0.35, 0.35, 0.4), "GridX_%d" % i)
_box_static(Vector3(0, 0.01, float(i)), Vector3(80, 0.02, 0.05), Color(0.35, 0.35, 0.4), "GridZ_%d" % i)
func _build_walls() -> void:
_box_static(Vector3(0, 3, -15), Vector3(30, 6, 0.4), Color(0.55, 0.45, 0.35))
_box_static(Vector3(15, 3, 0), Vector3(0.4, 6, 30), Color(0.55, 0.45, 0.35))
_box_static(Vector3(-15, 3, 0), Vector3(0.4, 6, 30), Color(0.55, 0.45, 0.35))
# ── Arena Walls ───────────────────────────────────────────────────────────────
func _build_walls_arena() -> void:
var wall_color := Color(0.35, 0.28, 0.22)
var wall_height := 10.0
# North
_box_static(Vector3(0, wall_height * 0.5, -40), Vector3(80, wall_height, 0.5), wall_color, "Wall_North")
# South
_box_static(Vector3(0, wall_height * 0.5, 40), Vector3(80, wall_height, 0.5), wall_color, "Wall_South")
# East
_box_static(Vector3(40, wall_height * 0.5, 0), Vector3(0.5, wall_height, 80), wall_color, "Wall_East")
# West
_box_static(Vector3(-40, wall_height * 0.5, 0), Vector3(0.5, wall_height, 80), wall_color, "Wall_West")
# ── Ramps ─────────────────────────────────────────────────────────────────────
func _build_ramps() -> void:
var ramp_color := Color(0.6, 0.35, 0.15) # Orange-brown
# Gentle ramp (15 degrees)
_ramp_static(Vector3(-25, 1.0, -25), Vector3(8, 0.4, 12), Vector3(15, 0, 0), ramp_color, "Ramp_Gentle")
# Medium ramp (30 degrees)
_ramp_static(Vector3(-25, 2.0, -15), Vector3(8, 0.4, 10), Vector3(30, 0, 0), ramp_color, "Ramp_Medium")
# Steep ramp (45 degrees)
_ramp_static(Vector3(-25, 3.0, -5), Vector3(8, 0.4, 8), Vector3(45, 0, 0), ramp_color, "Ramp_Steep")
# Quarter pipe (curved ramp approximation)
for i in range(6):
var angle := float(i) * 15.0
var x_off := sin(deg_to_rad(angle)) * 5.0
var y_off := (1.0 - cos(deg_to_rad(angle))) * 5.0
_ramp_static(
Vector3(-25 + x_off, y_off + 0.2, 8 + float(i) * 0.5),
Vector3(8, 0.3, 2),
Vector3(angle, 0, 0),
Color(0.7, 0.4, 0.2),
"QuarterPipe_%d" % i
)
# ── Platforms ─────────────────────────────────────────────────────────────────
func _build_platforms() -> void:
var plat_color := Color(0.2, 0.45, 0.55) # Teal
# Low platforms (jump height test)
_box_static(Vector3(10, 0.6, -20), Vector3(4, 1.2, 4), plat_color, "Plat_Low_1")
_box_static(Vector3(16, 0.6, -20), Vector3(4, 1.2, 4), plat_color, "Plat_Low_2")
_box_static(Vector3(22, 0.6, -20), Vector3(4, 1.2, 4), plat_color, "Plat_Low_3")
# Medium platforms (double jump required)
var med_color := Color(0.25, 0.5, 0.4)
_box_static(Vector3(10, 2.5, -28), Vector3(3, 0.5, 3), med_color, "Plat_Med_1")
_box_static(Vector3(16, 3.5, -28), Vector3(3, 0.5, 3), med_color, "Plat_Med_2")
_box_static(Vector3(22, 4.5, -28), Vector3(3, 0.5, 3), med_color, "Plat_Med_3")
_box_static(Vector3(28, 5.5, -28), Vector3(3, 0.5, 3), med_color, "Plat_Med_4")
# High tower
_box_static(Vector3(28, 4.0, -20), Vector3(5, 8.0, 5), Color(0.3, 0.3, 0.5), "Tower_1")
# Floating platforms (dash required)
var dash_color := Color(0.6, 0.2, 0.5) # Purple
_box_static(Vector3(10, 5.0, -35), Vector3(2.5, 0.3, 2.5), dash_color, "Plat_Dash_1")
_box_static(Vector3(18, 5.0, -35), Vector3(2.5, 0.3, 2.5), dash_color, "Plat_Dash_2")
_box_static(Vector3(26, 5.0, -35), Vector3(2.5, 0.3, 2.5), dash_color, "Plat_Dash_3")
# ── Wall Run Corridor ────────────────────────────────────────────────────────
func _build_wall_run_corridor() -> void:
var wall_color := Color(0.45, 0.25, 0.55) # Purple walls
# Two parallel walls — spaced for wall running
# Left wall
_box_static(Vector3(-5, 5, 15), Vector3(0.5, 10, 25), wall_color, "WallRun_Left")
# Right wall
_box_static(Vector3(5, 5, 15), Vector3(0.5, 10, 25), wall_color, "WallRun_Right")
# Obstacles in corridor to jump over
_box_static(Vector3(0, 0.75, 10), Vector3(10, 1.5, 1), Color(0.6, 0.3, 0.3), "Obstacle_1")
_box_static(Vector3(0, 0.75, 20), Vector3(10, 1.5, 1), Color(0.6, 0.3, 0.3), "Obstacle_2")
# Gap in floor (forces wall run)
# Just raise the floor slightly on each side with a gap
_box_static(Vector3(-3, -0.75, 25), Vector3(4, 0.5, 4), Color(0.22, 0.22, 0.25), "WallRun_Floor_L")
_box_static(Vector3(3, -0.75, 25), Vector3(4, 0.5, 4), Color(0.22, 0.22, 0.25), "WallRun_Floor_R")
# Label pillar at entrance
_box_static(Vector3(0, 2, 2), Vector3(1, 4, 1), Color(0.55, 0.35, 0.65), "WallRun_Marker")
# ── Speed Corridor ────────────────────────────────────────────────────────────
func _build_speed_corridor() -> void:
# Long straight for sprint→slide→bhop chains
var corridor_color := Color(0.2, 0.35, 0.2) # Dark green
# Side rails
_box_static(Vector3(30, 1.5, 0), Vector3(0.3, 3, 60), corridor_color, "SpeedRail_Left")
_box_static(Vector3(38, 1.5, 0), Vector3(0.3, 3, 60), corridor_color, "SpeedRail_Right")
# Speed bumps (small obstacles to hop over)
for i in range(-25, 26, 10):
_box_static(Vector3(34, 0.15, float(i)), Vector3(8, 0.3, 0.5), Color(0.8, 0.8, 0.2), "SpeedBump_%d" % i)
# ── Lighting ──────────────────────────────────────────────────────────────────
func _build_lighting() -> void:
var sun := DirectionalLight3D.new()
sun.name = "Sun"
sun.position = Vector3(0, 10, 0)
sun.rotation_degrees = Vector3(50, 0, 0)
sun.light_color = Color(1, 0.95, 0.85)
sun.light_energy = 1.1
sun.rotation_degrees = Vector3(-50, 30, 0)
sun.light_color = Color(1.0, 0.95, 0.85)
sun.light_energy = 1.2
sun.shadow_enabled = true
sun.directional_shadow_mode = DirectionalLight3D.SHADOW_PARALLEL_4_SPLITS
sun.directional_shadow_max_distance = 100.0
add_child(sun)
# Fill light (opposite side)
var fill := DirectionalLight3D.new()
fill.name = "FillLight"
fill.rotation_degrees = Vector3(-30, -150, 0)
fill.light_color = Color(0.6, 0.7, 0.9)
fill.light_energy = 0.3
fill.shadow_enabled = false
add_child(fill)
# ── Player ────────────────────────────────────────────────────────────────────
func _build_player() -> void:
var player := CharacterBody3D.new()
player.name = "Player"
player.position = Vector3(0, 1.4, 8)
player.position = Vector3(0, 1.0, 8)
add_child(player)
var shape := CollisionShape3D.new()
shape.shape = CapsuleShape3D.new()
shape.shape.radius = 0.4
shape.shape.height = 1.8
player.add_child(shape)
# Collision shape
var col_shape := CollisionShape3D.new()
col_shape.name = "CollisionShape3D"
var capsule := CapsuleShape3D.new()
capsule.radius = 0.4
capsule.height = 1.8
col_shape.shape = capsule
player.add_child(col_shape)
# Apply movement controller script
var mover_script = preload("res://movement/player_movement_controller.gd")
if mover_script:
player.set_script(mover_script)
# Movement State Machine
var sm := Node.new()
sm.name = "MovementStateMachine"
player.add_child(sm)
@@ -114,6 +293,7 @@ func _build_player() -> void:
if sm_script:
sm.set_script(sm_script)
# Add all movement states
var state_scripts := {
"ground": "res://movement/states/state_ground.gd",
"air": "res://movement/states/state_air.gd",
@@ -130,39 +310,175 @@ func _build_player() -> void:
if st_script:
st.set_script(st_script)
# FPS Camera Rig (HeadPivot → Camera3D)
var head_pivot := Node3D.new()
head_pivot.name = "HeadPivot"
head_pivot.position = Vector3(0, 0.7, 0) # Eye height
player.add_child(head_pivot)
var camera := Camera3D.new()
camera.name = "Camera3D"
camera.position = Vector3(0, 1.7, -3)
player.add_child(camera)
camera.current = true
camera.fov = 90.0
head_pivot.add_child(camera)
# set_script() does not call any callbacks; bootstrap them manually after children are in place
if player.has_method("_ready"):
player._ready()
var rig_script = preload("res://characters/player/fps_camera_rig.gd")
if rig_script:
head_pivot.set_script(rig_script)
# Store original capsule height
sm.original_capsule_height = capsule.height
# ── Bootstrap: set_script() doesn't trigger _ready() ─────────────────
# Set references BEFORE calling _ready() since sm._ready() will switch_to("ground")
# which needs machine.player to already be set.
sm.player = player
sm.params = player.params
head_pivot.params = player.params
# Initialize in dependency order: machine (enters initial state) → player → camera
if sm.has_method("_ready"):
sm._ready()
if player.has_method("_ensure_process"):
player._ensure_process()
if sm.has_method("set_physics_process"):
sm.set_physics_process(true)
if player.has_method("_ready"):
player._ready()
if head_pivot.has_method("_ready"):
head_pivot._ready()
# Ensure processing is enabled
player.set_physics_process(true)
player.set_process(true)
sm.set_physics_process(true)
_player = player
func _build_ui() -> void:
# ── HUD ───────────────────────────────────────────────────────────────────────
func _build_hud() -> void:
var canvas := CanvasLayer.new()
canvas.name = "UI"
add_child(canvas)
var speed := Label.new()
speed.name = "SpeedLabel"
speed.offset_left = 16
speed.offset_top = 16
speed.offset_right = 520
speed.offset_bottom = 48
speed.text = "Speed: 0.0 m/s | State: --- | Chain: +0%"
canvas.add_child(speed)
# ── Crosshair ─────────────────────────────────────────────────────────
var crosshair := Control.new()
crosshair.name = "Crosshair"
crosshair.set_anchors_preset(Control.PRESET_CENTER)
crosshair.custom_minimum_size = Vector2(20, 20)
canvas.add_child(crosshair)
var ch_dot := ColorRect.new()
ch_dot.name = "Dot"
ch_dot.color = Color(1, 1, 1, 0.8)
ch_dot.size = Vector2(4, 4)
ch_dot.position = Vector2(-2, -2)
crosshair.add_child(ch_dot)
# Crosshair lines
for data in [
{"pos": Vector2(-10, -1), "size": Vector2(6, 2)}, # Left
{"pos": Vector2(4, -1), "size": Vector2(6, 2)}, # Right
{"pos": Vector2(-1, -10), "size": Vector2(2, 6)}, # Top
{"pos": Vector2(-1, 4), "size": Vector2(2, 6)}, # Bottom
]:
var line := ColorRect.new()
line.color = Color(1, 1, 1, 0.6)
line.position = data["pos"]
line.size = data["size"]
crosshair.add_child(line)
# ── Info panel background ─────────────────────────────────────────────
var panel := PanelContainer.new()
panel.name = "InfoPanel"
panel.offset_left = 12
panel.offset_top = 12
panel.offset_right = 400
panel.offset_bottom = 120
var panel_style := StyleBoxFlat.new()
panel_style.bg_color = Color(0, 0, 0, 0.55)
panel_style.corner_radius_top_left = 8
panel_style.corner_radius_top_right = 8
panel_style.corner_radius_bottom_left = 8
panel_style.corner_radius_bottom_right = 8
panel_style.content_margin_left = 12
panel_style.content_margin_top = 8
panel_style.content_margin_right = 12
panel_style.content_margin_bottom = 8
panel.add_theme_stylebox_override("panel", panel_style)
canvas.add_child(panel)
var vbox := VBoxContainer.new()
vbox.name = "InfoVBox"
panel.add_child(vbox)
_speed_label = Label.new()
_speed_label.name = "SpeedLabel"
_speed_label.text = "Speed: 0.0 m/s"
_speed_label.add_theme_font_size_override("font_size", 18)
_speed_label.add_theme_color_override("font_color", Color(0.3, 1.0, 0.5))
vbox.add_child(_speed_label)
_state_label = Label.new()
_state_label.name = "StateLabel"
_state_label.text = "State: ground"
_state_label.add_theme_font_size_override("font_size", 16)
_state_label.add_theme_color_override("font_color", Color(0.7, 0.85, 1.0))
vbox.add_child(_state_label)
_chain_label = Label.new()
_chain_label.name = "ChainLabel"
_chain_label.text = "Chain: 0 (+0%)"
_chain_label.add_theme_font_size_override("font_size", 16)
_chain_label.add_theme_color_override("font_color", Color(1.0, 0.8, 0.3))
vbox.add_child(_chain_label)
# ── Help text ─────────────────────────────────────────────────────────
var help_panel := PanelContainer.new()
help_panel.name = "HelpPanel"
help_panel.set_anchors_preset(Control.PRESET_BOTTOM_LEFT)
help_panel.offset_left = 12
help_panel.offset_top = -90
help_panel.offset_right = 620
help_panel.offset_bottom = -12
var help_style := StyleBoxFlat.new()
help_style.bg_color = Color(0, 0, 0, 0.4)
help_style.corner_radius_top_left = 6
help_style.corner_radius_top_right = 6
help_style.corner_radius_bottom_left = 6
help_style.corner_radius_bottom_right = 6
help_style.content_margin_left = 10
help_style.content_margin_top = 6
help_style.content_margin_right = 10
help_style.content_margin_bottom = 6
help_panel.add_theme_stylebox_override("panel", help_style)
canvas.add_child(help_panel)
var help := Label.new()
help.name = "HelpLabel"
help.offset_left = 16
help.offset_top = 52
help.offset_right = 720
help.offset_bottom = 108
help.text = "WASD Move | Sprint=Shift | Slide=Ctrl+S | Double-Jump=Space twice | Dash=LShift | Wall-Jump=Jump at wall"
canvas.add_child(help)
help.text = "WASD Move | Shift Sprint | Space Jump | Double-tap Space Double Jump\nCtrl+Sprint Slide | LShift Dash (air) | Wall Run: run alongside walls | ESC toggle mouse"
help.add_theme_font_size_override("font_size", 13)
help.add_theme_color_override("font_color", Color(0.8, 0.8, 0.8, 0.7))
help_panel.add_child(help)
# ── HUD Update ────────────────────────────────────────────────────────────────
func _process(_delta: float) -> void:
if not _player or not is_instance_valid(_player):
return
var vel: Vector3 = _player.velocity
var hspeed := Vector2(vel.x, vel.z).length()
var total_speed := vel.length()
if _speed_label:
_speed_label.text = "Speed: %.1f m/s (total: %.1f)" % [hspeed, total_speed]
if _state_label:
var sm = _player.get_node_or_null("MovementStateMachine")
if sm:
_state_label.text = "State: %s" % sm.current_state
if _chain_label:
var sm = _player.get_node_or_null("MovementStateMachine")
if sm:
_chain_label.text = "Chain: %d (+%d%%)" % [sm.chain_count, int(sm.current_chain_bonus * 100)]