using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; public class BattlePlayerControls : MonoBehaviour { // TODO: Draw enemy permanents 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 BattleDeck heroDeck; private BattleDeck monsterDeck; private PlayArea playArea; private InsightLevel insightLevel; private HandControls heroHandControls; private HandControls monsterHandControls; private Dictionary cardsInPlayUIs; public void Init( BattleDeck heroDeck, BattleDeck monsterDeck, PlayArea playArea, InsightLevel insightLevel, HandPlayability heroHandPlayability) { this.heroDeck = heroDeck; this.monsterDeck = monsterDeck; this.playArea = playArea; this.insightLevel = insightLevel; this.heroHandControls = new HandControls( this.uiCanvas, this.cardPrefab, HandControlsPosition.Bottom, TeamChoice.Heroes, int.MaxValue, heroHandPlayability); this.heroHandControls.OnCardClicked += OnHeroCardClicked; this.monsterHandControls = new HandControls( this.uiCanvas, this.cardPrefab, HandControlsPosition.Top, TeamChoice.Monsters, 1, null); this.monsterHandControls.OnCardClicked += OnMonsterCardClicked; this.cardsInPlayUIs = new Dictionary(); NotifyInsightLevelChanged(); } // 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 OnHeroCardClicked(HeroCard card) { if (this.singleTargeting == null) { if (this.battleController.CanUseHeroCard(card)) { OnCardUsed(card); } } } private void OnMonsterCardClicked(MonsterCard 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 NotifyHeroHandPileChanged() { this.heroHandControls.SetHand(this.heroDeck.GetHandPile().ToList()); } public void NotifyMonsterHandPileChanged() { this.monsterHandControls.SetHand(this.monsterDeck.GetHandPile().ToList()); } private void NotifyPlayAreaChanged() { DestroyCardsThatLeftPlayArea(); CreateCardsThatEnteredPlayArea(); RepositionCardsInPlayArea(); } private void DestroyCardsThatLeftPlayArea() { List cardsToRemove = new List(); foreach (KeyValuePair 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().Init(card); this.cardsInPlayUIs.Add(card, cardUI); } } } private void RepositionCardsInPlayArea() { List 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 = BattleCardPrefabConstants.CARD_HEIGHT + SPACING; const int PADDING = 8; return new Vector3(PADDING + BattleCardPrefabConstants.CARD_WIDTH / 2, Screen.height - PADDING - DELTA * index - BattleCardPrefabConstants.CARD_HEIGHT / 2, 0); } public void NotifyInsightLevelChanged() { this.monsterHandControls.InsightLevel = this.insightLevel.Level; } }