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.
197 lines
9.1 KiB
197 lines
9.1 KiB
using System;
|
|
using System.Linq;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using Random = UnityEngine.Random;
|
|
|
|
public class PileProperties {
|
|
public string name;
|
|
public string owner;
|
|
public Game.GameStatus.Types.Card[] cards;
|
|
public bool faceDown;
|
|
public GameObject gameObject;
|
|
}
|
|
|
|
public class CardProperties {
|
|
public Quaternion rotation;
|
|
public string pileName;
|
|
public int idx;
|
|
public string kind;
|
|
public GameObject gameObject;
|
|
}
|
|
|
|
public class GameLoader : MonoBehaviour {
|
|
public GameObject table;
|
|
public GameObject playerCube;
|
|
public int myIndex;
|
|
public int playerCount;
|
|
public Color[] playerColors;
|
|
public GameObject cardPrefab;
|
|
public GameObject cardCanvas;
|
|
public GameObject handUI;
|
|
public GameObject deck;
|
|
public GameObject thrownCards;
|
|
private static float yOffsetTable = 0.51f / 2;
|
|
public static MainMenuController mmc;
|
|
public static GameLoader myself;
|
|
public Dictionary<string, Texture2D[]> cardImages = new Dictionary<string, Texture2D[]>();
|
|
public static Dictionary<string, CardProperties> cardRegistry = new Dictionary<string, CardProperties>();
|
|
public static Dictionary<string, PileProperties> pileRegistry = new Dictionary<string, PileProperties>();
|
|
private Client.ConnectionImpl conn;
|
|
|
|
void Awake() {
|
|
myself = this;
|
|
}
|
|
void Start() {
|
|
conn = Client.GetConnection();
|
|
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];
|
|
}
|
|
}
|
|
public static void RegisterCard(string uuid, CardProperties properties) {
|
|
if (!cardRegistry.ContainsKey(uuid)) cardRegistry.Add(uuid, properties);
|
|
}
|
|
public static void UpdateCard(string uuid, CardProperties properties) {
|
|
if (cardRegistry.ContainsKey(uuid)) cardRegistry[uuid] = properties;
|
|
}
|
|
public static CardProperties GetCard(string uuid) {
|
|
if (cardRegistry.ContainsKey(uuid)) return cardRegistry[uuid];
|
|
else return null;
|
|
}
|
|
public static void DeleteCard(string uuid) {
|
|
if (cardRegistry.ContainsKey(uuid)) cardRegistry.Remove(uuid);
|
|
}
|
|
|
|
|
|
public static void RegisterPile(string name, PileProperties properties) {
|
|
if (!pileRegistry.ContainsKey(name)) pileRegistry.Add(name, properties);
|
|
}
|
|
public static void UpdatePile(string name, PileProperties properties) {
|
|
if (pileRegistry.ContainsKey(name)) pileRegistry[name] = properties;
|
|
}
|
|
public static PileProperties GetPile(string name) {
|
|
if (pileRegistry.ContainsKey(name)) return pileRegistry[name];
|
|
else return null;
|
|
}
|
|
public static void DeletePile(string name) {
|
|
if (pileRegistry.ContainsKey(name)) pileRegistry.Remove(name);
|
|
}
|
|
|
|
bool SpawnCard(string uuid, string cardKind, int index, string pileName, bool visible, Game.Image image) {
|
|
if (image != null && cardKind != image.Kind) return false;
|
|
|
|
PileProperties pileProps = GetPile(pileName);
|
|
var card = Instantiate(cardPrefab, Vector3.zero, Quaternion.identity, pileProps.gameObject.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);
|
|
card.GetComponent<Card>().uuid = uuid;
|
|
|
|
CardProperties cardProps = GetCard(uuid);
|
|
|
|
GameObject canvas = null;
|
|
if (pileProps.owner == "") {
|
|
canvas = Instantiate(cardCanvas, pileProps.gameObject.transform.position + new Vector3(0, yOffsetTable + pileProps.gameObject.transform.childCount * 0.0075f, 0), cardProps != null ? cardProps.rotation : Quaternion.identity);
|
|
canvas.GetComponent<Canvas>().worldCamera = Camera.main;
|
|
card.transform.localScale = Vector3.one;
|
|
card.transform.SetParent(canvas.transform, false);
|
|
canvas.transform.SetParent(pileProps.gameObject.transform);
|
|
canvas.tag = "ThrownCard";
|
|
canvas.transform.localScale = new Vector3(0.01f, 0.01f);
|
|
|
|
if (canvas.transform.rotation == Quaternion.identity) {
|
|
canvas.transform.Rotate(new Vector3(90f, 0f, 0f));
|
|
canvas.transform.Rotate(new Vector3(0f, 0f, Random.Range(-20, 20)));
|
|
}
|
|
}
|
|
|
|
if(!visible || pileProps.faceDown) {
|
|
card.transform.GetChild(1).Rotate(0, 180f, 0);
|
|
}
|
|
|
|
if (cardProps != null) {
|
|
if (cardProps.kind != cardKind) cardProps.kind = cardKind;
|
|
if (cardProps.idx != index) cardProps.idx = index;
|
|
if (cardProps.pileName != pileName) cardProps.pileName = pileName;
|
|
if (cardProps.gameObject != card) cardProps.gameObject = card;
|
|
if (canvas != null && cardProps.rotation != canvas.transform.rotation) cardProps.rotation = canvas.transform.rotation;
|
|
UpdateCard(uuid, cardProps);
|
|
} else {
|
|
cardProps = new CardProperties { kind = cardKind, idx = index, pileName = pileName, rotation = canvas != null ? canvas.transform.rotation : Quaternion.identity, gameObject = card };
|
|
RegisterCard(uuid, cardProps);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void SpawnCards() {
|
|
foreach (string key in pileRegistry.Keys) {
|
|
int idx = 0;
|
|
foreach (Game.GameStatus.Types.Card card in pileRegistry[key].cards) {
|
|
if (cardImages.ContainsKey(card.Kind.Kind)) {
|
|
SpawnCard(card.Uuid, card.Kind.Kind, idx, key, card.Visible, null);
|
|
} else {
|
|
string uuid = card.Uuid;
|
|
string kind = card.Kind.Kind;
|
|
int idx2 = idx;
|
|
string key2 = key;
|
|
bool visible = card.Visible;
|
|
conn.eventManager.AddHandler("game_card_image_" + uuid, (Game.Image image) => {
|
|
if(!SpawnCard(uuid, kind, idx2, key2, visible, image)) return;
|
|
conn.eventManager.RemoveHandler(Protocol.ServerClientPacket.DataOneofCase.ReturnCardImage, "game_card_image_" + uuid);
|
|
});
|
|
conn.GetCardImage(card.Kind.Kind);
|
|
}
|
|
idx++;
|
|
}
|
|
}
|
|
}
|
|
public static void ReloadPiles() {
|
|
if (myself == null) return;
|
|
|
|
// Hardcoded values for pile gameobjects
|
|
var piles = myself.conn.GetPlayerPiles(mmc.currentUsername);
|
|
foreach(var pair in piles.Keys.Zip(piles.Values, (key, value) => new dynamic[] { key, value })) {
|
|
string key = (string)pair[0];
|
|
Game.GameStatus.Types.Pile value = (Game.GameStatus.Types.Pile)pair[1];
|
|
if(GetPile(key+mmc.currentUsername) == null) RegisterPile(key + mmc.currentUsername, new PileProperties { name = key, owner = mmc.currentUsername, cards = value.Cards.ToArray(), gameObject = myself.handUI, faceDown = value.FaceDown });
|
|
else UpdatePile(key + mmc.currentUsername, new PileProperties { name = key, owner = mmc.currentUsername, cards = value.Cards.ToArray(), gameObject = myself.handUI, faceDown = value.FaceDown });
|
|
}
|
|
piles = myself.conn.GetCommonPiles();
|
|
foreach (var pair in piles.Keys.Zip(piles.Values, (key, value) => new dynamic[] { key, value })) {
|
|
string key = (string)pair[0];
|
|
Game.GameStatus.Types.Pile value = (Game.GameStatus.Types.Pile)pair[1];
|
|
if (GetPile(key + "_common") == null) RegisterPile(key + "_common", new PileProperties { name = key, owner = "", cards = value.Cards.ToArray(), gameObject = key == "placed" ? myself.thrownCards : myself.deck, faceDown = value.FaceDown });
|
|
else UpdatePile(key+"_common", new PileProperties { name = key, owner = "", cards = value.Cards.ToArray(), gameObject = key == "placed" ? myself.thrownCards : myself.deck, faceDown = value.FaceDown });
|
|
}
|
|
var cards = FindObjectsOfType<Card>();
|
|
foreach (Card card in cards) {
|
|
if (card.IsThrown()) Destroy(card.transform.parent.gameObject);
|
|
else Destroy(card.gameObject);
|
|
}
|
|
myself.SpawnCards();
|
|
}
|
|
}
|
|
|