feat: Introduce LobbyManager to handle room management, player states, and game setting synchronization.

This commit is contained in:
Yogi Wiguna
2026-02-10 16:58:43 +08:00
parent a27ff8634d
commit 0c2a53c0cc
3 changed files with 49 additions and 46 deletions
+33 -18
View File
@@ -45,6 +45,7 @@ func _ready():
if message_bar:
message_bar.visible = false
# Ensure grid is randomized with Scarcity if server
if multiplayer.is_server():
randomize_game_grid()
@@ -57,6 +58,11 @@ func _ready():
respawn_check_timer.autostart = true
respawn_check_timer.timeout.connect(_check_respawns)
add_child(respawn_check_timer)
# Force gridmap cell size to match player logic (1, 0.05, 1) - >0.001 to avoid errors
var em = $EnhancedGridMap
if em:
em.cell_size = Vector3(1, 0.05, 1)
func _on_goal_count_updated(peer_id: int, count: int):
# Only update for local player
@@ -392,8 +398,9 @@ func _setup_host_game():
# IMMEDIATELY assign random spawn positions before any player _ready() completes
# Player _ready() has 0.1s await, so we assign before that completes
if LobbyManager.get_randomize_spawn():
_assign_random_spawn_positions()
# IMMEDIATELY assign spawn positions (Fixed or Random)
# Always run this to ensure players don't spawn at (0,0) overlap
_assign_random_spawn_positions()
# Wait for players to be fully ready (player.gd has 0.1s await in _ready before managers init)
await get_tree().create_timer(0.3).timeout
@@ -598,8 +605,10 @@ func _assign_random_spawn_positions():
# Fallback shuffle
all_spawns.shuffle()
# Get all players
# Get all players and sort them for deterministic assignment
var all_players = get_tree().get_nodes_in_group("Players")
all_players.sort_custom(func(a, b): return a.name.to_int() < b.name.to_int())
var spawn_index = 0
# Round-robin assignment to corners: TL, TR, BR, BL, TL, TR, BR, BL...
@@ -670,7 +679,9 @@ func create_bot(bot_id: int):
@rpc("any_peer", "call_local")
func add_player_character(peer_id: int, is_bot: bool = false):
print("[Main] add_player_character called for %d (is_bot: %s)" % [peer_id, is_bot])
if has_node(str(peer_id)):
print("[Main] Player %d already exists! Skipping spawn." % peer_id)
return
var player_character
@@ -1013,16 +1024,6 @@ func create_specific_player(data: Dictionary):
add_child(player_character)
player_character.add_to_group("Players", true)
# Set spawn flag directly so it doesn't hide itself
if data.has("spawn_point_selected") and data["spawn_point_selected"]:
player_character.spawn_point_selected = true
player_character.visible = true
# Ensure visual position matches logical
var new_pos = player_character.grid_to_world(data["position"])
player_character.global_position = new_pos
player_character.target_visual_position = new_pos
# Set display name
if data.has("name"):
player_character.display_name = data["name"]
@@ -1030,6 +1031,20 @@ func create_specific_player(data: Dictionary):
if data["is_bot"]:
player_character.add_to_group("Bots", true)
player_character.is_bot = true
# Set spawn flag and visibility for BOTH new and existing players
if data.has("spawn_point_selected") and data["spawn_point_selected"]:
player_character.spawn_point_selected = true
player_character.visible = true
# Ensure visual position matches logical
var new_pos = player_character.grid_to_world(data["position"])
player_character.global_position = new_pos
player_character.target_visual_position = new_pos
# Force collision update
if player_character.has_node("CollisionShape3D"):
player_character.get_node("CollisionShape3D").disabled = false
# Check if this is the local player (client's own player)
var is_local_player = (peer_id == multiplayer.get_unique_id())
@@ -1052,11 +1067,11 @@ func create_specific_player(data: Dictionary):
# Always update position (including for existing nodes, so client sees host correctly)
player_character.current_position = data["position"]
player_character.global_position = Vector3(
data["position"].x * 2 + 1,
1.0,
data["position"].y * 2 + 1
)
# Use the player's own grid_to_world to respect cell_size (1,1,1)
var new_world_pos = player_character.grid_to_world(data["position"])
player_character.global_position = new_world_pos
player_character.target_visual_position = new_world_pos
# Update playerboard UI for local player
if is_local_player: