303 lines
11 KiB
GDScript
303 lines
11 KiB
GDScript
extends Node3D
|
|
class_name LevelRuntime
|
|
|
|
var _player: CharacterBody3D
|
|
|
|
|
|
## The play volume for an AUTHORED map.
|
|
##
|
|
## The code-built levels each pass their own extents, because they know them as
|
|
## constants. A hand-placed .tscn does not, so this measures the level instead —
|
|
## merge every reasonably-sized visual's world AABB, then pad it generously.
|
|
##
|
|
## Without one, a map on this runtime had no out-of-bounds handling at all: a
|
|
## player who left the geometry simply fell for ever, with no warning and no
|
|
## death. That was the state of fps_blockout.
|
|
func _build_combat_area() -> void:
|
|
var bounds := AABB()
|
|
var any := false
|
|
for child in find_children("*", "VisualInstance3D", true, false):
|
|
var vi := child as VisualInstance3D
|
|
var box := vi.get_aabb()
|
|
# Skip the sky, a directional light's own AABB, and anything else
|
|
# absurd — one of them would swallow the level and put the boundary
|
|
# somewhere useless.
|
|
if box.size.length() > 5000.0 or box.size.length() < 0.001:
|
|
continue
|
|
box = vi.global_transform * box
|
|
if not any:
|
|
bounds = box
|
|
any = true
|
|
else:
|
|
bounds = bounds.merge(box)
|
|
if not any:
|
|
bounds = AABB(Vector3(-50, -10, -50), Vector3(100, 60, 100))
|
|
|
|
# Padding, not a tight fit: the volume marks where a player is TOLD they
|
|
# have left, and a boundary hugging the geometry fires while someone is
|
|
# still standing on a legitimate ledge. Deep below, because falling out of
|
|
# the world is the case this exists to catch.
|
|
var pad := Vector3(20, 0, 20)
|
|
var size := bounds.size + pad * 2.0 + Vector3(0, 60, 0)
|
|
var centre := bounds.get_center() + Vector3(0, 20.0, 0)
|
|
CombatArea.add_to(self, size, centre)
|
|
|
|
|
|
func _ready() -> void:
|
|
# Hide mouse
|
|
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
|
|
|
# Maps that don't ship their own environment get the shared stylized one
|
|
# (sky gradient, sun+fill, glow, cel color grade, ambient bed).
|
|
if not has_node("WorldEnvironment"):
|
|
LevelEnvironment.add_to(self)
|
|
|
|
_build_combat_area()
|
|
|
|
|
|
# Multiplayer Spawning
|
|
var spawner = MultiplayerSpawner.new()
|
|
spawner.name = "PlayerSpawner"
|
|
spawner.spawn_path = "." # Spawn directly as children of the level
|
|
spawner.spawn_function = _spawn_player
|
|
add_child(spawner)
|
|
|
|
var nm = get_node_or_null("/root/NetworkManager")
|
|
if nm and nm.multiplayer.is_server():
|
|
# Spawn existing players
|
|
for pid in nm.player_stats.keys():
|
|
spawner.spawn(pid)
|
|
|
|
# Spawn late joiners
|
|
nm.player_connected.connect(func(pid):
|
|
spawner.spawn(pid)
|
|
)
|
|
|
|
# Despawn on disconnect
|
|
nm.player_disconnected.connect(func(pid):
|
|
var p = get_node_or_null(str(pid))
|
|
if p:
|
|
p.queue_free()
|
|
)
|
|
|
|
|
|
# ── Spawning ──────────────────────────────────────────────────────────────────
|
|
|
|
func _get_dynamic_spawn_position() -> Vector3:
|
|
var spawn_nodes = get_tree().get_nodes_in_group("spawn_points")
|
|
if spawn_nodes.is_empty():
|
|
return Vector3(0, 10, 0)
|
|
|
|
var all_players = []
|
|
for p in get_tree().get_nodes_in_group("players"):
|
|
if is_instance_valid(p) and p is Node3D:
|
|
all_players.append(p)
|
|
|
|
if all_players.is_empty():
|
|
var pos = spawn_nodes.pick_random().global_position
|
|
pos += Vector3(randf_range(-1, 1), 0.5, randf_range(-1, 1))
|
|
return pos
|
|
|
|
var best_spawn = spawn_nodes[0].global_position
|
|
var max_min_dist = -1.0
|
|
|
|
for spawn_node in spawn_nodes:
|
|
var pos = spawn_node.global_position
|
|
var min_dist_to_player = 999999.0
|
|
for p in all_players:
|
|
var dist = pos.distance_to(p.global_position)
|
|
if dist < min_dist_to_player:
|
|
min_dist_to_player = dist
|
|
|
|
if min_dist_to_player > max_min_dist:
|
|
max_min_dist = min_dist_to_player
|
|
best_spawn = pos
|
|
|
|
# Add slight jitter to prevent exact stacking
|
|
best_spawn += Vector3(randf_range(-1, 1), 0.5, randf_range(-1, 1))
|
|
return best_spawn
|
|
|
|
func _spawn_player(pid: int) -> CharacterBody3D:
|
|
var player := CharacterBody3D.new()
|
|
player.name = str(pid)
|
|
player.set_multiplayer_authority(pid)
|
|
player.add_to_group("players")
|
|
|
|
# Dynamic Spawning
|
|
player.position = _get_dynamic_spawn_position()
|
|
|
|
# Server Synchronizer — host-owned gameplay state (health, kills, death)
|
|
var server_sync = MultiplayerSynchronizer.new()
|
|
server_sync.name = "ServerSynchronizer"
|
|
server_sync.set_multiplayer_authority(1) # Host always controls these
|
|
var server_rep_config = SceneReplicationConfig.new()
|
|
server_rep_config.add_property(":health")
|
|
server_rep_config.add_property(":shield")
|
|
server_rep_config.add_property(":is_dead")
|
|
server_sync.replication_config = server_rep_config
|
|
player.add_child(server_sync)
|
|
|
|
# Client Synchronizer — owner-simulated movement, aim, animation state.
|
|
# Movement is client-authoritative for instant response; remote peers
|
|
# interpolate synced_position/velocity (see PlayerMovementController).
|
|
var client_sync = MultiplayerSynchronizer.new()
|
|
client_sync.name = "MultiplayerSynchronizer"
|
|
client_sync.set_multiplayer_authority(pid)
|
|
var client_rep_config = SceneReplicationConfig.new()
|
|
client_rep_config.add_property(":synced_position")
|
|
client_rep_config.add_property(":synced_velocity")
|
|
client_rep_config.add_property(":rotation")
|
|
client_rep_config.add_property("HeadPivot:rotation")
|
|
client_rep_config.add_property(":synced_movement_state")
|
|
client_rep_config.add_property(":synced_animation_state")
|
|
client_rep_config.add_property(":synced_movement_speed")
|
|
client_rep_config.add_property(":synced_is_crouching")
|
|
client_rep_config.add_property(":synced_is_ads")
|
|
client_rep_config.add_property(":synced_wall_side")
|
|
client_rep_config.add_property(":synced_wall_normal")
|
|
client_rep_config.add_property(":synced_wall_contact_point")
|
|
client_rep_config.add_property(":synced_helmet_closed")
|
|
client_rep_config.add_property(":synced_is_dancing")
|
|
# Which of the five emotes, so other players see the one that was chosen.
|
|
client_rep_config.add_property(":synced_dance_index")
|
|
client_rep_config.add_property(":synced_grapple_point")
|
|
client_rep_config.add_property(":synced_is_grapple_shooting")
|
|
client_rep_config.add_property(":synced_skin_id")
|
|
client_rep_config.add_property(":synced_weapon_path")
|
|
client_rep_config.add_property(":synced_loadout_p1")
|
|
client_rep_config.add_property(":synced_loadout_p2")
|
|
client_rep_config.add_property(":synced_loadout_sp")
|
|
client_rep_config.add_property(":synced_loadout_melee")
|
|
client_rep_config.add_property(":synced_loadout_ready")
|
|
client_rep_config.add_property(":synced_action")
|
|
client_rep_config.add_property(":synced_action_seq")
|
|
client_rep_config.add_property(":synced_is_targeting")
|
|
client_rep_config.add_property(":synced_homing_target_pos")
|
|
client_sync.replication_config = client_rep_config
|
|
player.add_child(client_sync)
|
|
|
|
# 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)
|
|
|
|
# Humanoid Model for Player (Shadows only locally, visible to others)
|
|
var humanoid = load("res://characters/humanoid_model.gd").new()
|
|
humanoid.name = "HumanoidModel"
|
|
humanoid.color = Color(0.2, 0.4, 0.8) # Blueish for player
|
|
humanoid.shadows_only = (pid == multiplayer.get_unique_id())
|
|
humanoid.position = Vector3(0, -0.9, 0) # Offset from center to feet
|
|
player.add_child(humanoid)
|
|
|
|
# Movement State Machine
|
|
var sm := Node.new()
|
|
sm.name = "MovementStateMachine"
|
|
player.add_child(sm)
|
|
|
|
var sm_script = preload("res://movement/movement_state_machine.gd")
|
|
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",
|
|
"wall_run": "res://movement/states/state_wall_run.gd",
|
|
"wall_cling": "res://movement/states/state_wall_cling.gd",
|
|
"slide": "res://movement/states/state_slide.gd",
|
|
"dash": "res://movement/states/state_dash.gd",
|
|
}
|
|
for state_name in state_scripts:
|
|
var st := Node.new()
|
|
st.name = "state_" + state_name
|
|
sm.add_child(st)
|
|
var st_script = load(state_scripts[state_name])
|
|
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.current = (pid == multiplayer.get_unique_id())
|
|
camera.fov = 90.0
|
|
head_pivot.add_child(camera)
|
|
|
|
# ── Weapon Setup ────────────────────────────────────────────────────────────
|
|
var wmanager_script = preload("res://weapons/weapon_manager.gd")
|
|
if wmanager_script:
|
|
var wman = wmanager_script.new()
|
|
wman.name = "WeaponManager"
|
|
wman.player = player
|
|
wman.camera = camera
|
|
camera.add_child(wman)
|
|
|
|
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
|
|
|
|
# ── Dependencies ────────────────────────────────────────────────────────
|
|
sm.player = player
|
|
sm.params = player.params
|
|
head_pivot.params = player.params
|
|
|
|
# Authority configuration
|
|
var is_local = (pid == multiplayer.get_unique_id())
|
|
if is_local:
|
|
_player = player
|
|
player.set_physics_process(true)
|
|
player.set_process(true)
|
|
sm.set_physics_process(true)
|
|
else:
|
|
player.set_physics_process(false)
|
|
player.set_process(true)
|
|
player.set_process_input(false)
|
|
sm.set_physics_process(false)
|
|
sm.set_process(false)
|
|
sm.set_process_input(false)
|
|
head_pivot.set_process_input(false)
|
|
head_pivot.set_process(false)
|
|
head_pivot.set_physics_process(false)
|
|
var wman = camera.get_node_or_null("WeaponManager")
|
|
if wman:
|
|
wman.set_process_input(false)
|
|
wman.set_process(false)
|
|
wman.set_physics_process(false)
|
|
wman.visible = false
|
|
var cv = wman.get("canvas_layer")
|
|
if cv:
|
|
cv.visible = false
|
|
|
|
return player
|
|
|
|
|
|
# ── HUD ───────────────────────────────────────────────────────────────
|
|
#
|
|
# There is no HUD here any more, and no _process to drive one.
|
|
#
|
|
# Reticle, vitals, ammo, ability cooldowns, the chain meter and the debug
|
|
# readout all belong to ui/player_hud.gd, spawned by the player itself. Every
|
|
# one of those describes A PLAYER, so a level that owns them has to reach down
|
|
# into that player's state machine and weapon manager every frame to fill them
|
|
# in — which is exactly what this did, from three byte-identical copies across
|
|
# the three level runtimes. The visible symptom was two ammo panels on screen at
|
|
# once, in two different styles, overlapping in the bottom-right corner.
|
|
#
|
|
# A level owns the level.
|