Refactor BattleSprites out of BattleController

This commit is contained in:
henriquenakashima 2020-10-01 23:30:19 -04:00
parent 737a851283
commit eb51ebace0
8 changed files with 110 additions and 61 deletions

View file

@ -10,13 +10,11 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
private int STARTING_INSIGHT = 3; private int STARTING_INSIGHT = 3;
// Inspector public // Inspector public
public HeroInfoUI heroUIPrefab;
public GameObject uiCanvas; public GameObject uiCanvas;
public BattlePlayerControls battlePlayerControls; public BattlePlayerControls battlePlayerControls;
public EnergyUI heroEnergyUI; public EnergyUI heroEnergyUI;
public EnergyUI monsterEnergyUI; public EnergyUI monsterEnergyUI;
public Material hitEffectMaterial; public BattleSprites battleSprites;
public RuntimeAnimatorController runtimeAnimatorController;
// Private // Private
private HeroCardDatabase heroCardDatabase; private HeroCardDatabase heroCardDatabase;
@ -26,12 +24,8 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
private PlayArea playArea; private PlayArea playArea;
private BattleHero[] heroes; private BattleHero[] heroes;
private Dictionary<BattleHero, GameObject> heroSprites;
private Dictionary<BattleHero, HeroInfoUI> heroUIs;
private BattleMonster[] monsters; private BattleMonster[] monsters;
private Dictionary<BattleMonster, GameObject> monsterSprites;
private Dictionary<BattleMonster, HeroInfoUI> monsterUIs;
private BattleState state; private BattleState state;
private EnergyPool heroEnergy; private EnergyPool heroEnergy;
@ -66,55 +60,25 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
this.insightLevel = new InsightLevel(STARTING_INSIGHT); this.insightLevel = new InsightLevel(STARTING_INSIGHT);
this.heroes = new BattleHero[parameters.HeroIds.Length]; this.heroes = new BattleHero[parameters.HeroIds.Length];
this.heroSprites = new Dictionary<BattleHero, GameObject>();
this.heroUIs = new Dictionary<BattleHero, HeroInfoUI>();
for (int i = 0; i < this.heroes.Length; i++) { for (int i = 0; i < this.heroes.Length; i++) {
HeroId id = parameters.HeroIds[i]; HeroId id = parameters.HeroIds[i];
BattleHero hero = new BattleHero(id); BattleHero hero = new BattleHero(id);
hero.Init(); hero.Init();
this.heroes[i] = hero; this.heroes[i] = hero;
GameObject sprite = HeroDatabase.createHeroSprite(id);
this.heroSprites[hero] = sprite;
sprite.name = "Hero" + i + "_Sprite";
PositionSprite(TeamChoice.Heroes, sprite, i);
BattleParticipantSprite bps = sprite.AddComponent<BattleParticipantSprite>();
bps.Init(battlePlayerControls, hero, this.hitEffectMaterial, this.runtimeAnimatorController);
bps.OnSpriteClicked += battlePlayerControls.NotifyParticipantSpriteClicked;
hero.OnHit += bps.NotifyHit;
hero.OnDeath += bps.NotifyDeath;
HeroInfoUI heroUI = (HeroInfoUI) Instantiate(this.heroUIPrefab.gameObject, this.uiCanvas.transform).GetComponent(typeof(HeroInfoUI));
heroUI.Init(sprite, hero);
this.heroUIs[hero] = heroUI;
} }
this.monsters = new BattleMonster[parameters.MonsterIds.Length]; this.monsters = new BattleMonster[parameters.MonsterIds.Length];
this.monsterSprites = new Dictionary<BattleMonster, GameObject>(); for (int i = 0; i < this.monsters.Length; i++) {
this.monsterUIs = new Dictionary<BattleMonster, HeroInfoUI>();
for (int i = 0; i < 3; i++) {
MonsterId id = parameters.MonsterIds[i]; MonsterId id = parameters.MonsterIds[i];
BattleMonster monster = MonsterDatabase.createMonsterScript(id); BattleMonster monster = MonsterDatabase.createMonsterScript(id);
monster.Init(); monster.Init();
this.monsters[i] = monster; this.monsters[i] = monster;
GameObject sprite = MonsterDatabase.createMonsterSprite(id);
this.monsterSprites[monster] = sprite;
sprite.name = "Monster" + i + "_Sprite";
PositionSprite(TeamChoice.Monsters, sprite, i);
BattleParticipantSprite bps = sprite.AddComponent<BattleParticipantSprite>();
bps.Init(battlePlayerControls, monster, this.hitEffectMaterial, this.runtimeAnimatorController);
bps.OnSpriteClicked += battlePlayerControls.NotifyParticipantSpriteClicked;
monster.OnHit += bps.NotifyHit;
monster.OnDeath += bps.NotifyDeath;
HeroInfoUI monsterUI = (HeroInfoUI) Instantiate(this.heroUIPrefab.gameObject, this.uiCanvas.transform).GetComponent(typeof(HeroInfoUI));
monsterUI.Init(sprite, monster);
this.monsterUIs[monster] = monsterUI;
} }
this.battleSprites.Init(this.heroes, this.monsters);
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.heroes[parameters.HCardOwnerIndexes[i]];
@ -148,16 +112,6 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
this.insightLevel.OnChanged += this.battlePlayerControls.NotifyInsightLevelChanged; this.insightLevel.OnChanged += this.battlePlayerControls.NotifyInsightLevelChanged;
} }
private void PositionSprite(TeamChoice team, GameObject sprite, int i) {
Vector3 pos;
if (team == TeamChoice.Heroes) {
pos = new Vector3(-0.8f + -3 + 3 * i, -1.8f, 0);
} else {
pos = new Vector3(+0.8f + -3 + 3 * i, 1.2f, 0);
}
sprite.transform.position = pos;
}
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
@ -586,7 +540,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
BattleHero hero = this.heroes[i]; BattleHero hero = this.heroes[i];
newHeroes[newPositions[i]] = hero; newHeroes[newPositions[i]] = hero;
PositionSprite(team, heroSprites[hero], newPositions[i]); this.battleSprites.PositionHero(hero, newPositions[i]);
} }
this.heroes = newHeroes; this.heroes = newHeroes;
} else { } else {
@ -594,7 +548,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
BattleMonster monster = this.monsters[i]; BattleMonster monster = this.monsters[i];
newMonsters[newPositions[i]] = monster; newMonsters[newPositions[i]] = monster;
PositionSprite(team, monsterSprites[monster], newPositions[i]); this.battleSprites.PositionMonster(monster, newPositions[i]);
} }
this.monsters = newMonsters; this.monsters = newMonsters;
} }

