« get me outta code hell

Space/enter to queue selected action - csb-game - Pixelly spin-off of the Command Synergy Battle system used in Final Fantasy XIII
summary refs log tree commit diff
path: root/index.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-08-09 19:18:10 -0300
committerFlorrie <towerofnix@gmail.com>2018-08-09 19:18:10 -0300
commit9de7cad6799b551be1bc232cc6e9cff54a18eac6 (patch)
tree6a648ebec9de1ee034d98dc72d2778ca3abc5cd6 /index.js
parent5c32bcdb7d6b8cd923faed23c10692ce049d279e (diff)
Space/enter to queue selected action
Diffstat (limited to 'index.js')
-rw-r--r--index.js24
1 files changed, 23 insertions, 1 deletions
diff --git a/index.js b/index.js
index 72535a0..0e52b67 100644
--- a/index.js
+++ b/index.js
@@ -72,6 +72,10 @@ class ATBBar {
   update(dt) {
     this.progress = Math.min(1, this.progress + dt / 6)
   }
+
+  getRemainingSpace() {
+    return this.segmentCount - this.queuedActions.reduce((acc, { size }) => acc + size, 0)
+  }
 }
 
 class ActionMenu {
@@ -176,10 +180,26 @@ class ActionMenu {
       this.uiLevel = 1
     }
   }
+
+  queueTo(atbBar) {
+    const option = this.options[this.currentOptionIndex]
+    const maxLevel = option.length
+    const effectiveLevel = Math.min(maxLevel, this.uiLevel)
+    const remainingSpace = atbBar.getRemainingSpace()
+
+    if (effectiveLevel <= remainingSpace) {
+      atbBar.queuedActions.push({
+        label: option[effectiveLevel - 1],
+        size: effectiveLevel
+      })
+    } else {
+      // TODO: Feedback
+    }
+  }
 }
 
 const atbBar = new ATBBar()
-atbBar.queuedActions = [{label: 'Fire', size: 1}, {label: 'Fira', size: 2}, {label: 'Firaga', size: 3}]
+// atbBar.queuedActions = [{label: 'Fire', size: 1}, {label: 'Fira', size: 2}, {label: 'Firaga', size: 3}]
 
 const actionMenu = new ActionMenu()
 
@@ -213,6 +233,8 @@ canvas.addEventListener('keypress', evt => {
     actionMenu.decreaseLevel()
   } else if (evt.keyCode === 39) {
     actionMenu.increaseLevel()
+  } else if (evt.which === 32 || evt.keyCode === 13) {
+    actionMenu.queueTo(atbBar)
   }
 })