92 lines
2.7 KiB
C#
92 lines
2.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class BattlePlayerControls : MonoBehaviour
|
|
{
|
|
public BattleController battleController;
|
|
public GameObject uiCanvas;
|
|
public GameObject cardPrefab;
|
|
|
|
public delegate void CardUsedAction(BaseCard card);
|
|
public event CardUsedAction OnCardUsed;
|
|
public delegate void TurnPassedAction();
|
|
public event TurnPassedAction OnTurnPassed;
|
|
|
|
private List<BaseCard> cardsInHand;
|
|
private List<GameObject> cardsInHandUIs;
|
|
|
|
// 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<BaseCard>();
|
|
this.cardsInHandUIs = new List<GameObject>();
|
|
for (int i = 0; i < numberOfCards; i++) {
|
|
// TODO
|
|
BaseCard newCard = new CardCrashingPunch();
|
|
this.cardsInHand.Add(newCard);
|
|
|
|
GameObject newCardUI = Instantiate(this.cardPrefab, this.uiCanvas.transform);
|
|
this.cardsInHandUIs.Add(newCardUI);
|
|
newCardUI.transform.position = CalculateCardPosition(i, numberOfCards);
|
|
newCardUI.GetComponent<CardUI>().Init(newCard);
|
|
int cardIndex = i;
|
|
newCardUI.GetComponent<Button>().onClick.AddListener(delegate {OnCardClicked(newCard);});
|
|
}
|
|
}
|
|
|
|
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(BaseCard card) {
|
|
if (!this.waitingForTarget) {
|
|
OnCardUsed(card);
|
|
}
|
|
}
|
|
|
|
private bool waitingForTarget = false;
|
|
private BattleParticipant participantClicked = null;
|
|
|
|
public IEnumerator ChooseTarget(System.Action<BattleParticipant> targetCallback) {
|
|
this.waitingForTarget = true;
|
|
while (this.participantClicked == null) {
|
|
yield return null;
|
|
}
|
|
this.waitingForTarget = false;
|
|
targetCallback(participantClicked);
|
|
this.participantClicked = null;
|
|
yield break;
|
|
}
|
|
|
|
public void OnParticipantSpriteClicked(BattleParticipant paticipant) {
|
|
if (this.waitingForTarget) {
|
|
this.participantClicked = paticipant;
|
|
}
|
|
}
|
|
|
|
public void PassTurnButtonClicked() {
|
|
if (!this.waitingForTarget) {
|
|
OnTurnPassed();
|
|
}
|
|
}
|
|
|
|
public bool IsTargeting() {
|
|
return this.waitingForTarget;
|
|
}
|
|
}
|