2018-02-13 08:48:42 +01:00
|
|
|
if (data.entity === this.parentEntity) {
|
2018-02-13 10:35:25 +01:00
|
|
|
console.log('my entity loaded');
|
2018-02-13 08:48:42 +01:00
|
|
|
}
|
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
/**
|
|
|
|
* Custom Code Components
|
|
|
|
*/
|
|
|
|
this.ccBeforeRender = GameLib.EntityManager.Instance.findComponentById('o4c478xpx3');
|
|
|
|
this.ccKeyDown = GameLib.EntityManager.Instance.findComponentById('dgscoensvf');
|
|
|
|
this.ccTouchStart = GameLib.EntityManager.Instance.findComponentById('xjaoh824ff');
|
|
|
|
this.ccTouchEnd = GameLib.EntityManager.Instance.findComponentById('ohqwmnjfgf');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Images
|
|
|
|
*/
|
2018-02-14 11:35:18 +01:00
|
|
|
this.image_0 = GameLib.EntityManager.Instance.findComponentById('gvrik6h4yd');
|
|
|
|
this.image_1 = GameLib.EntityManager.Instance.findComponentById('ssdlmrpv9j');
|
|
|
|
this.image_2 = GameLib.EntityManager.Instance.findComponentById('zdg2qpu35c');
|
|
|
|
this.image_3 = GameLib.EntityManager.Instance.findComponentById('pjy42fg69n');
|
|
|
|
this.image_4 = GameLib.EntityManager.Instance.findComponentById('5t3olr10pw');
|
|
|
|
this.image_5 = GameLib.EntityManager.Instance.findComponentById('qrh0foyins');
|
|
|
|
this.image_6 = GameLib.EntityManager.Instance.findComponentById('0bjbsj4sss');
|
2018-02-18 21:16:10 +01:00
|
|
|
this.image_grid = GameLib.EntityManager.Instance.findComponentById('7yji3yzct9');
|
2018-02-14 15:12:30 +01:00
|
|
|
this.image_glow = GameLib.EntityManager.Instance.findComponentById('aei9gfkjb6');
|
2018-02-14 11:35:18 +01:00
|
|
|
this.image_flames_small = GameLib.EntityManager.Instance.findComponentById('vp4zkbx7yr');
|
2018-02-13 10:06:46 +01:00
|
|
|
|
2018-02-13 10:54:14 +01:00
|
|
|
/**
|
|
|
|
* Scene
|
|
|
|
*/
|
|
|
|
this.scene = GameLib.EntityManager.Instance.findComponentById('i1ac678a5d');
|
|
|
|
|
2018-02-13 15:13:56 +01:00
|
|
|
/**
|
|
|
|
* Materials
|
|
|
|
*/
|
2018-02-18 21:22:10 +01:00
|
|
|
this.gridMaterial = GameLib.EntityManager.Instance.findComponentById('1tp9zqjo6s');
|
2018-02-13 15:13:56 +01:00
|
|
|
|
2018-02-14 11:35:18 +01:00
|
|
|
/**
|
2018-02-14 15:12:30 +01:00
|
|
|
* Score
|
2018-02-14 11:35:18 +01:00
|
|
|
*/
|
2018-02-14 15:12:30 +01:00
|
|
|
this.scoreCanvas = GameLib.EntityManager.Instance.findComponentById('85d0jk6dhx');
|
|
|
|
this.scoreTexture = GameLib.EntityManager.Instance.findComponentById('kva68t4dm8');
|
2018-02-13 15:13:56 +01:00
|
|
|
|
2018-02-14 11:35:18 +01:00
|
|
|
/**
|
|
|
|
* Animations
|
|
|
|
*/
|
|
|
|
this.animation = GameLib.EntityManager.Instance.findComponentById('tnlwgvf12x');
|
2018-02-13 15:13:56 +01:00
|
|
|
|
2018-02-14 11:35:18 +01:00
|
|
|
/**
|
|
|
|
* Lights
|
|
|
|
*/
|
2018-02-18 13:02:34 +01:00
|
|
|
this.burnLight = GameLib.EntityManager.Instance.findComponentById('q13y5xwv6c');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Textures
|
|
|
|
*/
|
|
|
|
this.textureDisplacement = GameLib.EntityManager.Instance.findComponentById('b0du0xqeeu');
|
2018-02-13 14:58:12 +01:00
|
|
|
|
2018-02-14 10:59:27 +01:00
|
|
|
/**
|
|
|
|
* Initial Game Settings
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.score = 0;
|
|
|
|
this.level = 1;
|
|
|
|
this.rows = 0;
|
|
|
|
this.speed = 1;
|
|
|
|
|
2018-02-18 12:34:02 +01:00
|
|
|
this.grid = [];
|
|
|
|
|
|
|
|
this.gameOver = false;
|
|
|
|
|
2018-02-14 10:59:27 +01:00
|
|
|
GameLib.CustomCode.TETRIS_BLOCK_I = 0;
|
|
|
|
GameLib.CustomCode.TETRIS_BLOCK_L = 1;
|
|
|
|
GameLib.CustomCode.TETRIS_BLOCK_L2 = 2;
|
|
|
|
GameLib.CustomCode.TETRIS_BLOCK_Z = 3;
|
|
|
|
GameLib.CustomCode.TETRIS_BLOCK_Z2 = 4;
|
|
|
|
GameLib.CustomCode.TETRIS_BLOCK_T = 5;
|
|
|
|
GameLib.CustomCode.TETRIS_BLOCK_O = 6;
|
|
|
|
GameLib.CustomCode.TETRIS_BLOCK_MAX = 7;
|
|
|
|
|
|
|
|
GameLib.CustomCode.TETRIS_GRID_WIDTH = 11;
|
|
|
|
GameLib.CustomCode.TETRIS_GRID_HEIGHT = 24;
|
|
|
|
GameLib.CustomCode.TETRIS_GRID_HEADSPACE = 4;
|
|
|
|
|
|
|
|
GameLib.CustomCode.TETRIS_GRID_NOT_TAKEN = 0;
|
|
|
|
GameLib.CustomCode.TETRIS_GRID_TAKEN = 1;
|
|
|
|
GameLib.CustomCode.TETRIS_GRID_DISAPPEARING = 2;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
|
|
|
GameLib.CustomCode.prototype.visualizeGrid = function (color) {
|
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.starsMesh) {
|
|
|
|
this.scene.instance.remove(this.starsMesh);
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.starsGeometry = new THREE.Geometry();
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-18 12:34:02 +01:00
|
|
|
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) {
|
2018-02-13 10:35:25 +01:00
|
|
|
this.starsGeometry.vertices.push(
|
|
|
|
new THREE.Vector3(
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
5
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var starsMaterial = new THREE.PointsMaterial({color: color, size: 0.1});
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.starsMesh = new THREE.Points(this.starsGeometry, starsMaterial);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.scene.instance.add(this.starsMesh);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
|
|
|
}.bind(this)
|
|
|
|
|
|
|
|
GameLib.CustomCode.prototype.startAnimation = function (index) {
|
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
for (var x = 0; x < GameLib.CustomCode.TETRIS_GRID_WIDTH; x++) {
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-14 11:35:18 +01:00
|
|
|
var bacon = this.tile_bacon_disappearing.clone();
|
2018-02-14 15:44:15 +01:00
|
|
|
|
|
|
|
bacon.visible = true;
|
|
|
|
bacon.updateInstance('visible');
|
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
bacon.position.x = x;
|
|
|
|
bacon.position.y = index;
|
2018-02-18 18:40:07 +01:00
|
|
|
bacon.position.z = 2;
|
2018-02-13 10:35:25 +01:00
|
|
|
bacon.updateInstance('position');
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-18 12:34:02 +01:00
|
|
|
this.grid[index][x].baconDisappearing = bacon;
|
2018-02-18 18:46:39 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.baconMaterials.push(
|
2018-02-18 12:34:02 +01:00
|
|
|
this.grid[index][x].mesh.materials[0]
|
2018-02-13 10:35:25 +01:00
|
|
|
);
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-14 15:15:06 +01:00
|
|
|
var glow = this.tile_glow.clone();
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-14 15:44:15 +01:00
|
|
|
glow.visible = true;
|
|
|
|
glow.updateInstance('visible');
|
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
glow.position.y = index;
|
|
|
|
glow.position.x = (GameLib.CustomCode.TETRIS_GRID_WIDTH / 2) - 0.5;
|
2018-02-14 15:44:15 +01:00
|
|
|
glow.position.z = 4;
|
2018-02-13 10:35:25 +01:00
|
|
|
glow.updateInstance('position');
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-14 11:35:18 +01:00
|
|
|
var flames = this.tile_flames_small.clone();
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-14 15:44:15 +01:00
|
|
|
flames.visible = true;
|
|
|
|
flames.updateInstance('visible');
|
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
flames.position.y = index + 0.5;
|
|
|
|
flames.position.x = (GameLib.CustomCode.TETRIS_GRID_WIDTH / 2) - 0.5;
|
2018-02-14 15:44:15 +01:00
|
|
|
flames.position.z = 5;
|
2018-02-13 10:35:25 +01:00
|
|
|
flames.updateInstance('position');
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var animationObject = {
|
|
|
|
rowNumber: index,
|
|
|
|
glow: glow,
|
|
|
|
flames: flames,
|
|
|
|
flamesArray: [],
|
|
|
|
time: 0
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
animationObject.subscription = GameLib.Event.Subscribe(
|
|
|
|
GameLib.Event.BEFORE_RENDER,
|
|
|
|
function (__animationObject) {
|
|
|
|
return function (data) {
|
|
|
|
__animationObject.time += data.delta * 4;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-18 20:44:08 +01:00
|
|
|
this.burnLight.intensity = 2 * Math.sin(__animationObject.time);
|
2018-02-13 10:35:25 +01:00
|
|
|
this.burnLight.updateInstance('intensity');
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-14 15:15:06 +01:00
|
|
|
this.tile_glowMaterial.opacity = Math.sin(__animationObject.time);
|
|
|
|
this.tile_glowMaterial.updateInstance('opacity');
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (__animationObject.time < (Math.PI / 2)) {
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.baconMaterials.map(
|
|
|
|
function (material) {
|
|
|
|
material.displacementScale = -1 * Math.sin(__animationObject.time);
|
|
|
|
material.updateInstance('displacementScale');
|
|
|
|
}
|
|
|
|
);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (__animationObject.time > Math.PI) {
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
__animationObject.subscription.remove();
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
/**
|
|
|
|
* Done with animation
|
|
|
|
*/
|
|
|
|
this.burnLight.intensity = 0;
|
|
|
|
this.burnLight.updateInstance('intensity');
|
|
|
|
|
|
|
|
GameLib.Event.Emit(
|
|
|
|
GameLib.Event.REMOVE_COMPONENT,
|
|
|
|
{
|
|
|
|
component: __animationObject.glow
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
GameLib.Event.Emit(
|
|
|
|
GameLib.Event.REMOVE_COMPONENT,
|
|
|
|
{
|
|
|
|
component: __animationObject.flames
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove any additional flames
|
|
|
|
*/
|
|
|
|
__animationObject.flamesArray.map(
|
|
|
|
function (flame) {
|
|
|
|
GameLib.Event.Emit(
|
|
|
|
GameLib.Event.REMOVE_COMPONENT,
|
|
|
|
{
|
|
|
|
component: flame
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the meshes and the baconDisappearing objects
|
|
|
|
*/
|
|
|
|
for (var x = 0; x < GameLib.CustomCode.TETRIS_GRID_WIDTH; x++) {
|
|
|
|
|
2018-02-18 12:34:02 +01:00
|
|
|
if (!this.grid[__animationObject.rowNumber][x].mesh) {
|
2018-02-13 10:35:25 +01:00
|
|
|
throw new Error('mesh should exist but does not');
|
|
|
|
}
|
|
|
|
|
|
|
|
GameLib.Event.Emit(
|
|
|
|
GameLib.Event.REMOVE_COMPONENT,
|
|
|
|
{
|
2018-02-18 12:34:02 +01:00
|
|
|
component: this.grid[__animationObject.rowNumber][x].mesh
|
2018-02-13 10:35:25 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
GameLib.Event.Emit(
|
|
|
|
GameLib.Event.REMOVE_COMPONENT,
|
|
|
|
{
|
2018-02-18 12:34:02 +01:00
|
|
|
component: this.grid[__animationObject.rowNumber][x].baconDisappearing
|
2018-02-13 10:35:25 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new row to the top of the grid
|
|
|
|
*/
|
2018-02-18 12:34:02 +01:00
|
|
|
this.grid.splice(__animationObject.rowNumber, 1);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var row = [];
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
for (var x = 0; x < GameLib.CustomCode.TETRIS_GRID_WIDTH; x++) {
|
|
|
|
row.push(
|
|
|
|
{
|
|
|
|
value: GameLib.CustomCode.TETRIS_GRID_NOT_TAKEN,
|
|
|
|
mesh: null
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-18 12:34:02 +01:00
|
|
|
this.grid.push(row);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
/**
|
|
|
|
* Now - drop all blocks above the current index down one row
|
|
|
|
*/
|
|
|
|
for (var y = __animationObject.rowNumber; y < GameLib.CustomCode.TETRIS_GRID_HEIGHT - 1; y++) {
|
|
|
|
for (var x = 0; x < GameLib.CustomCode.TETRIS_GRID_WIDTH; x++) {
|
|
|
|
|
2018-02-18 12:34:02 +01:00
|
|
|
if (this.grid[y][x].mesh) {
|
|
|
|
this.grid[y][x].mesh.position.y = y;
|
|
|
|
this.grid[y][x].mesh.updateInstance('position');
|
2018-02-13 10:35:25 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 12:34:02 +01:00
|
|
|
if (this.grid[y][x].baconDisappearing) {
|
|
|
|
this.grid[y][x].baconDisappearing.position.y = y;
|
|
|
|
this.grid[y][x].baconDisappearing.updateInstance('position');
|
2018-02-13 10:35:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find any animation objects with a line number greater than our current line number, decrease it
|
|
|
|
*/
|
|
|
|
this.animationObjects.map(
|
|
|
|
function (animationObject) {
|
|
|
|
if (animationObject.rowNumber > __animationObject.rowNumber) {
|
|
|
|
animationObject.rowNumber -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var objectIndex = this.animationObjects.indexOf(__animationObject);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove ourselves from the animation objects
|
|
|
|
*/
|
|
|
|
this.animationObjects.splice(objectIndex, 1);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
/**
|
|
|
|
* Restore our bacon materials if we have no more animation objects
|
|
|
|
*/
|
|
|
|
if (this.animationObjects.length === 0) {
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.baconMaterials.map(
|
|
|
|
function (material) {
|
|
|
|
material.displacementScale = 0;
|
|
|
|
material.updateInstance('displacementScale');
|
|
|
|
}
|
|
|
|
);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.baconMaterials = [];
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
}.bind(this)
|
|
|
|
}.bind(this)(animationObject)
|
|
|
|
);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.animationObjects.push(animationObject);
|
2018-02-13 08:48:42 +01:00
|
|
|
}.bind(this)
|
|
|
|
|
|
|
|
GameLib.CustomCode.prototype.removeLines = function () {
|
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var indices = [];
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
for (var y = 0; y < GameLib.CustomCode.TETRIS_GRID_HEIGHT; y++) {
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var line = true;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
for (var x = 0; x < GameLib.CustomCode.TETRIS_GRID_WIDTH; x++) {
|
|
|
|
if (
|
2018-02-18 12:34:02 +01:00
|
|
|
this.grid[y][x].value === GameLib.CustomCode.TETRIS_GRID_NOT_TAKEN ||
|
|
|
|
this.grid[y][x].value === GameLib.CustomCode.TETRIS_GRID_DISAPPEARING
|
2018-02-13 10:35:25 +01:00
|
|
|
) {
|
|
|
|
line = false;
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (line) {
|
|
|
|
indices.push(y);
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var multiplier = 1;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (indices.length === 4) {
|
|
|
|
multiplier = 10000;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (indices.length === 3) {
|
|
|
|
multiplier = 1000;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (indices.length === 2) {
|
|
|
|
multiplier = 100;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (indices.length === 1) {
|
|
|
|
multiplier = 10;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.score += (multiplier * this.level);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.rows += indices.length;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.level = Math.floor(this.rows / 5) + 1;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.level === 1) {
|
|
|
|
this.speed = 1;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.level === 2) {
|
|
|
|
this.speed = 0.9;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.level === 3) {
|
|
|
|
this.speed = 0.85;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.level === 4) {
|
|
|
|
this.speed = 0.8;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.level === 5) {
|
|
|
|
this.speed = 0.75;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.level === 6) {
|
|
|
|
this.speed = 0.7;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.level === 7) {
|
|
|
|
this.speed = 0.65;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.level === 8) {
|
|
|
|
this.speed = 0.6;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.level === 9) {
|
|
|
|
this.speed = 0.55;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.level === 10) {
|
|
|
|
this.speed = 0.5;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.level === 11) {
|
|
|
|
this.speed = 0.45;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.level === 12) {
|
|
|
|
this.speed = 0.4;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.level === 13) {
|
|
|
|
this.speed = 0.35;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.level === 14) {
|
|
|
|
this.speed = 0.3;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.level === 15) {
|
|
|
|
this.speed = 0.25;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.level === 16) {
|
|
|
|
this.speed = 0.2;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (this.level >= 17) {
|
|
|
|
this.speed = 0.18;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.drawStatus();
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.animationsDone = [];
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
indices.map(
|
|
|
|
function (index) {
|
|
|
|
for (var x = 0; x < GameLib.CustomCode.TETRIS_GRID_WIDTH; x++) {
|
2018-02-18 12:34:02 +01:00
|
|
|
this.grid[index][x].value = GameLib.CustomCode.TETRIS_GRID_DISAPPEARING;
|
2018-02-13 10:35:25 +01:00
|
|
|
}
|
|
|
|
this.startAnimation(index);
|
|
|
|
}.bind(this)
|
|
|
|
);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
|
|
|
|
GameLib.CustomCode.prototype.drawStatus = function () {
|
2018-02-14 15:12:30 +01:00
|
|
|
var context = this.scoreCanvas.instance.getContext('2d');
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
context.textBaseline = "middle";
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-14 15:12:30 +01:00
|
|
|
context.clearRect(0, 0, this.scoreCanvas.width, this.scoreCanvas.height);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
context.fillStyle = "rgba(58, 36, 25, 0.7)";
|
|
|
|
context.fillRect(12, 30, 485, 178);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
context.lineWidth = 8;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-14 11:35:18 +01:00
|
|
|
context.strokeStyle = '#eeb999';
|
2018-02-13 10:35:25 +01:00
|
|
|
context.strokeRect(12, 30, 485, 178);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
context.beginPath();
|
|
|
|
context.moveTo(11, 68);
|
|
|
|
context.lineTo(498, 68);
|
|
|
|
context.stroke();
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
context.beginPath();
|
|
|
|
context.moveTo(172, 30);
|
|
|
|
context.lineTo(172, 208);
|
|
|
|
context.stroke();
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
context.beginPath();
|
|
|
|
context.moveTo(172, 135);
|
|
|
|
context.lineTo(498, 135);
|
|
|
|
context.stroke();
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
context.beginPath();
|
|
|
|
context.moveTo(172, 172);
|
|
|
|
context.lineTo(498, 172);
|
|
|
|
context.stroke();
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
/**
|
|
|
|
* Write text
|
|
|
|
*/
|
2018-02-14 11:35:18 +01:00
|
|
|
context.fillStyle = '#eeb999';
|
2018-02-13 10:35:25 +01:00
|
|
|
context.font = '20pt BkBold';
|
|
|
|
context.fillText('NEXT TILES', 23, 52);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
context.fillText('SCORE', 182, 52);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
context.fillText('LEVEL', 182, 156);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
context.fillText('ROWS', 182, 193);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-14 11:35:18 +01:00
|
|
|
context.fillStyle = '#eeb999';
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
context.font = '50pt BkBold';
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var str = this.score.toString();
|
|
|
|
var pad = "0000000";
|
|
|
|
var ans = pad.substring(0, pad.length - str.length) + str;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
context.fillText(ans, 225, 110);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
str = this.level.toString();
|
|
|
|
pad = "000";
|
|
|
|
ans = pad.substring(0, pad.length - str.length) + str;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
context.font = '20pt BkBold';
|
|
|
|
context.fillText(ans, 435, 156);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
str = this.rows.toString();
|
|
|
|
pad = "000";
|
|
|
|
ans = pad.substring(0, pad.length - str.length) + str;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
context.font = '20pt BkBold';
|
|
|
|
context.fillText(ans, 435, 193);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.scoreTexture.instance.needsUpdate = true;
|
2018-02-13 08:48:42 +01:00
|
|
|
}.bind(this)
|
|
|
|
|
|
|
|
GameLib.CustomCode.prototype.getBlockGridPositions = function (block) {
|
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var object = new THREE.Mesh();
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
object.position.x = block.center.position.x;
|
|
|
|
object.position.y = block.center.position.y;
|
|
|
|
object.position.z = block.center.position.z;
|
|
|
|
object.rotation.x = block.center.rotation.x;
|
|
|
|
object.rotation.y = block.center.rotation.y;
|
|
|
|
object.rotation.z = block.center.rotation.z;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var children = block.meshes.map(
|
|
|
|
function (mesh) {
|
|
|
|
var child = new THREE.Mesh();
|
|
|
|
child.position.x = mesh.position.x;
|
|
|
|
child.position.y = mesh.position.y;
|
|
|
|
child.position.z = mesh.position.z;
|
|
|
|
object.add(child);
|
|
|
|
child.mesh = mesh;
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
object.updateMatrixWorld();
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var positions = children.map(
|
|
|
|
function (child) {
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var vector = new THREE.Vector3();
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
vector.setFromMatrixPosition(child.matrixWorld);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var x = vector.x;
|
|
|
|
var y = vector.y;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
x = Math.round(x);
|
|
|
|
y = Math.round(y);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
return {
|
|
|
|
x: x,
|
|
|
|
y: y,
|
|
|
|
mesh: child.mesh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
return positions;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
|
|
|
}.bind(this);
|
2018-02-18 17:42:00 +01:00
|
|
|
|
2018-02-13 08:48:42 +01:00
|
|
|
GameLib.CustomCode.prototype.drawGrid = function () {
|
2018-02-18 21:22:10 +01:00
|
|
|
|
|
|
|
var geometry = new THREE.PlaneBufferGeometry();
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-18 21:22:10 +01:00
|
|
|
var mesh = new THREE.Mesh(
|
|
|
|
geometry,
|
|
|
|
this.gridMaterial.instance
|
|
|
|
);
|
2018-02-13 10:35:25 +01:00
|
|
|
|
|
|
|
for (var y = 0; y < (GameLib.CustomCode.TETRIS_GRID_HEIGHT - GameLib.CustomCode.TETRIS_GRID_HEADSPACE); y++) {
|
|
|
|
for (var x = 0; x < GameLib.CustomCode.TETRIS_GRID_WIDTH; x++) {
|
2018-02-18 21:22:10 +01:00
|
|
|
|
|
|
|
var clone = mesh.clone();
|
|
|
|
|
|
|
|
clone.position.x = x;
|
|
|
|
clone.position.y = y;
|
|
|
|
clone.position.z = 2;
|
|
|
|
|
|
|
|
this.scene.instance.add(clone);
|
2018-02-13 10:35:25 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-18 21:22:10 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
geometry = new THREE.Geometry();
|
|
|
|
|
|
|
|
geometry.vertices.push(
|
|
|
|
new THREE.Vector3(
|
|
|
|
-0.5,
|
|
|
|
GameLib.CustomCode.TETRIS_GRID_HEIGHT - GameLib.CustomCode.TETRIS_GRID_HEADSPACE - 0.5,
|
|
|
|
5
|
|
|
|
),
|
|
|
|
new THREE.Vector3(
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
5
|
|
|
|
),
|
|
|
|
new THREE.Vector3(
|
|
|
|
GameLib.CustomCode.TETRIS_GRID_WIDTH - 0.5,
|
|
|
|
-0.5,
|
|
|
|
5
|
|
|
|
),
|
|
|
|
new THREE.Vector3(
|
|
|
|
GameLib.CustomCode.TETRIS_GRID_WIDTH - 0.5,
|
|
|
|
GameLib.CustomCode.TETRIS_GRID_HEIGHT - GameLib.CustomCode.TETRIS_GRID_HEADSPACE - 0.5,
|
|
|
|
5
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
this.scene.instance.add(
|
|
|
|
new THREE.Line(
|
|
|
|
geometry,
|
|
|
|
new THREE.LineBasicMaterial(
|
|
|
|
{
|
|
|
|
linewidth: 3,
|
|
|
|
color: 0x612d1a
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
}.bind(this)
|
|
|
|
|
|
|
|
GameLib.CustomCode.prototype.checkBoundaries = function (block) {
|
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
/**
|
|
|
|
* collisions are true only for out of bounds indexes, and if the current block is taken
|
|
|
|
*/
|
|
|
|
var gridPositions = this.getBlockGridPositions(block);
|
|
|
|
|
|
|
|
var leftHit = false;
|
|
|
|
var rightHit = false;
|
|
|
|
var bottomHit = false;
|
|
|
|
var topHit = false;
|
|
|
|
var collision = false;
|
|
|
|
var bottomDisappearing = false;
|
|
|
|
|
|
|
|
gridPositions.map(
|
|
|
|
function (position) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check grid boundaries
|
|
|
|
*/
|
|
|
|
if (position.x === 0) {
|
|
|
|
leftHit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (position.x === (GameLib.CustomCode.TETRIS_GRID_WIDTH - 1)) {
|
|
|
|
rightHit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (position.y === 0) {
|
|
|
|
bottomHit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (position.y === (GameLib.CustomCode.TETRIS_GRID_HEIGHT - 1)) {
|
|
|
|
topHit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check outside of bounds
|
|
|
|
*/
|
|
|
|
if (position.x < 0) {
|
|
|
|
collision = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (position.x >= GameLib.CustomCode.TETRIS_GRID_WIDTH) {
|
|
|
|
collision = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (position.y < 0) {
|
|
|
|
collision = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (position.y >= GameLib.CustomCode.TETRIS_GRID_HEIGHT) {
|
|
|
|
collision = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We want to see if the block to the right is taken, but we cannot
|
|
|
|
* be sure x+1 is inside bounds
|
|
|
|
*/
|
|
|
|
if (
|
|
|
|
((position.x + 1) < GameLib.CustomCode.TETRIS_GRID_WIDTH) &&
|
|
|
|
((position.x + 1) >= 0)
|
|
|
|
) {
|
|
|
|
/**
|
|
|
|
* But we cannot be sure y is inside bounds
|
2018-02-13 10:06:46 +01:00
|
|
|
*/
|
2018-02-13 10:35:25 +01:00
|
|
|
if (position.y < GameLib.CustomCode.TETRIS_GRID_HEIGHT && position.y >= 0) {
|
2018-02-18 12:34:02 +01:00
|
|
|
if (this.grid[position.y][position.x + 1].value === GameLib.CustomCode.TETRIS_GRID_TAKEN) {
|
2018-02-13 10:35:25 +01:00
|
|
|
/**
|
|
|
|
* Ok - we found a block to the right - its over
|
|
|
|
*/
|
|
|
|
rightHit = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We want to see if the block to the left is taken, but we cannot be sure x - 1 is inside bounds
|
|
|
|
*/
|
|
|
|
if (
|
|
|
|
((position.x) > 0) &&
|
|
|
|
((position.x - 1) >= 0) &&
|
|
|
|
((position.x - 1) < GameLib.CustomCode.TETRIS_GRID_WIDTH)
|
|
|
|
) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* But we cannot be sure y is inside bounds
|
2018-02-13 10:06:46 +01:00
|
|
|
*/
|
2018-02-13 10:35:25 +01:00
|
|
|
if (position.y < GameLib.CustomCode.TETRIS_GRID_HEIGHT && position.y >= 0) {
|
2018-02-18 12:34:02 +01:00
|
|
|
if (this.grid[position.y][position.x - 1].value === GameLib.CustomCode.TETRIS_GRID_TAKEN) {
|
2018-02-13 10:35:25 +01:00
|
|
|
/**
|
|
|
|
* We found a block to the left - its over also
|
|
|
|
*/
|
|
|
|
leftHit = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Now we want to see if the block above is taken, we cannot be sure y+1 is inside bounds
|
|
|
|
*/
|
|
|
|
if (
|
|
|
|
((position.y + 1) < GameLib.CustomCode.TETRIS_GRID_HEIGHT) &&
|
|
|
|
((position.y + 1) >= 0)
|
|
|
|
) {
|
|
|
|
/**
|
|
|
|
* We cannot be sure, position.x is inside bounds
|
2018-02-13 10:06:46 +01:00
|
|
|
*/
|
2018-02-13 10:35:25 +01:00
|
|
|
if (position.x < GameLib.CustomCode.TETRIS_GRID_WIDTH && position.x >= 0) {
|
|
|
|
|
2018-02-18 12:34:02 +01:00
|
|
|
if (this.grid[position.y + 1][position.x].value === GameLib.CustomCode.TETRIS_GRID_TAKEN) {
|
2018-02-13 10:35:25 +01:00
|
|
|
/**
|
|
|
|
* Ok - this block is at the top
|
|
|
|
*/
|
|
|
|
topHit = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Now we want to see if the block below is taken, we cannot be sure y-1 is inside bounds
|
|
|
|
*/
|
|
|
|
if (
|
|
|
|
((position.y) > 0) &&
|
|
|
|
((position.y - 1) >= 0) &&
|
|
|
|
((position.y - 1) < GameLib.CustomCode.TETRIS_GRID_HEIGHT)
|
|
|
|
) {
|
|
|
|
/**
|
|
|
|
* We cannot be sure, position.x is inside bounds
|
2018-02-13 10:06:46 +01:00
|
|
|
*/
|
2018-02-13 10:35:25 +01:00
|
|
|
if (position.x < GameLib.CustomCode.TETRIS_GRID_WIDTH && position.x >= 0) {
|
2018-02-18 12:34:02 +01:00
|
|
|
if (this.grid[position.y - 1][position.x].value === GameLib.CustomCode.TETRIS_GRID_TAKEN) {
|
2018-02-13 10:35:25 +01:00
|
|
|
/**
|
|
|
|
* Ok - this block hit bottom
|
|
|
|
*/
|
|
|
|
bottomHit = true;
|
|
|
|
}
|
|
|
|
|
2018-02-18 12:34:02 +01:00
|
|
|
if (this.grid[position.y - 1][position.x].value === GameLib.CustomCode.TETRIS_GRID_DISAPPEARING) {
|
2018-02-13 10:35:25 +01:00
|
|
|
bottomDisappearing = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Now we want to see if the block is inside another block,
|
|
|
|
* we cannot be sure, x and y is inside bounds
|
|
|
|
*/
|
|
|
|
if (
|
|
|
|
(position.x < GameLib.CustomCode.TETRIS_GRID_WIDTH && position.x >= 0) &&
|
|
|
|
(position.y < GameLib.CustomCode.TETRIS_GRID_HEIGHT && position.y >= 0)
|
|
|
|
) {
|
2018-02-18 12:34:02 +01:00
|
|
|
if (this.grid[position.y][position.x].value === GameLib.CustomCode.TETRIS_GRID_TAKEN) {
|
2018-02-13 10:35:25 +01:00
|
|
|
collision = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
left: leftHit,
|
|
|
|
top: topHit,
|
|
|
|
right: rightHit,
|
|
|
|
bottom: bottomHit,
|
|
|
|
bottomDisappearing: bottomDisappearing,
|
|
|
|
collision: collision
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
|
|
|
}.bind(this)
|
|
|
|
|
|
|
|
GameLib.CustomCode.prototype.rotateBlock = function (clockwise) {
|
|
|
|
|
2018-02-18 12:34:02 +01:00
|
|
|
if (this.gameOver) {
|
2018-02-13 10:35:25 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (clockwise) {
|
|
|
|
var targetRotation = this.block.center.rotation.z - Math.PI / 2;
|
|
|
|
} else {
|
|
|
|
var targetRotation = this.block.center.rotation.z + Math.PI / 2;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-18 17:54:19 +01:00
|
|
|
var rotationBackup = {
|
|
|
|
instance : this.block.center.instance.rotation.z,
|
|
|
|
object : this.block.center.rotation.z
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.block.center.rotation.z = targetRotation;
|
|
|
|
this.block.center.updateInstance('rotation');
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var boundaries = this.checkBoundaries(this.block);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (boundaries.collision) {
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-18 17:54:19 +01:00
|
|
|
/**
|
|
|
|
* There's a collision, restore the instance rotation
|
|
|
|
*/
|
|
|
|
this.block.center.instance.rotation.z = rotationBackup.instance;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-18 17:54:19 +01:00
|
|
|
/**
|
|
|
|
* And also the object rotation
|
|
|
|
*/
|
|
|
|
this.block.center.rotation.z = rotationBackup.object;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
} else {
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-18 17:54:19 +01:00
|
|
|
/**
|
|
|
|
* Restore our instance rotation
|
|
|
|
*/
|
|
|
|
this.block.center.instance.rotation.z = rotationBackup.instance;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-18 17:54:19 +01:00
|
|
|
/**
|
|
|
|
* Update our object to the new rotation
|
|
|
|
*/
|
2018-02-13 10:35:25 +01:00
|
|
|
this.block.center.rotation.z = targetRotation;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
GameLib.CustomCode.prototype.moveBlock = function (block, direction, units, collisionCheck) {
|
|
|
|
|
2018-02-18 12:34:02 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(block)) {
|
|
|
|
block = this.block;
|
|
|
|
}
|
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (!units) {
|
|
|
|
units = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
var boundaries = null;
|
|
|
|
var moved = false;
|
|
|
|
|
|
|
|
if (collisionCheck) {
|
|
|
|
boundaries = this.checkBoundaries(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (boundaries && boundaries.collision) {
|
|
|
|
return moved;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direction.left) {
|
|
|
|
|
|
|
|
if (boundaries && boundaries.left) {
|
|
|
|
//Do nothing
|
|
|
|
} else {
|
|
|
|
block.center.position.x -= units;
|
|
|
|
moved = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direction.right) {
|
|
|
|
|
|
|
|
if (boundaries && boundaries.right) {
|
|
|
|
//Do nothing
|
|
|
|
} else {
|
|
|
|
block.center.position.x += units;
|
|
|
|
moved = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direction.down) {
|
|
|
|
|
|
|
|
if (boundaries && boundaries.bottom) {
|
|
|
|
//Do nothing
|
|
|
|
} else if (boundaries && boundaries.bottomDisappearing) {
|
|
|
|
/**
|
|
|
|
* Set moved to 'null' instead of 'false' - null indicates to called we should wait - while still being 'false'
|
|
|
|
*/
|
|
|
|
moved = null;
|
|
|
|
} else {
|
|
|
|
block.center.position.y -= units;
|
|
|
|
moved = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direction.up) {
|
|
|
|
|
|
|
|
if (boundaries && boundaries.top) {
|
|
|
|
//Do nothing
|
|
|
|
} else {
|
|
|
|
block.center.position.y += units;
|
|
|
|
moved = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return moved;
|
2018-02-13 08:48:42 +01:00
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
GameLib.CustomCode.prototype.spawnBlock = function () {
|
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var blockType = Math.floor(Math.random() * 7);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var mesh0 = null;
|
|
|
|
var mesh1 = null;
|
|
|
|
var mesh2 = null;
|
|
|
|
var mesh3 = null;
|
|
|
|
|
|
|
|
var offsetX = 0;
|
|
|
|
var offsetY = 0;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var tinyPositionX = 0;
|
|
|
|
var tinyPositionY = 0;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var nextPositionX = 0;
|
|
|
|
var nextPositionY = 0;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:54:14 +01:00
|
|
|
var center = this.tile_center.clone();
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-14 15:45:32 +01:00
|
|
|
center.visible = true;
|
|
|
|
center.updateInstance('visible');
|
2018-02-14 15:46:54 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
center.position.y = 0;
|
|
|
|
center.position.x = 0;
|
2018-02-14 15:44:15 +01:00
|
|
|
center.position.z = 3.5;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (blockType === GameLib.CustomCode.TETRIS_BLOCK_I) {
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
mesh0 = this.tile_4.clone();
|
|
|
|
mesh0.position.setFrom(center.position);
|
|
|
|
mesh0.position.z = -0.1;
|
|
|
|
mesh0.position.x -= 1;
|
|
|
|
|
|
|
|
mesh1 = this.tile_5.clone();
|
|
|
|
mesh1.position.setFrom(center.position);
|
|
|
|
mesh1.position.z = -0.1;
|
|
|
|
mesh1.position.x -= 2;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
mesh2 = this.tile_6.clone();
|
|
|
|
mesh2.position.setFrom(center.position);
|
|
|
|
mesh2.position.z = -0.1;
|
|
|
|
mesh2.position.x += 0;
|
|
|
|
|
|
|
|
mesh3 = this.tile_2.clone();
|
|
|
|
mesh3.position.setFrom(center.position);
|
|
|
|
mesh3.position.z = -0.1;
|
|
|
|
mesh3.position.x += 1;
|
|
|
|
|
|
|
|
offsetX = 0.5;
|
|
|
|
offsetY = 1;
|
|
|
|
|
|
|
|
tinyPositionX = 1.32;
|
|
|
|
tinyPositionY = 16.7;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
nextPositionX = 2.12;
|
|
|
|
nextPositionY = 15.5;
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (blockType === GameLib.CustomCode.TETRIS_BLOCK_L) {
|
|
|
|
mesh0 = this.tile_6.clone();
|
|
|
|
mesh0.position.setFrom(center.position);
|
|
|
|
mesh0.position.z = -0.1;
|
|
|
|
mesh0.position.x += 1;
|
|
|
|
mesh0.position.y += 1;
|
|
|
|
|
|
|
|
mesh1 = this.tile_4.clone();
|
|
|
|
mesh1.position.setFrom(center.position);
|
|
|
|
mesh1.position.z = -0.1;
|
|
|
|
mesh1.position.x += 1;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
mesh2 = this.tile_6.clone();
|
|
|
|
mesh2.position.setFrom(center.position);
|
|
|
|
mesh2.position.z = -0.1;
|
|
|
|
mesh2.position.x += 0;
|
|
|
|
|
|
|
|
mesh3 = this.tile_2.clone();
|
|
|
|
mesh3.position.setFrom(center.position);
|
|
|
|
mesh3.position.z = -0.1;
|
|
|
|
mesh3.position.x -= 1;
|
|
|
|
|
|
|
|
offsetX = 0.5;
|
|
|
|
offsetY = 2;
|
|
|
|
|
|
|
|
tinyPositionX = 1.00;
|
|
|
|
tinyPositionY = 16.65;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
nextPositionX = 1.78;
|
|
|
|
nextPositionY = 15.3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blockType === GameLib.CustomCode.TETRIS_BLOCK_L2) {
|
|
|
|
mesh0 = this.tile_6.clone();
|
|
|
|
mesh0.position.setFrom(center.position);
|
|
|
|
mesh0.position.z = -0.1;
|
|
|
|
mesh0.position.x -= 1;
|
|
|
|
mesh0.position.y += 1;
|
|
|
|
|
|
|
|
mesh1 = this.tile_4.clone();
|
|
|
|
mesh1.position.setFrom(center.position);
|
|
|
|
mesh1.position.z = -0.1;
|
|
|
|
mesh1.position.x += 1;
|
|
|
|
|
|
|
|
mesh2 = this.tile_6.clone();
|
|
|
|
mesh2.position.setFrom(center.position);
|
|
|
|
mesh2.position.z = -0.1;
|
|
|
|
mesh2.position.x += 0;
|
|
|
|
|
|
|
|
mesh3 = this.tile_2.clone();
|
|
|
|
mesh3.position.setFrom(center.position);
|
|
|
|
mesh3.position.z = -0.1;
|
|
|
|
mesh3.position.x -= 1;
|
|
|
|
|
|
|
|
offsetX = 0.5;
|
|
|
|
offsetY = 2;
|
|
|
|
|
|
|
|
tinyPositionX = 1.00;
|
|
|
|
tinyPositionY = 16.65;
|
|
|
|
|
|
|
|
nextPositionX = 1.78;
|
|
|
|
nextPositionY = 15.3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blockType === GameLib.CustomCode.TETRIS_BLOCK_Z) {
|
|
|
|
mesh0 = this.tile_3.clone();
|
|
|
|
mesh0.position.setFrom(center.position);
|
|
|
|
mesh0.position.z = -0.1;
|
|
|
|
mesh0.position.y += 1;
|
|
|
|
|
|
|
|
mesh1 = this.tile_4.clone();
|
|
|
|
mesh1.position.setFrom(center.position);
|
|
|
|
mesh1.position.z = -0.1;
|
|
|
|
mesh1.position.x -= 1;
|
|
|
|
mesh1.position.y += 1;
|
|
|
|
|
|
|
|
mesh2 = this.tile_2.clone();
|
|
|
|
mesh2.position.setFrom(center.position);
|
|
|
|
mesh2.position.z = -0.1;
|
|
|
|
|
|
|
|
mesh3 = this.tile_1.clone();
|
|
|
|
mesh3.position.setFrom(center.position);
|
|
|
|
mesh3.position.z = -0.1;
|
|
|
|
mesh3.position.x += 1;
|
|
|
|
|
|
|
|
offsetX = 0.5;
|
|
|
|
offsetY = 2;
|
|
|
|
|
|
|
|
tinyPositionX = 1.00;
|
|
|
|
tinyPositionY = 16.65;
|
|
|
|
|
|
|
|
nextPositionX = 1.78;
|
|
|
|
nextPositionY = 15.3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blockType === GameLib.CustomCode.TETRIS_BLOCK_Z2) {
|
|
|
|
|
|
|
|
mesh0 = this.tile_3.clone();
|
|
|
|
mesh0.position.setFrom(center.position);
|
|
|
|
mesh0.position.z = -0.1;
|
|
|
|
mesh0.position.y += 1;
|
|
|
|
|
|
|
|
mesh1 = this.tile_4.clone();
|
|
|
|
mesh1.position.setFrom(center.position);
|
|
|
|
mesh1.position.z = -0.1;
|
|
|
|
mesh1.position.x += 1;
|
|
|
|
mesh1.position.y += 1;
|
|
|
|
|
|
|
|
mesh2 = this.tile_2.clone();
|
|
|
|
mesh2.position.setFrom(center.position);
|
|
|
|
mesh2.position.z = -0.1;
|
|
|
|
|
|
|
|
mesh3 = this.tile_1.clone();
|
|
|
|
mesh3.position.setFrom(center.position);
|
|
|
|
mesh3.position.z = -0.1;
|
|
|
|
mesh3.position.x -= 1;
|
|
|
|
|
|
|
|
offsetX = 0.5;
|
|
|
|
offsetY = 2;
|
|
|
|
|
|
|
|
tinyPositionX = 1.00;
|
|
|
|
tinyPositionY = 16.65;
|
|
|
|
|
|
|
|
nextPositionX = 1.78;
|
|
|
|
nextPositionY = 15.3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blockType === GameLib.CustomCode.TETRIS_BLOCK_T) {
|
|
|
|
mesh0 = this.tile_6.clone();
|
|
|
|
mesh0.position.setFrom(center.position);
|
|
|
|
mesh0.position.z = -0.1;
|
|
|
|
mesh0.position.y -= 1;
|
|
|
|
|
|
|
|
mesh1 = this.tile_5.clone();
|
|
|
|
mesh1.position.setFrom(center.position);
|
|
|
|
mesh1.position.z = -0.1;
|
|
|
|
|
|
|
|
mesh2 = this.tile_4.clone();
|
|
|
|
mesh2.position.setFrom(center.position);
|
|
|
|
mesh2.position.z = -0.1;
|
|
|
|
mesh2.position.x -= 1;
|
|
|
|
|
|
|
|
mesh3 = this.tile_2.clone();
|
|
|
|
mesh3.position.setFrom(center.position);
|
|
|
|
mesh3.position.z = -0.1;
|
|
|
|
mesh3.position.x += 1;
|
|
|
|
|
|
|
|
offsetX = 0.5;
|
|
|
|
offsetY = 1;
|
|
|
|
|
|
|
|
tinyPositionX = 1.00;
|
|
|
|
tinyPositionY = 17;
|
|
|
|
|
|
|
|
nextPositionX = 1.78;
|
|
|
|
nextPositionY = 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blockType === GameLib.CustomCode.TETRIS_BLOCK_O) {
|
|
|
|
mesh0 = this.tile_3.clone();
|
|
|
|
mesh0.position.setFrom(center.position);
|
|
|
|
mesh0.position.z = -0.1;
|
|
|
|
mesh0.position.y += 0.5;
|
|
|
|
mesh0.position.x += 0.5;
|
|
|
|
|
|
|
|
mesh1 = this.tile_0.clone();
|
|
|
|
mesh1.position.setFrom(center.position);
|
|
|
|
mesh1.position.z = -0.1;
|
|
|
|
mesh1.position.y += 0.5;
|
|
|
|
mesh1.position.x -= 0.5;
|
|
|
|
|
|
|
|
mesh2 = this.tile_5.clone();
|
|
|
|
mesh2.position.setFrom(center.position);
|
|
|
|
mesh2.position.z = -0.1;
|
|
|
|
mesh2.position.y -= 0.5;
|
|
|
|
mesh2.position.x -= 0.5;
|
|
|
|
|
|
|
|
mesh3 = this.tile_2.clone();
|
|
|
|
mesh3.position.setFrom(center.position);
|
|
|
|
mesh3.position.z = -0.1;
|
|
|
|
mesh3.position.y -= 0.5;
|
|
|
|
mesh3.position.x += 0.5;
|
|
|
|
|
|
|
|
offsetX = 0;
|
|
|
|
offsetY = 1.5;
|
|
|
|
|
|
|
|
tinyPositionX = 1.00;
|
|
|
|
tinyPositionY = 16.85;
|
|
|
|
|
|
|
|
nextPositionX = 1.78;
|
|
|
|
nextPositionY = 15.65;
|
|
|
|
}
|
|
|
|
|
2018-02-14 15:46:54 +01:00
|
|
|
mesh0.visible = true;
|
|
|
|
mesh0.updateInstance('visible');
|
|
|
|
mesh1.visible = true;
|
|
|
|
mesh1.updateInstance('visible');
|
|
|
|
mesh2.visible = true;
|
|
|
|
mesh2.updateInstance('visible');
|
|
|
|
mesh3.visible = true;
|
|
|
|
mesh3.updateInstance('visible');
|
2018-02-18 12:07:12 +01:00
|
|
|
|
2018-02-18 16:53:08 +01:00
|
|
|
mesh0.parentMesh = center;
|
|
|
|
mesh0.updateInstance('parentMesh');
|
|
|
|
|
|
|
|
mesh1.parentMesh = center;
|
|
|
|
mesh1.updateInstance('parentMesh');
|
|
|
|
|
|
|
|
mesh2.parentMesh = center;
|
|
|
|
mesh2.updateInstance('parentMesh');
|
|
|
|
|
|
|
|
mesh3.parentMesh = center;
|
|
|
|
mesh3.updateInstance('parentMesh');
|
2018-02-13 10:35:25 +01:00
|
|
|
|
|
|
|
center.updateInstance('position');
|
|
|
|
|
|
|
|
mesh0.updateInstance('position');
|
|
|
|
mesh1.updateInstance('position');
|
|
|
|
mesh2.updateInstance('position');
|
|
|
|
mesh3.updateInstance('position');
|
|
|
|
|
|
|
|
var block = {
|
|
|
|
type: blockType,
|
|
|
|
offset: {
|
|
|
|
x: offsetX,
|
|
|
|
y: offsetY
|
|
|
|
},
|
|
|
|
tinyPosition: {
|
|
|
|
x: tinyPositionX,
|
|
|
|
y: tinyPositionY
|
|
|
|
},
|
|
|
|
nextPosition: {
|
|
|
|
x: nextPositionX,
|
|
|
|
y: nextPositionY
|
|
|
|
},
|
|
|
|
meshes: [
|
|
|
|
mesh0,
|
|
|
|
mesh1,
|
|
|
|
mesh2,
|
|
|
|
mesh3
|
|
|
|
],
|
|
|
|
angle: center.rotation.z,
|
|
|
|
center: center
|
|
|
|
};
|
|
|
|
|
|
|
|
return block;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
|
|
|
}.bind(this)
|
|
|
|
|
|
|
|
GameLib.CustomCode.prototype.spawnTinyBlock = function () {
|
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var block = this.spawnBlock();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the scale for the blocks
|
|
|
|
*/
|
|
|
|
block.center.scale.x = 0.333;
|
|
|
|
block.center.scale.y = 0.333;
|
|
|
|
block.center.scale.z = 0.333;
|
|
|
|
block.center.updateInstance('scale');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* move tiny block a bit up - then a bit right
|
|
|
|
*/
|
|
|
|
block.center.position.x = block.tinyPosition.x;
|
|
|
|
block.center.position.y = block.tinyPosition.y;
|
|
|
|
block.center.position.z = 0.4;
|
|
|
|
block.center.updateInstance('position');
|
|
|
|
|
|
|
|
return block;
|
2018-02-13 08:48:42 +01:00
|
|
|
}.bind(this)
|
|
|
|
|
|
|
|
GameLib.CustomCode.prototype.spawnNextBlock = function () {
|
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (!this.tinyBlock) {
|
|
|
|
this.tinyBlock = this.spawnTinyBlock();
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var block = this.tinyBlock;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
block.center.scale.x = 0.666;
|
|
|
|
block.center.scale.y = 0.666;
|
|
|
|
block.center.scale.z = 0.666;
|
|
|
|
block.center.updateInstance('scale');
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
block.center.position.x = block.nextPosition.x;
|
|
|
|
block.center.position.y = block.nextPosition.y;
|
|
|
|
block.center.position.z = 0.4;
|
|
|
|
block.center.updateInstance('position');
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.tinyBlock = this.spawnTinyBlock();
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
return block;
|
2018-02-13 08:48:42 +01:00
|
|
|
}.bind(this)
|
|
|
|
|
|
|
|
GameLib.CustomCode.prototype.spawnBigBlock = function () {
|
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
if (!this.nextBlock) {
|
|
|
|
this.nextBlock = this.spawnNextBlock();
|
|
|
|
}
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
var block = this.nextBlock;
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
block.center.scale.x = 1;
|
|
|
|
block.center.scale.y = 1;
|
|
|
|
block.center.scale.z = 1;
|
|
|
|
block.center.updateInstance('scale');
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
block.center.position.x = 0;
|
|
|
|
block.center.position.y = 0;
|
2018-02-18 13:22:22 +01:00
|
|
|
block.center.position.z = 2.5;
|
2018-02-13 10:35:25 +01:00
|
|
|
this.moveBlock(block, {up: true}, (GameLib.CustomCode.TETRIS_GRID_HEIGHT - GameLib.CustomCode.TETRIS_GRID_HEADSPACE), false);
|
|
|
|
this.moveBlock(block, {right: true}, GameLib.CustomCode.TETRIS_GRID_WIDTH / 2, false);
|
|
|
|
this.moveBlock(block, {left: true}, block.offset.x, false);
|
|
|
|
this.moveBlock(block, {down: true}, block.offset.y, false);
|
2018-02-18 18:02:24 +01:00
|
|
|
block.center.updateInstance('position');
|
2018-02-18 18:14:47 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.nextBlock = this.spawnNextBlock();
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
return block;
|
2018-02-13 08:48:42 +01:00
|
|
|
}.bind(this)
|
|
|
|
|
|
|
|
GameLib.CustomCode.prototype.getNextBlock = function () {
|
|
|
|
|
2018-02-18 12:34:02 +01:00
|
|
|
this.block = this.spawnBigBlock();
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
GameLib.Event.Emit(
|
|
|
|
GameLib.Event.COMPONENT_REGISTER,
|
|
|
|
{
|
|
|
|
component: this.block.center
|
|
|
|
}
|
|
|
|
);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
2018-02-18 17:42:00 +01:00
|
|
|
this.animation.meshes.push(this.block.center);
|
2018-02-13 08:48:42 +01:00
|
|
|
|
|
|
|
}.bind(this);
|
2018-02-18 18:46:39 +01:00
|
|
|
|
2018-02-18 12:07:12 +01:00
|
|
|
/**
|
|
|
|
* This code executes when the 'bottom' has been reached (the block is stopping)
|
|
|
|
*/
|
2018-02-18 12:59:30 +01:00
|
|
|
GameLib.CustomCode.prototype.stopBlock = function() {
|
2018-02-18 12:07:12 +01:00
|
|
|
|
|
|
|
var gridPositions = this.getBlockGridPositions(this.block);
|
|
|
|
|
|
|
|
gridPositions.map(
|
2018-02-18 12:34:02 +01:00
|
|
|
|
2018-02-18 12:07:12 +01:00
|
|
|
function (position) {
|
2018-02-18 12:59:30 +01:00
|
|
|
|
2018-02-18 12:07:12 +01:00
|
|
|
/**
|
|
|
|
* If there already is a mesh - remove it -
|
|
|
|
*/
|
2018-02-18 16:39:23 +01:00
|
|
|
var block = this.grid[position.y][position.x];
|
2018-02-18 16:53:08 +01:00
|
|
|
|
2018-02-18 16:39:23 +01:00
|
|
|
if (block.mesh) {
|
|
|
|
this.scene.removeObject(block.mesh);
|
2018-02-18 12:07:12 +01:00
|
|
|
}
|
2018-02-18 16:39:23 +01:00
|
|
|
if (block.baconDisappearing) {
|
|
|
|
this.scene.removeObject(block.baconDisappearing);
|
|
|
|
}
|
2018-02-18 16:53:08 +01:00
|
|
|
|
2018-02-18 16:39:23 +01:00
|
|
|
block = {
|
|
|
|
value : GameLib.CustomCode.TETRIS_GRID_TAKEN,
|
|
|
|
mesh : position.mesh,
|
|
|
|
baconDisappearing : null
|
|
|
|
}
|
2018-02-18 16:53:08 +01:00
|
|
|
|
2018-02-18 16:39:23 +01:00
|
|
|
this.grid[position.y][position.x] = block;
|
2018-02-18 12:59:30 +01:00
|
|
|
|
2018-02-18 12:07:12 +01:00
|
|
|
}.bind(this)
|
2018-02-18 12:34:02 +01:00
|
|
|
|
2018-02-18 12:07:12 +01:00
|
|
|
);
|
|
|
|
|
2018-02-18 16:39:23 +01:00
|
|
|
/**
|
|
|
|
* We apply our current rotation and position to the instance
|
|
|
|
*/
|
|
|
|
this.block.center.updateInstance('position');
|
|
|
|
this.block.center.updateInstance('rotation');
|
2018-02-18 18:40:07 +01:00
|
|
|
|
2018-02-18 16:39:23 +01:00
|
|
|
/**
|
|
|
|
* Now we unlink the meshes from the controlling center block
|
|
|
|
*/
|
2018-02-18 12:07:12 +01:00
|
|
|
this.block.meshes.map(
|
|
|
|
function (mesh) {
|
|
|
|
|
2018-02-18 16:53:08 +01:00
|
|
|
mesh.parentMesh = null;
|
2018-02-18 16:53:59 +01:00
|
|
|
mesh.updateInstance('parentMesh');
|
2018-02-18 16:53:08 +01:00
|
|
|
|
2018-02-18 13:13:09 +01:00
|
|
|
mesh.position.z = 2.5;
|
2018-02-18 16:39:23 +01:00
|
|
|
mesh.updateInstance('position');
|
2018-02-18 12:07:12 +01:00
|
|
|
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
2018-02-18 16:39:23 +01:00
|
|
|
/**
|
|
|
|
* We remove the controlling block
|
|
|
|
*/
|
2018-02-18 12:07:12 +01:00
|
|
|
GameLib.Event.Emit(
|
|
|
|
GameLib.Event.REMOVE_COMPONENT,
|
|
|
|
{
|
|
|
|
component: this.block.center
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-02-18 16:39:23 +01:00
|
|
|
/**
|
|
|
|
* We remove any lines
|
|
|
|
*/
|
2018-02-18 12:07:12 +01:00
|
|
|
this.removeLines();
|
|
|
|
|
2018-02-18 18:40:07 +01:00
|
|
|
//this.visualizeGrid(0xff0000);
|
2018-02-18 12:07:12 +01:00
|
|
|
|
2018-02-18 16:39:23 +01:00
|
|
|
/**
|
|
|
|
* Check if the game is over
|
|
|
|
*/
|
2018-02-18 12:07:12 +01:00
|
|
|
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) {
|
2018-02-18 16:39:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* End the game if game over
|
|
|
|
*/
|
|
|
|
GameLib.Event.Emit(
|
|
|
|
GameLib.Event.GAME_OVER,
|
|
|
|
{
|
|
|
|
score: this.score,
|
|
|
|
level: this.level,
|
|
|
|
rows: this.rows
|
|
|
|
}
|
|
|
|
);
|
2018-02-18 16:53:08 +01:00
|
|
|
|
2018-02-18 12:34:02 +01:00
|
|
|
} else {
|
2018-02-18 16:39:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Otherwise continue
|
|
|
|
*/
|
2018-02-18 12:34:02 +01:00
|
|
|
this.getNextBlock();
|
|
|
|
}
|
2018-02-18 12:07:12 +01:00
|
|
|
|
2018-02-18 12:34:02 +01:00
|
|
|
}.bind(this);
|
2018-02-18 12:07:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the grid
|
|
|
|
*/
|
|
|
|
GameLib.CustomCode.prototype.clearGrid = function() {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all stale components (if any)
|
|
|
|
*/
|
|
|
|
if (this.grid instanceof Array) {
|
|
|
|
this.grid.map(
|
2018-02-18 15:19:35 +01:00
|
|
|
function(row) {
|
|
|
|
row.map(
|
|
|
|
function(block){
|
|
|
|
if (block.mesh) {
|
|
|
|
this.scene.removeObject(block.mesh);
|
|
|
|
}
|
|
|
|
if (block.baconDisappearing) {
|
|
|
|
this.scene.removeObject(block.baconDisappearing);
|
|
|
|
}
|
|
|
|
}.bind(this)
|
|
|
|
)
|
2018-02-18 12:07:12 +01:00
|
|
|
}.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] = {
|
2018-02-18 15:19:35 +01:00
|
|
|
value : GameLib.CustomCode.TETRIS_GRID_NOT_TAKEN,
|
|
|
|
mesh : null,
|
|
|
|
baconDisappearing : null
|
2018-02-18 12:07:12 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}.bind(this)
|
|
|
|
|
2018-02-18 18:40:07 +01:00
|
|
|
GameLib.CustomCode.prototype.createTile = function(tileName, imageName, displace) {
|
2018-02-13 10:54:14 +01:00
|
|
|
|
|
|
|
var apiMaterial;
|
|
|
|
|
|
|
|
if (imageName) {
|
2018-02-18 18:40:07 +01:00
|
|
|
if (displace) {
|
|
|
|
apiMaterial = new GameLib.D3.API.Material.Phong(
|
|
|
|
null,
|
|
|
|
null,
|
2018-02-13 10:54:14 +01:00
|
|
|
null,
|
2018-02-18 18:40:07 +01:00
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
this.textureDisplacement,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
new GameLib.D3.API.Texture.Image(
|
|
|
|
null,
|
|
|
|
this[imageName]
|
|
|
|
)
|
2018-02-13 10:54:14 +01:00
|
|
|
)
|
2018-02-18 18:40:07 +01:00
|
|
|
} else {
|
|
|
|
apiMaterial = new GameLib.D3.API.Material.Phong(
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
new GameLib.D3.API.Texture.Image(
|
|
|
|
null,
|
|
|
|
this[imageName]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
2018-02-13 10:54:14 +01:00
|
|
|
} else {
|
|
|
|
apiMaterial = new GameLib.D3.API.Material.Phong();
|
|
|
|
}
|
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this[tileName] = new GameLib.D3.Mesh(
|
|
|
|
this.graphics,
|
|
|
|
new GameLib.D3.API.Mesh(
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
2018-02-13 10:54:14 +01:00
|
|
|
null,
|
2018-02-18 18:46:39 +01:00
|
|
|
new GameLib.D3.API.Geometry.Buffer.Plane(
|
|
|
|
null,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
10,
|
|
|
|
10
|
|
|
|
),
|
2018-02-13 10:35:25 +01:00
|
|
|
[
|
2018-02-13 10:54:14 +01:00
|
|
|
apiMaterial
|
|
|
|
],
|
|
|
|
null,
|
|
|
|
this.scene
|
2018-02-13 10:35:25 +01:00
|
|
|
)
|
|
|
|
)
|
2018-02-13 10:54:14 +01:00
|
|
|
|
2018-02-14 15:44:15 +01:00
|
|
|
this[tileName].visible = false;
|
|
|
|
this[tileName].updateInstance('visible');
|
|
|
|
|
2018-02-13 10:54:14 +01:00
|
|
|
/**
|
|
|
|
* Remove this tile from the 'meshes' so it is treated like a clone and doesn't get stored
|
|
|
|
*/
|
|
|
|
this[tileName].parentScene.addClone(this[tileName]);
|
|
|
|
|
2018-02-13 10:06:46 +01:00
|
|
|
}.bind(this);
|
|
|
|
|
2018-02-18 18:40:07 +01:00
|
|
|
GameLib.Event.Subscribe(
|
|
|
|
GameLib.Event.CUSTOM_GAME_START,
|
|
|
|
|
|
|
|
function () {
|
|
|
|
|
|
|
|
this.animationObjects = [];
|
|
|
|
|
|
|
|
this.baconMaterials = [];
|
|
|
|
|
|
|
|
this.score = 0;
|
|
|
|
this.level = 1;
|
|
|
|
this.rows = 0;
|
|
|
|
this.speed = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw our new status
|
|
|
|
*/
|
|
|
|
this.drawStatus();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure the grid is cleared
|
|
|
|
*/
|
|
|
|
this.clearGrid();
|
|
|
|
|
|
|
|
|
|
|
|
if (this.block && 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.getNextBlock();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the game parameters
|
|
|
|
*/
|
|
|
|
this.ccBeforeRender.entityLoaded = null;
|
|
|
|
this.ccBeforeRender.initialized = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Link the classes
|
|
|
|
*/
|
|
|
|
this.ccBeforeRender.entityLoaded = this;
|
|
|
|
this.ccKeyDown.entityLoaded = this;
|
|
|
|
this.ccTouchStart.entityLoaded = this;
|
|
|
|
this.ccTouchEnd.entityLoaded = this;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow the game to proceed
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.gameOver = false;
|
|
|
|
|
|
|
|
console.log('game start complete');
|
|
|
|
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
2018-02-13 10:06:46 +01:00
|
|
|
GameLib.Event.Emit(
|
2018-02-13 10:35:25 +01:00
|
|
|
GameLib.Event.GET_RUNTIME,
|
|
|
|
{},
|
|
|
|
function(runtime) {
|
2018-02-13 10:06:46 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
this.graphics = runtime.graphics;
|
2018-02-13 10:06:46 +01:00
|
|
|
|
2018-02-18 18:40:07 +01:00
|
|
|
this.createTile('tile_0', 'image_0', true);
|
|
|
|
this.createTile('tile_1', 'image_1', true);
|
|
|
|
this.createTile('tile_2', 'image_2', true);
|
|
|
|
this.createTile('tile_3', 'image_3', true);
|
|
|
|
this.createTile('tile_4', 'image_4', true);
|
|
|
|
this.createTile('tile_5', 'image_5', true);
|
|
|
|
this.createTile('tile_6', 'image_6', true);
|
2018-02-14 11:35:18 +01:00
|
|
|
|
2018-02-18 21:22:10 +01:00
|
|
|
this.createTile('tile_grid', 'image_grid');
|
|
|
|
|
2018-02-14 15:44:15 +01:00
|
|
|
this.createTile('tile_flames_small', 'image_flames_small');
|
|
|
|
|
|
|
|
this.tile_flames_small.materials[0].transparent = true;
|
2018-02-14 15:18:19 +01:00
|
|
|
this.tile_flames_small.materials[0].updateInstance('transparent');
|
|
|
|
|
2018-02-14 15:44:15 +01:00
|
|
|
this.tile_flames_small.geometry.width = '11';
|
2018-02-14 15:36:16 +01:00
|
|
|
this.tile_flames_small.geometry.height = '11';
|
|
|
|
this.tile_flames_small.geometry.updateInstance('width');
|
2018-02-14 15:44:15 +01:00
|
|
|
|
|
|
|
this.createTile('tile_glow', 'image_glow');
|
|
|
|
|
2018-02-14 15:36:16 +01:00
|
|
|
this.tile_glow.materials[0].transparent = true;
|
|
|
|
this.tile_glow.materials[0].updateInstance('transparent');
|
|
|
|
|
2018-02-14 15:20:53 +01:00
|
|
|
this.tile_glow.geometry.width = '12';
|
|
|
|
this.tile_glow.geometry.height = '12';
|
2018-02-14 15:18:19 +01:00
|
|
|
this.tile_glow.geometry.updateInstance('width');
|
2018-02-14 15:44:15 +01:00
|
|
|
|
2018-02-14 15:15:06 +01:00
|
|
|
this.tile_glowMaterial = this.tile_glow.materials[0];
|
|
|
|
|
2018-02-14 11:35:18 +01:00
|
|
|
this.createTile('tile_center');
|
2018-02-14 11:37:40 +01:00
|
|
|
|
2018-02-14 13:53:15 +01:00
|
|
|
this.tile_center.materials[0].transparent = true;
|
2018-02-14 13:54:04 +01:00
|
|
|
this.tile_center.materials[0].updateInstance('transparent');
|
2018-02-14 13:53:15 +01:00
|
|
|
|
2018-02-14 15:12:30 +01:00
|
|
|
this.tile_center.materials[0].opacity = 0;
|
2018-02-14 13:54:04 +01:00
|
|
|
this.tile_center.materials[0].updateInstance('opacity');
|
2018-02-14 11:37:40 +01:00
|
|
|
|
2018-02-18 18:16:07 +01:00
|
|
|
this.tile_center.useQuaternion = false;
|
|
|
|
|
2018-02-14 11:35:18 +01:00
|
|
|
this.createTile('tile_bacon_disappearing');
|
|
|
|
|
2018-02-14 15:44:15 +01:00
|
|
|
this.tile_bacon_disappearing.materials[0].color.fromHex('#3e1d1d');
|
|
|
|
this.tile_bacon_disappearing.materials[0].updateInstance('color');
|
|
|
|
|
2018-02-13 15:22:08 +01:00
|
|
|
this.drawGrid();
|
2018-02-14 10:59:27 +01:00
|
|
|
this.drawStatus();
|
2018-02-18 13:02:34 +01:00
|
|
|
|
2018-02-18 22:01:16 +01:00
|
|
|
GameLib.Event.Emit(GameLib.Event.WINDOW_RESIZE, GameLib.Utils.GetWindowSize());
|
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
GameLib.Event.Emit(GameLib.Event.GAME_LOADED);
|
2018-02-13 10:06:46 +01:00
|
|
|
|
2018-02-13 10:35:25 +01:00
|
|
|
}.bind(this)
|
2018-02-13 10:06:46 +01:00
|
|
|
)
|
2018-02-13 08:48:42 +01:00
|
|
|
//@ sourceURL=entityLoaded.js
|