From 1501a51022d549b46185dd64f1343fdab70a8dc5 Mon Sep 17 00:00:00 2001 From: Florrie Date: Sun, 12 Aug 2018 10:13:37 -0300 Subject: Damage, more or less --- index.js | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index d1273cb..0789074 100644 --- a/index.js +++ b/index.js @@ -336,21 +336,13 @@ class BattleCharacter extends Sprite { this.isExecutingAction = false this.actionExecuteTime = 0 + this.targetCharacter = null + this.hp = 500 this.maxHP = 500 this.hpBar = new HPBar(this) } - executeAction(action) { - if (this.isExecutingActions) { - throw new Error('Called executeAction while already executing actions') - } - - this.isExecutingChain = true - this.isExecutingAction = true - this.actionExecuteTime = 0.4 + 0.2 * action.size - } - update(dt) { this.atbBar.update(dt) this.hpBar.update(dt) @@ -368,6 +360,26 @@ class BattleCharacter extends Sprite { } } } + + executeAction(action) { + if (this.isExecutingActions) { + throw new Error('Called executeAction while already executing actions') + } + + this.isExecutingChain = true + this.isExecutingAction = true + this.actionExecuteTime = 0.4 + 0.2 * action.size + + if (this.targetCharacter) { + this.targetCharacter.takeDamage(action.size * 60) + } + } + + takeDamage(damage) { + this.hp -= damage + // TODO: Death + this.hp = Math.max(1, this.hp) + } } class Camera extends Sprite { @@ -399,7 +411,7 @@ const enemyCharacter = new BattleCharacter() enemyCharacter.x = 0 enemyCharacter.y = 200 -const targetCharacter = enemyCharacter +battleCharacter.targetCharacter = enemyCharacter const atbBar = battleCharacter.atbBar const hpBar = battleCharacter.hpBar @@ -440,7 +452,7 @@ function drawLoop() { sprite.draw() ctx.drawImage(sprite.canvas, x, y) - if (targetCharacter === sprite) { + if (battleCharacter.targetCharacter === sprite) { targetX = sprite.x - camera.x targetY = y - camera.y } @@ -463,8 +475,8 @@ function drawLoop() { hpBar.draw() ctx.drawImage(hpBar.canvas, canvas.width - 20 - hpBar.canvas.width, y) - if (targetCharacter) { - const hpBar = targetCharacter.hpBar + if (battleCharacter.targetCharacter) { + const hpBar = battleCharacter.targetCharacter.hpBar const x = targetX - hpBar.canvas.width / 2 const y = targetY - hpBar.canvas.height hpBar.draw() -- cgit 1.3.0-6-gf8a5