From 898c0d21ea30ae29f47a6caa4b45341a9c42bbdc Mon Sep 17 00:00:00 2001 From: denizk0461 Date: Tue, 8 Jul 2025 17:40:13 +0200 Subject: [PATCH] started adding player hand creation --- components/player_hand_association.gd | 12 ++++++++++ components/player_hand_association.gd.uid | 1 + components/web_client_tcp.gd | 13 +++++++++++ environment_manager.gd | 28 +++++++++++++++++++++++ environment_manager.gd.uid | 1 + game.tscn | 26 +++++++++++++-------- 6 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 components/player_hand_association.gd create mode 100644 components/player_hand_association.gd.uid create mode 100644 environment_manager.gd create mode 100644 environment_manager.gd.uid diff --git a/components/player_hand_association.gd b/components/player_hand_association.gd new file mode 100644 index 0000000..d85ff9e --- /dev/null +++ b/components/player_hand_association.gd @@ -0,0 +1,12 @@ +class_name PlayerHandAssociation +extends Resource + +@export var client_id: String +@export var player_hand: NodePath + +func _init( + client_id: String, + player_hand: NodePath, +) -> void: + self.client_id = client_id + self.player_hand = player_hand diff --git a/components/player_hand_association.gd.uid b/components/player_hand_association.gd.uid new file mode 100644 index 0000000..e35f665 --- /dev/null +++ b/components/player_hand_association.gd.uid @@ -0,0 +1 @@ +uid://b7jhvapb5344t diff --git a/components/web_client_tcp.gd b/components/web_client_tcp.gd index 93f66d5..4ef3700 100644 --- a/components/web_client_tcp.gd +++ b/components/web_client_tcp.gd @@ -26,6 +26,7 @@ signal disconnected_from_server() signal connected_to_lobby(lobby_id: String) signal disconnected_from_lobby() +signal received_client_id(client_id: String) signal client_joined_lobby(client_name: String, connected_client_names: Array[String]) signal client_left_lobby(client_name: String, connected_client_names: Array[String]) @@ -35,6 +36,9 @@ signal received_chat_message(client_name: String, message: String) signal connection_status_changed(new_status: ConnectionStatus) +signal client_order_received(client_ids: Array[String]) +signal cards_received(card_ids: Array[int]) + enum ConnectionStatus { DISCONNECTED, ATTEMPTING_CONNECTION, @@ -117,6 +121,14 @@ func _handle_response(response: String) -> void: "new_round_started": print(data) + if data["current_round"] == 1: + var playing_order: Array[String] = [] + playing_order.assign(data["playing_order"]) + client_order_received.emit.call_deferred(playing_order) + + var received_cards: Array[int] = [] + received_cards.assign(data["cards"]) + cards_received.emit.call_deferred(received_cards) "client_submitted_bet": print(data) "client_played_card": @@ -133,6 +145,7 @@ func _handle_join(data) -> void: "ok": _client_id = data["client_id"] connected_to_lobby.emit.call_deferred(_lobby_id) + received_client_id.emit.call_deferred(_client_id) connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED) func _exit_tree() -> void: diff --git a/environment_manager.gd b/environment_manager.gd new file mode 100644 index 0000000..dae9a66 --- /dev/null +++ b/environment_manager.gd @@ -0,0 +1,28 @@ +class_name EnvironmentManager +extends Node3D + +var player_hands: Dictionary[int, PlayerHandAssociation] +var _own_client_id: String +@export var own_player_hand: PlayerHand + +@export var player_hand_scene: PackedScene + +func _on_web_client_tcp_client_order_received(client_ids: Array[String]) -> void: + for index in client_ids.size(): + if client_ids[index] == _own_client_id: + player_hands[index] = PlayerHandAssociation.new( + _own_client_id, + own_player_hand.get_path(), + ) + else: + var hand: PlayerHand = player_hand_scene.instantiate() + + # TODO + self.add_child(hand) # needs position! + player_hands[index] = PlayerHandAssociation.new( + client_ids[index], + hand.get_path(), + ) + +func _on_web_client_tcp_received_client_id(client_id: String) -> void: + _own_client_id = client_id diff --git a/environment_manager.gd.uid b/environment_manager.gd.uid new file mode 100644 index 0000000..a1b2ae5 --- /dev/null +++ b/environment_manager.gd.uid @@ -0,0 +1 @@ +uid://bipfa366flrfa diff --git a/game.tscn b/game.tscn index 9af5d23..c481b4b 100644 --- a/game.tscn +++ b/game.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=8 format=3 uid="uid://gqrdpjslr4np"] +[gd_scene load_steps=9 format=3 uid="uid://gqrdpjslr4np"] [ext_resource type="Script" uid="uid://p5gvnyhjqe6o" path="res://components/web_client_tcp.gd" id="1_feb5d"] [ext_resource type="Script" uid="uid://c0cqkatyo4uj1" path="res://components/client_interface.gd" id="2_e2o6t"] [ext_resource type="Script" uid="uid://utt8atbh7b2a" path="res://game_client.gd" id="3_feb5d"] +[ext_resource type="Script" uid="uid://bipfa366flrfa" path="res://environment_manager.gd" id="4_7jktm"] [ext_resource type="PackedScene" uid="uid://jlovgr2iojrg" path="res://elements/player_hand.tscn" id="4_fc0e3"] [sub_resource type="SystemFont" id="SystemFont_80nbo"] @@ -181,31 +182,35 @@ grow_vertical = 0 disabled = true text = "Play Card" -[node name="Node3D" type="Node3D" parent="."] +[node name="EnvironmentManager" type="Node3D" parent="." node_paths=PackedStringArray("own_player_hand")] +script = ExtResource("4_7jktm") +own_player_hand = NodePath("PlayerHand") +player_hand_scene = ExtResource("4_fc0e3") -[node name="Camera3D" type="Camera3D" parent="Node3D"] +[node name="Camera3D" type="Camera3D" parent="EnvironmentManager"] transform = Transform3D(1, 0, 0, 0, 0.997882, 0.0650548, 0, -0.0650548, 0.997882, 0, 0.171315, 1.65638) -[node name="Label3D" type="Label3D" parent="Node3D"] +[node name="Label3D" type="Label3D" parent="EnvironmentManager"] billboard = 1 text = "Magician Online!" -[node name="DirectionalLight3D" type="DirectionalLight3D" parent="Node3D"] +[node name="DirectionalLight3D" type="DirectionalLight3D" parent="EnvironmentManager"] transform = Transform3D(0.970511, 0, 0.241058, -0.137972, 0.820002, 0.555482, -0.197668, -0.572361, 0.795821, 2.41581, 2.80426, 0) -[node name="WorldEnvironment" type="WorldEnvironment" parent="Node3D"] +[node name="WorldEnvironment" type="WorldEnvironment" parent="EnvironmentManager"] environment = SubResource("Environment_fc0e3") -[node name="PlayerHand" parent="Node3D" instance=ExtResource("4_fc0e3")] +[node name="PlayerHand" parent="EnvironmentManager" instance=ExtResource("4_fc0e3")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1.91793) -[node name="MeshInstance3D" type="MeshInstance3D" parent="Node3D"] +[node name="MeshInstance3D" type="MeshInstance3D" parent="EnvironmentManager"] mesh = SubResource("BoxMesh_fc0e3") [connection signal="client_joined_lobby" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_client_joined_lobby"] [connection signal="client_joined_lobby" from="WebClientTCP" to="HBoxContainer/GameUI" method="_on_web_client_tcp_client_joined_lobby"] [connection signal="client_left_lobby" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_client_left_lobby"] [connection signal="client_left_lobby" from="WebClientTCP" to="HBoxContainer/GameUI" method="_on_web_client_tcp_client_left_lobby"] +[connection signal="client_order_received" from="WebClientTCP" to="EnvironmentManager" method="_on_web_client_tcp_client_order_received"] [connection signal="connected_to_lobby" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_connected_to_lobby"] [connection signal="connected_to_server" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_connected_to_server"] [connection signal="connection_error_occurred" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_connection_error_occurred"] @@ -213,6 +218,7 @@ mesh = SubResource("BoxMesh_fc0e3") [connection signal="disconnected_from_lobby" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_disconnected_from_lobby"] [connection signal="disconnected_from_server" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_disconnected_from_server"] [connection signal="received_chat_message" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_received_chat_message"] +[connection signal="received_client_id" from="WebClientTCP" to="EnvironmentManager" method="_on_web_client_tcp_received_client_id"] [connection signal="received_connection_message" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_received_connection_message"] [connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ServerInputs/ConnectButton" to="HBoxContainer/ClientInterface" method="_on_connect_button_pressed"] [connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ServerInputs/CreateLobbyButton" to="HBoxContainer/ClientInterface" method="_on_create_lobby_button_pressed"] @@ -222,5 +228,5 @@ mesh = SubResource("BoxMesh_fc0e3") [connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ServerInputs/LeaveLobbyButton" to="HBoxContainer/ClientInterface" method="_on_disconnect_lobby_button_pressed"] [connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ChatContainer/SendMessageButton" to="HBoxContainer/ClientInterface" method="_on_send_message_button_pressed"] [connection signal="pressed" from="HBoxContainer/GameUI/StartGameButton" to="HBoxContainer/GameUI" method="_on_start_game_button_pressed"] -[connection signal="pressed" from="HBoxContainer/GameUI/PlayCardButton" to="Node3D/PlayerHand" method="_on_play_card_button_pressed"] -[connection signal="card_selected" from="Node3D/PlayerHand" to="HBoxContainer/GameUI" method="_on_player_hand_card_selected"] +[connection signal="pressed" from="HBoxContainer/GameUI/PlayCardButton" to="EnvironmentManager/PlayerHand" method="_on_play_card_button_pressed"] +[connection signal="card_selected" from="EnvironmentManager/PlayerHand" to="HBoxContainer/GameUI" method="_on_player_hand_card_selected"]