« get me outta code hell

Change leader upon 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 18:26:35 -0300
committerFlorrie <towerofnix@gmail.com>2018-08-12 18:26:35 -0300
commitaa0905c2c1358bc5ee218f9b5a6443c9c8c4501d (patch)
tree6e779c86c0874b0fb7998968e4ccfa91decd232c
parent906e9b47fda9a944b742d8f85151753fad3f229b (diff)
Change leader upon death
-rw-r--r--index.js64
1 files changed, 52 insertions, 12 deletions
diff --git a/index.js b/index.js
index db2beed..384f314 100644
--- a/index.js
+++ b/index.js
@@ -612,11 +612,11 @@ class Battle {
 
     // State
 
-    this.gameState = 'battle' // battle, pre-win, win
+    this.gameState = 'battle' // battle, pre-win, win, pre-lose, lose
 
     this.currentMenu = this.targetTypeMenu
     this.changeMenuAnim = {time: 1, direction: 1, old: null}
-    this.preWinAnim = null
+    this.preEndAnim = null
   }
 
   draw() {
@@ -644,8 +644,8 @@ class Battle {
     const { atbBar } = this.playerCharacter
 
     ctx.save()
-    if (this.preWinAnim || this.gameState === 'win') {
-      const { time } = this.preWinAnim || {time: 0}
+    if (this.preEndAnim || this.gameState === 'win' || this.gameState === 'lose') {
+      const { time } = this.preEndAnim || {time: 0}
       ctx.translate(0, Math.floor(-20 * (time - 1)))
       ctx.filter = `opacity(${time * 100}%)`
     }
@@ -706,6 +706,8 @@ class Battle {
       this.battleUpdate(dt)
     } else if (this.gameState === 'pre-win') {
       this.preWinUpdate(dt)
+    } else if (this.gameState === 'pre-lose') {
+      this.preLoseUpdate(dt)
     }
   }
 
@@ -729,17 +731,42 @@ class Battle {
 
     this.animationEntities = this.animationEntities.filter(e => !e.discarded)
 
+    // Assign a new player character if the current one died
+    if (this.playerCharacter.dead) {
+      const newCharacter = this.playerCharacter.team.characters.find(c => !c.dead)
+      if (newCharacter) {
+        this.playerCharacter = newCharacter
+        this.camera.follow(newCharacter)
+        newCharacter.atbBar.queuedActions.splice(0)
+        newCharacter.isExecutingChain = false
+        this.showTargetTypeMenu()
+        // TODO: "Leader changed" message
+      }
+    }
+
+    // SOMEHOW managed to have all teams die in the same instant? Player wins :)
     if (!this.getAllBattleCharacters().find(c => c.team !== this.playerCharacter.team && !c.dead)) {
       this.gameState = 'pre-win'
-      this.preWinAnim = {time: 1}
+      this.preEndAnim = {time: 1}
+    } else if (!this.playerCharacter.team.characters.find(c => !c.dead)) {
+      this.gameState = 'pre-lose'
+      this.preEndAnim = {time: 1}
     }
   }
 
   preWinUpdate(dt) {
-    this.preWinAnim.time -= dt * 3
-    if (this.preWinAnim.time <= 0) {
-      this.preWinAnim = null
-      this.gameState = 'win'
+    this.preEndUpdate(dt, 'win')
+  }
+
+  preLoseUpdate(dt) {
+    this.preEndUpdate(dt, 'lose')
+  }
+
+  preEndUpdate(dt, nextGameState) {
+    this.preEndAnim.time -= dt * 3
+    if (this.preEndAnim.time <= 0) {
+      this.preEndAnim = null
+      this.gameState = nextGameState
     }
   }
 
@@ -868,6 +895,10 @@ for (let ti = 0; ti < battle.teams.length; ti++) {
         {x: 40, y: 200, name: 'Flaprat'}
       ]
     ][ti][ci])
+
+    if (ti === 0) {
+      team.characters[ci].takeDamage(300)
+    }
   }
 }
 
@@ -894,13 +925,22 @@ function drawLoop() {
   ctx.fillRect(0, 0, canvas.width, canvas.height)
 
   battle.draw()
-  if (battle.preWinAnim) {
-    ctx.filter = `grayscale(${(1 - battle.preWinAnim.time) * 100}%)`
-  } else if (battle.gameState === 'win') {
+  if (battle.preEndAnim) {
+    ctx.filter = `grayscale(${(1 - battle.preEndAnim.time) * 100}%)`
+  } else if (battle.gameState === 'win' || battle.gameState === 'lose') {
     ctx.filter = 'grayscale(100%)'
   }
   ctx.drawImage(battle.canvas, 0, 0)
 
+  ctx.font = '5px pixel-font'
+  ctx.fillStyle = 'white'
+  ctx.textAlign = 'center'
+  if (battle.gameState === 'win') {
+    ctx.fillText('You won!', canvas.width / 2, canvas.height / 2)
+  } else if (battle.gameState === 'lose') {
+    ctx.fillText('You lost.', canvas.width / 2, canvas.height / 2)
+  }
+
   requestAnimationFrame(drawLoop)
 }