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.
116 lines
4.7 KiB
116 lines
4.7 KiB
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;
|
|
public Dictionary<string, Texture2D[]> cardImages = new Dictionary<string, Texture2D[]>();
|
|
|
|
void Awake() {
|
|
myself = this;
|
|
}
|
|
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];
|
|
}
|
|
ReloadPiles();
|
|
}
|
|
|
|
void SpawnCard(Client.ConnectionImpl conn, string cardKind, GameObject parent, System.Action<GameObject> callback) {
|
|
string uuid = Guid.NewGuid().ToString();
|
|
conn.eventManager.AddHandler("game_card_image_"+uuid, (Game.Image image) => {
|
|
if (cardKind != image.Kind) return;
|
|
var card = Instantiate(cardPrefab, Vector3.zero, Quaternion.identity, parent.transform);
|
|
var front = new Texture2D(1, 1);
|
|
var back = new Texture2D(1, 1);
|
|
if (cardImages.ContainsKey(cardKind)) {
|
|
front = cardImages[cardKind][0];
|
|
back = cardImages[cardKind][1];
|
|
} else {
|
|
front.LoadImage(image.Face.Span.ToArray());
|
|
back.LoadImage(image.Back.Span.ToArray());
|
|
cardImages.Add(cardKind, new Texture2D[] { front, back });
|
|
}
|
|
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);
|
|
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) {
|
|
string key2 = key;
|
|
int idx2 = idx;
|
|
SpawnCard(conn, card.Kind.Kind, handUI, (GameObject card) => {
|
|
card.GetComponent<Card>().InitData(key2, false, idx2);
|
|
});
|
|
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) {
|
|
string key2 = key;
|
|
int idx2 = idx;
|
|
SpawnCard(conn, card.Kind.Kind, key == "placed" ? thrownCards : deck, (GameObject card) => {
|
|
card.GetComponent<Card>().InitData(key2, true, idx2);
|
|
card.GetComponent<Card>().ThrowCard(key2 == "placed" ? thrownCards : deck);
|
|
});
|
|
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();
|
|
}
|
|
}
|
|
|