diff --git a/21g30t1e75.js b/21g30t1e75.js index 4232cc6..ed667aa 100644 --- a/21g30t1e75.js +++ b/21g30t1e75.js @@ -134,35 +134,93 @@ this.state = { flip : 0 }; -GameLib.CustomCode.prototype.visualizeGrid = function (color) { +GameLib.CustomCode.prototype.visualizeGrid = function () { - if (this.starsMesh) { - this.scene.instance.remove(this.starsMesh); + if (this.noneMesh) { + this.scene.instance.remove(this.noneMesh); } - this.starsGeometry = new THREE.Geometry(); - + if (this.foodMesh) { + this.scene.instance.remove(this.foodMesh); + } + + if (this.powerupMesh) { + this.scene.instance.remove(this.powerupMesh); + } + + if (this.snakeMesh) { + this.scene.instance.remove(this.snakeMesh); + } + + this.noneGeometry = new THREE.Geometry(); + this.foodGeometry = new THREE.Geometry(); + this.powerupGeometry = new THREE.Geometry(); + this.snakeGeometry = new THREE.Geometry(); + this.grid.map( function(x, xIndex) { x.map( - function(y, yIndex) { - this.starsGeometry.vertices.push( - new THREE.Vector3( - (xIndex * GameLib.CustomCode.BODY_SCALE_X) + GameLib.CustomCode.GRID_OFFSET_X, - (yIndex * GameLib.CustomCode.BODY_SCALE_Y) + GameLib.CustomCode.GRID_OFFSET_Y, - 5 - ) - ); + function(object, yIndex) { + + if (object.objectType === GameLib.CustomCode.OBJECT_TYPE_NONE) { + this.noneGeometry.vertices.push( + new THREE.Vector3( + (xIndex * GameLib.CustomCode.BODY_SCALE_X) + GameLib.CustomCode.GRID_OFFSET_X, + (yIndex * GameLib.CustomCode.BODY_SCALE_Y) + GameLib.CustomCode.GRID_OFFSET_Y, + 5 + ) + ); + } + + if (object.objectType === GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY) { + this.snakeGeometry.vertices.push( + new THREE.Vector3( + (xIndex * GameLib.CustomCode.BODY_SCALE_X) + GameLib.CustomCode.GRID_OFFSET_X, + (yIndex * GameLib.CustomCode.BODY_SCALE_Y) + GameLib.CustomCode.GRID_OFFSET_Y, + 5 + ) + ); + } + + if (object.objectType === GameLib.CustomCode.OBJECT_TYPE_FOOD) { + this.foodGeometry.vertices.push( + new THREE.Vector3( + (xIndex * GameLib.CustomCode.BODY_SCALE_X) + GameLib.CustomCode.GRID_OFFSET_X, + (yIndex * GameLib.CustomCode.BODY_SCALE_Y) + GameLib.CustomCode.GRID_OFFSET_Y, + 5 + ) + ); + } + + if (object.objectType === GameLib.CustomCode.OBJECT_TYPE_POWERUP) { + this.powerupGeometry.vertices.push( + new THREE.Vector3( + (xIndex * GameLib.CustomCode.BODY_SCALE_X) + GameLib.CustomCode.GRID_OFFSET_X, + (yIndex * GameLib.CustomCode.BODY_SCALE_Y) + GameLib.CustomCode.GRID_OFFSET_Y, + 5 + ) + ); + } + }.bind(this) ) }.bind(this) ); - var starsMaterial = new THREE.PointsMaterial({color: color, size: 0.1}); + var noneMaterial = new THREE.PointsMaterial({color: 0xffffff, size: 0.1}); + var foodMaterial = new THREE.PointsMaterial({color: 0x00ff00, size: 0.1}); + var snakeMaterial = new THREE.PointsMaterial({color: 0x0000ff, size: 0.1}); + var powerupMaterial = new THREE.PointsMaterial({color: 0xff0000, size: 0.1}); - this.starsMesh = new THREE.Points(this.starsGeometry, starsMaterial); - - this.scene.instance.add(this.starsMesh); + this.noneMesh = new THREE.Points(this.noneGeometry, noneMaterial); + this.foodMesh = new THREE.Points(this.foodGeometry, foodMaterial); + this.powerupMesh = new THREE.Points(this.powerupGeometry, powerupMaterial); + this.snakeMesh = new THREE.Points(this.snakeGeometry, snakeMaterial); + + this.scene.instance.add(this.noneMesh); + this.scene.instance.add(this.foodMesh); + this.scene.instance.add(this.powerupMesh); + this.scene.instance.add(this.snakeMesh); }.bind(this) @@ -303,23 +361,23 @@ GameLib.CustomCode.prototype.createGameObject = function( case GameLib.CustomCode.POWERUP_SPEED : mesh = this.createGameMesh(this.materialPowerupSpeed); break; - case GameLib.CustomCode.POWERUP_LIFE : + case GameLib.CustomCode.POWERUP_LIFE : mesh = this.createGameMesh(this.materialPowerupLife); break; case GameLib.CustomCode.POWERUP_SLOW : mesh = this.createGameMesh(this.materialPowerupSlow); break; - default: + default: throw new Error('unhandled power up type : ' + type) } - /** + /** * We apply a scale to the powerupss too - since they appear too big when normal */ mesh.scale.x = 0.8; mesh.scale.y = 0.8; mesh.updateInstance('scale'); - + array = this.powerups; } @@ -458,7 +516,16 @@ GameLib.CustomCode.prototype.createGameObject = function( /** * Save a reference to this object to the specific object array */ - array.push(gameObject); + + if ( + currentGameObject.objectType === GameLib.CustomCode.OBJECT_TYPE_POWERUP || + currentGameObject.objectType === GameLib.CustomCode.OBJECT_TYPE_FOOD + ) { + /** + * Food and powerups automatically get added, snake bodys need careful insertion + */ + array.push(gameObject); + } /** * If we have too many objects - dispose of the first on in the queue (fifo) @@ -472,8 +539,8 @@ GameLib.CustomCode.prototype.createGameObject = function( gameObject = this.powerups.shift(); this.grid[gameObject.position.x][gameObject.position.y].dispose(); } - - return gameObject; + + return gameObject; }.bind(this); @@ -952,35 +1019,34 @@ GameLib.Event.Subscribe( powerup.dispose(); } ); - + this.state = { orientation : GameLib.CustomCode.ORIENTATION_UP }; this.initializeGrid(); + this.visualizeGrid(); - this.createGameObject( - GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY, - GameLib.CustomCode.BODY_TYPE_BREAD_HEAD, - { - x : 4, - y : 4 - }, - GameLib.CustomCode.ORIENTATION_UP - ); - - this.createGameObject( - GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY, - GameLib.CustomCode.BODY_TYPE_BREAD_TAIL, - { - x : 4, - y : 3 - }, - GameLib.CustomCode.ORIENTATION_UP - ); - - - //this.visualizeGrid(); + this.snake = [ + this.createGameObject( + GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY, + GameLib.CustomCode.BODY_TYPE_BREAD_HEAD, + { + x : 4, + y : 4 + }, + GameLib.CustomCode.ORIENTATION_UP + ), + this.createGameObject( + GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY, + GameLib.CustomCode.BODY_TYPE_BREAD_TAIL, + { + x : 4, + y : 3 + }, + GameLib.CustomCode.ORIENTATION_UP + ) + ] /** * Other Settings