Update: CC - Bacon - Entity Loaded (2xswm1bwq8.js) 147 bytes modified

beta.r3js.org
-=yb4f310 2018-02-18 12:34:02 +01:00
parent 3058e99089
commit 794c2a678b
1 changed files with 71 additions and 52 deletions

View File

@ -59,6 +59,10 @@ this.level = 1;
this.rows = 0;
this.speed = 1;
this.grid = [];
this.gameOver = false;
GameLib.CustomCode.TETRIS_BLOCK_I = 0;
GameLib.CustomCode.TETRIS_BLOCK_L = 1;
GameLib.CustomCode.TETRIS_BLOCK_L2 = 2;
@ -84,9 +88,9 @@ GameLib.CustomCode.prototype.visualizeGrid = function (color) {
this.starsGeometry = new THREE.Geometry();
for (var y = 0; y < this.ccBeforeRender.grid.length; y++) {
for (var x = 0; x < this.ccBeforeRender.grid[y].length; x++) {
if (this.ccBeforeRender.grid[y][x].value === GameLib.CustomCode.TETRIS_GRID_TAKEN) {
for (var y = 0; y < this.grid.length; y++) {
for (var x = 0; x < this.grid[y].length; x++) {
if (this.grid[y][x].value === GameLib.CustomCode.TETRIS_GRID_TAKEN) {
this.starsGeometry.vertices.push(
new THREE.Vector3(
x,
@ -120,10 +124,10 @@ GameLib.CustomCode.prototype.startAnimation = function (index) {
bacon.position.z = 3;
bacon.updateInstance('position');
this.ccBeforeRender.grid[index][x].baconDisappearing = bacon;
this.grid[index][x].baconDisappearing = bacon;
this.baconMaterials.push(
this.ccBeforeRender.grid[index][x].mesh.materials[0]
this.grid[index][x].mesh.materials[0]
);
}
@ -221,21 +225,21 @@ GameLib.CustomCode.prototype.startAnimation = function (index) {
*/
for (var x = 0; x < GameLib.CustomCode.TETRIS_GRID_WIDTH; x++) {
if (!this.ccBeforeRender.grid[__animationObject.rowNumber][x].mesh) {
if (!this.grid[__animationObject.rowNumber][x].mesh) {
throw new Error('mesh should exist but does not');
}
GameLib.Event.Emit(
GameLib.Event.REMOVE_COMPONENT,
{
component: this.ccBeforeRender.grid[__animationObject.rowNumber][x].mesh
component: this.grid[__animationObject.rowNumber][x].mesh
}
);
GameLib.Event.Emit(
GameLib.Event.REMOVE_COMPONENT,
{
component: this.ccBeforeRender.grid[__animationObject.rowNumber][x].baconDisappearing
component: this.grid[__animationObject.rowNumber][x].baconDisappearing
}
);
@ -244,7 +248,7 @@ GameLib.CustomCode.prototype.startAnimation = function (index) {
/**
* Add a new row to the top of the grid
*/
this.ccBeforeRender.grid.splice(__animationObject.rowNumber, 1);
this.grid.splice(__animationObject.rowNumber, 1);
var row = [];
@ -257,7 +261,7 @@ GameLib.CustomCode.prototype.startAnimation = function (index) {
)
}
this.ccBeforeRender.grid.push(row);
this.grid.push(row);
/**
* Now - drop all blocks above the current index down one row
@ -265,14 +269,14 @@ GameLib.CustomCode.prototype.startAnimation = function (index) {
for (var y = __animationObject.rowNumber; y < GameLib.CustomCode.TETRIS_GRID_HEIGHT - 1; y++) {
for (var x = 0; x < GameLib.CustomCode.TETRIS_GRID_WIDTH; x++) {
if (this.ccBeforeRender.grid[y][x].mesh) {
this.ccBeforeRender.grid[y][x].mesh.position.y = y;
this.ccBeforeRender.grid[y][x].mesh.updateInstance('position');
if (this.grid[y][x].mesh) {
this.grid[y][x].mesh.position.y = y;
this.grid[y][x].mesh.updateInstance('position');
}
if (this.ccBeforeRender.grid[y][x].baconDisappearing) {
this.ccBeforeRender.grid[y][x].baconDisappearing.position.y = y;
this.ccBeforeRender.grid[y][x].baconDisappearing.updateInstance('position');
if (this.grid[y][x].baconDisappearing) {
this.grid[y][x].baconDisappearing.position.y = y;
this.grid[y][x].baconDisappearing.updateInstance('position');
}
}
}
@ -329,8 +333,8 @@ GameLib.CustomCode.prototype.removeLines = function () {
for (var x = 0; x < GameLib.CustomCode.TETRIS_GRID_WIDTH; x++) {
if (
this.ccBeforeRender.grid[y][x].value === GameLib.CustomCode.TETRIS_GRID_NOT_TAKEN ||
this.ccBeforeRender.grid[y][x].value === GameLib.CustomCode.TETRIS_GRID_DISAPPEARING
this.grid[y][x].value === GameLib.CustomCode.TETRIS_GRID_NOT_TAKEN ||
this.grid[y][x].value === GameLib.CustomCode.TETRIS_GRID_DISAPPEARING
) {
line = false;
}
@ -440,7 +444,7 @@ GameLib.CustomCode.prototype.removeLines = function () {
indices.map(
function (index) {
for (var x = 0; x < GameLib.CustomCode.TETRIS_GRID_WIDTH; x++) {
this.ccBeforeRender.grid[index][x].value = GameLib.CustomCode.TETRIS_GRID_DISAPPEARING;
this.grid[index][x].value = GameLib.CustomCode.TETRIS_GRID_DISAPPEARING;
}
this.startAnimation(index);
}.bind(this)
@ -717,7 +721,7 @@ GameLib.CustomCode.prototype.checkBoundaries = function (block) {
* But we cannot be sure y is inside bounds
*/
if (position.y < GameLib.CustomCode.TETRIS_GRID_HEIGHT && position.y >= 0) {
if (this.ccBeforeRender.grid[position.y][position.x + 1].value === GameLib.CustomCode.TETRIS_GRID_TAKEN) {
if (this.grid[position.y][position.x + 1].value === GameLib.CustomCode.TETRIS_GRID_TAKEN) {
/**
* Ok - we found a block to the right - its over
*/
@ -740,7 +744,7 @@ GameLib.CustomCode.prototype.checkBoundaries = function (block) {
* But we cannot be sure y is inside bounds
*/
if (position.y < GameLib.CustomCode.TETRIS_GRID_HEIGHT && position.y >= 0) {
if (this.ccBeforeRender.grid[position.y][position.x - 1].value === GameLib.CustomCode.TETRIS_GRID_TAKEN) {
if (this.grid[position.y][position.x - 1].value === GameLib.CustomCode.TETRIS_GRID_TAKEN) {
/**
* We found a block to the left - its over also
*/
@ -761,7 +765,7 @@ GameLib.CustomCode.prototype.checkBoundaries = function (block) {
*/
if (position.x < GameLib.CustomCode.TETRIS_GRID_WIDTH && position.x >= 0) {
if (this.ccBeforeRender.grid[position.y + 1][position.x].value === GameLib.CustomCode.TETRIS_GRID_TAKEN) {
if (this.grid[position.y + 1][position.x].value === GameLib.CustomCode.TETRIS_GRID_TAKEN) {
/**
* Ok - this block is at the top
*/
@ -782,14 +786,14 @@ GameLib.CustomCode.prototype.checkBoundaries = function (block) {
* We cannot be sure, position.x is inside bounds
*/
if (position.x < GameLib.CustomCode.TETRIS_GRID_WIDTH && position.x >= 0) {
if (this.ccBeforeRender.grid[position.y - 1][position.x].value === GameLib.CustomCode.TETRIS_GRID_TAKEN) {
if (this.grid[position.y - 1][position.x].value === GameLib.CustomCode.TETRIS_GRID_TAKEN) {
/**
* Ok - this block hit bottom
*/
bottomHit = true;
}
if (this.ccBeforeRender.grid[position.y - 1][position.x].value === GameLib.CustomCode.TETRIS_GRID_DISAPPEARING) {
if (this.grid[position.y - 1][position.x].value === GameLib.CustomCode.TETRIS_GRID_DISAPPEARING) {
bottomDisappearing = true;
}
}
@ -803,7 +807,7 @@ GameLib.CustomCode.prototype.checkBoundaries = function (block) {
(position.x < GameLib.CustomCode.TETRIS_GRID_WIDTH && position.x >= 0) &&
(position.y < GameLib.CustomCode.TETRIS_GRID_HEIGHT && position.y >= 0)
) {
if (this.ccBeforeRender.grid[position.y][position.x].value === GameLib.CustomCode.TETRIS_GRID_TAKEN) {
if (this.grid[position.y][position.x].value === GameLib.CustomCode.TETRIS_GRID_TAKEN) {
collision = true;
}
}
@ -823,7 +827,7 @@ GameLib.CustomCode.prototype.checkBoundaries = function (block) {
GameLib.CustomCode.prototype.rotateBlock = function (clockwise) {
if (this.ccBeforeRender.gameOver) {
if (this.gameOver) {
return;
}
@ -864,6 +868,10 @@ GameLib.CustomCode.prototype.rotateBlock = function (clockwise) {
GameLib.CustomCode.prototype.moveBlock = function (block, direction, units, collisionCheck) {
if (GameLib.Utils.UndefinedOrNull(block)) {
block = this.block;
}
if (!units) {
units = 1;
}
@ -1309,9 +1317,9 @@ GameLib.CustomCode.prototype.getNextBlock = function () {
this.stopAnimations();
this.ccBeforeRender.block = this.spawnBigBlock();
this.block = this.spawnBigBlock();
this.block = this.ccBeforeRender.block;
this.block = this.block;
GameLib.Event.Emit(
GameLib.Event.COMPONENT_REGISTER,
@ -1382,6 +1390,8 @@ if (GameLib.Utils.UndefinedOrNull(this.subscriptions)) {
}
this.getNextBlock();
/**
* Set the game parameters
*/
@ -1396,10 +1406,13 @@ if (GameLib.Utils.UndefinedOrNull(this.subscriptions)) {
this.ccTouchStart.entityLoaded = this;
this.ccTouchEnd.entityLoaded = this;
/**
* Allow the game to proceed
* @type {boolean}
*/
this.gameOver = false;
this.getNextBlock();
console.log('custom game start complete');
console.log('game start complete');
}.bind(this)
)
@ -1414,6 +1427,7 @@ GameLib.CustomCode.prototype.bottomReached = function() {
var gridPositions = this.getBlockGridPositions(this.block);
gridPositions.map(
function (position) {
/**
* If there already is a mesh - remove it -
@ -1427,6 +1441,7 @@ GameLib.CustomCode.prototype.bottomReached = function() {
mesh: position.mesh
};
}.bind(this)
);
this.block.center.instance.position.x = this.block.center.position.x;
@ -1460,7 +1475,7 @@ GameLib.CustomCode.prototype.bottomReached = function() {
this.removeLines();
//this.visualizeGrid(0xff0000);
this.visualizeGrid(0xff0000);
for (var x = 0; x < GameLib.CustomCode.TETRIS_GRID_WIDTH; x++) {
if (
@ -1475,31 +1490,35 @@ GameLib.CustomCode.prototype.bottomReached = function() {
}
if (this.gameOver) {
this.block.meshes.map(
function (mesh) {
this.scene.removeObject(mesh);
}.bind(this)
);
delete this.block;
GameLib.Event.Emit(
GameLib.Event.GAME_OVER,
{
score: this.entityLoaded.score,
level: this.entityLoaded.level,
rows: this.entityLoaded.rows
}
);
//this.visualizeGrid(0xff0000);
this.endGame();
} else {
this.getNextBlock();
}
}.bind(this);
GameLib.CustomCode.prototype.endGame = function() {
this.block.meshes.map(
function (mesh) {
this.scene.removeObject(mesh);
}.bind(this)
);
delete this.block;
GameLib.Event.Emit(
GameLib.Event.GAME_OVER,
{
score: this.entityLoaded.score,
level: this.entityLoaded.level,
rows: this.entityLoaded.rows
}
);
}.bind(this)
/**
* Clears the grid
*/