« get me outta code hell

Very basic AI - 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-12 15:08:43 -0300
committerFlorrie <towerofnix@gmail.com>2018-08-12 15:08:43 -0300
commit367facda08882fc0ac68fcf9a7cb2ea235040a7f (patch)
tree9b810430351757d797c18f8dd8ad4112e4319b10 /index.js
parent612cc9f68b2d0491d4a2175d82dbc0c316696b5a (diff)
Very basic AI
Diffstat (limited to 'index.js')
-rw-r--r--index.js52
1 files changed, 40 insertions, 12 deletions
diff --git a/index.js b/index.js
index 63b8895..6e0af27 100644
--- a/index.js
+++ b/index.js
@@ -116,6 +116,10 @@ class ATBBar {
     // ATB gauge visual update
     this.visualProgress += 8 * dt * (this.progress - this.visualProgress)
 
+    if (this.battle.playerCharacter !== this.battleCharacter) {
+      this.aiUpdate(dt)
+    }
+
     // Start action chain once user has confirmed (willExecuteChain = true) and IP gauge is full enough to execute all actions
     if (this.battleCharacter.willExecuteChain && this.progress >= 1 - this.getRemainingSpace() / this.segmentCount) {
       this.battleCharacter.willExecuteChain = false
@@ -130,10 +134,32 @@ class ATBBar {
         this.progress -= 1 / this.segmentCount * action.size
       } else {
         this.battleCharacter.isExecutingChain = false
-        this.battle.changeMenuAnim = {old: null, direction: 1, time: 1}
-        this.battle.currentMenu = this.battle.actionMenu
+        if (this.battle.playerCharacter === this.battleCharacter) {
+          this.battle.changeMenuAnim = {old: null, direction: 1, time: 1}
+          this.battle.currentMenu = this.battle.actionMenu
+        }
+      }
+    }
+  }
+
+  aiUpdate(dt) {
+    // Determine a target
+    if (!this.battleCharacter.targetCharacter) {
+      this.battleCharacter.determineNewOffenseTarget()
+    }
+
+    // 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.queuedActions.push({label: 'Fire', size: 1})
       }
     }
+
+    // ATB gauge full? Plan to execute queued actions
+    if (!this.battleCharacter.isExecutingChain && !this.getRemainingSpace()) {
+      this.battleCharacter.willExecuteChain = true
+    }
   }
 
   getRemainingSpace() {
@@ -316,7 +342,7 @@ class ActionMenu extends BaseBattleMenu {
     ].map(arr => ({levelTexts: arr}))})
 
     this.battle = battle
-    this.uiLevel = 1 // 1-3 -- which of "fire", "fira", "firaga" is selected.
+    this.uiLevel = 6 // 1-3 -- which of "fire", "fira", "firaga" is selected.
   }
 
   drawOption(option, ctx) {
@@ -486,7 +512,7 @@ class BattleCharacter extends Sprite {
 
   getValidOffenseTargets() {
     return battle.getAllBattleCharacters()
-      .filter(char => char.team !== battle.playerCharacter.team)
+      .filter(char => char.team !== this.team)
       .filter(char => !char.dead)
   }
 
@@ -567,6 +593,13 @@ class Battle {
 
     const { atbBar } = this.playerCharacter
 
+    ctx.save()
+    if (this.preWinAnim || this.gameState === 'win') {
+      const { time } = this.preWinAnim || {time: 0}
+      ctx.translate(0, Math.floor(-20 * (time - 1)))
+      ctx.filter = `opacity(${time * 100}%)`
+    }
+
     // TODO: transition y for menus.. let targetY
     let y = canvas.height - 20
     if (this.changeMenuAnim && this.changeMenuAnim.old) {
@@ -593,12 +626,6 @@ class Battle {
     const oldOffset = (this.changeMenuAnim && this.changeMenuAnim.old) ? this.changeMenuAnim.old.canvas.height : 0
     y -= oldOffset + (targetOffset - oldOffset) * (this.changeMenuAnim ? 1 - this.changeMenuAnim.time : 1)
     y -= 2
-    ctx.save()
-    if (this.preWinAnim || this.gameState === 'win') {
-      const { time } = this.preWinAnim || {time: 0}
-      ctx.translate(0, Math.floor(-20 * (time - 1)))
-      ctx.filter = `opacity(${time * 100}%)`
-    }
     atbBar.draw()
     ctx.drawImage(atbBar.canvas, 20, y - atbBar.canvas.height)
 
@@ -609,7 +636,6 @@ class Battle {
       ctx.drawImage(hpBar.canvas, canvas.width - 20 - hpBar.canvas.width, y)
       y -= 2
     }
-    ctx.restore()
 
     if (targetCharacter) {
       const hpBar = targetCharacter.hpBar
@@ -618,6 +644,8 @@ class Battle {
       hpBar.draw()
       ctx.drawImage(hpBar.canvas, x, y)
     }
+
+    ctx.restore()
   }
 
   update(dt) {
@@ -735,7 +763,7 @@ 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) {
-      this.target.takeDamage(this.action.size * 60)
+      this.target.takeDamage(this.action.size * 20)
       this.discarded = true
     }
   }