Move Heroes and Monster to BattleState

This commit is contained in:
henriquenakashima 2020-10-13 22:23:56 -04:00
parent c2fc8b0904
commit 2e5a971581
2 changed files with 72 additions and 62 deletions

View file

@ -4,7 +4,7 @@ using System.Linq;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffectResolver, MonsterViewpoint public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffectResolver
{ {
private bool STABLE_HAND_STYLE = true; private bool STABLE_HAND_STYLE = true;
@ -19,10 +19,6 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
private HeroCardDatabase heroCardDatabase; private HeroCardDatabase heroCardDatabase;
private MonsterCardDatabase monsterCardDatabase; private MonsterCardDatabase monsterCardDatabase;
private BattleHero[] heroes;
private BattleMonster[] monsters;
private BattleState state; private BattleState state;
private HandPlayability<HeroCard> heroHandPlayability; private HandPlayability<HeroCard> heroHandPlayability;
private HandPlayability<MonsterCard> monsterHandPlayability; private HandPlayability<MonsterCard> monsterHandPlayability;
@ -42,41 +38,23 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
this.heroCardDatabase = new HeroCardDatabase(); this.heroCardDatabase = new HeroCardDatabase();
this.monsterCardDatabase = new MonsterCardDatabase(); this.monsterCardDatabase = new MonsterCardDatabase();
this.state = new BattleState(); this.state = new BattleState(parameters);
this.heroEnergyUI.Init(this.state.HeroEnergy); this.heroEnergyUI.Init(this.state.HeroEnergy);
this.monsterEnergyUI.Init(this.state.MonsterEnergy); this.monsterEnergyUI.Init(this.state.MonsterEnergy);
this.heroes = new BattleHero[parameters.HeroIds.Length]; this.battleSprites.Init(this.state.Heroes, this.state.Monsters, this.state);
for (int i = 0; i < this.heroes.Length; i++) {
HeroId id = parameters.HeroIds[i];
BattleHero hero = new BattleHero(id);
hero.Init();
this.heroes[i] = hero;
}
this.monsters = new BattleMonster[parameters.MonsterIds.Length];
for (int i = 0; i < this.monsters.Length; i++) {
MonsterId id = parameters.MonsterIds[i];
BattleMonster monster = MonsterDatabase.createMonsterScript(id);
monster.Init();
this.monsters[i] = monster;
}
this.battleSprites.Init(this.heroes, this.monsters, this.state);
BattleParticipant[] cardOwners = new BattleParticipant[parameters.HCardIds.Length]; BattleParticipant[] cardOwners = new BattleParticipant[parameters.HCardIds.Length];
for (int i = 0; i < parameters.HCardIds.Length; i++) { for (int i = 0; i < parameters.HCardIds.Length; i++) {
cardOwners[i] = this.heroes[parameters.HCardOwnerIndexes[i]]; cardOwners[i] = this.state.Heroes[parameters.HCardOwnerIndexes[i]];
} }
BattleDeck<HeroCard> heroDeck = new BattleDeck<HeroCard>( BattleDeck<HeroCard> heroDeck = new BattleDeck<HeroCard>(
this.heroCardDatabase, parameters.HCardIds.Cast<int>().ToArray(), cardOwners); this.heroCardDatabase, parameters.HCardIds.Cast<int>().ToArray(), cardOwners);
BattleParticipant[] mCardOwners = new BattleParticipant[parameters.MCardIds.Length]; BattleParticipant[] mCardOwners = new BattleParticipant[parameters.MCardIds.Length];
for (int i = 0; i < parameters.MCardIds.Length; i++) { for (int i = 0; i < parameters.MCardIds.Length; i++) {
mCardOwners[i] = this.monsters[parameters.MCardOwnerIndexes[i]]; mCardOwners[i] = this.state.Monsters[parameters.MCardOwnerIndexes[i]];
} }
BattleDeck<MonsterCard> monsterDeck = new BattleDeck<MonsterCard>( BattleDeck<MonsterCard> monsterDeck = new BattleDeck<MonsterCard>(
this.monsterCardDatabase, parameters.MCardIds.Cast<int>().ToArray(), mCardOwners); this.monsterCardDatabase, parameters.MCardIds.Cast<int>().ToArray(), mCardOwners);
@ -112,7 +90,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
this.state.InsightLevel.Modify(+5); this.state.InsightLevel.Modify(+5);
}; };
cheat.OnOneHPCode += delegate { cheat.OnOneHPCode += delegate {
foreach (BattleMonster m in this.monsters) { foreach (BattleMonster m in this.state.Monsters) {
if (m.IsAlive()) { if (m.IsAlive()) {
m.DealDamage(m.GetHP() - 1); m.DealDamage(m.GetHP() - 1);
} }
@ -170,7 +148,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
OnBattleStateChanged(); OnBattleStateChanged();
this.monsterIntentions.ResumeIntentions(); this.monsterIntentions.ResumeIntentions();
foreach (BattleHero hero in this.heroes) { foreach (BattleHero hero in this.state.Heroes) {
hero.ResetBlock(); hero.ResetBlock();
} }
OnBattleStateChanged(); OnBattleStateChanged();
@ -191,13 +169,13 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
} }
// End of hero turn // End of hero turn
yield return this.state.PlayArea.UseHeroDefaults(this.heroes, this.participantActivity, UseHeroDefault); yield return this.state.PlayArea.UseHeroDefaults(this.state.Heroes, this.participantActivity, UseHeroDefault);
if (STABLE_HAND_STYLE) { if (STABLE_HAND_STYLE) {
this.state.HeroDeck.DiscardInExcessOf(3); this.state.HeroDeck.DiscardInExcessOf(3);
} else { } else {
this.state.HeroDeck.DiscardHand(); this.state.HeroDeck.DiscardHand();
} }
foreach (BattleHero hero in this.heroes) { foreach (BattleHero hero in this.state.Heroes) {
hero.TickStatuses(); hero.TickStatuses();
} }
@ -213,7 +191,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
// Start of monster turn // Start of monster turn
this.state.Clock.AdvanceToStep(RoundStep.StartOfMonsterTurn); this.state.Clock.AdvanceToStep(RoundStep.StartOfMonsterTurn);
this.state.ClearEmotionPlayed(); this.state.ClearEmotionPlayed();
foreach (BattleMonster monster in this.monsters) { foreach (BattleMonster monster in this.state.Monsters) {
monster.ResetBlock(); monster.ResetBlock();
} }
OnBattleStateChanged(); OnBattleStateChanged();
@ -247,7 +225,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
} else { } else {
this.state.MonsterDeck.DiscardHand(); this.state.MonsterDeck.DiscardHand();
} }
foreach (BattleMonster monster in this.monsters) { foreach (BattleMonster monster in this.state.Monsters) {
monster.TickStatuses(); monster.TickStatuses();
} }
@ -275,7 +253,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
this.state.HeroEnergy.PayForCard(card); this.state.HeroEnergy.PayForCard(card);
this.state.HeroDeck.RemoveCardFromHand(card); this.state.HeroDeck.RemoveCardFromHand(card);
this.participantActivity.NotifyParticipantUsedCard(card.Owner, card); this.participantActivity.NotifyParticipantUsedCard(card.Owner, card);
yield return card.Use(this, this, battlePlayerControls); yield return card.Use(this, this.state, this.battlePlayerControls);
if (card.Type == CardType.Default) { if (card.Type == CardType.Default) {
// Already added to playArea by effect // Already added to playArea by effect
} else if (card.IsPermanent()) { } else if (card.IsPermanent()) {
@ -295,7 +273,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
yield break; yield break;
} }
yield return card.UseDefault(this, this, battlePlayerControls); yield return card.UseDefault(this, this.state, this.battlePlayerControls);
CheckState(); CheckState();
OnBattleStateChanged(); OnBattleStateChanged();
@ -304,7 +282,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
public IEnumerator UseMonsterCard(MonsterCard card) { public IEnumerator UseMonsterCard(MonsterCard card) {
this.state.MonsterEnergy.PayForCard(card); this.state.MonsterEnergy.PayForCard(card);
this.state.MonsterDeck.RemoveCardFromHand(card); this.state.MonsterDeck.RemoveCardFromHand(card);
yield return card.Use(this, this); yield return card.Use(this, this.state);
if (card.IsPermanent()) { if (card.IsPermanent()) {
this.state.PlayArea.AddPlayedMonsterPermanent(card); this.state.PlayArea.AddPlayedMonsterPermanent(card);
} else if (card.IsEphemeral()) { } else if (card.IsEphemeral()) {
@ -341,24 +319,24 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
} }
private void CheckState() { private void CheckState() {
foreach (BattleParticipant p in this.heroes) { foreach (BattleParticipant p in this.state.Heroes) {
p.CheckState(); p.CheckState();
} }
foreach (BattleParticipant p in this.monsters) { foreach (BattleParticipant p in this.state.Monsters) {
p.CheckState(); p.CheckState();
} }
if (System.Array.TrueForAll(this.heroes, h => !h.IsAlive())) { if (System.Array.TrueForAll(this.state.Heroes, h => !h.IsAlive())) {
BattleResult.FinishBattle(new BattleResult(false)); BattleResult.FinishBattle(new BattleResult(false));
} else if (System.Array.TrueForAll(this.monsters, m => !m.IsAlive())) { } else if (System.Array.TrueForAll(this.state.Monsters, m => !m.IsAlive())) {
BattleResult.FinishBattle(new BattleResult(true)); BattleResult.FinishBattle(new BattleResult(true));
} }
} }
private BattleParticipant[] GetTeam(TeamChoice team) { private BattleParticipant[] GetTeam(TeamChoice team) {
return (team == TeamChoice.Heroes return (team == TeamChoice.Heroes
? this.heroes.Cast<BattleParticipant>().ToArray() ? this.state.Heroes.Cast<BattleParticipant>().ToArray()
: this.monsters.Cast<BattleParticipant>().ToArray()); : this.state.Monsters.Cast<BattleParticipant>().ToArray());
} }
public BattleState State {get => this.state;} public BattleState State {get => this.state;}
@ -546,23 +524,16 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
} }
private void MoveTeamToNewPositions(TeamChoice team, int[] newPositions) { private void MoveTeamToNewPositions(TeamChoice team, int[] newPositions) {
int n = newPositions.Length; this.state.MoveTeamToNewPositions(team, newPositions);
if (team == TeamChoice.Heroes) { if (team == TeamChoice.Heroes) {
BattleHero[] newHeroes = new BattleHero[n]; for (int i = 0; i < this.state.Heroes.Length; i++) {
for (int i = 0; i < n; i++) { this.battleSprites.PositionHero(this.state.Heroes[i], i);
BattleHero hero = this.heroes[i];
newHeroes[newPositions[i]] = hero;
this.battleSprites.PositionHero(hero, newPositions[i]);
} }
this.heroes = newHeroes;
} else { } else {
BattleMonster[] newMonsters = new BattleMonster[n]; for (int i = 0; i < this.state.Monsters.Length; i++) {
for (int i = 0; i < n; i++) { this.battleSprites.PositionMonster(this.state.Monsters[i], i);
BattleMonster monster = this.monsters[i];
newMonsters[newPositions[i]] = monster;
this.battleSprites.PositionMonster(monster, newPositions[i]);
} }
this.monsters = newMonsters;
} }
} }
@ -576,8 +547,4 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
public void ArmDamageTrigger(DamageTrigger damageTrigger) { public void ArmDamageTrigger(DamageTrigger damageTrigger) {
this.damageTriggers.Add(damageTrigger); this.damageTriggers.Add(damageTrigger);
} }
// MonsterViewpoint
public BattleHero[] Heroes {get => this.heroes;}
public BattleMonster[] Monsters {get => this.monsters;}
} }

