Simplify OnParticipantSpriteClicked setup

This commit is contained in:
henriquenakashima 2020-09-18 23:51:44 -04:00
parent d8af43db53
commit ee8d66610e
2 changed files with 9 additions and 9 deletions

View file

@ -35,9 +35,8 @@ public class BattleController : MonoBehaviour
heroSprites[i].name = "Hero" + i + "_Sprite"; heroSprites[i].name = "Hero" + i + "_Sprite";
heroSprites[i].transform.position = new Vector3(-6 + 6 * i, 0, 0); heroSprites[i].transform.position = new Vector3(-6 + 6 * i, 0, 0);
BattleParticipantSprite bps = heroSprites[i].AddComponent<BattleParticipantSprite>(); BattleParticipantSprite bps = heroSprites[i].AddComponent<BattleParticipantSprite>();
bps.Init(battlePlayerControls); bps.Init(battlePlayerControls, heroes[i]);
BattleParticipant participant = heroes[i]; bps.OnSpriteClicked += battlePlayerControls.OnParticipantSpriteClicked;
bps.OnSpriteClicked += delegate() {battlePlayerControls.OnParticipantSpriteClicked(participant);};
heroUIs[i] = (HeroInfoUI) Instantiate(heroUIPrefab.gameObject, uiCanvas.transform).GetComponent(typeof(HeroInfoUI)); heroUIs[i] = (HeroInfoUI) Instantiate(heroUIPrefab.gameObject, uiCanvas.transform).GetComponent(typeof(HeroInfoUI));
heroUIs[i].Init(heroSprites[i], heroes[i]); heroUIs[i].Init(heroSprites[i], heroes[i]);
@ -56,9 +55,8 @@ public class BattleController : MonoBehaviour
monsterSprites[i].name = "Monster" + i + "_Sprite"; monsterSprites[i].name = "Monster" + i + "_Sprite";
monsterSprites[i].transform.position = new Vector3(-6 + 6 * i, 3, 0); monsterSprites[i].transform.position = new Vector3(-6 + 6 * i, 3, 0);
BattleParticipantSprite bps = monsterSprites[i].AddComponent<BattleParticipantSprite>(); BattleParticipantSprite bps = monsterSprites[i].AddComponent<BattleParticipantSprite>();
bps.Init(battlePlayerControls); bps.Init(battlePlayerControls, monsters[i]);
BattleParticipant participant = monsters[i]; bps.OnSpriteClicked += battlePlayerControls.OnParticipantSpriteClicked;
bps.OnSpriteClicked += delegate() {battlePlayerControls.OnParticipantSpriteClicked(participant);};
monsterUIs[i] = (HeroInfoUI) Instantiate(heroUIPrefab.gameObject, uiCanvas.transform).GetComponent(typeof(HeroInfoUI)); monsterUIs[i] = (HeroInfoUI) Instantiate(heroUIPrefab.gameObject, uiCanvas.transform).GetComponent(typeof(HeroInfoUI));
monsterUIs[i].Init(monsterSprites[i], monsters[i]); monsterUIs[i].Init(monsterSprites[i], monsters[i]);

View file

@ -4,13 +4,15 @@ using UnityEngine;
public class BattleParticipantSprite : MonoBehaviour public class BattleParticipantSprite : MonoBehaviour
{ {
public delegate void SpriteClickedAction(); public delegate void SpriteClickedAction(BattleParticipant participant);
public event SpriteClickedAction OnSpriteClicked; public event SpriteClickedAction OnSpriteClicked;
private BattlePlayerControls playerControls; private BattlePlayerControls playerControls;
private BattleParticipant participant;
public void Init(BattlePlayerControls playerControls) { public void Init(BattlePlayerControls playerControls, BattleParticipant participant) {
this.playerControls = playerControls; this.playerControls = playerControls;
this.participant = participant;
} }
// Start is called before the first frame update // Start is called before the first frame update
@ -27,7 +29,7 @@ public class BattleParticipantSprite : MonoBehaviour
void OnMouseDown() { void OnMouseDown() {
Debug.Log("Clicked " + this.name); Debug.Log("Clicked " + this.name);
OnSpriteClicked(); OnSpriteClicked(participant);
} }
private Color savedColor; private Color savedColor;