From 78521509347247bfd0c9c7f957203ed4bdeef32f Mon Sep 17 00:00:00 2001 From: Florrie Date: Sun, 12 Aug 2018 11:46:14 -0300 Subject: Character death --- index.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 9faed1e..9553b52 100644 --- a/index.js +++ b/index.js @@ -17,9 +17,12 @@ class Sprite { this.canvas.width = this.image.width this.canvas.height = this.image.height const ctx = this.canvas.getContext('2d') + this.applySpriteEffects(ctx) ctx.drawImage(this.image, 0, 0) } } + + applySpriteEffects(ctx) {} } class Backdrop extends Sprite { @@ -365,8 +368,7 @@ class TargetMenu extends BaseBattleMenu { } buildOptions(battle) { - this.options = battle.getAllBattleCharacters() - .filter(char => char.team !== battle.playerCharacter.team) + this.options = battle.playerCharacter.getValidOffenseTargets() .map(char => ({label: char.name, battleCharacter: char})) } } @@ -394,13 +396,24 @@ class BattleCharacter extends Sprite { this.hp = 500 this.maxHP = 500 + this.dead = false + this.hpBar = new HPBar(this) } update(dt) { - this.atbBar.update(dt) this.hpBar.update(dt) + if (this.dead) { + this.deadUpdate(dt) + } else { + this.aliveUpdate(dt) + } + } + + aliveUpdate(dt) { + this.atbBar.update(dt) + if (this.isExecutingChain && !this.isExecutingAction) { throw new Error('Executing chain but not action for more than one update.. ATB Bar should have queued an action or ended the chain') } @@ -415,6 +428,16 @@ class BattleCharacter extends Sprite { } } + deadUpdate(dt) { + // Do nothing, for now. + } + + applySpriteEffects(ctx) { + if (this.dead) { + ctx.filter = 'grayscale(100%)' + } + } + executeAction(action) { if (this.isExecutingActions) { throw new Error('Called executeAction while already executing actions') @@ -426,13 +449,36 @@ class BattleCharacter extends Sprite { if (this.targetCharacter) { this.targetCharacter.takeDamage(action.size * 60) + + if (this.targetCharacter.dead) { + this.determineNewOffenseTarget() + } } } + determineNewOffenseTarget() { + const targets = this.getValidOffenseTargets() + if (targets.length) { + this.targetCharacter = targets[0] + } else { + this.targetCharacter = null + } + } + + getValidOffenseTargets() { + return battle.getAllBattleCharacters() + .filter(char => char.team !== battle.playerCharacter.team) + .filter(char => !char.dead) + } + takeDamage(damage) { - this.hp -= damage - // TODO: Death - this.hp = Math.max(1, this.hp) + if (!this.dead) { + this.hp -= damage + if (this.hp <= 0) { + this.hp = 0 + this.dead = true + } + } } } -- cgit 1.3.0-6-gf8a5