104 lines
2.9 KiB
C#
104 lines
2.9 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 HeroDeck heroDeck;
|
|
private List<GameObject> cardsInHandUIs;
|
|
|
|
public void Init(HeroDeck heroDeck) {
|
|
this.heroDeck = heroDeck;
|
|
}
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
private void SetHand(List<BaseCard> hand) {
|
|
if (this.cardsInHandUIs != null) {
|
|
foreach (GameObject cardInHandUI in this.cardsInHandUIs) {
|
|
Destroy(cardInHandUI);
|
|
}
|
|
}
|
|
|
|
this.cardsInHandUIs = new List<GameObject>();
|
|
for (int i = 0; i < hand.Count; i++) {
|
|
BaseCard card = hand[i];
|
|
GameObject cardUI = Instantiate(this.cardPrefab, this.uiCanvas.transform);
|
|
this.cardsInHandUIs.Add(cardUI);
|
|
cardUI.transform.position = CalculateCardPosition(i, hand.Count);
|
|
cardUI.GetComponent<CardUI>().Init(card);
|
|
int cardIndex = i;
|
|
cardUI.GetComponent<Button>().onClick.AddListener(delegate {OnCardClicked(card);});
|
|
}
|
|
}
|
|
|
|
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) {
|
|
if (this.battleController.CanUseCard(card)) {
|
|
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;
|
|
}
|
|
|
|
public void NotifyHandPileChanged() {
|
|
SetHand(this.heroDeck.GetHandPile());
|
|
}
|
|
}
|