You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

103 lines
4.2 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
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 System.Action onReloadPiles;
private int tmpCounter = 0;
void Start() {
mmc = FindObjectOfType<MainMenuController>();
mmc.gameObject.SetActive(false);
var angleDelta = (270) / playerCount;
var dst = 15;
var angleOffset = 45;
for (int i=0;i<playerCount;i++) {
if (i == myIndex)
continue;
var offsetMult = (i - myIndex) > 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<Renderer>().material.color = playerColors[i];
}
SpawnClientCards();
SpawnCommonCards();
onReloadPiles += SpawnCommonCards;
onReloadPiles += SpawnClientCards;
}
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) => {
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<RawImage>()[0].texture = front;
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.GetCardImage(cardKind);
}
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];
int idx = 0;
foreach (Game.GameStatus.Types.Card card in pile.Cards) {
SpawnCard(conn, card.Kind.Kind, handUI, (GameObject card) => {
card.GetComponent<Card>().InitData(key, false, idx);
});
idx++;
}
}
}
}
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>().InitData(key, true, idx);
});
idx++;
}
}
}
}
public static void ReloadPiles() {
var cards = GameObject.FindGameObjectsWithTag("Card");
foreach (GameObject card in cards) {
Destroy(card);
}
onReloadPiles();
}
}