using System.Collections; using System.Collections.Generic; using UnityEngine; public class BattleController : MonoBehaviour { public HeroId[] heroIds; public MonsterId[] monsterIds; public int maxEnergy; public CardId[] cardIds; public HeroInfoUI heroUIPrefab; public GameObject uiCanvas; public BattlePlayerControls battlePlayerControls; private HeroDeck heroDeck; public BattleHero[] heroes; private GameObject[] heroSprites; private HeroInfoUI[] heroUIs; public BattleMonster[] monsters; private GameObject[] monsterSprites; private HeroInfoUI[] monsterUIs; private bool heroTurn; private int energy; void Awake() { } // Start is called before the first frame update void Start() { this.heroDeck = new HeroDeck(cardIds); heroes = new BattleHero[3]; heroSprites = new GameObject[3]; heroUIs = new HeroInfoUI[3]; for (int i = 0; i < 3; i++) { HeroId id = heroIds[i]; heroes[i] = new BattleHero(id); heroes[i].Init(); heroSprites[i] = HeroDatabase.createHeroSprite(id); heroSprites[i].name = "Hero" + i + "_Sprite"; heroSprites[i].transform.position = new Vector3(-6 + 6 * i, 0, 0); BattleParticipantSprite bps = heroSprites[i].AddComponent(); bps.Init(battlePlayerControls, heroes[i]); bps.OnSpriteClicked += battlePlayerControls.OnParticipantSpriteClicked; heroUIs[i] = (HeroInfoUI) Instantiate(heroUIPrefab.gameObject, uiCanvas.transform).GetComponent(typeof(HeroInfoUI)); heroUIs[i].Init(heroSprites[i], heroes[i]); } monsters = new BattleMonster[3]; monsterSprites = new GameObject[3]; monsterUIs = new HeroInfoUI[3]; for (int i = 0; i < 3; i++) { MonsterId id = monsterIds[i]; monsters[i] = MonsterDatabase.createMonsterScript(id); monsters[i].Init(); monsterSprites[i] = MonsterDatabase.createMonsterSprite(id); monsterSprites[i].name = "Monster" + i + "_Sprite"; monsterSprites[i].transform.position = new Vector3(-6 + 6 * i, 3, 0); BattleParticipantSprite bps = monsterSprites[i].AddComponent(); bps.Init(battlePlayerControls, monsters[i]); bps.OnSpriteClicked += battlePlayerControls.OnParticipantSpriteClicked; monsterUIs[i] = (HeroInfoUI) Instantiate(heroUIPrefab.gameObject, uiCanvas.transform).GetComponent(typeof(HeroInfoUI)); monsterUIs[i].Init(monsterSprites[i], monsters[i]); } this.battlePlayerControls.Init(this.heroDeck); this.battlePlayerControls.OnCardUsed += this.NotifyCardUsed; this.battlePlayerControls.OnTurnPassed += this.NotifyTurnPassed; this.heroDeck.OnHandPileChanged += this.battlePlayerControls.NotifyHandPileChanged; DecideEnemyTarget(); StartHeroTurn(); } // Update is called once per frame void Update() { } private void StartHeroTurn() { this.heroTurn = true; this.energy = this.maxEnergy; foreach (BattleHero hero in this.heroes) { hero.ResetBlock(); } this.heroDeck.DrawHand(); this.battlePlayerControls.NotifyHandPileChanged(); } private void StartEnemyTurn() { this.heroTurn = false; this.heroDeck.DiscardHand(); foreach (BattleMonster monster in this.monsters) { monster.ResetBlock(); } heroes[this.enemyTarget].DealDamage(2); DecideEnemyTarget(); StartHeroTurn(); } private int enemyTarget; private void DecideEnemyTarget() { this.enemyTarget = Random.Range(0, 3); Debug.Log("Will attack " + this.heroes[this.enemyTarget].GetDisplayName()); } public void NotifyCardUsed(BaseCard card) { Debug.Log("NotifyCardUsed " + card); if (!CanUseCard(card)) { throw new System.InvalidOperationException("Cannot use card " + card.GetDisplayName()); } StartCoroutine(UseCard(card)); } public IEnumerator UseCard(BaseCard card) { yield return card.Use(battlePlayerControls); PayCardCost(card); this.heroDeck.DiscardPlayedCard(card); } public void NotifyTurnPassed() { if (this.heroTurn) { Debug.Log("NotifyTurnPassed"); StartEnemyTurn(); } } public bool CanUseCard(BaseCard card) { return this.heroTurn && this.energy >= card.GetCost(); } private void PayCardCost(BaseCard card) { this.energy -= card.GetCost(); } public int GetEnergy() { return this.energy; } public int GetMaxEnergy() { return this.maxEnergy; } }