feat: implement player movement controller and state machine with dash, slide, and wall-run mechanics
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
extends Node3D
|
||||
class_name FPSCameraRig
|
||||
|
||||
## FPS camera rig — handles mouse look, head bob, FOV kick, wall-run tilt.
|
||||
## Attach as child of the CharacterBody3D player. Camera3D is a child of this node.
|
||||
|
||||
@export var sensitivity: float = 0.002
|
||||
@export var pitch_limit: float = 89.0
|
||||
|
||||
var params: MovementParams
|
||||
var camera: Camera3D
|
||||
var _bob_timer: float = 0.0
|
||||
var _target_tilt: float = 0.0
|
||||
var _current_tilt: float = 0.0
|
||||
var _target_fov: float = 90.0
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
||||
camera = get_node_or_null("Camera3D")
|
||||
if camera and params:
|
||||
camera.fov = params.base_fov
|
||||
_target_fov = params.base_fov
|
||||
# Explicitly enable callbacks — set_script() at runtime doesn't auto-register them
|
||||
set_process_input(true)
|
||||
set_process(true)
|
||||
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
||||
var sens := sensitivity
|
||||
if params:
|
||||
sens = params.mouse_sensitivity
|
||||
# Yaw: rotate the player (parent)
|
||||
get_parent().rotate_y(-event.relative.x * sens)
|
||||
# Pitch: rotate this pivot
|
||||
rotation.x = clampf(rotation.x - event.relative.y * sens, deg_to_rad(-pitch_limit), deg_to_rad(pitch_limit))
|
||||
|
||||
# Toggle mouse capture with Escape
|
||||
if event is InputEventKey and event.pressed and event.keycode == KEY_ESCAPE:
|
||||
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
||||
else:
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if not camera or not params:
|
||||
return
|
||||
|
||||
var player: CharacterBody3D = get_parent() as CharacterBody3D
|
||||
if not player:
|
||||
return
|
||||
|
||||
# ── FOV kick ──────────────────────────────────────────────────────────
|
||||
_target_fov = params.base_fov
|
||||
var hspeed := Vector2(player.velocity.x, player.velocity.z).length()
|
||||
|
||||
# Dash FOV (highest priority)
|
||||
if hspeed > params.dash_speed * 0.8:
|
||||
_target_fov = params.dash_fov
|
||||
elif hspeed > params.sprint_speed * 0.8:
|
||||
_target_fov = params.sprint_fov
|
||||
|
||||
camera.fov = lerpf(camera.fov, _target_fov, 1.0 - exp(-params.fov_lerp_speed * delta))
|
||||
|
||||
# ── Head bob ──────────────────────────────────────────────────────────
|
||||
if player.is_on_floor() and hspeed > 1.0:
|
||||
_bob_timer += delta * params.head_bob_frequency * (hspeed / params.walk_speed)
|
||||
var bob_y := sin(_bob_timer) * params.head_bob_amplitude
|
||||
var bob_x := cos(_bob_timer * 0.5) * params.head_bob_amplitude * 0.5
|
||||
camera.position.y = lerp(camera.position.y, bob_y, 0.3)
|
||||
camera.position.x = lerp(camera.position.x, bob_x, 0.3)
|
||||
else:
|
||||
_bob_timer = 0.0
|
||||
camera.position.y = lerp(camera.position.y, 0.0, 0.15)
|
||||
camera.position.x = lerp(camera.position.x, 0.0, 0.15)
|
||||
|
||||
# ── Wall-run tilt ─────────────────────────────────────────────────────
|
||||
_current_tilt = lerpf(_current_tilt, _target_tilt, 1.0 - exp(-params.wall_run_tilt_speed * delta))
|
||||
camera.rotation.z = deg_to_rad(_current_tilt)
|
||||
|
||||
|
||||
## Called by the movement system to set wall-run tilt direction.
|
||||
## side: -1.0 (left wall), 1.0 (right wall), 0.0 (no tilt)
|
||||
func set_wall_tilt(side: float) -> void:
|
||||
if params:
|
||||
_target_tilt = side * params.wall_run_tilt_angle
|
||||
else:
|
||||
_target_tilt = side * 12.0
|
||||
|
||||
|
||||
## Called to reset tilt (e.g., on landing).
|
||||
func clear_wall_tilt() -> void:
|
||||
_target_tilt = 0.0
|
||||
@@ -0,0 +1 @@
|
||||
uid://b1nbudf4cu2dl
|
||||
+364
-48
@@ -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)
|
||||
|
||||
|
||||
# ── 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"):
|
||||
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)]
|
||||
|
||||
+36
-21
@@ -5,20 +5,24 @@ class_name MovementParams
|
||||
@export var walk_speed: float = 7.0
|
||||
@export var sprint_speed: float = 11.0
|
||||
@export var crouch_speed: float = 4.0
|
||||
@export var ground_friction: float = 8.0
|
||||
@export var ground_acceleration: float = 30.0
|
||||
@export var ground_deceleration: float = 20.0
|
||||
@export var ground_friction: float = 10.0
|
||||
@export var ground_acceleration: float = 50.0
|
||||
@export var ground_deceleration: float = 40.0
|
||||
|
||||
# ── Jump / Air ────────────────────────────────────────────────────────────────
|
||||
@export var jump_velocity: float = 8.5
|
||||
@export var coyote_time: float = 0.15
|
||||
@export var jump_buffer: float = 0.12
|
||||
@export var air_control: float = 0.3
|
||||
@export var air_acceleration: float = 25.0
|
||||
@export var air_control: float = 0.35
|
||||
@export var air_acceleration: float = 30.0
|
||||
@export var max_air_speed: float = 16.0
|
||||
@export var gravity: float = 20.0
|
||||
@export var fall_multiplier: float = 2.0
|
||||
@export var low_jump_multiplier: float = 1.5
|
||||
@export var gravity: float = 22.0
|
||||
@export var fall_multiplier: float = 2.2
|
||||
@export var low_jump_multiplier: float = 1.6
|
||||
|
||||
# ── Air Strafe (Quake-style) ──────────────────────────────────────────────────
|
||||
@export var air_strafe_accel: float = 80.0
|
||||
@export var air_strafe_max_gain: float = 1.0
|
||||
|
||||
# ── Bunny Hop ─────────────────────────────────────────────────────────────────
|
||||
@export var bunny_hop_impulse: float = 1.15
|
||||
@@ -27,27 +31,27 @@ class_name MovementParams
|
||||
|
||||
# ── Slide ─────────────────────────────────────────────────────────────────────
|
||||
@export var slide_speed: float = 14.0
|
||||
@export var slide_friction: float = 0.88
|
||||
@export var slide_min_speed: float = 12.0
|
||||
@export var slide_friction: float = 0.96
|
||||
@export var slide_min_speed: float = 5.0
|
||||
@export var slide_duration: float = 0.8
|
||||
@export var slide_into_sprint_delay: float = 0.15
|
||||
@export var slide_cooldown: float = 0.4 # Prevents rapid slide re-entry
|
||||
@export var slide_jump_speed_boost: float = 1.5 # Extra m/s added when jumping out of slide
|
||||
|
||||
# ── Wall Run ──────────────────────────────────────────────────────────────────
|
||||
@export var wall_run_speed: float = 12.0
|
||||
@export var wall_run_vertical_speed: float = 8.0
|
||||
@export var wall_run_vertical_speed: float = 4.0
|
||||
@export var wall_run_duration: float = 1.2
|
||||
@export var wall_run_gravity: float = -2.0
|
||||
@export var wall_run_auto_jump_speed: float = 15.0
|
||||
@export var wall_run_gravity: float = 6.0
|
||||
@export var wall_run_auto_jump_speed: float = 10.0
|
||||
@export var wall_run_jump_horizontal: float = 14.0
|
||||
@export var wall_run_jump_off_normal: float = 9.0
|
||||
@export var wall_angle_threshold: float = 70.0
|
||||
@export var wall_detect_distance: float = 1.0
|
||||
@export var wall_ray_up_height: float = 1.2
|
||||
@export var wall_ray_down_height: float = 0.4
|
||||
@export var wall_detect_distance: float = 0.7
|
||||
@export var wall_ray_up_height: float = 0.6
|
||||
@export var wall_ray_down_height: float = -0.3
|
||||
|
||||
# ── Wall Cling ────────────────────────────────────────────────────────────────
|
||||
@export var wall_cling_horizontal_speed: float = 0.0
|
||||
@export var wall_cling_vertical_speed: float = 0.0
|
||||
@export var wall_cling_slide_speed: float = -1.5
|
||||
@export var wall_cling_stamina_drain: float = 2.0
|
||||
@export var wall_cling_max_stamina: float = 2.0
|
||||
|
||||
@@ -68,9 +72,20 @@ class_name MovementParams
|
||||
|
||||
# ── Double Jump ───────────────────────────────────────────────────────────────
|
||||
@export var double_jump_velocity: float = 8.0
|
||||
@export var double_jump_max_count: int = 1
|
||||
@export var double_jump_max_count: int = 2 # 1 base jump + 1 double
|
||||
|
||||
# ── Chaining ──────────────────────────────────────────────────────────────────
|
||||
@export var chain_bonus_per_success: float = 0.05
|
||||
@export var chain_bonus_cap: float = 0.50
|
||||
@export var chain_window: float = 0.4
|
||||
@export var chain_window: float = 1.5
|
||||
|
||||
# ── Camera ────────────────────────────────────────────────────────────────────
|
||||
@export var mouse_sensitivity: float = 0.002
|
||||
@export var base_fov: float = 90.0
|
||||
@export var sprint_fov: float = 100.0
|
||||
@export var dash_fov: float = 110.0
|
||||
@export var fov_lerp_speed: float = 10.0
|
||||
@export var head_bob_frequency: float = 12.0
|
||||
@export var head_bob_amplitude: float = 0.04
|
||||
@export var wall_run_tilt_angle: float = 12.0
|
||||
@export var wall_run_tilt_speed: float = 8.0
|
||||
|
||||
@@ -3,7 +3,6 @@ class_name MovementStateMachine
|
||||
|
||||
## Generic state machine for player movement.
|
||||
## Each state is a Node child; the machine switches between them.
|
||||
## States must call `self.transition_to(new_state_name)` to change states.
|
||||
|
||||
signal state_changed(from_state: String, to_state: String)
|
||||
signal movement_event(event_name: String, data: Dictionary)
|
||||
@@ -13,13 +12,19 @@ var states: Dictionary = {}
|
||||
|
||||
var player: CharacterBody3D
|
||||
var params: MovementParams
|
||||
|
||||
# ── Input (set by controller each physics tick) ───────────────────────────────
|
||||
var input_dir: Vector2 = Vector2.ZERO
|
||||
var wish_dir_world: Vector3 = Vector3.ZERO
|
||||
var input_jump_pressed: bool = false
|
||||
var input_jump_just_pressed: bool = false
|
||||
var input_sprint: bool = false
|
||||
var input_crouch: bool = false
|
||||
var input_dash: bool = false
|
||||
|
||||
# ── State tracking ────────────────────────────────────────────────────────────
|
||||
var wall_normal: Vector3 = Vector3.ZERO
|
||||
var wall_side: float = 0.0 # -1 left, +1 right, 0 none
|
||||
var on_ground: bool = false
|
||||
var last_ground_time: float = 0.0
|
||||
var jump_buffer_time: float = 0.0
|
||||
@@ -28,6 +33,10 @@ var current_jump_count: int = 0
|
||||
var chain_timer: float = 0.0
|
||||
var chain_count: int = 0
|
||||
var current_chain_bonus: float = 0.0
|
||||
var original_capsule_height: float = 1.8
|
||||
var slide_cooldown_timer: float = 0.0 # Prevents instant slide re-entry
|
||||
var wall_cooldown_timer: float = 0.0 # Prevents instant re-attachment after wall jump
|
||||
var is_crouched: bool = false
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
@@ -44,52 +53,73 @@ func _physics_process(delta: float) -> void:
|
||||
if on_ground:
|
||||
coyote_timer = params.coyote_time
|
||||
else:
|
||||
coyote_timer -= delta
|
||||
coyote_timer = maxf(coyote_timer - delta, 0.0)
|
||||
|
||||
# Update jump buffer
|
||||
if input_jump_just_pressed:
|
||||
jump_buffer_time = params.jump_buffer
|
||||
else:
|
||||
jump_buffer_time -= delta
|
||||
jump_buffer_time = maxf(jump_buffer_time - delta, 0.0)
|
||||
|
||||
# Update chain timer
|
||||
if chain_timer > 0.0:
|
||||
chain_timer -= delta
|
||||
else:
|
||||
if chain_timer <= 0.0:
|
||||
chain_count = 0
|
||||
current_chain_bonus = 0.0
|
||||
|
||||
# Manage global crouch state
|
||||
var want_crouch = input_crouch
|
||||
if current_state == "slide":
|
||||
want_crouch = true
|
||||
if want_crouch != is_crouched:
|
||||
is_crouched = want_crouch
|
||||
_apply_crouch(is_crouched)
|
||||
|
||||
# Update slide cooldown
|
||||
if slide_cooldown_timer > 0.0:
|
||||
slide_cooldown_timer = maxf(slide_cooldown_timer - delta, 0.0)
|
||||
|
||||
# Update wall cooldown
|
||||
if wall_cooldown_timer > 0.0:
|
||||
wall_cooldown_timer = maxf(wall_cooldown_timer - delta, 0.0)
|
||||
|
||||
if current_state.is_empty():
|
||||
return
|
||||
if not states.has(current_state):
|
||||
return
|
||||
var state_node = states[current_state]
|
||||
if state_node.has_method("update"):
|
||||
state_node.update(delta)
|
||||
# Apply gravity that was accumulated during state update, regardless of state.
|
||||
# CharacterBody3D handles gravity automatically via move_and_slide but
|
||||
# we tune it per-position here.
|
||||
|
||||
|
||||
func switch_to(new_state_name: String, data: Dictionary = {}) -> void:
|
||||
if new_state_name == current_state:
|
||||
return
|
||||
if not states.has(new_state_name):
|
||||
push_warning("MovementStateMachine: state '%s' not found" % new_state_name)
|
||||
return
|
||||
var prev = current_state
|
||||
# Exit only the current state
|
||||
if states.has(prev):
|
||||
var old_state = states[prev]
|
||||
if old_state.has_method("exit"):
|
||||
old_state.exit()
|
||||
# Enter the new state
|
||||
current_state = new_state_name
|
||||
for key in states:
|
||||
var st = states[key]
|
||||
if key == new_state_name:
|
||||
st.enter(data)
|
||||
else:
|
||||
st.exit()
|
||||
var new_state = states[new_state_name]
|
||||
if new_state.has_method("enter"):
|
||||
new_state.enter(data)
|
||||
state_changed.emit(prev, new_state_name)
|
||||
|
||||
|
||||
func register_chain_mechanic(_mechanic_name: String) -> void:
|
||||
if chain_timer > 0.0 and chain_count > 0:
|
||||
chain_count = min(chain_count + 1, 10)
|
||||
chain_count = mini(chain_count + 1, 10)
|
||||
else:
|
||||
chain_count = 1
|
||||
chain_timer = params.chain_window
|
||||
current_chain_bonus = min(
|
||||
current_chain_bonus = minf(
|
||||
float(chain_count) * params.chain_bonus_per_success,
|
||||
params.chain_bonus_cap
|
||||
)
|
||||
@@ -101,3 +131,71 @@ func register_chain_mechanic(_mechanic_name: String) -> void:
|
||||
|
||||
func get_effective_speed(base_speed: float) -> float:
|
||||
return base_speed * (1.0 + current_chain_bonus)
|
||||
|
||||
|
||||
## Utility: detect wall to left or right of player using horizontal raycasts.
|
||||
## Returns the wall normal and sets wall_side. Returns Vector3.ZERO if no wall.
|
||||
func detect_wall_horizontal() -> Vector3:
|
||||
if not player:
|
||||
return Vector3.ZERO
|
||||
var origin := player.global_position + Vector3.UP * params.wall_ray_up_height
|
||||
var right_dir := player.global_transform.basis.x.normalized()
|
||||
var space_state := player.get_world_3d().direct_space_state
|
||||
|
||||
# Cast two rays: one left, one right
|
||||
for side_data in [{"dir": right_dir, "side": 1.0}, {"dir": -right_dir, "side": -1.0}]:
|
||||
var ray_end: Vector3 = origin + side_data["dir"] * params.wall_detect_distance
|
||||
var ray := PhysicsRayQueryParameters3D.create(origin, ray_end)
|
||||
ray.exclude = [player.get_rid()]
|
||||
var hit := space_state.intersect_ray(ray)
|
||||
if not hit.is_empty():
|
||||
var n: Vector3 = hit.get("normal", Vector3.ZERO)
|
||||
# Wall must be roughly vertical (normal mostly horizontal)
|
||||
if absf(n.y) < 0.3 and n.length_squared() > 0.0:
|
||||
wall_normal = n.normalized()
|
||||
wall_side = side_data["side"]
|
||||
return wall_normal
|
||||
|
||||
# Also cast along movement direction for head-on walls
|
||||
var hvel := Vector3(player.velocity.x, 0.0, player.velocity.z)
|
||||
if hvel.length_squared() > 0.1:
|
||||
var move_dir := hvel.normalized()
|
||||
var perp := move_dir.cross(Vector3.UP).normalized()
|
||||
for side_data in [{"dir": perp, "side": 1.0}, {"dir": -perp, "side": -1.0}]:
|
||||
var ray_end: Vector3 = origin + side_data["dir"] * params.wall_detect_distance
|
||||
var ray := PhysicsRayQueryParameters3D.create(origin, ray_end)
|
||||
ray.exclude = [player.get_rid()]
|
||||
var hit := space_state.intersect_ray(ray)
|
||||
if not hit.is_empty():
|
||||
var n: Vector3 = hit.get("normal", Vector3.ZERO)
|
||||
if absf(n.y) < 0.3 and n.length_squared() > 0.0:
|
||||
wall_normal = n.normalized()
|
||||
wall_side = side_data["side"]
|
||||
return wall_normal
|
||||
|
||||
wall_normal = Vector3.ZERO
|
||||
wall_side = 0.0
|
||||
return Vector3.ZERO
|
||||
|
||||
|
||||
func _apply_crouch(crouched: bool) -> void:
|
||||
var shape_node = null
|
||||
for child in player.get_children():
|
||||
if child is CollisionShape3D and child.shape is CapsuleShape3D:
|
||||
shape_node = child
|
||||
break
|
||||
if not shape_node:
|
||||
return
|
||||
var shape = shape_node.shape as CapsuleShape3D
|
||||
var head = player.get_node_or_null("HeadPivot")
|
||||
|
||||
if crouched:
|
||||
shape.height = original_capsule_height * 0.5
|
||||
shape_node.position.y = -original_capsule_height * 0.25
|
||||
if head:
|
||||
head.position.y = 0.7 - (original_capsule_height * 0.5)
|
||||
else:
|
||||
shape.height = original_capsule_height
|
||||
shape_node.position.y = 0.0
|
||||
if head:
|
||||
head.position.y = 0.7
|
||||
|
||||
@@ -6,9 +6,22 @@ signal chain_updated(count: int, bonus: float)
|
||||
@export var params: MovementParams = preload("res://movement/movement_params.gd").new()
|
||||
|
||||
var _machine: MovementStateMachine = null
|
||||
var head_pivot: Node3D = null # FPSCameraRig node
|
||||
var camera: Camera3D = null
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
var sm := _ensure_machine()
|
||||
if sm:
|
||||
sm.player = self
|
||||
sm.params = params
|
||||
sm.movement_event.connect(_on_movement_event)
|
||||
|
||||
# Find camera rig
|
||||
head_pivot = get_node_or_null("HeadPivot")
|
||||
if head_pivot:
|
||||
camera = head_pivot.get_node_or_null("Camera3D")
|
||||
|
||||
# Explicitly ensure this node ticks after being attached by set_script().
|
||||
func _ensure_process() -> void:
|
||||
set_process(true)
|
||||
set_physics_process(true)
|
||||
|
||||
@@ -25,49 +38,35 @@ func _ensure_machine() -> MovementStateMachine:
|
||||
return null
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
func _physics_process(_delta: float) -> void:
|
||||
var sm := _ensure_machine()
|
||||
if not sm:
|
||||
return
|
||||
|
||||
# Poll input in _physics_process so it's synchronized with the state machine tick
|
||||
sm.input_jump_just_pressed = Input.is_action_just_pressed("jump")
|
||||
sm.input_jump_pressed = Input.is_action_pressed("jump")
|
||||
sm.input_sprint = Input.is_action_pressed("sprint")
|
||||
sm.input_crouch = Input.is_action_pressed("crouch")
|
||||
sm.input_dash = Input.is_action_just_pressed("dash")
|
||||
sm.input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
|
||||
|
||||
# Raw 2D input
|
||||
var raw_input := Input.get_vector("move_left", "move_right", "move_forward", "move_back")
|
||||
|
||||
func _ready() -> void:
|
||||
var sm := _ensure_machine()
|
||||
if sm:
|
||||
sm.player = self
|
||||
sm.params = params
|
||||
sm.movement_event.connect(_on_movement_event)
|
||||
_ensure_process()
|
||||
# Transform input direction by player yaw so movement is camera-relative
|
||||
var forward := -global_transform.basis.z
|
||||
forward.y = 0.0
|
||||
forward = forward.normalized()
|
||||
var right := global_transform.basis.x
|
||||
right.y = 0.0
|
||||
right = right.normalized()
|
||||
|
||||
var world_dir := (forward * (-raw_input.y) + right * raw_input.x)
|
||||
if world_dir.length_squared() > 1.0:
|
||||
world_dir = world_dir.normalized()
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
var sm := _ensure_machine()
|
||||
if not sm:
|
||||
return
|
||||
sm.input_jump_just_pressed = Input.is_action_just_pressed("jump")
|
||||
sm.input_jump_pressed = Input.is_action_pressed("jump")
|
||||
sm.input_sprint = Input.is_action_pressed("sprint")
|
||||
sm.input_crouch = Input.is_action_pressed("crouch")
|
||||
sm.input_dash = Input.is_action_just_pressed("dash")
|
||||
sm.input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
|
||||
|
||||
# DEBUG: emit every frame so we can see whether inputs reach the controller.
|
||||
var p = sm.player
|
||||
print("MOVEMENT_DEBUG state=%s vel=%s input_dir=%s jump=%s sprint=%s crouch=%s dash=%s" % [
|
||||
sm.current_state,
|
||||
p.velocity if p else "NO_PLAYER",
|
||||
sm.input_dir,
|
||||
sm.input_jump_pressed,
|
||||
sm.input_sprint,
|
||||
sm.input_crouch,
|
||||
sm.input_dash,
|
||||
])
|
||||
sm.input_dir = raw_input
|
||||
sm.wish_dir_world = world_dir
|
||||
|
||||
|
||||
func _on_movement_event(ev: String, data: Dictionary) -> void:
|
||||
|
||||
@@ -7,7 +7,7 @@ var params: MovementParams:
|
||||
|
||||
|
||||
func enter(_data: Dictionary = {}) -> void:
|
||||
pass
|
||||
machine.on_ground = false
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
@@ -15,56 +15,89 @@ func exit() -> void:
|
||||
|
||||
|
||||
func update(delta: float) -> void:
|
||||
var vel: Vector3 = machine.player.velocity
|
||||
var player := machine.player
|
||||
var vel: Vector3 = player.velocity
|
||||
|
||||
# Apply standard gravity
|
||||
vel.y -= params.gravity * delta
|
||||
|
||||
# Input direction
|
||||
var input_v := machine.input_dir
|
||||
var wish_dir := Vector3(input_v.x, 0.0, input_v.y)
|
||||
if wish_dir.length_squared() > 1.0:
|
||||
wish_dir = wish_dir.normalized()
|
||||
|
||||
var effective_speed := machine.get_effective_speed(params.max_air_speed)
|
||||
# ── Gravity with variable jump height ─────────────────────────────────
|
||||
var grav := params.gravity
|
||||
if vel.y < 0.0:
|
||||
# Falling: heavier gravity for snappy arcs
|
||||
grav *= params.fall_multiplier
|
||||
elif vel.y > 0.0 and not machine.input_jump_pressed:
|
||||
# Released jump early: cut the jump short
|
||||
grav *= params.low_jump_multiplier
|
||||
vel.y -= grav * delta
|
||||
|
||||
# ── Air control ───────────────────────────────────────────────────────
|
||||
var wish_dir: Vector3 = machine.wish_dir_world
|
||||
var hvel := Vector3(vel.x, 0.0, vel.z)
|
||||
var target_vel := wish_dir * effective_speed * params.air_control
|
||||
var current_speed := hvel.length()
|
||||
|
||||
hvel = hvel.lerp(target_vel, 1.0 - exp(-params.air_acceleration * delta))
|
||||
if wish_dir.length_squared() > 0.01:
|
||||
var target_speed := maxf(current_speed, params.walk_speed)
|
||||
target_speed = minf(target_speed, params.max_air_speed)
|
||||
|
||||
var target_vel := wish_dir * target_speed
|
||||
var new_hvel = hvel.lerp(target_vel, 1.0 - exp(-params.air_control * params.air_acceleration * delta))
|
||||
|
||||
# Prevent speed loss during sharp turns, but allow braking if holding backward
|
||||
if hvel.dot(target_vel) >= 0.0 and current_speed > params.walk_speed:
|
||||
if new_hvel.length_squared() > 0.01:
|
||||
new_hvel = new_hvel.normalized() * current_speed
|
||||
|
||||
hvel = new_hvel
|
||||
else:
|
||||
# No input: slight air drag (very subtle)
|
||||
hvel *= (1.0 - 0.5 * delta)
|
||||
|
||||
vel.x = hvel.x
|
||||
vel.z = hvel.z
|
||||
|
||||
# Bunny hop: if jump pressed and on ground near-peak of arc → extra boost
|
||||
if machine.input_jump_just_pressed and machine.on_ground:
|
||||
vel.y = params.jump_velocity * params.bunny_hop_impulse
|
||||
machine.player.velocity = vel
|
||||
machine.register_chain_mechanic("bunny_hop")
|
||||
machine.current_jump_count += 1
|
||||
machine.on_ground = false
|
||||
machine.switch_to("air")
|
||||
return
|
||||
player.velocity = vel
|
||||
player.move_and_slide()
|
||||
|
||||
# Double Jump
|
||||
if machine.input_jump_just_pressed and not machine.on_ground and machine.current_jump_count < params.double_jump_max_count:
|
||||
vel.y = params.double_jump_velocity
|
||||
machine.player.velocity = vel
|
||||
machine.current_jump_count += 1
|
||||
machine.register_chain_mechanic("double_jump")
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
machine.player.velocity = vel
|
||||
machine.player.move_and_slide()
|
||||
|
||||
# Landing
|
||||
if machine.player.is_on_floor():
|
||||
# ── Landing ───────────────────────────────────────────────────────────
|
||||
if player.is_on_floor():
|
||||
machine.on_ground = true
|
||||
machine.current_jump_count = 0
|
||||
|
||||
# Bunny hop: if jump was buffered or pressed on landing frame
|
||||
if machine.input_jump_pressed or machine.jump_buffer_time > 0.0:
|
||||
var land_speed := Vector3(player.velocity.x, 0.0, player.velocity.z).length()
|
||||
var bhop_speed := minf(land_speed + params.bunny_hop_speed_gain, params.bunny_hop_speed_cap)
|
||||
# Maintain horizontal direction, boost speed
|
||||
var hdir := Vector3(player.velocity.x, 0.0, player.velocity.z)
|
||||
if hdir.length_squared() > 0.01:
|
||||
hdir = hdir.normalized()
|
||||
else:
|
||||
hdir = machine.wish_dir_world
|
||||
player.velocity.x = hdir.x * bhop_speed
|
||||
player.velocity.z = hdir.z * bhop_speed
|
||||
player.velocity.y = params.jump_velocity * params.bunny_hop_impulse
|
||||
machine.current_jump_count = 1
|
||||
machine.on_ground = false
|
||||
machine.jump_buffer_time = 0.0
|
||||
machine.register_chain_mechanic("bunny_hop")
|
||||
# Stay in air state
|
||||
return
|
||||
|
||||
machine.switch_to("ground")
|
||||
return
|
||||
|
||||
# Wall run transition
|
||||
if not machine.wall_normal.is_zero_approx():
|
||||
# ── Double Jump ───────────────────────────────────────────────────────
|
||||
if machine.input_jump_just_pressed and machine.current_jump_count < params.double_jump_max_count:
|
||||
player.velocity.y = params.double_jump_velocity
|
||||
machine.current_jump_count += 1
|
||||
machine.register_chain_mechanic("double_jump")
|
||||
return
|
||||
|
||||
# ── Wall run transition ───────────────────────────────────────────────
|
||||
var wall_n := machine.detect_wall_horizontal()
|
||||
if wall_n != Vector3.ZERO and machine.input_dir.length() > 0.1 and machine.wall_cooldown_timer <= 0.0:
|
||||
machine.switch_to("wall_run")
|
||||
return
|
||||
|
||||
# ── Dash ──────────────────────────────────────────────────────────────
|
||||
if machine.input_dash:
|
||||
machine.switch_to("dash")
|
||||
return
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -7,7 +7,15 @@ var params: MovementParams:
|
||||
|
||||
|
||||
func enter(_data: Dictionary = {}) -> void:
|
||||
pass
|
||||
machine.on_ground = true
|
||||
machine.current_jump_count = 0
|
||||
# Clear wall tilt on landing
|
||||
var rig = _get_camera_rig()
|
||||
if rig:
|
||||
rig.clear_wall_tilt()
|
||||
|
||||
# Ensure capsule is correct when entering ground state
|
||||
_update_capsule_height()
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
@@ -15,96 +23,113 @@ func exit() -> void:
|
||||
|
||||
|
||||
func update(delta: float) -> void:
|
||||
var vel: Vector3 = machine.player.velocity
|
||||
|
||||
# Input direction
|
||||
var input_v := machine.input_dir
|
||||
var wish_dir := Vector3(input_v.x, 0.0, input_v.y)
|
||||
if wish_dir.length_squared() > 1.0:
|
||||
wish_dir = wish_dir.normalized()
|
||||
var player := machine.player
|
||||
var vel: Vector3 = player.velocity
|
||||
|
||||
# ── Determine target speed ────────────────────────────────────────────
|
||||
var wish_dir: Vector3 = machine.wish_dir_world
|
||||
var speed := params.walk_speed
|
||||
|
||||
if machine.input_sprint and wish_dir.length_squared() > 0.1:
|
||||
if machine.input_crouch:
|
||||
speed = params.crouch_speed
|
||||
elif machine.input_sprint and wish_dir.length_squared() > 0.01:
|
||||
speed = params.sprint_speed
|
||||
|
||||
var effective_speed := machine.get_effective_speed(speed)
|
||||
|
||||
# Horizontal movement
|
||||
# ── Horizontal movement (exponential interpolation for smoothness) ───
|
||||
var hvel := Vector3(vel.x, 0.0, vel.z)
|
||||
var target_vel := wish_dir * effective_speed
|
||||
|
||||
if wish_dir.length_squared() > 0.0:
|
||||
var accel := params.ground_acceleration
|
||||
hvel = hvel.lerp(target_vel, 1.0 - exp(-accel * delta))
|
||||
hvel = hvel.lerp(target_vel, 1.0 - exp(-params.ground_acceleration * delta))
|
||||
else:
|
||||
hvel = hvel.lerp(Vector3.ZERO, 1.0 - exp(-params.ground_deceleration * delta))
|
||||
|
||||
vel.x = hvel.x
|
||||
vel.z = hvel.z
|
||||
|
||||
# Gravity (we apply tuned gravity so we don't double-apply from _velocity)
|
||||
var actual_gravity := params.gravity
|
||||
if vel.y < 0.0:
|
||||
vel.y += actual_gravity * params.fall_multiplier * delta
|
||||
elif vel.y > 0.0 and not machine.input_jump_pressed:
|
||||
vel.y += actual_gravity * params.low_jump_multiplier * delta
|
||||
# ── Gravity (correct direction: downward) ─────────────────────────────
|
||||
if not player.is_on_floor():
|
||||
vel.y -= params.gravity * delta
|
||||
else:
|
||||
vel.y -= actual_gravity * delta
|
||||
# Snap to floor: small downward velocity keeps is_on_floor() stable
|
||||
vel.y = -0.5
|
||||
|
||||
machine.player.velocity = vel
|
||||
machine.player.move_and_slide()
|
||||
player.velocity = vel
|
||||
player.move_and_slide()
|
||||
|
||||
var on_wall := detect_wall()
|
||||
if on_wall and machine.input_dir.length() > 0.0:
|
||||
machine.wall_normal = on_wall
|
||||
machine.switch_to("wall_run")
|
||||
_update_capsule_height()
|
||||
|
||||
if machine.input_jump_just_pressed and machine.coyote_timer > 0.0:
|
||||
machine.player.velocity.y = params.jump_velocity
|
||||
machine.current_jump_count = 1
|
||||
machine.on_ground = false
|
||||
machine.register_chain_mechanic("jump")
|
||||
machine.switch_to("air")
|
||||
|
||||
if machine.player.is_on_floor():
|
||||
# ── Update ground status ──────────────────────────────────────────────
|
||||
if player.is_on_floor():
|
||||
machine.on_ground = true
|
||||
machine.last_ground_time = machine.get_process_delta_time()
|
||||
machine.current_jump_count = 0
|
||||
else:
|
||||
machine.on_ground = false
|
||||
if machine.input_jump_just_pressed and machine.jump_buffer_time > 0.0:
|
||||
machine.player.velocity.y = params.jump_velocity
|
||||
|
||||
# ── Jump (coyote time + jump buffer) ──────────────────────────────────
|
||||
if machine.input_jump_just_pressed and machine.coyote_timer > 0.0:
|
||||
player.velocity.y = params.jump_velocity
|
||||
machine.current_jump_count = 1
|
||||
machine.on_ground = false
|
||||
machine.coyote_timer = 0.0
|
||||
machine.register_chain_mechanic("jump")
|
||||
machine.switch_to("air")
|
||||
elif vel.y < 0.0:
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# Slide
|
||||
if machine.input_crouch and machine.input_sprint and machine.player.velocity.length() > params.slide_min_speed:
|
||||
# Jump buffer: player pressed jump just before landing
|
||||
if machine.on_ground and machine.jump_buffer_time > 0.0:
|
||||
player.velocity.y = params.jump_velocity
|
||||
machine.current_jump_count = 1
|
||||
machine.on_ground = false
|
||||
machine.jump_buffer_time = 0.0
|
||||
machine.register_chain_mechanic("jump")
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# ── Falling off ledge → air state ─────────────────────────────────────
|
||||
if not machine.on_ground and machine.coyote_timer <= 0.0:
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# ── Slide (crouch while moving fast enough) ───────────────────────────
|
||||
if machine.input_crouch and machine.slide_cooldown_timer <= 0.0:
|
||||
var hspeed := Vector3(player.velocity.x, 0.0, player.velocity.z).length()
|
||||
if hspeed > params.slide_min_speed:
|
||||
machine.switch_to("slide")
|
||||
return
|
||||
|
||||
# Dash
|
||||
if machine.input_dash and machine.player.velocity.length() > 0.0:
|
||||
# ── Wall run check ────────────────────────────────────────────────────
|
||||
var wall_n := machine.detect_wall_horizontal()
|
||||
if wall_n != Vector3.ZERO and machine.input_dir.length() > 0.1:
|
||||
if not player.is_on_floor():
|
||||
machine.switch_to("wall_run")
|
||||
return
|
||||
|
||||
# ── Dash ──────────────────────────────────────────────────────────────
|
||||
if machine.input_dash:
|
||||
var hspeed := Vector3(player.velocity.x, 0.0, player.velocity.z).length()
|
||||
if hspeed > 0.1 or machine.input_dir.length() > 0.1:
|
||||
machine.switch_to("dash")
|
||||
return
|
||||
|
||||
|
||||
func detect_wall() -> Vector3:
|
||||
var origin := machine.player.global_position
|
||||
var hvel := Vector3(machine.player.velocity.x, 0.0, machine.player.velocity.z).normalized()
|
||||
if hvel.length_squared() < 0.01:
|
||||
hvel = Vector3.FORWARD
|
||||
var space_state := machine.player.get_world_3d().direct_space_state
|
||||
for offset_sign in [1.0, -1.0]:
|
||||
var dir: Vector3 = (hvel.cross(Vector3.UP) * offset_sign).normalized()
|
||||
var offset: Vector3 = dir * 0.4
|
||||
var ray := PhysicsRayQueryParameters3D.create(origin + offset + Vector3.UP * params.wall_ray_up_height, origin + offset - Vector3.DOWN * params.wall_ray_down_height)
|
||||
ray.exclude = [machine.player.get_rid()]
|
||||
var hit := space_state.intersect_ray(ray)
|
||||
if not hit.is_empty():
|
||||
var n: Vector3 = hit.get("normal", Vector3.ZERO)
|
||||
if n.y < 0.3 and n.length_squared() > 0.0:
|
||||
return n.normalized()
|
||||
return Vector3.ZERO
|
||||
func _get_camera_rig():
|
||||
if machine.player and machine.player.has_node("HeadPivot"):
|
||||
return machine.player.get_node("HeadPivot")
|
||||
return null
|
||||
|
||||
|
||||
func _update_capsule_height() -> void:
|
||||
var shape = _get_capsule()
|
||||
if shape:
|
||||
if machine.input_crouch:
|
||||
shape.height = machine.original_capsule_height * 0.5
|
||||
else:
|
||||
shape.height = machine.original_capsule_height
|
||||
|
||||
|
||||
func _get_capsule() -> CapsuleShape3D:
|
||||
for child in machine.player.get_children():
|
||||
if child is CollisionShape3D and child.shape is CapsuleShape3D:
|
||||
return child.shape as CapsuleShape3D
|
||||
return null
|
||||
|
||||
@@ -6,55 +6,105 @@ var params: MovementParams:
|
||||
get: return machine.params
|
||||
|
||||
var elapsed: float = 0.0
|
||||
var _saved_capsule_height: float = 0.0
|
||||
var _slide_direction: Vector3 = Vector3.ZERO
|
||||
|
||||
|
||||
func enter(_data: Dictionary = {}) -> void:
|
||||
elapsed = 0.0
|
||||
machine.register_chain_mechanic("slide")
|
||||
# Lower player capsule
|
||||
var player_node = machine.player
|
||||
var shape: CapsuleShape3D = machine.player.shape_owner_get_shape(0, 0)
|
||||
|
||||
# Save original capsule height and halve it
|
||||
var shape: CapsuleShape3D = _get_capsule()
|
||||
if shape:
|
||||
shape.height = shape.height * 0.5
|
||||
_saved_capsule_height = shape.height
|
||||
shape.height = _saved_capsule_height * 0.5
|
||||
|
||||
# Slide in the direction of current velocity (momentum-based)
|
||||
var hvel := Vector3(machine.player.velocity.x, 0.0, machine.player.velocity.z)
|
||||
if hvel.length_squared() > 0.01:
|
||||
_slide_direction = hvel.normalized()
|
||||
else:
|
||||
# Fallback: player forward
|
||||
_slide_direction = -machine.player.global_transform.basis.z
|
||||
_slide_direction.y = 0.0
|
||||
_slide_direction = _slide_direction.normalized()
|
||||
|
||||
# Set initial slide velocity
|
||||
var slide_speed := maxf(hvel.length(), params.slide_speed)
|
||||
machine.player.velocity.x = _slide_direction.x * machine.get_effective_speed(slide_speed)
|
||||
machine.player.velocity.z = _slide_direction.z * machine.get_effective_speed(slide_speed)
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
pass
|
||||
machine.slide_cooldown_timer = params.slide_cooldown
|
||||
# Restore original capsule height
|
||||
var shape: CapsuleShape3D = _get_capsule()
|
||||
if shape and _saved_capsule_height > 0.0:
|
||||
shape.height = _saved_capsule_height
|
||||
|
||||
|
||||
func update(delta: float) -> void:
|
||||
elapsed += delta
|
||||
var vel: Vector3 = machine.player.velocity
|
||||
var forward: Vector3 = -machine.player.global_transform.basis.z
|
||||
if forward.length_squared() > 1.0:
|
||||
forward = forward.normalized()
|
||||
vel.x = forward.x * params.slide_speed * machine.get_effective_speed(1.0)
|
||||
vel.z = forward.z * params.slide_speed * machine.get_effective_speed(1.0)
|
||||
vel.x *= pow(params.slide_friction, delta)
|
||||
vel.z *= pow(params.slide_friction, delta)
|
||||
machine.player.velocity = vel
|
||||
machine.player.move_and_slide()
|
||||
var player := machine.player
|
||||
var vel: Vector3 = player.velocity
|
||||
|
||||
if machine.player.is_on_floor():
|
||||
# ── Apply friction to horizontal velocity ─────────────────────────────
|
||||
vel.x *= pow(params.slide_friction, delta * 10.0)
|
||||
vel.z *= pow(params.slide_friction, delta * 10.0)
|
||||
|
||||
# ── Gravity (for slopes) ──────────────────────────────────────────────
|
||||
if not player.is_on_floor():
|
||||
vel.y -= params.gravity * delta
|
||||
else:
|
||||
vel.y = -2.0 # Strong floor snap
|
||||
|
||||
player.velocity = vel
|
||||
player.move_and_slide()
|
||||
|
||||
# ── Update ground status ──────────────────────────────────────────────
|
||||
if player.is_on_floor():
|
||||
machine.on_ground = true
|
||||
var speed := Vector3(vel.x, 0.0, vel.z).length()
|
||||
if speed < params.slide_min_speed or elapsed > params.slide_duration:
|
||||
var end_shape: CapsuleShape3D = machine.player.shape_owner_get_shape(0, 0)
|
||||
if end_shape:
|
||||
end_shape.height = end_shape.height * 2.0
|
||||
machine.register_chain_mechanic("slide_end")
|
||||
machine.current_jump_count = 0
|
||||
else:
|
||||
machine.on_ground = false
|
||||
|
||||
# ── End conditions ────────────────────────────────────────────────────
|
||||
var hspeed := Vector3(player.velocity.x, 0.0, player.velocity.z).length()
|
||||
|
||||
# Slide ended: too slow
|
||||
if hspeed < params.slide_min_speed:
|
||||
machine.switch_to("ground")
|
||||
return
|
||||
else:
|
||||
var fall_shape: CapsuleShape3D = machine.player.shape_owner_get_shape(0, 0)
|
||||
if fall_shape:
|
||||
fall_shape.height = fall_shape.height * 2.0
|
||||
machine.on_ground = false
|
||||
machine.switch_to("air")
|
||||
|
||||
if not machine.input_crouch or not machine.input_sprint:
|
||||
var cancel_shape: CapsuleShape3D = machine.player.shape_owner_get_shape(0, 0)
|
||||
if cancel_shape:
|
||||
cancel_shape.height = cancel_shape.height * 2.0
|
||||
machine.register_chain_mechanic("slide_cancel")
|
||||
# Player released crouch
|
||||
if not machine.input_crouch:
|
||||
machine.switch_to("ground")
|
||||
return
|
||||
|
||||
# Fell off edge
|
||||
if not machine.on_ground and machine.coyote_timer <= 0.0:
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# Jump out of slide
|
||||
if machine.input_jump_just_pressed:
|
||||
player.velocity.y = params.jump_velocity
|
||||
var hvel := Vector3(player.velocity.x, 0.0, player.velocity.z)
|
||||
if hvel.length_squared() > 0.01:
|
||||
var dir := hvel.normalized()
|
||||
var jump_speed := minf(hvel.length() + params.slide_jump_speed_boost, params.bunny_hop_speed_cap)
|
||||
player.velocity.x = dir.x * jump_speed
|
||||
player.velocity.z = dir.z * jump_speed
|
||||
machine.current_jump_count = 1
|
||||
machine.on_ground = false
|
||||
machine.register_chain_mechanic("slide_jump")
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
|
||||
func _get_capsule() -> CapsuleShape3D:
|
||||
for child in machine.player.get_children():
|
||||
if child is CollisionShape3D and child.shape is CapsuleShape3D:
|
||||
return child.shape as CapsuleShape3D
|
||||
return null
|
||||
|
||||
@@ -6,11 +6,15 @@ var params: MovementParams:
|
||||
get: return machine.params
|
||||
|
||||
var elapsed: float = 0.0
|
||||
var stamina: float = 0.0
|
||||
|
||||
|
||||
func enter(_data: Dictionary = {}) -> void:
|
||||
elapsed = 0.0
|
||||
stamina = params.wall_cling_max_stamina
|
||||
machine.register_chain_mechanic("wall_cling")
|
||||
# Kill most velocity but keep slight downward
|
||||
machine.player.velocity = Vector3.ZERO
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
@@ -19,34 +23,56 @@ func exit() -> void:
|
||||
|
||||
func update(delta: float) -> void:
|
||||
elapsed += delta
|
||||
var vel: Vector3 = machine.player.velocity
|
||||
vel.x = params.wall_cling_horizontal_speed
|
||||
vel.y = params.wall_cling_vertical_speed
|
||||
vel.z = params.wall_cling_horizontal_speed
|
||||
machine.player.velocity = vel
|
||||
machine.player.move_and_slide()
|
||||
stamina -= params.wall_cling_stamina_drain * delta
|
||||
|
||||
# Jump off wall
|
||||
var player := machine.player
|
||||
var vel: Vector3 = player.velocity
|
||||
|
||||
# ── Slow slide down ───────────────────────────────────────────────────
|
||||
var slide_factor := 1.0 - (stamina / params.wall_cling_max_stamina) # 0→1 as stamina drains
|
||||
vel.x = 0.0
|
||||
vel.z = 0.0
|
||||
vel.y = params.wall_cling_slide_speed * (1.0 + slide_factor * 2.0)
|
||||
|
||||
# Push into wall to maintain contact
|
||||
vel -= machine.wall_normal * 1.0
|
||||
|
||||
player.velocity = vel
|
||||
player.move_and_slide()
|
||||
|
||||
# ── Stamina depleted → fall ───────────────────────────────────────────
|
||||
if stamina <= 0.0:
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# ── Jump off wall ─────────────────────────────────────────────────────
|
||||
if machine.input_jump_just_pressed:
|
||||
var push_dir := machine.wall_normal * params.wall_run_jump_off_normal
|
||||
var vel2 := Vector3(push_dir.x, params.jump_velocity, push_dir.z)
|
||||
machine.player.velocity = vel2
|
||||
player.velocity = Vector3(push_dir.x, params.jump_velocity, push_dir.z)
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
machine.register_chain_mechanic("wall_cling_jump")
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# Re-enter wall run with input
|
||||
# ── Re-enter wall run with movement input ─────────────────────────────
|
||||
if machine.input_dir.length() > 0.1:
|
||||
machine.switch_to("wall_run")
|
||||
return
|
||||
|
||||
# Fall
|
||||
if not machine.wall_normal or machine.wall_normal.is_zero_approx():
|
||||
# ── Lost wall contact ─────────────────────────────────────────────────
|
||||
var wall := machine.detect_wall_horizontal()
|
||||
if wall == Vector3.ZERO:
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
if machine.player.is_on_floor():
|
||||
# ── Hit floor ─────────────────────────────────────────────────────────
|
||||
if player.is_on_floor():
|
||||
machine.on_ground = true
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.register_chain_mechanic("cling_land")
|
||||
machine.wall_side = 0.0
|
||||
machine.switch_to("ground")
|
||||
|
||||
@@ -1,87 +1,114 @@
|
||||
extends Node
|
||||
class_name StateWallRun
|
||||
|
||||
const FLAG: int = 0 # unused
|
||||
|
||||
var machine: MovementStateMachine
|
||||
var params: MovementParams:
|
||||
get: return machine.params
|
||||
|
||||
var elapsed: float = 0.0
|
||||
var _initial_y_vel: float = 0.0
|
||||
|
||||
|
||||
func enter(_data: Dictionary = {}) -> void:
|
||||
elapsed = 0.0
|
||||
machine.register_chain_mechanic("wall_run")
|
||||
machine.player.velocity.y = params.wall_run_vertical_speed
|
||||
# Cap the upward momentum so they don't fly up the wall,
|
||||
# but preserve some if they just jumped onto it.
|
||||
machine.player.velocity.y = minf(machine.player.velocity.y, 1.5)
|
||||
machine.on_ground = false
|
||||
|
||||
# Camera tilt
|
||||
var rig = _get_camera_rig()
|
||||
if rig:
|
||||
rig.set_wall_tilt(machine.wall_side)
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
pass
|
||||
# Clear camera tilt
|
||||
var rig = _get_camera_rig()
|
||||
if rig:
|
||||
rig.clear_wall_tilt()
|
||||
|
||||
|
||||
func update(delta: float) -> void:
|
||||
elapsed += delta
|
||||
if elapsed > params.wall_run_duration:
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
machine.wall_cooldown_timer = 0.3
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
var vel: Vector3 = machine.player.velocity
|
||||
var player := machine.player
|
||||
var vel: Vector3 = player.velocity
|
||||
|
||||
# Stick to wall
|
||||
vel.y = min(vel.y, params.wall_run_vertical_speed)
|
||||
# ── Gradual gravity pull (starts light, increases over time) ──────────
|
||||
var gravity_factor := 0.1 + 0.9 * (elapsed / params.wall_run_duration)
|
||||
vel.y -= params.wall_run_gravity * gravity_factor * delta
|
||||
|
||||
# Move along wall: project velocity onto wall tangent
|
||||
var wall_tangent := (machine.wall_normal.cross(Vector3.UP)).normalized()
|
||||
if machine.input_dir.y < 0.0:
|
||||
# ── Move along wall tangent ───────────────────────────────────────────
|
||||
var wall_tangent := machine.wall_normal.cross(Vector3.UP).normalized()
|
||||
# Choose tangent direction based on player's movement direction
|
||||
var hvel := Vector3(vel.x, 0.0, vel.z)
|
||||
if hvel.dot(wall_tangent) < 0.0:
|
||||
wall_tangent = -wall_tangent
|
||||
var move_dir: Vector3 = wall_tangent * params.wall_run_speed
|
||||
vel.x = move_dir.x
|
||||
vel.z = move_dir.z
|
||||
|
||||
if machine.input_jump_just_pressed:
|
||||
var jump_vel: Vector3 = vel + (machine.wall_normal * params.wall_run_jump_off_normal)
|
||||
jump_vel.y = params.wall_run_auto_jump_speed
|
||||
machine.player.velocity = jump_vel
|
||||
var effective_speed := machine.get_effective_speed(params.wall_run_speed)
|
||||
vel.x = wall_tangent.x * effective_speed
|
||||
vel.z = wall_tangent.z * effective_speed
|
||||
|
||||
# ── Look away to break wall run ───────────────────────────────────────
|
||||
var look_dir := -player.global_transform.basis.z
|
||||
if look_dir.dot(machine.wall_normal) > 0.4:
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
machine.wall_cooldown_timer = 0.3
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# ── Push slightly toward wall to maintain contact ─────────────────────
|
||||
vel -= machine.wall_normal * 2.0
|
||||
|
||||
# ── Wall jump ─────────────────────────────────────────────────────────
|
||||
if machine.input_jump_just_pressed:
|
||||
var jump_vel: Vector3 = machine.wall_normal * params.wall_run_jump_off_normal
|
||||
var h_look := Vector3(look_dir.x, 0.0, look_dir.z).normalized()
|
||||
jump_vel += h_look * params.wall_run_jump_horizontal
|
||||
jump_vel.y = params.wall_run_auto_jump_speed
|
||||
player.velocity = jump_vel
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
machine.wall_cooldown_timer = 0.3
|
||||
machine.register_chain_mechanic("wall_jump")
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# Wall cling fall-through
|
||||
# ── Wall cling (no movement input) ────────────────────────────────────
|
||||
if machine.input_dir.length() < 0.1:
|
||||
machine.switch_to("wall_cling")
|
||||
return
|
||||
|
||||
# Lose wall contact
|
||||
var still_on_wall: bool = machine.wall_normal != Vector3.ZERO and detect_wall(machine.wall_normal).length_squared() > 0.0
|
||||
if not still_on_wall:
|
||||
player.velocity = vel
|
||||
player.move_and_slide()
|
||||
|
||||
# ── Check still on wall ───────────────────────────────────────────────
|
||||
var still_on_wall := machine.detect_wall_horizontal()
|
||||
if still_on_wall == Vector3.ZERO:
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.register_chain_mechanic("wall_run_off")
|
||||
machine.wall_side = 0.0
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
machine.player.velocity = vel
|
||||
machine.player.move_and_slide()
|
||||
|
||||
if machine.player.is_on_floor():
|
||||
# ── Hit floor during wall run ─────────────────────────────────────────
|
||||
if player.is_on_floor():
|
||||
machine.on_ground = true
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
machine.current_jump_count = 0
|
||||
machine.register_chain_mechanic("wall_run_land")
|
||||
machine.switch_to("ground")
|
||||
|
||||
|
||||
func detect_wall(expected_normal: Vector3) -> Vector3:
|
||||
var origin := machine.player.global_position
|
||||
var offset := expected_normal.cross(Vector3.UP).normalized()
|
||||
var space_state := machine.player.get_world_3d().direct_space_state
|
||||
for side in [1.0, -1.0]:
|
||||
var ray_origin: Vector3 = origin + offset * side * params.wall_detect_distance + Vector3.UP * params.wall_ray_up_height
|
||||
var ray_end: Vector3 = origin + offset * side * params.wall_detect_distance - Vector3.DOWN * params.wall_ray_down_height
|
||||
var ray := PhysicsRayQueryParameters3D.create(ray_origin, ray_end)
|
||||
ray.exclude = [machine.player.get_rid()]
|
||||
var hit := space_state.intersect_ray(ray)
|
||||
if not hit.is_empty():
|
||||
return hit.get("normal", Vector3.ZERO).normalized()
|
||||
return Vector3.ZERO
|
||||
func _get_camera_rig():
|
||||
if machine.player and machine.player.has_node("HeadPivot"):
|
||||
return machine.player.get_node("HeadPivot")
|
||||
return null
|
||||
|
||||
@@ -15,6 +15,69 @@ run/main_scene="res://scenes/test_level/test_level.tscn"
|
||||
config/features=PackedStringArray("4.6", "Forward Plus")
|
||||
config/icon="res://icon.svg"
|
||||
|
||||
[input]
|
||||
|
||||
move_forward={
|
||||
"deadzone": 0.0,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":87,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
move_back={
|
||||
"deadzone": 0.0,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":83,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
move_left={
|
||||
"deadzone": 0.0,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":65,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
move_right={
|
||||
"deadzone": 0.0,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":68,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
jump={
|
||||
"deadzone": 0.0,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":32,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
sprint={
|
||||
"deadzone": 0.0,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194325,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
crouch={
|
||||
"deadzone": 0.0,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194326,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
dash={
|
||||
"deadzone": 0.0,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194325,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
fire={
|
||||
"deadzone": 0.0,
|
||||
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"canceled":false,"pressed":false,"double_click":false,"script":null)
|
||||
]
|
||||
}
|
||||
fire_alt={
|
||||
"deadzone": 0.0,
|
||||
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":2,"canceled":false,"pressed":false,"double_click":false,"script":null)
|
||||
]
|
||||
}
|
||||
interact={
|
||||
"deadzone": 0.0,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":69,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
reload={
|
||||
"deadzone": 0.0,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":82,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
|
||||
[physics]
|
||||
|
||||
3d/physics_engine="Jolt Physics"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[gd_scene load_steps=2 format=3]
|
||||
[gd_scene format=3 uid="uid://c5k51gbegvxss"]
|
||||
|
||||
[ext_resource type="Script" path="res://debug/test_level_builder.gd" id="1"]
|
||||
[ext_resource type="Script" uid="uid://c7ltcn37gfd71" path="res://debug/test_level_builder.gd" id="1"]
|
||||
|
||||
[node name="TestLevel" type="Node3D"]
|
||||
[node name="TestLevel" type="Node3D" unique_id=130984349]
|
||||
script = ExtResource("1")
|
||||
|
||||
Reference in New Issue
Block a user