« get me outta code hell

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:
-rw-r--r--index.js48
1 files changed, 31 insertions, 17 deletions
diff --git a/index.js b/index.js
index fa9df29..9117502 100644
--- a/index.js
+++ b/index.js
@@ -3,6 +3,8 @@
 const canvas = document.getElementById('canvas')
 
 const last = arr => arr[arr.length - 1]
+const pickRandom = arr => arr[randomIndex(arr)]
+const randomIndex = arr => Math.floor(Math.random() * arr.length)
 
 const chainDatabase = {
   fire: ['fire', 'fira', 'firaga'],
@@ -11,13 +13,17 @@ const chainDatabase = {
 }
 
 const actionDatabase = {
-  fire: {id: 'fire',   chain: 'fire', label: 'Fire',   size: 1, target: 'enemy', color: '#F77'},
-  fira: {id: 'fira',   chain: 'fire', label: 'Fira',   size: 2, target: 'enemy', color: '#F77'},
-  firaga: {id: 'firaga', chain: 'fire', label: 'Firaga', size: 3, target: 'enemy', color: '#F77'},
+  fire: {id: 'fire',     chain: 'fire', label: 'Fire',   size: 1, target: 'enemy', color: '#F77', damage: 20, stun: 0.1},
+  fira: {id: 'fira',     chain: 'fire', label: 'Fira',   size: 2, target: 'enemy', color: '#F77', damage: 40, stun: 0.2},
+  firaga: {id: 'firaga', chain: 'fire', label: 'Firaga', size: 3, target: 'enemy', color: '#F77', damage: 60, stun: 0.3},
+
   cure: {id: 'cure', chain: 'cure', label: 'Cure', size: 1, target: 'ally', color: '#AFA'},
-  blizz: {id: 'blizz',    chain: 'blizz', label: 'Blizz',    size: 1, target: 'enemy', color: '#AAF'},
-  blizzara: {id: 'blizzara', chain: 'blizz', label: 'Blizzara', size: 2, target: 'enemy', color: '#AAF'},
-  blizzaga: {id: 'blizzaga', chain: 'blizz', label: 'Blizzaga', size: 3, target: 'enemy', color: '#AAF'}
+
+  blizz: {id: 'blizz',       chain: 'blizz', label: 'Blizz',    size: 1, target: 'enemy', color: '#AAF', damage: 15, stun: 0.2},
+  blizzara: {id: 'blizzara', chain: 'blizz', label: 'Blizzara', size: 2, target: 'enemy', color: '#AAF', damage: 30, stun: 0.35},
+  blizzaga: {id: 'blizzaga', chain: 'blizz', label: 'Blizzaga', size: 3, target: 'enemy', color: '#AAF', damage: 45, stun: 0.5},
+
+  manasvinSwipe: {id: 'manasvinSwipe', label: 'Swipe', size: 3, target: 'enemy', color: '#CCC', damage: 75, stun: 1.25},
 }
 
 class Sprite {
@@ -184,14 +190,20 @@ class ATBBar {
 
     // ATB gauge not full? Try to queue actions
     if (!this.battleCharacter.isExecutingChain && this.battleCharacter.targetCharacter && this.getRemainingSpace() >= 0) {
-      // TODO: More moves
-      while (this.getRemainingSpace()) {
-        this.enqueue(actionDatabase.fire)
+      // TODO: Actual AI logic here
+      enqueueMoves: while (this.getRemainingSpace()) {
+        const moves = this.battleCharacter.knownActions.slice()
+        while (moves.length) {
+          const index = randomIndex(moves)
+          const moveID = moves[index]
+          moves.splice(index, 1)
+          if (this.enqueue(actionDatabase[moveID])) {
+            continue enqueueMoves
+          }
+        }
+        break
       }
-    }
 
-    // ATB gauge full? Plan to execute queued actions
-    if (!this.battleCharacter.isExecutingChain && !this.getRemainingSpace()) {
       this.battleCharacter.willExecuteChain = true
     }
   }
@@ -207,6 +219,8 @@ class ATBBar {
   enqueue(action) {
     if (this.stunIsSignificant) {
       return false
+    } else if (this.getRemainingSpace() < action.size) {
+      return false
     } else {
       this.queuedActions.push(action)
       return true
@@ -644,7 +658,7 @@ class BattleCharacter extends Sprite {
     const targets = this.getValidTargets(targetType)
     if (targets.length) {
       // TODO: Some sort of "follow the leader" mechanic
-      this.targetCharacter = targets[Math.floor(Math.random() * targets.length)]
+      this.targetCharacter = pickRandom(targets)
     } else {
       this.targetCharacter = null
     }
@@ -1026,9 +1040,9 @@ class MagicProjectile {
     this.y += dt * 5 * (this.target.y - this.y)
 
     if (Math.abs(this.target.x - this.x) <= 5 && Math.abs(this.target.y - this.y) <= 5) {
-      if (['fire', 'blizz'].includes(this.action.chain)) {
-        this.target.takeDamage(this.action.size * 20)
-        this.target.addStunTime([0.1, 0.25, 0.4][this.action.size - 1])
+      if (this.action.target === 'enemy') {
+        if (this.action.damage) this.target.takeDamage(this.action.damage)
+        if (this.action.stun) this.target.addStunTime(this.action.stun || 0)
       } else if (this.action.id === 'cure') {
         this.target.recoverHP(40)
       }
@@ -1096,7 +1110,7 @@ const battle = new Battle({
       {x: -70, y: 200, name: 'Ren', knownActions: ['fire', 'fira', 'firaga', 'blizz', 'blizzara', 'blizzaga', 'cure']}
     ]},
     {characterData: [
-      {x: 70, y: 200, name: 'Manasvin Warmech'}
+      {x: 70, y: 200, name: 'Manasvin Warmech', knownActions: ['firaga', 'manasvinSwipe']}
     ]}
   ]
 })