Restructure battle with Coroutines, monsters play from hand

This commit is contained in:
henriquenakashima 2020-09-20 13:54:14 -04:00
parent 28db788635
commit 6135ea1b24
4 changed files with 80 additions and 21 deletions

View file

@ -30,8 +30,11 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
private HeroInfoUI[] monsterUIs;
private bool heroTurn;
private bool heroPassedTurn;
private int maxEnergy;
private int energy;
private int maxMEnergy;
private int mEnergy;
void Awake() {
this.heroCardDatabase = new HeroCardDatabase();
@ -113,7 +116,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
// Start is called before the first frame update
void Start()
{
StartHeroTurn();
StartCoroutine(RunBattleRounds());
}
// Update is called once per frame
@ -121,37 +124,66 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
{
}
private void StartHeroTurn() {
private IEnumerator RunBattleRounds() {
while (true) {
yield return RunBattleRound();
}
}
private IEnumerator RunBattleRound() {
this.heroTurn = true;
this.damageTriggers.RemoveAll(t => t.Expiration == TriggerExpiration.StartOfNextHeroTurn);
this.energy = this.maxEnergy;
foreach (BattleHero hero in this.heroes) {
hero.ResetBlock();
}
this.mEnergy = this.maxMEnergy;
this.heroDeck.DrawHand();
this.battlePlayerControls.NotifyHandPileChanged();
this.monsterDeck.DrawHand();
yield return RunHeroTurn();
this.heroTurn = false;
yield return RunMonsterTurn();
}
private IEnumerator RunHeroTurn() {
// Start of hero turn
if (this.maxEnergy < 9) {
this.maxEnergy++;
this.energy++;
}
foreach (BattleHero hero in this.heroes) {
hero.ResetBlock();
}
private void StartEnemyTurn() {
this.heroTurn = false;
// Heroes act until they pas
while (!this.heroPassedTurn) {
yield return null;
}
this.heroPassedTurn = false;
// End of hero turn
this.heroDeck.DiscardHand();
}
private IEnumerator RunMonsterTurn() {
// Start of monster turn
if (this.maxMEnergy < 9) {
this.maxMEnergy++;
this.mEnergy++;
}
foreach (BattleMonster monster in this.monsters) {
monster.ResetBlock();
}
MonsterCard card = this.monsterCardDatabase.CreateCard((int) MCardId.VampireFangs, this.monsters[0]);
StartCoroutine(UseMonsterCard(card));
StartHeroTurn();
// Start of monster turn
List<MonsterCard> monsterHand = this.monsterDeck.GetHandPile();
for (int i = 0; i < monsterHand.Count; i++) {
MonsterCard monsterCard = monsterHand[i];
if (CanUseMonsterCard(monsterCard)) {
yield return UseMonsterCard(monsterCard);
}
}
}
public void NotifyHeroCardUsed(HeroCard card) {
@ -163,7 +195,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
public IEnumerator UseHeroCard(HeroCard card) {
yield return card.Use(this, battlePlayerControls);
PayCardCost(card);
PayHeroCardCost(card);
this.heroDeck.RemoveCardFromHand(card);
if (card.IsPermanent()) {
this.playArea.AddPlayedHeroPermanent(card);
@ -174,12 +206,18 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
public IEnumerator UseMonsterCard(MonsterCard card) {
yield return card.Use(this, this);
// TODO: Manage monster deck.
PayMonsterCardCost(card);
this.monsterDeck.RemoveCardFromHand(card);
if (card.IsPermanent()) {
this.playArea.AddPlayedMonsterPermanent(card);
} else {
this.monsterDeck.MoveCardToDiscard(card);
}
}
public void NotifyHeroTurnPassed() {
if (this.heroTurn) {
StartEnemyTurn();
this.heroPassedTurn = true;
}
}
@ -187,10 +225,18 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
return this.heroTurn && this.energy >= card.GetCost();
}
private void PayCardCost(HeroCard card) {
public bool CanUseMonsterCard(MonsterCard card) {
return !this.heroTurn && this.mEnergy >= card.GetCost();
}
private void PayHeroCardCost(HeroCard card) {
this.energy -= card.GetCost();
}
private void PayMonsterCardCost(MonsterCard card) {
this.mEnergy -= card.GetCost();
}
public int GetEnergy() {
return this.energy;
}

View file

@ -6,6 +6,8 @@ using UnityEngine.UI;
public class BattlePlayerControls : MonoBehaviour
{
// TODO: Draw enemy permanents
public BattleController battleController;
public GameObject uiCanvas;
public GameObject cardPrefab;

View file

@ -9,9 +9,11 @@ public class PlayArea
public event PlayAreaChangedAction OnPlayAreaChanged;
private List<BaseCard> heroPermanents;
private List<BaseCard> monsterPermanents;
public PlayArea() {
this.heroPermanents = new List<BaseCard>();
this.monsterPermanents = new List<BaseCard>();
}
public List<BaseCard> GetHeroPermanents() {
@ -28,6 +30,16 @@ public class PlayArea
TriggerPlayAreaChanged();
}
public void AddPlayedMonsterPermanent(BaseCard permanent) {
this.monsterPermanents.Add(permanent);
TriggerPlayAreaChanged();
}
public void RemoveMonsterPermanent(BaseCard permanent) {
this.monsterPermanents.Remove(permanent);
TriggerPlayAreaChanged();
}
private void TriggerPlayAreaChanged() {
if (OnPlayAreaChanged != null) OnPlayAreaChanged();
}

View file

@ -1,3 +1,2 @@
- Control flow of battle with state machine or coroutines
- Make battle end
- Do not allow dead heroes to act