mixedcards/Assets/Battle/BattlePlayerControls.cs
2020-09-19 23:25:42 -04:00

159 lines
5.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class BattlePlayerControls : MonoBehaviour
{
public BattleController battleController;
public GameObject uiCanvas;
public GameObject cardPrefab;
public delegate void CardUsedAction(HeroCard card);
public event CardUsedAction OnCardUsed;
public delegate void TurnPassedAction();
public event TurnPassedAction OnTurnPassed;
private HeroDeck heroDeck;
private List<GameObject> cardsInHandUIs;
private PlayArea playArea;
private Dictionary<BaseCard, GameObject> cardsInPlayUIs;
public void Init(HeroDeck heroDeck, PlayArea playArea) {
this.heroDeck = heroDeck;
this.playArea = playArea;
this.cardsInPlayUIs = new Dictionary<BaseCard, GameObject>();
}
// Start is called before the first frame update
void Start()
{
NotifyPlayAreaChanged();
this.playArea.OnPlayAreaChanged += NotifyPlayAreaChanged;
}
// Update is called once per frame
void Update()
{
}
private void SetHand(List<HeroCard> 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++) {
HeroCard card = hand[i];
GameObject cardUI = 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 const int CARD_WIDTH = 122;
private const int CARD_HEIGHT = 155;
private Vector3 CalculateHandCardPosition(int index, int count) {
const int SPACING = 8;
const int DELTA = CARD_WIDTH + SPACING;
return new Vector3(Screen.width * 0.5f - DELTA / 2 * (count - 1) + DELTA * index,
160,
0);
}
private void OnCardClicked(HeroCard card) {
if (this.singleTargeting == null) {
if (this.battleController.CanUseHeroCard(card)) {
OnCardUsed(card);
}
}
}
private SingleTargeting singleTargeting;
public IEnumerator ChooseTarget(SingleTargeting targeting) {
this.singleTargeting = targeting;
yield return this.singleTargeting.ChooseTarget();
this.singleTargeting = null;
}
public void NotifyParticipantSpriteClicked(BattleParticipant participant) {
if (this.singleTargeting != null) {
this.singleTargeting.NotifyParticipantSpriteClicked(participant);
}
}
public void PassTurnButtonClicked() {
if (this.singleTargeting == null) {
OnTurnPassed();
}
}
public bool CouldTarget(BattleParticipant participant) {
return this.singleTargeting != null && this.singleTargeting.CanTarget(participant);
}
public void NotifyHandPileChanged() {
SetHand(this.heroDeck.GetHandPile().Cast<HeroCard>().ToList());
}
private void NotifyPlayAreaChanged() {
DestroyCardsThatLeftPlayArea();
CreateCardsThatEnteredPlayArea();
RepositionCardsInPlayArea();
}
private void DestroyCardsThatLeftPlayArea() {
List<BaseCard> cardsToRemove = new List<BaseCard>();
foreach (KeyValuePair<BaseCard, GameObject> pair in this.cardsInPlayUIs) {
if (!this.playArea.GetHeroPermanents().Contains(pair.Key)) {
Destroy(pair.Value);
cardsToRemove.Add(pair.Key);
}
}
foreach (BaseCard cardToRemove in cardsToRemove) {
this.cardsInPlayUIs.Remove(cardToRemove);
}
}
private void CreateCardsThatEnteredPlayArea() {
foreach (BaseCard card in this.playArea.GetHeroPermanents()) {
if (!this.cardsInPlayUIs.ContainsKey(card)) {
GameObject cardUI = Instantiate(this.cardPrefab, this.uiCanvas.transform);
// Leave position to be set by RepositionCardsInPlayArea();
cardUI.GetComponent<CardUI>().Init(card);
this.cardsInPlayUIs.Add(card, cardUI);
}
}
}
private void RepositionCardsInPlayArea() {
List<BaseCard> permanentList = this.playArea.GetHeroPermanents();
for (int i = 0; i < permanentList.Count; i++) {
BaseCard card = permanentList[i];
GameObject cardUI = this.cardsInPlayUIs[card];
cardUI.transform.position = CalculatePlayAreaCardPosition(i, permanentList.Count);
}
}
private Vector3 CalculatePlayAreaCardPosition(int index, int count) {
const int SPACING = 8;
const int DELTA = CARD_HEIGHT + SPACING;
const int PADDING = 8;
return new Vector3(PADDING + CARD_WIDTH / 2,
Screen.height - PADDING - DELTA * index - CARD_HEIGHT / 2,
0);
}
}