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;
// Inspector public
public HeroInfoUI heroUIPrefab;
public GameObject uiCanvas;
public BattlePlayerControls battlePlayerControls;
public EnergyUI heroEnergyUI;
public EnergyUI monsterEnergyUI;
public Material hitEffectMaterial;
public RuntimeAnimatorController runtimeAnimatorController;
public BattleSprites battleSprites;
// Private
private HeroCardDatabase heroCardDatabase;
@ -26,12 +24,8 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
private PlayArea playArea;
private BattleHero[] heroes;
private Dictionary<BattleHero, GameObject> heroSprites;
private Dictionary<BattleHero, HeroInfoUI> heroUIs;
private BattleMonster[] monsters;
private Dictionary<BattleMonster, GameObject> monsterSprites;
private Dictionary<BattleMonster, HeroInfoUI> monsterUIs;
private BattleState state;
private EnergyPool heroEnergy;
@ -66,55 +60,25 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
this.insightLevel = new InsightLevel(STARTING_INSIGHT);
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++) {
HeroId id = parameters.HeroIds[i];
BattleHero hero = new BattleHero(id);
hero.Init();
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.monsterSprites = new Dictionary<BattleMonster, GameObject>();
this.monsterUIs = new Dictionary<BattleMonster, HeroInfoUI>();
for (int i = 0; i < 3; i++) {
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;
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];
for (int i = 0; i < parameters.HCardIds.Length; i++) {
cardOwners[i] = this.heroes[parameters.HCardOwnerIndexes[i]];
@ -148,16 +112,6 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
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
void Start()
{
@ -586,7 +540,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
for (int i = 0; i < n; i++) {
BattleHero hero = this.heroes[i];
newHeroes[newPositions[i]] = hero;
PositionSprite(team, heroSprites[hero], newPositions[i]);
this.battleSprites.PositionHero(hero, newPositions[i]);
}
this.heroes = newHeroes;
} else {
@ -594,7 +548,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
for (int i = 0; i < n; i++) {
BattleMonster monster = this.monsters[i];
newMonsters[newPositions[i]] = monster;
PositionSprite(team, monsterSprites[monster], newPositions[i]);
this.battleSprites.PositionMonster(monster, newPositions[i]);
}
this.monsters = newMonsters;
}

View file

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

View file

@ -4,6 +4,11 @@ using UnityEngine;
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 BatMonster() {
public BatMonster() : base(MonsterId.Bat) {
this.displayName = "Bat";
this.maxHP = 10;
}

View file

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