Only alive participants can act

This commit is contained in:
henriquenakashima 2020-09-20 16:02:13 -04:00
parent a5a96edf25
commit adc84f9aba
5 changed files with 44 additions and 8 deletions

View file

@ -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() {

View file

@ -62,6 +62,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
BattleParticipantSprite bps = heroSprites[i].AddComponent<BattleParticipantSprite>();
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<BattleParticipantSprite>();
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<HeroCard> GetHeroDeck() {

View file

@ -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);
}
}
}

View file

@ -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() {
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<SpriteRenderer>();
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<SpriteRenderer>();
sprite.enabled = false;
BoxCollider2D collider = GetComponent<BoxCollider2D>();
collider.enabled = false;
this.removedFromBattle = true;
}
}
}

View file

@ -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