diff --git a/21g30t1e75.js b/21g30t1e75.js index 51e8378..5f1069f 100644 --- a/21g30t1e75.js +++ b/21g30t1e75.js @@ -158,7 +158,7 @@ this.state = { lives : 3, getReady : false, message : null, - gameOver : false + gameOver : false }; GameLib.CustomCode.prototype.displayHUD = function() { @@ -205,7 +205,13 @@ GameLib.CustomCode.prototype.displayHUD = function() { this.canvasHUD.text('slow', 420, 500, '12px Pixeled', '#ffffff'); if (this.state.message) { - this.canvasHUD.text(this.state.message, 220, 250, '30px Pixeled', '#ffffff'); + this.canvasHUD.text( + this.state.message.text, + this.state.message.x, + this.state.message.y, + '30px Pixeled', + '#ffffff' + ); } this.canvasHUD.image(this.imagePowerupSpeed.instance, 50, 460, 48, 48); @@ -223,7 +229,7 @@ GameLib.CustomCode.prototype.waitReload = function(delta) { if (this.explodeTime < GameLib.CustomCode.EXPLODE_LIFETIME) { return; } - + /** * We need to revert to a good state */ @@ -231,17 +237,15 @@ GameLib.CustomCode.prototype.waitReload = function(delta) { this.state.exploding = false; - if (this.state.lives <= 0) { - this.state.gameOver = true; - this.state.message = "GAME OVER"; - this.displayHUD(); - return; - } + if (this.state.lives <= 0) { + this.gameOver(false); + return; + } - this.displayHUD(); + this.displayHUD(); this.restore(); - + this.state.getReady = true; window.setTimeout( @@ -254,7 +258,11 @@ GameLib.CustomCode.prototype.waitReload = function(delta) { window.setTimeout( function(){ - this.state.message = "GO!"; + this.state.message = { + text : "GO!", + x : 150, + y : 256 + }; this.displayHUD(); this.state.getReady = false; }.bind(this), @@ -263,7 +271,11 @@ GameLib.CustomCode.prototype.waitReload = function(delta) { window.setTimeout( function(){ - this.state.message = "1"; + this.state.message = { + text: "1", + x: 160, + y: 256 + }; this.displayHUD(); }.bind(this), 3000 @@ -271,7 +283,11 @@ GameLib.CustomCode.prototype.waitReload = function(delta) { window.setTimeout( function(){ - this.state.message = "2"; + this.state.message = { + text: "2", + x: 160, + y: 256 + }; this.displayHUD(); }.bind(this), 2000 @@ -280,13 +296,21 @@ GameLib.CustomCode.prototype.waitReload = function(delta) { window.setTimeout( function(){ - this.state.message = "3"; + this.state.message = { + text: "3", + x: 160, + y: 256 + }; this.displayHUD(); }.bind(this), 1000 ); - this.state.message = "READY?"; + this.state.message = { + text: "GET READY", + x: 100, + y: 256 + }; this.displayHUD(); }.bind(this) @@ -438,16 +462,6 @@ GameLib.CustomCode.GameObject = function( this.position = position; } -/* GameLib.CustomCode.GameObject.prototype.clone = function() { - - return new GameLib.CustomCode.GameObject( - this.objectType, - this.type, - this.mesh, - this.position - ) -}; -*/ /** * Disposes of the mesh object and resets this object to its defaults */ @@ -513,6 +527,10 @@ GameLib.CustomCode.prototype.getFreeGridPosition = function() { } ); + if (positions.length === 0) { + return null; + } + var index = GameLib.Utils.GetRandomInt(0, positions.length); return positions[index]; @@ -755,11 +773,19 @@ GameLib.CustomCode.prototype.createPowerup = function(delta) { return; } - this.createGameObject( - GameLib.CustomCode.OBJECT_TYPE_POWERUP, - null, - this.getFreeGridPosition() - ); + var position = this.getFreeGridPosition(); + + if (position === null) { + this.gameOver(true); + } else { + + this.createGameObject( + GameLib.CustomCode.OBJECT_TYPE_POWERUP, + null, + position + ); + + } }.bind(this); @@ -777,14 +803,29 @@ GameLib.CustomCode.prototype.createFood = function(delta) { return; } - this.createGameObject( - GameLib.CustomCode.OBJECT_TYPE_FOOD, - null, - this.getFreeGridPosition() - ); + var position = this.getFreeGridPosition(); + + if (position === null) { + this.gameOver(true); + } else { + this.createGameObject( + GameLib.CustomCode.OBJECT_TYPE_FOOD, + null, + position + ); + } }.bind(this); +GameLib.CustomCode.prototype.gameOver = function(won) { + this.state.gameOver = true; + this.state.message = { + text : "GAME OVER", + x : 100, + y : 256 + }; + this.displayHUD(); +}.bind(this) /** * GameLib.CustomCode.SnakeBody @@ -981,7 +1022,7 @@ GameLib.CustomCode.prototype.explode = function(position) { this.particleEnginePickle.updateInstance('position'); this.particleEnginePickle.enabled = true; - + }.bind(this) /** @@ -1102,6 +1143,10 @@ GameLib.CustomCode.prototype.extend = function(gameObject, position, orientation this.grid[body.position.x][body.position.y] = body; + if (this.snake.length === GameLib.CustomCode.GRID_WIDTH * GameLib.CustomCode.GRID_HEIGHT) { + this.gameOver(true); + } + }.bind(this); /** @@ -1146,8 +1191,8 @@ GameLib.CustomCode.prototype.createCorner = function(body, temp) { body.backupMesh.position.x -= 0.5 * GameLib.CustomCode.BODY_SCALE_X; } - body.orientation = backupOrientation; - + body.orientation = backupOrientation; + body.mesh = this.createGameMesh(this.materialBreadCorner); body.mesh.position.z = 5; @@ -1239,55 +1284,55 @@ GameLib.CustomCode.prototype.restore = function() { var clone = body.clone(); - if (clone.backupMesh) { - - if (clone.mesh) { - clone.mesh.geometry = null; - clone.mesh.materials = null; - clone.mesh.remove(); - } - - clone.mesh = clone.backupMesh; + if (clone.backupMesh) { - clone.backupMesh = null; - clone.mesh.visible = true; - clone.mesh.updateInstance('visible'); - clone.applyToMesh(); - clone.backupMesh = clone.mesh; - - if (clone.orientation === GameLib.CustomCode.ORIENTATION_UP) { - clone.backupMesh.position.y += 0.5 * GameLib.CustomCode.BODY_SCALE_Y; - } + if (clone.mesh) { + clone.mesh.geometry = null; + clone.mesh.materials = null; + clone.mesh.remove(); + } - if (clone.orientation === GameLib.CustomCode.ORIENTATION_DOWN) { - clone.backupMesh.position.y -= 0.5 * GameLib.CustomCode.BODY_SCALE_Y; - } + clone.mesh = clone.backupMesh; - if (clone.orientation === GameLib.CustomCode.ORIENTATION_LEFT) { - clone.backupMesh.position.x -= 0.5 * GameLib.CustomCode.BODY_SCALE_X; - } + clone.backupMesh = null; + clone.mesh.visible = true; + clone.mesh.updateInstance('visible'); + clone.applyToMesh(); + clone.backupMesh = clone.mesh; - if (clone.orientation === GameLib.CustomCode.ORIENTATION_RIGHT) { - clone.backupMesh.position.x += 0.5 * GameLib.CustomCode.BODY_SCALE_X; - } - - if (clone.isTail) { - clone.mesh = clone.backupMesh; - clone.backupMesh = null; - } else { - clone.mesh = this.createGameMesh(this.materialBreadCorner); - } - - clone.applyToMesh(); - - } else { - clone.mesh.visible = true; - clone.mesh.updateInstance('visible'); - clone.applyToMesh(); - } + if (clone.orientation === GameLib.CustomCode.ORIENTATION_UP) { + clone.backupMesh.position.y += 0.5 * GameLib.CustomCode.BODY_SCALE_Y; + } + + if (clone.orientation === GameLib.CustomCode.ORIENTATION_DOWN) { + clone.backupMesh.position.y -= 0.5 * GameLib.CustomCode.BODY_SCALE_Y; + } + + if (clone.orientation === GameLib.CustomCode.ORIENTATION_LEFT) { + clone.backupMesh.position.x -= 0.5 * GameLib.CustomCode.BODY_SCALE_X; + } + + if (clone.orientation === GameLib.CustomCode.ORIENTATION_RIGHT) { + clone.backupMesh.position.x += 0.5 * GameLib.CustomCode.BODY_SCALE_X; + } + + if (clone.isTail) { + clone.mesh = clone.backupMesh; + clone.backupMesh = null; + } else { + clone.mesh = this.createGameMesh(this.materialBreadCorner); + } + + clone.applyToMesh(); + + } else { + clone.mesh.visible = true; + clone.mesh.updateInstance('visible'); + clone.applyToMesh(); + } + + this.grid[clone.position.x][clone.position.y] = clone; - this.grid[clone.position.x][clone.position.y] = clone; - return clone; }.bind(this) ); @@ -1322,31 +1367,31 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) { this.snake.map( function(body, index) { - + if (index === 0) { - if (body.backupMesh) { - /** - * We used to be a corner, change back - * @type {null} - */ - body.mesh.geometry = null; + if (body.backupMesh) { + /** + * We used to be a corner, change back + * @type {null} + */ + body.mesh.geometry = null; - body.mesh.materials = null; + body.mesh.materials = null; - body.mesh.remove(); + body.mesh.remove(); - body.mesh = body.backupMesh; + body.mesh = body.backupMesh; - body.backupMesh = null; + body.backupMesh = null; - body.mesh.visible = true; + body.mesh.visible = true; - body.mesh.updateInstance('visible'); + body.mesh.updateInstance('visible'); + + body.mesh.updateInstance('position'); + } - body.mesh.updateInstance('position'); - } - backup = { position : { x : body.position.x, @@ -1496,28 +1541,28 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) { */ this.grid[body.position.x][body.position.y] = body; - if (body.backupMesh) { - /** - * We used to be a corner, change back - * @type {null} - */ - body.mesh.geometry = null; + if (body.backupMesh) { + /** + * We used to be a corner, change back + * @type {null} + */ + body.mesh.geometry = null; - body.mesh.materials = null; + body.mesh.materials = null; - body.mesh.remove(); + body.mesh.remove(); - body.mesh = body.backupMesh; + body.mesh = body.backupMesh; - body.backupMesh = null; + body.backupMesh = null; - body.mesh.visible = true; + body.mesh.visible = true; - body.mesh.updateInstance('visible'); + body.mesh.updateInstance('visible'); + + body.mesh.updateInstance('position'); + } - body.mesh.updateInstance('position'); - } - if (body.orientation !== temp.orientation) { this.createCorner(body, temp); } @@ -1555,7 +1600,7 @@ GameLib.Event.Subscribe( lives : 3, getReady : false, message : null, - gameOver : false + gameOver : false }; this.initializeGrid();