|
|
|
@ -1,7 +1,9 @@ |
|
|
|
using System.Collections; |
|
|
|
using System; |
|
|
|
using System.Collections; |
|
|
|
using System.Collections.Generic; |
|
|
|
using UnityEngine; |
|
|
|
using UnityEngine.UI; |
|
|
|
using Random = UnityEngine.Random; |
|
|
|
|
|
|
|
public class GameLoader : MonoBehaviour { |
|
|
|
public GameObject table; |
|
|
|
@ -14,12 +16,15 @@ public class GameLoader : MonoBehaviour { |
|
|
|
public GameObject deck; |
|
|
|
public GameObject thrownCards; |
|
|
|
public static MainMenuController mmc; |
|
|
|
public static System.Action onReloadPiles; |
|
|
|
public static GameLoader myself; |
|
|
|
|
|
|
|
private int tmpCounter = 0; |
|
|
|
void Awake() { |
|
|
|
myself = this; |
|
|
|
} |
|
|
|
void Start() { |
|
|
|
mmc = FindObjectOfType<MainMenuController>(); |
|
|
|
mmc.gameObject.SetActive(false); |
|
|
|
|
|
|
|
var angleDelta = (270) / playerCount; |
|
|
|
var dst = 15; |
|
|
|
var angleOffset = 45; |
|
|
|
@ -32,15 +37,13 @@ public class GameLoader : MonoBehaviour { |
|
|
|
var player = Instantiate(playerCube, pos, Quaternion.AngleAxis(-angle, Vector3.up)); |
|
|
|
player.GetComponent<Renderer>().material.color = playerColors[i]; |
|
|
|
} |
|
|
|
SpawnClientCards(); |
|
|
|
SpawnCommonCards(); |
|
|
|
onReloadPiles += SpawnCommonCards; |
|
|
|
onReloadPiles += SpawnClientCards; |
|
|
|
ReloadPiles(); |
|
|
|
} |
|
|
|
|
|
|
|
void SpawnCard(Client.ConnectionImpl conn, string cardKind, GameObject parent, System.Action<GameObject> callback) { |
|
|
|
var card = Instantiate(cardPrefab, Vector3.zero, Quaternion.identity, parent.transform); |
|
|
|
conn.eventManager.AddHandler("game_card_image", (Game.Image image) => { |
|
|
|
string uuid = Guid.NewGuid().ToString(); |
|
|
|
conn.eventManager.AddHandler("game_card_image_"+uuid, (Game.Image image) => { |
|
|
|
var card = Instantiate(cardPrefab, Vector3.zero, Quaternion.identity, parent.transform); |
|
|
|
var front = new Texture2D(1, 1); |
|
|
|
front.LoadImage(image.Face.Span.ToArray()); |
|
|
|
var back = new Texture2D(1, 1); |
|
|
|
@ -49,7 +52,7 @@ public class GameLoader : MonoBehaviour { |
|
|
|
card.transform.Find("Images").GetComponentsInChildren<RawImage>()[1].texture = back; |
|
|
|
card.transform.localScale = new Vector3(0.15f, 0.15f, 1f); |
|
|
|
callback.Invoke(card); |
|
|
|
Client.GetConnection().eventManager.RemoveHandler(Protocol.ServerClientPacket.DataOneofCase.ReturnCardImage, "game_card_image"); |
|
|
|
conn.eventManager.RemoveHandler(Protocol.ServerClientPacket.DataOneofCase.ReturnCardImage, "game_card_image_"+uuid); |
|
|
|
}); |
|
|
|
conn.GetCardImage(cardKind); |
|
|
|
} |
|
|
|
@ -57,9 +60,7 @@ public class GameLoader : MonoBehaviour { |
|
|
|
void SpawnClientCards() { |
|
|
|
var conn = Client.GetConnection(); |
|
|
|
if (conn != null) { |
|
|
|
Debug.Log("Spawning player cards."); |
|
|
|
var clientPiles = conn.GetPlayerPiles(mmc.currentUsername); |
|
|
|
Debug.Log("[player] clientPiles = "+clientPiles); |
|
|
|
if (clientPiles == null) return; |
|
|
|
foreach (string key in clientPiles.Keys) { |
|
|
|
var pile = clientPiles[key]; |
|
|
|
@ -76,15 +77,14 @@ public class GameLoader : MonoBehaviour { |
|
|
|
void SpawnCommonCards() { |
|
|
|
var conn = Client.GetConnection(); |
|
|
|
if (conn != null) { |
|
|
|
Debug.Log("Spawning common cards."); |
|
|
|
var clientPiles = conn.GetCommonPiles(); |
|
|
|
Debug.Log("[common] clientPiles = " + clientPiles); |
|
|
|
if (clientPiles == null) return; |
|
|
|
foreach (string key in clientPiles.Keys) { |
|
|
|
var pile = clientPiles[key]; |
|
|
|
int idx = 0; |
|
|
|
foreach (Game.GameStatus.Types.Card card in pile.Cards) { |
|
|
|
SpawnCard(conn, card.Kind.Kind, key == "placed" ? thrownCards : deck, (GameObject card) => { |
|
|
|
card.GetComponent<Card>().ThrowCard(key == "placed" ? thrownCards : deck); |
|
|
|
card.GetComponent<Card>().InitData(key, true, idx); |
|
|
|
}); |
|
|
|
idx++; |
|
|
|
@ -92,12 +92,13 @@ public class GameLoader : MonoBehaviour { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static void ReloadPiles() { |
|
|
|
if (myself == null) return; |
|
|
|
var cards = GameObject.FindGameObjectsWithTag("Card"); |
|
|
|
foreach (GameObject card in cards) { |
|
|
|
Destroy(card); |
|
|
|
} |
|
|
|
onReloadPiles(); |
|
|
|
myself.SpawnCommonCards(); |
|
|
|
myself.SpawnClientCards(); |
|
|
|
} |
|
|
|
} |
|
|
|
|