diff --git a/21g30t1e75.js b/21g30t1e75.js index 5ec27e0..d01521c 100644 --- a/21g30t1e75.js +++ b/21g30t1e75.js @@ -34,6 +34,11 @@ GameLib.CustomCode.FOOD_ONION_RING = 3; GameLib.CustomCode.FOOD_PATTY = 4; GameLib.CustomCode.FOOD_TOMATO = 5; +GameLib.CustomCode.OCCUPIED_TYPE_NONE = 0; +GameLib.CustomCode.OCCUPIED_TYPE_SNAKE_BODY = 1; +GameLib.CustomCode.OCCUPIED_TYPE_FOOD = 2; +GameLib.CustomCode.OCCUPIED_TYPE_POWERUP = 3; + /** * Get runtime */ @@ -98,12 +103,13 @@ this.animationRotation = GameLib.EntityManager.Instance.findComponentById('z628k * Game objects */ this.snake = []; -this.grid = []; +this.grid = [[]]; this.speed = GameLib.CustomCode.SPEED_INITIAL; this.advanceTime = 0; -this.foodTime = 0; -this.foodSpeed = GameLib.CustomCode.FOOD_SPEED_INITIAL; -this.foodItems = []; +this.foodTime = 0; +this.foodSpeed = GameLib.CustomCode.FOOD_SPEED_INITIAL; +this.food = []; +this.powerups = []; /** * Orientation is 0, 1, 2 or 3, (up, left, down, right) - @@ -226,6 +232,13 @@ GameLib.CustomCode.prototype.createFood = function(delta) { //mesh.updateInstance('scale'); mesh.updateInstance('visible'); + this.food.push( + { + type : foodType, + mesh : mesh + } + ) + }.bind(this); @@ -289,18 +302,11 @@ GameLib.CustomCode.SnakeBody = function( this.applyToMesh(); }; -GameLib.CustomCode.SnakeBody.LastZ = 0; - GameLib.CustomCode.SnakeBody.prototype.applyToMesh = function() { this.mesh.position.x = (this.position.x * GameLib.CustomCode.BODY_SCALE_X) + GameLib.CustomCode.GRID_OFFSET_X; this.mesh.position.y = (this.position.y * GameLib.CustomCode.BODY_SCALE_Y) + GameLib.CustomCode.GRID_OFFSET_Y; - //if ((this.mesh.rotation.z - (this.orientation * Math.PI / 2)) > Math.PI) { - // this.mesh.rotation.z -= Math.PI; - // this.mesh.updateInstance('rotation'); - //} - this.mesh.rotation.z = this.orientation * Math.PI / 2; if (this.backupMesh) { @@ -309,13 +315,7 @@ GameLib.CustomCode.SnakeBody.prototype.applyToMesh = function() { this.mesh.updateInstance('rotation'); } - /** - * TODO: We don't update instance position - animation should do this - */ - // this.mesh.updateInstance('position'); - //if (!this.isTail) { - this.mesh.updateInstance('rotation'); - //} + this.mesh.updateInstance('rotation'); } GameLib.CustomCode.SnakeBody.prototype.advance = function(orientation, flip) { @@ -362,12 +362,73 @@ GameLib.CustomCode.SnakeBody.prototype.advance = function(orientation, flip) { console.log('crashed!'); } - this.orientation = orientation; this.flip = flip; } +GameLib.CustomCode.prototype.initializeGrid = function() { + + this.grid = []; + + for (var x = 0; x < GameLib.CustomCode.GRID_WIDTH; x++) { + this.grid[x] = []; + for (var y = 0; y < GameLib.CustomCode.GRID_HEIGHT; y++) { + this.grid[x][y] = { + occupied : false, + type : GameLib.CustomCode.OCCUPIED_TYPE_NONE, + index : -1 + } + } + } + +}.bind(this); + +GameLib.CustomCode.prototype.rebuildGrid = function() { + + this.initializeGrid(); + + /** + * First add all the snake body parts + */ + this.snake.map( + function(body, index) { + this.grid[body.position.x][body.position.y] = { + occupied: true, + type : GameLib.CustomCode.OCCUPIED_TYPE_SNAKE_BODY, + index : index + } + } + ); + + /** + * Then add all the food bits + */ + this.food.map( + function(food, index) { + this.grid[food.position.x][food.position.y] = { + occupied: true, + type : GameLib.CustomCode.OCCUPIED_TYPE_FOOD, + index : index + } + } + ); + + /** + * And also all the power ups + */ + this.powerups.map( + function(powerup, index) { + this.grid[powerup.position.x][powerup.position.y] = { + occupied: true, + type : GameLib.CustomCode.OCCUPIED_TYPE_POWERUP, + index : index + } + } + ); + +}.bind(this); + GameLib.CustomCode.prototype.advanceSnake = function(delta) { this.advanceTime += delta; @@ -504,25 +565,13 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) { this.state.turning = false; + /** + * At this point - we advanced the whole snake - time to rebuild the grid + */ + this.rebuildGrid(); + }.bind(this); - -GameLib.CustomCode.prototype.initializeGrid = function() { - - this.grid = []; - - for (var x = 0; x < GameLib.CustomCode.GRID_WIDTH; x++) { - this.grid[x] = []; - for (var y = 0; y < GameLib.CustomCode.GRID_HEIGHT; y++) { - this.grid[x][y] = { - mesh : null - } - } - } - -}.bind(this); - - GameLib.Event.Subscribe( GameLib.Event.GAME_START, function() { @@ -533,11 +582,27 @@ GameLib.Event.Subscribe( this.snake.map( function(body) { body.mesh.geometry = null; - body.mesh.materials = null; + body.mesh.materials = null; body.mesh.remove(); } ) + this.food.map( + function(food) { + food.mesh.geometry = null; + food.mesh.materials = null; + food.mesh.remove(); + } + ) + + this.powerups.map( + function(powerup) { + powerup.mesh.geometry = null; + powerup.mesh.materials = null; + powerup.mesh.remove(); + } + ); + this.state = { orientation : GameLib.CustomCode.ORIENTATION_UP }; @@ -579,8 +644,6 @@ GameLib.Event.Subscribe( true ) ]; - - //this.snake[3].mesh.position.z += 1; this.snake[0].mesh.updateInstance('position'); this.snake[1].mesh.updateInstance('position'); @@ -590,28 +653,11 @@ GameLib.Event.Subscribe( this.animation.meshes.push(this.snake[0].mesh); this.animation.meshes.push(this.snake[1].mesh); this.animation.meshes.push(this.snake[2].mesh); - - //this.snake[2].mesh.instance.add(this.snake[3].mesh.instance); this.animation.meshes.push(this.snake[3].mesh); + //this.snake[2].mesh.instance.add(this.snake[3].mesh.instance); //this.animationRotation.meshes.push(this.snake[3].mesh); - /** - * Cleanup grid - * - this.grid.map( - function(x) { -x.map( -function(y) { - y.mesh.geometry = null; - y.mesh.materials = null; - y.mesh.remove(); -} -); -} - ); -*/ - this.initializeGrid(); //this.visualizeGrid(); @@ -622,7 +668,8 @@ function(y) { this.speed = GameLib.CustomCode.SPEED_INITIAL; this.foodTime = 0; this.foodSpeed = GameLib.CustomCode.FOOD_SPEED_INITIAL; - this.foodItems = []; + this.food = []; + this.powerups = []; /** * Re-initialize our other custom code components