From aa0905c2c1358bc5ee218f9b5a6443c9c8c4501d Mon Sep 17 00:00:00 2001 From: Florrie Date: Sun, 12 Aug 2018 18:26:35 -0300 Subject: Change leader upon death --- index.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file 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) } -- cgit 1.3.0-6-gf8a5