Update: CC - Snake FS - Entity Loaded (21g30t1e75.js) 3399 bytes modified

beta.r3js.org
-=yb4f310 2018-03-22 12:20:14 +01:00
parent b5f55bc1f5
commit 3359bfdaad
1 changed files with 113 additions and 47 deletions

View File

@ -134,35 +134,93 @@ this.state = {
flip : 0 flip : 0
}; };
GameLib.CustomCode.prototype.visualizeGrid = function (color) { GameLib.CustomCode.prototype.visualizeGrid = function () {
if (this.starsMesh) { if (this.noneMesh) {
this.scene.instance.remove(this.starsMesh); 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( this.grid.map(
function(x, xIndex) { function(x, xIndex) {
x.map( x.map(
function(y, yIndex) { function(object, yIndex) {
this.starsGeometry.vertices.push(
new THREE.Vector3( if (object.objectType === GameLib.CustomCode.OBJECT_TYPE_NONE) {
(xIndex * GameLib.CustomCode.BODY_SCALE_X) + GameLib.CustomCode.GRID_OFFSET_X, this.noneGeometry.vertices.push(
(yIndex * GameLib.CustomCode.BODY_SCALE_Y) + GameLib.CustomCode.GRID_OFFSET_Y, new THREE.Vector3(
5 (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)
) )
}.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.noneMesh = new THREE.Points(this.noneGeometry, noneMaterial);
this.foodMesh = new THREE.Points(this.foodGeometry, foodMaterial);
this.scene.instance.add(this.starsMesh); 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) }.bind(this)
@ -303,23 +361,23 @@ GameLib.CustomCode.prototype.createGameObject = function(
case GameLib.CustomCode.POWERUP_SPEED : case GameLib.CustomCode.POWERUP_SPEED :
mesh = this.createGameMesh(this.materialPowerupSpeed); mesh = this.createGameMesh(this.materialPowerupSpeed);
break; break;
case GameLib.CustomCode.POWERUP_LIFE : case GameLib.CustomCode.POWERUP_LIFE :
mesh = this.createGameMesh(this.materialPowerupLife); mesh = this.createGameMesh(this.materialPowerupLife);
break; break;
case GameLib.CustomCode.POWERUP_SLOW : case GameLib.CustomCode.POWERUP_SLOW :
mesh = this.createGameMesh(this.materialPowerupSlow); mesh = this.createGameMesh(this.materialPowerupSlow);
break; break;
default: default:
throw new Error('unhandled power up type : ' + type) throw new Error('unhandled power up type : ' + type)
} }
/** /**
* We apply a scale to the powerupss too - since they appear too big when normal * We apply a scale to the powerupss too - since they appear too big when normal
*/ */
mesh.scale.x = 0.8; mesh.scale.x = 0.8;
mesh.scale.y = 0.8; mesh.scale.y = 0.8;
mesh.updateInstance('scale'); mesh.updateInstance('scale');
array = this.powerups; array = this.powerups;
} }
@ -458,7 +516,16 @@ GameLib.CustomCode.prototype.createGameObject = function(
/** /**
* Save a reference to this object to the specific object array * 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) * 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(); gameObject = this.powerups.shift();
this.grid[gameObject.position.x][gameObject.position.y].dispose(); this.grid[gameObject.position.x][gameObject.position.y].dispose();
} }
return gameObject; return gameObject;
}.bind(this); }.bind(this);
@ -952,35 +1019,34 @@ GameLib.Event.Subscribe(
powerup.dispose(); powerup.dispose();
} }
); );
this.state = { this.state = {
orientation : GameLib.CustomCode.ORIENTATION_UP orientation : GameLib.CustomCode.ORIENTATION_UP
}; };
this.initializeGrid(); this.initializeGrid();
this.visualizeGrid();
this.createGameObject( this.snake = [
GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY, this.createGameObject(
GameLib.CustomCode.BODY_TYPE_BREAD_HEAD, GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY,
{ GameLib.CustomCode.BODY_TYPE_BREAD_HEAD,
x : 4, {
y : 4 x : 4,
}, y : 4
GameLib.CustomCode.ORIENTATION_UP },
); GameLib.CustomCode.ORIENTATION_UP
),
this.createGameObject( this.createGameObject(
GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY, GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY,
GameLib.CustomCode.BODY_TYPE_BREAD_TAIL, GameLib.CustomCode.BODY_TYPE_BREAD_TAIL,
{ {
x : 4, x : 4,
y : 3 y : 3
}, },
GameLib.CustomCode.ORIENTATION_UP GameLib.CustomCode.ORIENTATION_UP
); )
]
//this.visualizeGrid();
/** /**
* Other Settings * Other Settings