From eb51ebace0892af3935d5f418b63aaf3030b791a Mon Sep 17 00:00:00 2001 From: henriquenakashima Date: Thu, 1 Oct 2020 23:30:19 -0400 Subject: [PATCH] Refactor BattleSprites out of BattleController --- Assets/Battle/BattleController.cs | 60 +++----------------- Assets/Battle/BattleHero.cs | 8 +-- Assets/Battle/BattleMonster.cs | 7 ++- Assets/Battle/BattleSprites.cs | 78 ++++++++++++++++++++++++++ Assets/Battle/BattleSprites.cs.meta | 11 ++++ Assets/Battle/Monsters/BatMonster.cs | 2 +- Assets/Battle/Monsters/GolemMonster.cs | 2 +- TODO.txt | 3 + 8 files changed, 110 insertions(+), 61 deletions(-) create mode 100644 Assets/Battle/BattleSprites.cs create mode 100644 Assets/Battle/BattleSprites.cs.meta diff --git a/Assets/Battle/BattleController.cs b/Assets/Battle/BattleController.cs index 49e6d93..ac2562a 100644 --- a/Assets/Battle/BattleController.cs +++ b/Assets/Battle/BattleController.cs @@ -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 heroSprites; - private Dictionary heroUIs; private BattleMonster[] monsters; - private Dictionary monsterSprites; - private Dictionary 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(); - this.heroUIs = new Dictionary(); 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(); - 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(); - this.monsterUIs = new Dictionary(); - 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(); - 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; } diff --git a/Assets/Battle/BattleHero.cs b/Assets/Battle/BattleHero.cs index d8dc860..62b163c 100644 --- a/Assets/Battle/BattleHero.cs +++ b/Assets/Battle/BattleHero.cs @@ -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;} } diff --git a/Assets/Battle/BattleMonster.cs b/Assets/Battle/BattleMonster.cs index 8de83a5..9157b8a 100644 --- a/Assets/Battle/BattleMonster.cs +++ b/Assets/Battle/BattleMonster.cs @@ -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;} } diff --git a/Assets/Battle/BattleSprites.cs b/Assets/Battle/BattleSprites.cs new file mode 100644 index 0000000..1d7acfe --- /dev/null +++ b/Assets/Battle/BattleSprites.cs @@ -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 mHeroSprites; + private Dictionary mHeroUIs; + private Dictionary mMonsterSprites; + private Dictionary mMonsterUIs; + + public void Init(BattleHero[] heroes, BattleMonster[] monsters) { + mHeroSprites = new Dictionary(); + mHeroUIs = new Dictionary(); + mMonsterSprites = new Dictionary(); + mMonsterUIs = new Dictionary(); + + 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(); + 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(); + 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); + } +} diff --git a/Assets/Battle/BattleSprites.cs.meta b/Assets/Battle/BattleSprites.cs.meta new file mode 100644 index 0000000..b3a87bc --- /dev/null +++ b/Assets/Battle/BattleSprites.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e14a27496b13a884ba7353e3dbba6ca4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Battle/Monsters/BatMonster.cs b/Assets/Battle/Monsters/BatMonster.cs index 6812ba6..60a5659 100644 --- a/Assets/Battle/Monsters/BatMonster.cs +++ b/Assets/Battle/Monsters/BatMonster.cs @@ -4,7 +4,7 @@ using UnityEngine; public class BatMonster : BattleMonster { - public BatMonster() { + public BatMonster() : base(MonsterId.Bat) { this.displayName = "Bat"; this.maxHP = 10; } diff --git a/Assets/Battle/Monsters/GolemMonster.cs b/Assets/Battle/Monsters/GolemMonster.cs index 3b49894..6650fba 100644 --- a/Assets/Battle/Monsters/GolemMonster.cs +++ b/Assets/Battle/Monsters/GolemMonster.cs @@ -4,7 +4,7 @@ using UnityEngine; public class GolemMonster : BattleMonster { - public GolemMonster() { + public GolemMonster() : base(MonsterId.Golem) { this.displayName = "Golem"; this.maxHP = 16; } diff --git a/TODO.txt b/TODO.txt index dc00ce1..6945bda 100644 --- a/TODO.txt +++ b/TODO.txt @@ -4,6 +4,9 @@ Battle - Show on who enemies will use the cards - Highlight which card is 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 - Make dialogue show letter by letter.