mixedcards/Assets/Battle/BattlePlayerControls.cs
2020-09-18 00:23:38 -04:00

51 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BattlePlayerControls : MonoBehaviour
{
public GameObject uiCanvas;
public GameObject cardPrefab;
public delegate void CardUsedAction(int i);
public event CardUsedAction OnCardUsed;
private List<GameObject> cardsInHand;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void SetHand(int numberOfCards) {
this.cardsInHand = new List<GameObject>();
for (int i = 0; i < numberOfCards; i++) {
GameObject newCard = Instantiate(this.cardPrefab, this.uiCanvas.transform);
this.cardsInHand.Add(newCard);
newCard.transform.position = CalculateCardPosition(i, numberOfCards);
Button button = newCard.GetComponent<Button>();
int cardIndex = i;
button.onClick.AddListener(delegate {OnCardClicked(cardIndex);});
}
}
private Vector3 CalculateCardPosition(int index, int count) {
const int CARD_SIZE = 120;
const int SPACING = 8;
const int DELTA = CARD_SIZE + SPACING;
return new Vector3(Screen.width * 0.5f - DELTA / 2 * (count - 1) + DELTA * index, 160, 0);
}
private void OnCardClicked(int index) {
OnCardUsed(index);
}
}