add a chat window
This commit is contained in:
@@ -36,6 +36,9 @@ func _ready():
|
||||
network_manager.player_connected.connect(_on_player_connected)
|
||||
network_manager.player_disconnected.connect(_on_player_disconnected)
|
||||
|
||||
# Create chat UI
|
||||
_create_chat_ui()
|
||||
|
||||
# Generate dungeon on host
|
||||
if multiplayer.is_server() or not multiplayer.has_multiplayer_peer():
|
||||
print("GameWorld: _ready() - Will generate dungeon (is_server: ", multiplayer.is_server(), ", has_peer: ", multiplayer.has_multiplayer_peer(), ")")
|
||||
@@ -59,6 +62,10 @@ func _spawn_all_players():
|
||||
func _on_player_connected(peer_id: int, player_info: Dictionary):
|
||||
print("GameWorld: Player connected signal received for peer ", peer_id, " with info: ", player_info)
|
||||
|
||||
# Send join message to chat (only on server to avoid duplicates)
|
||||
if multiplayer.is_server():
|
||||
_send_player_join_message(peer_id, player_info)
|
||||
|
||||
# Reset ready status for this peer (they need to notify again after spawning)
|
||||
if multiplayer.is_server():
|
||||
clients_ready[peer_id] = false
|
||||
@@ -139,8 +146,13 @@ func _sync_existing_enemies_to_client(client_peer_id: int):
|
||||
_sync_enemy_spawn.rpc_id(client_peer_id, spawner.name, pos, scene_index, humanoid_type)
|
||||
print("GameWorld: Sent enemy spawn sync to client ", client_peer_id, ": spawner=", spawner.name, " pos=", pos, " scene_index=", scene_index, " humanoid_type=", humanoid_type)
|
||||
|
||||
func _on_player_disconnected(peer_id: int):
|
||||
func _on_player_disconnected(peer_id: int, player_info: Dictionary):
|
||||
print("GameWorld: Player disconnected - ", peer_id)
|
||||
|
||||
# Send disconnect message to chat (only on server to avoid duplicates)
|
||||
if multiplayer.is_server():
|
||||
_send_player_disconnect_message(peer_id, player_info)
|
||||
|
||||
player_manager.despawn_players_for_peer(peer_id)
|
||||
|
||||
@rpc("authority", "reliable")
|
||||
@@ -2030,6 +2042,60 @@ func _initialize_hud():
|
||||
else:
|
||||
print("GameWorld: HUD not found or not ready - this is OK if HUD scene is missing")
|
||||
|
||||
func _create_chat_ui():
|
||||
# Load chat UI scene
|
||||
var chat_ui_scene = load("res://scenes/chat_ui.tscn")
|
||||
if not chat_ui_scene:
|
||||
push_error("GameWorld: Could not load chat_ui.tscn scene!")
|
||||
return
|
||||
|
||||
var chat_ui = chat_ui_scene.instantiate()
|
||||
if chat_ui:
|
||||
add_child(chat_ui)
|
||||
print("GameWorld: Chat UI scene instantiated and added to scene tree")
|
||||
else:
|
||||
push_error("GameWorld: Failed to instantiate chat_ui.tscn!")
|
||||
|
||||
func _send_player_join_message(peer_id: int, player_info: Dictionary):
|
||||
# Send a chat message when a player joins
|
||||
# Only send from server to avoid duplicate messages
|
||||
if not multiplayer.is_server():
|
||||
return
|
||||
|
||||
# Get player name (use first player name from the player_info)
|
||||
var player_names = player_info.get("player_names", [])
|
||||
var player_name = ""
|
||||
if player_names.size() > 0:
|
||||
player_name = player_names[0]
|
||||
else:
|
||||
player_name = "Player%d" % peer_id
|
||||
|
||||
# Get chat UI
|
||||
var chat_ui = get_node_or_null("ChatUI")
|
||||
if chat_ui and chat_ui.has_method("send_system_message"):
|
||||
var message = "%s joined the game" % player_name
|
||||
chat_ui.send_system_message(message)
|
||||
|
||||
func _send_player_disconnect_message(peer_id: int, player_info: Dictionary):
|
||||
# Send a chat message when a player disconnects
|
||||
# Only send from server to avoid duplicate messages
|
||||
if not multiplayer.is_server():
|
||||
return
|
||||
|
||||
# Get player name from player_info
|
||||
var player_name = ""
|
||||
var player_names = player_info.get("player_names", [])
|
||||
if player_names.size() > 0:
|
||||
player_name = player_names[0]
|
||||
else:
|
||||
player_name = "Player%d" % peer_id
|
||||
|
||||
# Get chat UI
|
||||
var chat_ui = get_node_or_null("ChatUI")
|
||||
if chat_ui and chat_ui.has_method("send_system_message"):
|
||||
var message = "%s left/disconnected" % player_name
|
||||
chat_ui.send_system_message(message)
|
||||
|
||||
func _create_level_complete_ui_programmatically() -> Node:
|
||||
# Create level complete UI programmatically
|
||||
var canvas_layer = CanvasLayer.new()
|
||||
|
||||
Reference in New Issue
Block a user