Refactor BattleSprites out of BattleController
This commit is contained in:
parent
737a851283
commit
eb51ebace0
8 changed files with 110 additions and 61 deletions
78
Assets/Battle/BattleSprites.cs
Normal file
78
Assets/Battle/BattleSprites.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue