r3-custom-code/o4c478xpx3.js

230 lines
5.4 KiB
JavaScript
Raw Normal View History

if (!this.entityLoaded) {
return;
}
if (!this.initialized) {
this.scene = this.entityLoaded.scene;
this.animation = this.entityLoaded.animation;
this.drawStatus = this.entityLoaded.drawStatus;
this.getNextBlock = this.entityLoaded.getNextBlock;
this.totalTime = 0;
this.waiting = false;
this.gameOver = false;
this.moveQueue = [];
if (this.grid instanceof Array) {
this.grid.map(
function(y) {
if (this.grid[y] instanceof Array) {
this.grid[y].map(
function(x){
if (this.grid[y][x].mesh) {
this.scene.removeObject(this.grid[y][x].mesh);
}
if (this.grid[y][x].baconDisappearing) {
this.scene.removeObject(this.grid[y][x].baconDisappearing);
}
}.bind(this)
)
}
}.bind(this)
)
}
/**
* Initialize the grid
*/
this.grid = [];
for (var y = 0; y < GameLib.CustomCode.TETRIS_GRID_HEIGHT; y++) {
this.grid[y] = [];
for (var x = 0; x < GameLib.CustomCode.TETRIS_GRID_WIDTH; x++) {
this.grid[y][x] = {
value: GameLib.CustomCode.TETRIS_GRID_NOT_TAKEN,
mesh: null
};
}
}
if (this.block && this.block.center) {
this.animation.removeMesh(this.block.center);
GameLib.Event.Emit(
GameLib.Event.REMOVE_COMPONENT,
{
component: this.block.center
}
);
}
if (this.block && this.block.meshes) {
this.block.meshes.map(
function (mesh) {
GameLib.Event.Emit(
GameLib.Event.REMOVE_COMPONENT,
{
component: mesh
}
);
}.bind(this)
);
}
this.drawStatus();
this.getNextBlock();
this.initialized = true;
}
if (this.gameOver) {
return;
}
var moved = true;
var fall = false;
if (this.waiting) {
fall = true;
} else {
this.totalTime += data.delta;
}
if (this.drop) {
fall = true;
if (this.moveQueue && this.moveQueue.length > 0) {
var tryMove = function () {
if (this.moveQueue.length > 0) {
var move = this.moveQueue[0];
if (this.moveBlock(this.block, move, 1, true)) {
this.moveQueue.splice(0, 1);
tryMove();
}
}
}.bind(this);
tryMove();
}
}
if (this.totalTime > this.entityLoaded.speed) {
this.totalTime = 0;
fall = true;
}
if (fall) {
moved = this.moveBlock(this.block, {down: true}, 1, true);
}
if (moved === null) {
this.waiting = true;
} else {
this.waiting = false;
}
if (moved === false) {
/**
* We reached the bottom -
*/
this.drop = false;
this.moveQueue = [];
var gridPositions = this.getBlockGridPositions(this.block);
gridPositions.map(
function (position) {
/**
* If there already is a mesh - remove it -
*/
if (this.grid[position.y][position.x].mesh) {
this.scene.removeObject(this.grid[position.y][position.x].mesh);
}
this.grid[position.y][position.x] = {
value: GameLib.CustomCode.TETRIS_GRID_TAKEN,
mesh: position.mesh
};
}.bind(this)
);
this.block.center.instance.position.x = this.block.center.position.x;
this.block.center.instance.position.y = this.block.center.position.y;
this.block.center.instance.position.z = this.block.center.position.z;
this.block.center.instance.rotation.x = this.block.center.rotation.x;
this.block.center.instance.rotation.y = this.block.center.rotation.y;
this.block.center.instance.rotation.z = this.block.center.rotation.z;
this.block.center.instance.updateMatrixWorld();
this.block.meshes.map(
function (mesh) {
mesh.setParentMesh(null);
//mesh.position.z = 0.1;
mesh.updateInstancePosition();
}.bind(this)
);
this.animation.removeMesh(this.block.center);
GameLib.Event.Emit(
GameLib.Event.REMOVE_COMPONENT,
{
component: this.block.center
}
);
this.removeLines();
//this.visualizeGrid(0xff0000);
for (var x = 0; x < GameLib.CustomCode.TETRIS_GRID_WIDTH; x++) {
if (
this.grid[19][x].value === GameLib.CustomCode.TETRIS_GRID_TAKEN ||
this.grid[20][x].value === GameLib.CustomCode.TETRIS_GRID_TAKEN ||
this.grid[21][x].value === GameLib.CustomCode.TETRIS_GRID_TAKEN ||
this.grid[22][x].value === GameLib.CustomCode.TETRIS_GRID_TAKEN ||
this.grid[23][x].value === GameLib.CustomCode.TETRIS_GRID_TAKEN
) {
this.gameOver = true;
}
}
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);
} else {
this.getNextBlock();
}
}
//@ sourceURL=beforeRender.js