« get me outta code hell

Damage, more or less - csb-game - Pixelly spin-off of the Command Synergy Battle system used in Final Fantasy XIII
summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-08-12 10:13:37 -0300
committerFlorrie <towerofnix@gmail.com>2018-08-12 10:13:37 -0300
commit1501a51022d549b46185dd64f1343fdab70a8dc5 (patch)
treedea7d8139fb4a08e19e735987702ebb8cc92d19a
parent0c1fe62b8b6486ce057edf41a25af5c5dc3b340b (diff)
Damage, more or less
-rw-r--r--index.js40
1 files 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()