mixedcards/Assets/Battle/BattlePlayerControls.cs
2020-10-13 21:32:42 -04:00

159 lines
5.6 KiB
C#

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;
private BattleDeck<HeroCard> heroDeck;
private BattleDeck<MonsterCard> monsterDeck;
private PlayArea playArea;
private InsightLevel insightLevel;
private float scale;
private HandControls<HeroCard> heroHandControls;
private HandControls<MonsterCard> monsterHandControls;
private Dictionary<BaseCard, GameObject> cardsInPlayUIs;
public PlayerAction playerAction;
public void Init(
BattleDeck<HeroCard> heroDeck, BattleDeck<MonsterCard> monsterDeck, PlayArea playArea,
InsightLevel insightLevel, HandPlayability<HeroCard> heroHandPlayability,
HandPlayability<MonsterCard> monsterHandPlayability,
TurnIntentions<MonsterCard> monsterIntentions, float scale) {
this.heroDeck = heroDeck;
this.monsterDeck = monsterDeck;
this.playArea = playArea;
this.insightLevel = insightLevel;
this.scale = scale;
this.heroHandControls = new HandControls<HeroCard>(
heroDeck, this.uiCanvas, this.cardPrefab, HandControlsPosition.Bottom,
TeamChoice.Heroes, int.MaxValue, heroHandPlayability, null, this.scale);
this.heroHandControls.OnCardClicked += OnHeroCardClicked;
this.monsterHandControls = new HandControls<MonsterCard>(
monsterDeck, this.uiCanvas, this.cardPrefab, HandControlsPosition.Top,
TeamChoice.Monsters, insightLevel.Level, monsterHandPlayability, monsterIntentions,
this.scale);
this.monsterHandControls.OnCardClicked += OnMonsterCardClicked;
this.cardsInPlayUIs = new Dictionary<BaseCard, GameObject>();
insightLevel.OnChanged += this.NotifyInsightLevelChanged;
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)) {
this.playerAction = new PlayerAction(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) {
this.playerAction = new PlayerAction(null);
}
}
public bool CouldTarget(BattleParticipant participant) {
return this.singleTargeting != null && this.singleTargeting.CanTarget(participant);
}
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) {
float SPACING = 8 * this.scale;
float DELTA = (BattleCardPrefabConstants.CARD_HEIGHT + SPACING) * this.scale;
float PADDING = 8 * this.scale;
return new Vector3(PADDING + BattleCardPrefabConstants.CARD_WIDTH * this.scale / 2,
Screen.height - PADDING - DELTA * index - BattleCardPrefabConstants.CARD_HEIGHT * this.scale / 2,
0);
}
public void NotifyInsightLevelChanged() {
this.monsterHandControls.InsightLevel = this.insightLevel.Level;
}
public PlayerAction RetrievePlayerAction() {
PlayerAction result = this.playerAction;
this.playerAction = null;
return result;
}
}