Create cards and make them buttons

This commit is contained in:
henriquenakashima 2020-09-18 00:23:38 -04:00
parent 581d9d7811
commit 6b0200f518
7 changed files with 643 additions and 0 deletions

View file

@ -0,0 +1,51 @@
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);
}
}