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; public GameObject playerCube; public int myIndex; public int playerCount; public Color[] playerColors; public GameObject cardPrefab; public GameObject handUI; public GameObject deck; public GameObject thrownCards; public static MainMenuController mmc; public static GameLoader myself; void Awake() { myself = this; } void Start() { mmc = FindObjectOfType(); mmc.gameObject.SetActive(false); var angleDelta = (270) / playerCount; var dst = 15; var angleOffset = 45; for (int i=0;i 0 ? 1 : -1; var angle = (angleDelta * (i - myIndex) + angleOffset * offsetMult); Vector3 pos = new Vector3(dst * Mathf.Sin(angle * Mathf.Deg2Rad), 2.6f, -dst * Mathf.Cos(angle * Mathf.Deg2Rad)); var player = Instantiate(playerCube, pos, Quaternion.AngleAxis(-angle, Vector3.up)); player.GetComponent().material.color = playerColors[i]; } ReloadPiles(); } void SpawnCard(Client.ConnectionImpl conn, string cardKind, GameObject parent, System.Action callback) { 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); back.LoadImage(image.Back.Span.ToArray()); card.transform.Find("Images").GetComponentsInChildren()[0].texture = front; card.transform.Find("Images").GetComponentsInChildren()[1].texture = back; card.transform.localScale = new Vector3(0.15f, 0.15f, 1f); callback.Invoke(card); conn.eventManager.RemoveHandler(Protocol.ServerClientPacket.DataOneofCase.ReturnCardImage, "game_card_image_"+uuid); }); conn.GetCardImage(cardKind); } void SpawnClientCards() { var conn = Client.GetConnection(); if (conn != null) { var clientPiles = conn.GetPlayerPiles(mmc.currentUsername); 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, handUI, (GameObject card) => { card.GetComponent().InitData(key, false, idx); }); idx++; } } } } void SpawnCommonCards() { var conn = Client.GetConnection(); if (conn != null) { var clientPiles = conn.GetCommonPiles(); 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().ThrowCard(key == "placed" ? thrownCards : deck); card.GetComponent().InitData(key, true, idx); }); idx++; } } } } public static void ReloadPiles() { if (myself == null) return; var cards = GameObject.FindGameObjectsWithTag("Card"); foreach (GameObject card in cards) { Destroy(card); } myself.SpawnCommonCards(); myself.SpawnClientCards(); } }