156 lines
4.7 KiB
C#
156 lines
4.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class BattleController : MonoBehaviour
|
|
{
|
|
public HeroId[] heroIds;
|
|
public MonsterId[] monsterIds;
|
|
public int maxEnergy = 6;
|
|
public CardId[] cardIds;
|
|
|
|
public HeroInfoUI heroUIPrefab;
|
|
public GameObject uiCanvas;
|
|
public BattlePlayerControls battlePlayerControls;
|
|
|
|
private List<BaseCard> hand;
|
|
|
|
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.hand = new List<BaseCard>();
|
|
for (int i = 0; i < cardIds.Length; i++) {
|
|
BaseCard newCard = CardDatabase.CreateCard(cardIds[i]);
|
|
this.hand.Add(newCard);
|
|
}
|
|
|
|
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<BattleParticipantSprite>();
|
|
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<BattleParticipantSprite>();
|
|
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.OnCardUsed += this.OnCardUsed;
|
|
this.battlePlayerControls.OnTurnPassed += this.OnTurnPassed;
|
|
this.battlePlayerControls.SetHand(this.hand);
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
private void StartEnemyTurn() {
|
|
this.heroTurn = false;
|
|
|
|
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 OnCardUsed(BaseCard card) {
|
|
Debug.Log("OnCardUsed " + 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);
|
|
}
|
|
|
|
public void OnTurnPassed() {
|
|
if (this.heroTurn) {
|
|
Debug.Log("OnTurnPassed");
|
|
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;
|
|
}
|
|
}
|