« get me outta code hell

Character death - 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 11:46:14 -0300
committerFlorrie <towerofnix@gmail.com>2018-08-12 11:46:14 -0300
commit78521509347247bfd0c9c7f957203ed4bdeef32f (patch)
tree59fd26d5bdad970da616bf2551fee41426afe4c6
parentab45c4c7da290dcd5071b9e7729538ae3374f80b (diff)
Character death
-rw-r--r--index.js58
1 files 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
+      }
+    }
   }
 }