mixedcards/Assets/Battle/HandControls.cs
2020-09-30 19:33:53 -04:00

113 lines
3.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public enum HandControlsPosition {
Top,
Bottom,
}
public class HandControls<BaseCardT> where BaseCardT : BaseCard
{
public delegate void CardClickedAction(BaseCardT card);
public event CardClickedAction OnCardClicked;
private BattleDeck<BaseCardT> deck;
private GameObject uiCanvas;
private GameObject cardPrefab;
private HandControlsPosition position;
private List<BaseCardT> hand;
private List<GameObject> cardsInHandUIs;
private TeamChoice team;
private int insightLevel;
private HandPlayability<BaseCardT> playability;
private float scale;
public HandControls(BattleDeck<BaseCardT> deck, GameObject uiCanvas, GameObject cardPrefab,
HandControlsPosition position, TeamChoice team, int insightLevel,
HandPlayability<BaseCardT> playability, float scale) {
this.deck = deck;
this.uiCanvas = uiCanvas;
this.cardPrefab = cardPrefab;
this.position = position;
this.team = team;
this.insightLevel = insightLevel;
this.playability = playability;
this.scale = scale;
deck.OnHandPileChanged += this.UpdateState;
if (playability != null) {
playability.OnPlayabilityChanged += this.UpdateState;
}
}
private Vector3 CalculateHandCardPosition(int index, int count) {
float SPACING = 0 * this.scale;
float DELTA = BattleCardPrefabConstants.CARD_WIDTH * this.scale + SPACING;
float x = Screen.width * 0.5f - DELTA / 2 * (count - 1) + DELTA * index;
float y = (this.position == HandControlsPosition.Bottom
? SPACING + BattleCardPrefabConstants.CARD_HEIGHT * this.scale / 2
: Screen.height - SPACING - BattleCardPrefabConstants.CARD_HEIGHT * this.scale / 2);
return new Vector3(x, y, 0);
}
public int InsightLevel {
get => this.insightLevel;
set {
this.insightLevel = value;
UpdateState();
}
}
private void RenderHand() {
if (this.hand == null) {
return;
}
if (this.cardsInHandUIs != null) {
foreach (GameObject cardInHandUI in this.cardsInHandUIs) {
GameObject.Destroy(cardInHandUI);
}
}
this.cardsInHandUIs = new List<GameObject>();
for (int i = 0; i < this.hand.Count; i++) {
BaseCardT card = this.hand[i];
GameObject cardUI = GameObject.Instantiate(this.cardPrefab, this.uiCanvas.transform);
this.cardsInHandUIs.Add(cardUI);
cardUI.transform.position = CalculateHandCardPosition(i, this.hand.Count);
CardUI cardUIScript = cardUI.GetComponent<CardUI>();
if (i < this.insightLevel) {
cardUIScript.Init(card);
} else {
cardUIScript.InitHidden();
}
int cardIndex = i;
cardUI.GetComponent<Button>().onClick.AddListener(delegate {OnCardClicked(card);});
}
}
private void UpdateState() {
this.hand = this.deck.GetHandPile().ToList();
RenderHand();
UpdateCardPlayability();
}
private void UpdateCardPlayability() {
if (this.hand == null || this.playability == null) {
return;
}
for (int i = 0; i < this.hand.Count; i++) {
BaseCardT card = this.hand[i];
CardUI cardUI = this.cardsInHandUIs[i].GetComponent<CardUI>();
cardUI.Playable = this.playability.IsPlayable(card);
}
}
}