View file

@ -3,9 +3,12 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class BattleState public class BattleState: MonsterViewpoint
{ {
private bool playedEmotion;
private BattleHero[] heroes;
private BattleMonster[] monsters;
private BattleClock clock; private BattleClock clock;
private BuffManager buffManager; private BuffManager buffManager;
private EnergyPool heroEnergy; private EnergyPool heroEnergy;
@ -15,9 +18,11 @@ public class BattleState
private PlayArea playArea; private PlayArea playArea;
private InsightLevel insightLevel; private InsightLevel insightLevel;
private bool playedEmotion;
private int STARTING_INSIGHT = 3; private int STARTING_INSIGHT = 3;
public BattleState() { public BattleState(BattleParameters parameters) {
this.clock = new BattleClock(); this.clock = new BattleClock();
this.buffManager = new BuffManager(this.clock); this.buffManager = new BuffManager(this.clock);
this.heroEnergy = new EnergyPool(); this.heroEnergy = new EnergyPool();
@ -25,6 +30,23 @@ public class BattleState
this.playArea = new PlayArea(); this.playArea = new PlayArea();
this.insightLevel = new InsightLevel(STARTING_INSIGHT); this.insightLevel = new InsightLevel(STARTING_INSIGHT);
this.heroes = new BattleHero[parameters.HeroIds.Length];
for (int i = 0; i < this.heroes.Length; i++) {
HeroId id = parameters.HeroIds[i];
BattleHero hero = new BattleHero(id);
hero.Init();
this.heroes[i] = hero;
}
this.monsters = new BattleMonster[parameters.MonsterIds.Length];
for (int i = 0; i < this.monsters.Length; i++) {
MonsterId id = parameters.MonsterIds[i];
BattleMonster monster = MonsterDatabase.createMonsterScript(id);
monster.Init();
this.monsters[i] = monster;
}
} }
public void SetDecks(BattleDeck<HeroCard> heroDeck, BattleDeck<MonsterCard> monsterDeck) { public void SetDecks(BattleDeck<HeroCard> heroDeck, BattleDeck<MonsterCard> monsterDeck) {
@ -57,6 +79,27 @@ public class BattleState
this.playedEmotion = false; this.playedEmotion = false;
} }
public void MoveTeamToNewPositions(TeamChoice team, int[] newPositions) {
int n = newPositions.Length;
if (team == TeamChoice.Heroes) {
BattleHero[] newHeroes = new BattleHero[n];
for (int i = 0; i < n; i++) {
BattleHero hero = this.heroes[i];
newHeroes[newPositions[i]] = hero;
}
this.heroes = newHeroes;
} else {
BattleMonster[] newMonsters = new BattleMonster[n];
for (int i = 0; i < n; i++) {
BattleMonster monster = this.monsters[i];
newMonsters[newPositions[i]] = monster;
}
this.monsters = newMonsters;
}
}
public BattleHero[] Heroes {get => this.heroes;}
public BattleMonster[] Monsters {get => this.monsters;}
public BattleClock Clock {get => this.clock;} public BattleClock Clock {get => this.clock;}
public BuffManager BuffManager {get => this.buffManager;} public BuffManager BuffManager {get => this.buffManager;}