49 lines
1.7 KiB
C#
49 lines
1.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class HandControls<BaseCardT> where BaseCardT : BaseCard
|
|
{
|
|
public delegate void CardClickedAction(BaseCardT card);
|
|
public event CardClickedAction OnCardClicked;
|
|
|
|
private GameObject uiCanvas;
|
|
private GameObject cardPrefab;
|
|
|
|
private List<GameObject> cardsInHandUIs;
|
|
|
|
public HandControls(GameObject uiCanvas, GameObject cardPrefab) {
|
|
this.uiCanvas = uiCanvas;
|
|
this.cardPrefab = cardPrefab;
|
|
}
|
|
|
|
public void SetHeroHand(List<BaseCardT> hand) {
|
|
if (this.cardsInHandUIs != null) {
|
|
foreach (GameObject cardInHandUI in this.cardsInHandUIs) {
|
|
GameObject.Destroy(cardInHandUI);
|
|
}
|
|
}
|
|
|
|
this.cardsInHandUIs = new List<GameObject>();
|
|
for (int i = 0; i < hand.Count; i++) {
|
|
BaseCardT card = hand[i];
|
|
GameObject cardUI = GameObject.Instantiate(this.cardPrefab, this.uiCanvas.transform);
|
|
this.cardsInHandUIs.Add(cardUI);
|
|
cardUI.transform.position = CalculateHandCardPosition(i, hand.Count);
|
|
cardUI.GetComponent<CardUI>().Init(card);
|
|
int cardIndex = i;
|
|
cardUI.GetComponent<Button>().onClick.AddListener(delegate {OnCardClicked(card);});
|
|
}
|
|
}
|
|
|
|
private Vector3 CalculateHandCardPosition(int index, int count) {
|
|
const int SPACING = 8;
|
|
const int DELTA = BattleCardPrefabConstants.CARD_WIDTH + SPACING;
|
|
|
|
return new Vector3(Screen.width * 0.5f - DELTA / 2 * (count - 1) + DELTA * index,
|
|
SPACING + BattleCardPrefabConstants.CARD_HEIGHT / 2,
|
|
0);
|
|
}
|
|
}
|