diff --git a/Assets/Battle/BaseCard.cs b/Assets/Battle/BaseCard.cs index d9caf9c..3faaf08 100644 --- a/Assets/Battle/BaseCard.cs +++ b/Assets/Battle/BaseCard.cs @@ -18,6 +18,7 @@ public abstract class BaseCard this.effectText = effectText; } + public BattleParticipant Owner {get => this.owner;} public string GetDisplayName() {return this.displayName;} public int GetCost() {return this.cost;} public string GetDisplayType() { diff --git a/Assets/Battle/BattleController.cs b/Assets/Battle/BattleController.cs index 3158af3..ec3a997 100644 --- a/Assets/Battle/BattleController.cs +++ b/Assets/Battle/BattleController.cs @@ -62,6 +62,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe BattleParticipantSprite bps = heroSprites[i].AddComponent(); bps.Init(battlePlayerControls, heroes[i]); bps.OnSpriteClicked += battlePlayerControls.NotifyParticipantSpriteClicked; + heroes[i].OnDeath += bps.NotifyDeath; heroUIs[i] = (HeroInfoUI) Instantiate(heroUIPrefab.gameObject, uiCanvas.transform).GetComponent(typeof(HeroInfoUI)); heroUIs[i].Init(heroSprites[i], heroes[i]); @@ -82,6 +83,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe BattleParticipantSprite bps = monsterSprites[i].AddComponent(); bps.Init(battlePlayerControls, monsters[i]); bps.OnSpriteClicked += battlePlayerControls.NotifyParticipantSpriteClicked; + monsters[i].OnDeath += bps.NotifyDeath; monsterUIs[i] = (HeroInfoUI) Instantiate(heroUIPrefab.gameObject, uiCanvas.transform).GetComponent(typeof(HeroInfoUI)); monsterUIs[i].Init(monsterSprites[i], monsters[i]); @@ -237,11 +239,11 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe } public bool CanUseHeroCard(HeroCard card) { - return this.heroTurn && this.heroEnergy.CanPayForCard(card); + return this.heroTurn && card.Owner.IsAlive() && this.heroEnergy.CanPayForCard(card); } public bool CanUseMonsterCard(MonsterCard card) { - return !this.heroTurn && this.monsterEnergy.CanPayForCard(card); + return !this.heroTurn && card.Owner.IsAlive() && this.monsterEnergy.CanPayForCard(card); } public BattleDeck GetHeroDeck() { diff --git a/Assets/Battle/BattleParticipant.cs b/Assets/Battle/BattleParticipant.cs index 1e66f99..c296f9f 100644 --- a/Assets/Battle/BattleParticipant.cs +++ b/Assets/Battle/BattleParticipant.cs @@ -5,11 +5,15 @@ using UnityEngine; public class BattleParticipant { + public delegate void DeathAction(BattleParticipant participant); + public event DeathAction OnDeath; + protected bool isHero; protected string displayName; protected int hp; protected int maxHP; protected int block; + protected bool dead; public BattleParticipant(bool isHero) { this.isHero = isHero; @@ -32,7 +36,7 @@ public class BattleParticipant } public bool IsAlive() { - return this.hp > 0; + return !this.dead; } public int DealDamage(int damage) { @@ -43,6 +47,10 @@ public class BattleParticipant } damage = Math.Min(this.hp, damage); this.hp -= damage; + if (this.hp <= 0) { + this.dead = true; + TriggerOnDeath(); + } return damage; } @@ -78,4 +86,10 @@ public class BattleParticipant this.hp += amount; } } + + private void TriggerOnDeath() { + if (this.OnDeath != null) { + this.OnDeath(this); + } + } } diff --git a/Assets/Battle/BattleParticipantSprite.cs b/Assets/Battle/BattleParticipantSprite.cs index b306630..3326124 100644 --- a/Assets/Battle/BattleParticipantSprite.cs +++ b/Assets/Battle/BattleParticipantSprite.cs @@ -9,10 +9,12 @@ public class BattleParticipantSprite : MonoBehaviour private BattlePlayerControls playerControls; private BattleParticipant participant; + private bool removedFromBattle; public void Init(BattlePlayerControls playerControls, BattleParticipant participant) { this.playerControls = playerControls; this.participant = participant; + this.removedFromBattle = false; } // Start is called before the first frame update @@ -22,20 +24,22 @@ public class BattleParticipantSprite : MonoBehaviour // Update is called once per frame void Update() { - if (colorChanged && !this.playerControls.CouldTarget(participant)) { + if (this.colorChanged && !this.playerControls.CouldTarget(participant)) { OnMouseExit(); } } void OnMouseDown() { - OnSpriteClicked(participant); + if (!this.removedFromBattle) { + OnSpriteClicked(participant); + } } private Color savedColor; private bool colorChanged; void OnMouseEnter() { - if (this.playerControls.CouldTarget(participant)) { + if (!this.removedFromBattle && this.playerControls.CouldTarget(participant)) { SpriteRenderer sprite = GetComponent(); this.savedColor = sprite.color; sprite.color = Color.Lerp(this.savedColor, Color.white, .5f); @@ -50,4 +54,19 @@ public class BattleParticipantSprite : MonoBehaviour this.colorChanged = false; } } + + public void NotifyDeath(BattleParticipant deadParticipant) { + if (deadParticipant != this.participant) { + throw new System.InvalidOperationException( + "BattleParticipantSprite received NotifyDeath from another battle participant."); + } + + if (!this.removedFromBattle) { + SpriteRenderer sprite = GetComponent(); + sprite.enabled = false; + BoxCollider2D collider = GetComponent(); + collider.enabled = false; + this.removedFromBattle = true; + } + } } diff --git a/TODO.txt b/TODO.txt index 0d49c25..f30f8a5 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,3 +1,3 @@ -- Make battle end -- Do not allow dead heroes to act +- Make battle end as state based effect +- Check death as state based effect - Rename CardId to HCardId \ No newline at end of file