117 lines
3.6 KiB
GDScript
117 lines
3.6 KiB
GDScript
class_name EnvironmentManager
|
|
extends Node3D
|
|
|
|
var hand_associations: Dictionary[String, PlayerHand]
|
|
|
|
@export var web_client: WebClientTCP
|
|
|
|
@export var trump_container: Node3D
|
|
|
|
# In gameplay order
|
|
#var hand_associations: Dictionary[int, PlayerHandAssociation]
|
|
var _own_client_id: String
|
|
@export var own_player_hand: PlayerHand
|
|
|
|
## Positions for hands in 3-player game (starting from player 2).
|
|
@export var hand_markers_3: Array[Marker3D]
|
|
|
|
## Positions for hands in 4-player game (starting from player 2).
|
|
@export var hand_markers_4: Array[Marker3D]
|
|
|
|
## Positions for hands in 5-player game (starting from player 2).
|
|
@export var hand_markers_5: Array[Marker3D]
|
|
|
|
## Positions for hands in 6-player game (starting from player 2).
|
|
@export var hand_markers_6: Array[Marker3D]
|
|
|
|
@export var player_hand_scene: PackedScene
|
|
@export var card_scene: PackedScene
|
|
|
|
## Cards played in the current play.
|
|
var _plays: Array[Play]
|
|
var _trump_id: int = -1
|
|
|
|
func _on_web_client_tcp_client_order_received(client_ids: Array[String]) -> void:
|
|
var marker_positions: Array[Marker3D]
|
|
match client_ids.size():
|
|
3:
|
|
marker_positions = hand_markers_3
|
|
4:
|
|
marker_positions = hand_markers_4
|
|
5:
|
|
marker_positions = hand_markers_5
|
|
6:
|
|
marker_positions = hand_markers_6
|
|
|
|
var own_index: int = client_ids.find(_own_client_id)
|
|
|
|
var new_ids: Array[String]
|
|
if own_index == 0:
|
|
# no change necessary!
|
|
new_ids = client_ids
|
|
elif own_index == client_ids.size() - 1:
|
|
# you are at the end of the array
|
|
new_ids = client_ids
|
|
# take away own client id
|
|
new_ids.erase(_own_client_id)
|
|
# add it to the start of the array
|
|
new_ids.insert(0, _own_client_id)
|
|
else:
|
|
# you are somewhere in the middle of the array
|
|
# create array with you at the start
|
|
new_ids = client_ids.slice(own_index)
|
|
# add the rest of the original array
|
|
new_ids.append_array(client_ids.slice(0, own_index))
|
|
|
|
for index in new_ids.size():
|
|
var hand: PlayerHand
|
|
if client_ids[index] == _own_client_id:
|
|
hand = own_player_hand
|
|
else:
|
|
hand = player_hand_scene.instantiate()
|
|
self.add_child(hand)
|
|
var reference_marker = marker_positions[index - 1]
|
|
hand.global_position = reference_marker.global_position
|
|
hand.global_rotation = reference_marker.global_rotation
|
|
|
|
#hand_associations[index] = PlayerHandAssociation.new(
|
|
#client_ids[index],
|
|
#hand.get_path(),
|
|
#)
|
|
hand_associations[client_ids[index]] = hand
|
|
|
|
func _on_web_client_tcp_received_client_id(client_id: String) -> void:
|
|
_own_client_id = client_id
|
|
|
|
|
|
func _on_web_client_tcp_cards_received(cards: Dictionary) -> void:
|
|
for client_id in cards.keys():
|
|
var card_ids: Array[int]
|
|
card_ids.assign(cards[client_id])
|
|
hand_associations[client_id].add_cards(card_ids, client_id)
|
|
|
|
func _on_web_client_tcp_received_played_card(client_id: String, card_id: int) -> void:
|
|
print(client_id + " played card " + str(card_id))
|
|
_plays.append(Play.new(client_id, card_id))
|
|
|
|
func _on_web_client_tcp_received_play_winner_request() -> void:
|
|
if not $"../HBoxContainer/GameUI"._is_host:
|
|
return
|
|
|
|
var card_ids: Array[int] = []
|
|
card_ids.assign(_plays.map(func(p: Play): return p.card_id))
|
|
var winning_card_id: int = CardManager.get_winning_card(card_ids, _trump_id)
|
|
|
|
for play in _plays:
|
|
if play.card_id == winning_card_id:
|
|
web_client.submit_play_winner(play.client_id)
|
|
return
|
|
|
|
func _on_web_client_tcp_trump_received(trump_id: int) -> void:
|
|
if trump_container.get_child_count() > 0:
|
|
trump_container.remove_child(trump_container.get_child(0))
|
|
_trump_id = trump_id
|
|
var trump_card: PlayingCard = card_scene.instantiate()
|
|
trump_card.setup_card_details(_trump_id)
|
|
trump_container.add_child(trump_card)
|