Restructure battle with Coroutines, monsters play from hand
This commit is contained in:
parent
28db788635
commit
6135ea1b24
4 changed files with 80 additions and 21 deletions
|
|
@ -30,8 +30,11 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
|
||||||
private HeroInfoUI[] monsterUIs;
|
private HeroInfoUI[] monsterUIs;
|
||||||
|
|
||||||
private bool heroTurn;
|
private bool heroTurn;
|
||||||
|
private bool heroPassedTurn;
|
||||||
private int maxEnergy;
|
private int maxEnergy;
|
||||||
private int energy;
|
private int energy;
|
||||||
|
private int maxMEnergy;
|
||||||
|
private int mEnergy;
|
||||||
|
|
||||||
void Awake() {
|
void Awake() {
|
||||||
this.heroCardDatabase = new HeroCardDatabase();
|
this.heroCardDatabase = new HeroCardDatabase();
|
||||||
|
|
@ -113,7 +116,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
|
||||||
// Start is called before the first frame update
|
// Start is called before the first frame update
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
StartHeroTurn();
|
StartCoroutine(RunBattleRounds());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update is called once per frame
|
// 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.heroTurn = true;
|
||||||
|
|
||||||
this.damageTriggers.RemoveAll(t => t.Expiration == TriggerExpiration.StartOfNextHeroTurn);
|
this.damageTriggers.RemoveAll(t => t.Expiration == TriggerExpiration.StartOfNextHeroTurn);
|
||||||
|
|
||||||
this.energy = this.maxEnergy;
|
this.energy = this.maxEnergy;
|
||||||
foreach (BattleHero hero in this.heroes) {
|
this.mEnergy = this.maxMEnergy;
|
||||||
hero.ResetBlock();
|
|
||||||
}
|
|
||||||
this.heroDeck.DrawHand();
|
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) {
|
if (this.maxEnergy < 9) {
|
||||||
this.maxEnergy++;
|
this.maxEnergy++;
|
||||||
this.energy++;
|
this.energy++;
|
||||||
}
|
}
|
||||||
|
foreach (BattleHero hero in this.heroes) {
|
||||||
|
hero.ResetBlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Heroes act until they pas
|
||||||
|
while (!this.heroPassedTurn) {
|
||||||
|
yield return null;
|
||||||
|
}
|
||||||
|
this.heroPassedTurn = false;
|
||||||
|
|
||||||
|
// End of hero turn
|
||||||
|
this.heroDeck.DiscardHand();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void StartEnemyTurn() {
|
private IEnumerator RunMonsterTurn() {
|
||||||
this.heroTurn = false;
|
// Start of monster turn
|
||||||
|
if (this.maxMEnergy < 9) {
|
||||||
this.heroDeck.DiscardHand();
|
this.maxMEnergy++;
|
||||||
|
this.mEnergy++;
|
||||||
|
}
|
||||||
foreach (BattleMonster monster in this.monsters) {
|
foreach (BattleMonster monster in this.monsters) {
|
||||||
monster.ResetBlock();
|
monster.ResetBlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
MonsterCard card = this.monsterCardDatabase.CreateCard((int) MCardId.VampireFangs, this.monsters[0]);
|
// Start of monster turn
|
||||||
StartCoroutine(UseMonsterCard(card));
|
List<MonsterCard> monsterHand = this.monsterDeck.GetHandPile();
|
||||||
|
for (int i = 0; i < monsterHand.Count; i++) {
|
||||||
StartHeroTurn();
|
MonsterCard monsterCard = monsterHand[i];
|
||||||
|
if (CanUseMonsterCard(monsterCard)) {
|
||||||
|
yield return UseMonsterCard(monsterCard);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void NotifyHeroCardUsed(HeroCard card) {
|
public void NotifyHeroCardUsed(HeroCard card) {
|
||||||
|
|
@ -163,7 +195,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
|
||||||
|
|
||||||
public IEnumerator UseHeroCard(HeroCard card) {
|
public IEnumerator UseHeroCard(HeroCard card) {
|
||||||
yield return card.Use(this, battlePlayerControls);
|
yield return card.Use(this, battlePlayerControls);
|
||||||
PayCardCost(card);
|
PayHeroCardCost(card);
|
||||||
this.heroDeck.RemoveCardFromHand(card);
|
this.heroDeck.RemoveCardFromHand(card);
|
||||||
if (card.IsPermanent()) {
|
if (card.IsPermanent()) {
|
||||||
this.playArea.AddPlayedHeroPermanent(card);
|
this.playArea.AddPlayedHeroPermanent(card);
|
||||||
|
|
@ -174,12 +206,18 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
|
||||||
|
|
||||||
public IEnumerator UseMonsterCard(MonsterCard card) {
|
public IEnumerator UseMonsterCard(MonsterCard card) {
|
||||||
yield return card.Use(this, this);
|
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() {
|
public void NotifyHeroTurnPassed() {
|
||||||
if (this.heroTurn) {
|
if (this.heroTurn) {
|
||||||
StartEnemyTurn();
|
this.heroPassedTurn = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -187,10 +225,18 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
|
||||||
return this.heroTurn && this.energy >= card.GetCost();
|
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();
|
this.energy -= card.GetCost();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void PayMonsterCardCost(MonsterCard card) {
|
||||||
|
this.mEnergy -= card.GetCost();
|
||||||
|
}
|
||||||
|
|
||||||
public int GetEnergy() {
|
public int GetEnergy() {
|
||||||
return this.energy;
|
return this.energy;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ using UnityEngine.UI;
|
||||||
|
|
||||||
public class BattlePlayerControls : MonoBehaviour
|
public class BattlePlayerControls : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
// TODO: Draw enemy permanents
|
||||||
|
|
||||||
public BattleController battleController;
|
public BattleController battleController;
|
||||||
public GameObject uiCanvas;
|
public GameObject uiCanvas;
|
||||||
public GameObject cardPrefab;
|
public GameObject cardPrefab;
|
||||||
|
|
|
||||||
|
|
@ -9,9 +9,11 @@ public class PlayArea
|
||||||
public event PlayAreaChangedAction OnPlayAreaChanged;
|
public event PlayAreaChangedAction OnPlayAreaChanged;
|
||||||
|
|
||||||
private List<BaseCard> heroPermanents;
|
private List<BaseCard> heroPermanents;
|
||||||
|
private List<BaseCard> monsterPermanents;
|
||||||
|
|
||||||
public PlayArea() {
|
public PlayArea() {
|
||||||
this.heroPermanents = new List<BaseCard>();
|
this.heroPermanents = new List<BaseCard>();
|
||||||
|
this.monsterPermanents = new List<BaseCard>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<BaseCard> GetHeroPermanents() {
|
public List<BaseCard> GetHeroPermanents() {
|
||||||
|
|
@ -28,6 +30,16 @@ public class PlayArea
|
||||||
TriggerPlayAreaChanged();
|
TriggerPlayAreaChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void AddPlayedMonsterPermanent(BaseCard permanent) {
|
||||||
|
this.monsterPermanents.Add(permanent);
|
||||||
|
TriggerPlayAreaChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveMonsterPermanent(BaseCard permanent) {
|
||||||
|
this.monsterPermanents.Remove(permanent);
|
||||||
|
TriggerPlayAreaChanged();
|
||||||
|
}
|
||||||
|
|
||||||
private void TriggerPlayAreaChanged() {
|
private void TriggerPlayAreaChanged() {
|
||||||
if (OnPlayAreaChanged != null) OnPlayAreaChanged();
|
if (OnPlayAreaChanged != null) OnPlayAreaChanged();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
1
TODO.txt
1
TODO.txt
|
|
@ -1,3 +1,2 @@
|
||||||
- Control flow of battle with state machine or coroutines
|
|
||||||
- Make battle end
|
- Make battle end
|
||||||
- Do not allow dead heroes to act
|
- Do not allow dead heroes to act
|
||||||
Loading…
Add table
Add a link
Reference in a new issue