View file

@ -4,10 +4,10 @@ using UnityEngine;
public class BattleHero: BattleParticipant public class BattleHero: BattleParticipant
{ {
HeroId id; protected HeroId mId;
public BattleHero(HeroId id) : base(TeamChoice.Heroes) { public BattleHero(HeroId id) : base(TeamChoice.Heroes) {
this.id = id; mId = id;
switch (id) switch (id)
{ {
case HeroId.Alchemist: case HeroId.Alchemist:
@ -31,7 +31,5 @@ public class BattleHero: BattleParticipant
} }
} }
public HeroId GetId() { public HeroId Id {get => mId;}
return this.id;
}
} }

View file

@ -4,6 +4,11 @@ using UnityEngine;
public class BattleMonster: BattleParticipant public class BattleMonster: BattleParticipant
{ {
public BattleMonster() : base(TeamChoice.Monsters) { protected MonsterId mId;
public BattleMonster(MonsterId id) : base(TeamChoice.Monsters) {
mId = id;
} }
public MonsterId Id {get => mId;}
} }

View file

@ -0,0 +1,78 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class BattleSprites: MonoBehaviour
{
public HeroInfoUI mHeroUIPrefab;
public GameObject mUiCanvas;
public Material mHitEffectMaterial;
public RuntimeAnimatorController mRuntimeAnimatorController;
public BattlePlayerControls mBattlePlayerControls;
private Dictionary<BattleHero, GameObject> mHeroSprites;
private Dictionary<BattleHero, HeroInfoUI> mHeroUIs;
private Dictionary<BattleMonster, GameObject> mMonsterSprites;
private Dictionary<BattleMonster, HeroInfoUI> mMonsterUIs;
public void Init(BattleHero[] heroes, BattleMonster[] monsters) {
mHeroSprites = new Dictionary<BattleHero, GameObject>();
mHeroUIs = new Dictionary<BattleHero, HeroInfoUI>();
mMonsterSprites = new Dictionary<BattleMonster, GameObject>();
mMonsterUIs = new Dictionary<BattleMonster, HeroInfoUI>();
for (int i = 0; i < heroes.Length; i++) {
BattleHero hero = heroes[i];
GameObject sprite = HeroDatabase.createHeroSprite(hero.Id);
mHeroSprites[hero] = sprite;
sprite.name = "Hero" + i + "_Sprite";
PositionSprite(TeamChoice.Heroes, sprite, i);
BattleParticipantSprite bps = sprite.AddComponent<BattleParticipantSprite>();
bps.Init(mBattlePlayerControls, hero, mHitEffectMaterial, mRuntimeAnimatorController);
bps.OnSpriteClicked += mBattlePlayerControls.NotifyParticipantSpriteClicked;
hero.OnHit += bps.NotifyHit;
hero.OnDeath += bps.NotifyDeath;
HeroInfoUI heroUI = (HeroInfoUI) Instantiate(mHeroUIPrefab.gameObject, mUiCanvas.transform).GetComponent(typeof(HeroInfoUI));
heroUI.Init(sprite, hero);
mHeroUIs[hero] = heroUI;
}
for (int i = 0; i < monsters.Length; i++) {
BattleMonster monster = monsters[i];
GameObject sprite = MonsterDatabase.createMonsterSprite(monster.Id);
mMonsterSprites[monster] = sprite;
sprite.name = "Monster" + i + "_Sprite";
PositionSprite(TeamChoice.Monsters, sprite, i);
BattleParticipantSprite bps = sprite.AddComponent<BattleParticipantSprite>();
bps.Init(mBattlePlayerControls, monster, mHitEffectMaterial, mRuntimeAnimatorController);
bps.OnSpriteClicked += mBattlePlayerControls.NotifyParticipantSpriteClicked;
monster.OnHit += bps.NotifyHit;
monster.OnDeath += bps.NotifyDeath;
HeroInfoUI monsterUI = (HeroInfoUI) Instantiate(mHeroUIPrefab.gameObject, mUiCanvas.transform).GetComponent(typeof(HeroInfoUI));
monsterUI.Init(sprite, monster);
mMonsterUIs[monster] = monsterUI;
}
}
private void PositionSprite(TeamChoice team, GameObject sprite, int i) {
Vector3 pos;
if (team == TeamChoice.Heroes) {
pos = new Vector3(-0.8f + -3 + 3 * i, -1.8f, 0);
} else {
pos = new Vector3(+0.8f + -3 + 3 * i, 1.2f, 0);
}
sprite.transform.position = pos;
}
public void PositionHero(BattleHero hero, int i) {
PositionSprite(TeamChoice.Heroes, mHeroSprites[hero], i);
}
public void PositionMonster(BattleMonster monster, int i) {
PositionSprite(TeamChoice.Monsters, mMonsterSprites[monster], i);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e14a27496b13a884ba7353e3dbba6ca4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -4,7 +4,7 @@ using UnityEngine;
public class BatMonster : BattleMonster public class BatMonster : BattleMonster
{ {
public BatMonster() { public BatMonster() : base(MonsterId.Bat) {
this.displayName = "Bat"; this.displayName = "Bat";
this.maxHP = 10; this.maxHP = 10;
} }

View file

@ -4,7 +4,7 @@ using UnityEngine;
public class GolemMonster : BattleMonster public class GolemMonster : BattleMonster
{ {
public GolemMonster() { public GolemMonster() : base(MonsterId.Golem) {
this.displayName = "Golem"; this.displayName = "Golem";
this.maxHP = 16; this.maxHP = 16;
} }

View file

@ -4,6 +4,9 @@ Battle
- Show on who enemies will use the cards - Show on who enemies will use the cards
- Highlight which card is targeting - Highlight which card is targeting
- Allow cancelling targeting - Allow cancelling targeting
- Extend BattleState object to contain everything for effect resolver to work
- Implement a stack/queue to control effects
- Make battle work with numbers different than 3 vs 3
Story Story
- Make dialogue show letter by letter. - Make dialogue show letter by letter.