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

beta.r3js.org
-=yb4f310 2018-03-24 14:41:44 +01:00
parent 483ddfdc0b
commit c8c9a3b8d0
1 changed files with 160 additions and 115 deletions

View File

@ -158,7 +158,7 @@ this.state = {
lives : 3, lives : 3,
getReady : false, getReady : false,
message : null, message : null,
gameOver : false gameOver : false
}; };
GameLib.CustomCode.prototype.displayHUD = function() { GameLib.CustomCode.prototype.displayHUD = function() {
@ -205,7 +205,13 @@ GameLib.CustomCode.prototype.displayHUD = function() {
this.canvasHUD.text('slow', 420, 500, '12px Pixeled', '#ffffff'); this.canvasHUD.text('slow', 420, 500, '12px Pixeled', '#ffffff');
if (this.state.message) { if (this.state.message) {
this.canvasHUD.text(this.state.message, 220, 250, '30px Pixeled', '#ffffff'); this.canvasHUD.text(
this.state.message.text,
this.state.message.x,
this.state.message.y,
'30px Pixeled',
'#ffffff'
);
} }
this.canvasHUD.image(this.imagePowerupSpeed.instance, 50, 460, 48, 48); this.canvasHUD.image(this.imagePowerupSpeed.instance, 50, 460, 48, 48);
@ -223,7 +229,7 @@ GameLib.CustomCode.prototype.waitReload = function(delta) {
if (this.explodeTime < GameLib.CustomCode.EXPLODE_LIFETIME) { if (this.explodeTime < GameLib.CustomCode.EXPLODE_LIFETIME) {
return; return;
} }
/** /**
* We need to revert to a good state * We need to revert to a good state
*/ */
@ -231,17 +237,15 @@ GameLib.CustomCode.prototype.waitReload = function(delta) {
this.state.exploding = false; this.state.exploding = false;
if (this.state.lives <= 0) { if (this.state.lives <= 0) {
this.state.gameOver = true; this.gameOver(false);
this.state.message = "GAME OVER"; return;
this.displayHUD(); }
return;
}
this.displayHUD(); this.displayHUD();
this.restore(); this.restore();
this.state.getReady = true; this.state.getReady = true;
window.setTimeout( window.setTimeout(
@ -254,7 +258,11 @@ GameLib.CustomCode.prototype.waitReload = function(delta) {
window.setTimeout( window.setTimeout(
function(){ function(){
this.state.message = "GO!"; this.state.message = {
text : "GO!",
x : 150,
y : 256
};
this.displayHUD(); this.displayHUD();
this.state.getReady = false; this.state.getReady = false;
}.bind(this), }.bind(this),
@ -263,7 +271,11 @@ GameLib.CustomCode.prototype.waitReload = function(delta) {
window.setTimeout( window.setTimeout(
function(){ function(){
this.state.message = "1"; this.state.message = {
text: "1",
x: 160,
y: 256
};
this.displayHUD(); this.displayHUD();
}.bind(this), }.bind(this),
3000 3000
@ -271,7 +283,11 @@ GameLib.CustomCode.prototype.waitReload = function(delta) {
window.setTimeout( window.setTimeout(
function(){ function(){
this.state.message = "2"; this.state.message = {
text: "2",
x: 160,
y: 256
};
this.displayHUD(); this.displayHUD();
}.bind(this), }.bind(this),
2000 2000
@ -280,13 +296,21 @@ GameLib.CustomCode.prototype.waitReload = function(delta) {
window.setTimeout( window.setTimeout(
function(){ function(){
this.state.message = "3"; this.state.message = {
text: "3",
x: 160,
y: 256
};
this.displayHUD(); this.displayHUD();
}.bind(this), }.bind(this),
1000 1000
); );
this.state.message = "READY?"; this.state.message = {
text: "GET READY",
x: 100,
y: 256
};
this.displayHUD(); this.displayHUD();
}.bind(this) }.bind(this)
@ -438,16 +462,6 @@ GameLib.CustomCode.GameObject = function(
this.position = position; this.position = position;
} }
/* GameLib.CustomCode.GameObject.prototype.clone = function() {
return new GameLib.CustomCode.GameObject(
this.objectType,
this.type,
this.mesh,
this.position
)
};
*/
/** /**
* Disposes of the mesh object and resets this object to its defaults * Disposes of the mesh object and resets this object to its defaults
*/ */
@ -513,6 +527,10 @@ GameLib.CustomCode.prototype.getFreeGridPosition = function() {
} }
); );
if (positions.length === 0) {
return null;
}
var index = GameLib.Utils.GetRandomInt(0, positions.length); var index = GameLib.Utils.GetRandomInt(0, positions.length);
return positions[index]; return positions[index];
@ -755,11 +773,19 @@ GameLib.CustomCode.prototype.createPowerup = function(delta) {
return; return;
} }
this.createGameObject( var position = this.getFreeGridPosition();
GameLib.CustomCode.OBJECT_TYPE_POWERUP,
null, if (position === null) {
this.getFreeGridPosition() this.gameOver(true);
); } else {
this.createGameObject(
GameLib.CustomCode.OBJECT_TYPE_POWERUP,
null,
position
);
}
}.bind(this); }.bind(this);
@ -777,14 +803,29 @@ GameLib.CustomCode.prototype.createFood = function(delta) {
return; return;
} }
this.createGameObject( var position = this.getFreeGridPosition();
GameLib.CustomCode.OBJECT_TYPE_FOOD,
null, if (position === null) {
this.getFreeGridPosition() this.gameOver(true);
); } else {
this.createGameObject(
GameLib.CustomCode.OBJECT_TYPE_FOOD,
null,
position
);
}
}.bind(this); }.bind(this);
GameLib.CustomCode.prototype.gameOver = function(won) {
this.state.gameOver = true;
this.state.message = {
text : "GAME OVER",
x : 100,
y : 256
};
this.displayHUD();
}.bind(this)
/** /**
* GameLib.CustomCode.SnakeBody * GameLib.CustomCode.SnakeBody
@ -981,7 +1022,7 @@ GameLib.CustomCode.prototype.explode = function(position) {
this.particleEnginePickle.updateInstance('position'); this.particleEnginePickle.updateInstance('position');
this.particleEnginePickle.enabled = true; this.particleEnginePickle.enabled = true;
}.bind(this) }.bind(this)
/** /**
@ -1102,6 +1143,10 @@ GameLib.CustomCode.prototype.extend = function(gameObject, position, orientation
this.grid[body.position.x][body.position.y] = body; this.grid[body.position.x][body.position.y] = body;
if (this.snake.length === GameLib.CustomCode.GRID_WIDTH * GameLib.CustomCode.GRID_HEIGHT) {
this.gameOver(true);
}
}.bind(this); }.bind(this);
/** /**
@ -1146,8 +1191,8 @@ GameLib.CustomCode.prototype.createCorner = function(body, temp) {
body.backupMesh.position.x -= 0.5 * GameLib.CustomCode.BODY_SCALE_X; body.backupMesh.position.x -= 0.5 * GameLib.CustomCode.BODY_SCALE_X;
} }
body.orientation = backupOrientation; body.orientation = backupOrientation;
body.mesh = this.createGameMesh(this.materialBreadCorner); body.mesh = this.createGameMesh(this.materialBreadCorner);
body.mesh.position.z = 5; body.mesh.position.z = 5;
@ -1239,55 +1284,55 @@ GameLib.CustomCode.prototype.restore = function() {
var clone = body.clone(); var clone = body.clone();
if (clone.backupMesh) { if (clone.backupMesh) {
if (clone.mesh) {
clone.mesh.geometry = null;
clone.mesh.materials = null;
clone.mesh.remove();
}
clone.mesh = clone.backupMesh;
clone.backupMesh = null; if (clone.mesh) {
clone.mesh.visible = true; clone.mesh.geometry = null;
clone.mesh.updateInstance('visible'); clone.mesh.materials = null;
clone.applyToMesh(); clone.mesh.remove();
clone.backupMesh = clone.mesh; }
if (clone.orientation === GameLib.CustomCode.ORIENTATION_UP) {
clone.backupMesh.position.y += 0.5 * GameLib.CustomCode.BODY_SCALE_Y;
}
if (clone.orientation === GameLib.CustomCode.ORIENTATION_DOWN) { clone.mesh = clone.backupMesh;
clone.backupMesh.position.y -= 0.5 * GameLib.CustomCode.BODY_SCALE_Y;
}
if (clone.orientation === GameLib.CustomCode.ORIENTATION_LEFT) { clone.backupMesh = null;
clone.backupMesh.position.x -= 0.5 * GameLib.CustomCode.BODY_SCALE_X; clone.mesh.visible = true;
} clone.mesh.updateInstance('visible');
clone.applyToMesh();
clone.backupMesh = clone.mesh;
if (clone.orientation === GameLib.CustomCode.ORIENTATION_RIGHT) { if (clone.orientation === GameLib.CustomCode.ORIENTATION_UP) {
clone.backupMesh.position.x += 0.5 * GameLib.CustomCode.BODY_SCALE_X; clone.backupMesh.position.y += 0.5 * GameLib.CustomCode.BODY_SCALE_Y;
} }
if (clone.isTail) { if (clone.orientation === GameLib.CustomCode.ORIENTATION_DOWN) {
clone.mesh = clone.backupMesh; clone.backupMesh.position.y -= 0.5 * GameLib.CustomCode.BODY_SCALE_Y;
clone.backupMesh = null; }
} else {
clone.mesh = this.createGameMesh(this.materialBreadCorner); if (clone.orientation === GameLib.CustomCode.ORIENTATION_LEFT) {
} clone.backupMesh.position.x -= 0.5 * GameLib.CustomCode.BODY_SCALE_X;
}
clone.applyToMesh();
if (clone.orientation === GameLib.CustomCode.ORIENTATION_RIGHT) {
} else { clone.backupMesh.position.x += 0.5 * GameLib.CustomCode.BODY_SCALE_X;
clone.mesh.visible = true; }
clone.mesh.updateInstance('visible');
clone.applyToMesh(); if (clone.isTail) {
} clone.mesh = clone.backupMesh;
clone.backupMesh = null;
} else {
clone.mesh = this.createGameMesh(this.materialBreadCorner);
}
clone.applyToMesh();
} else {
clone.mesh.visible = true;
clone.mesh.updateInstance('visible');
clone.applyToMesh();
}
this.grid[clone.position.x][clone.position.y] = clone;
this.grid[clone.position.x][clone.position.y] = clone;
return clone; return clone;
}.bind(this) }.bind(this)
); );
@ -1322,31 +1367,31 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) {
this.snake.map( this.snake.map(
function(body, index) { function(body, index) {
if (index === 0) { if (index === 0) {
if (body.backupMesh) { if (body.backupMesh) {
/** /**
* We used to be a corner, change back * We used to be a corner, change back
* @type {null} * @type {null}
*/ */
body.mesh.geometry = null; body.mesh.geometry = null;
body.mesh.materials = null; body.mesh.materials = null;
body.mesh.remove(); body.mesh.remove();
body.mesh = body.backupMesh; body.mesh = body.backupMesh;
body.backupMesh = null; body.backupMesh = null;
body.mesh.visible = true; body.mesh.visible = true;
body.mesh.updateInstance('visible'); body.mesh.updateInstance('visible');
body.mesh.updateInstance('position');
}
body.mesh.updateInstance('position');
}
backup = { backup = {
position : { position : {
x : body.position.x, x : body.position.x,
@ -1496,28 +1541,28 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) {
*/ */
this.grid[body.position.x][body.position.y] = body; this.grid[body.position.x][body.position.y] = body;
if (body.backupMesh) { if (body.backupMesh) {
/** /**
* We used to be a corner, change back * We used to be a corner, change back
* @type {null} * @type {null}
*/ */
body.mesh.geometry = null; body.mesh.geometry = null;
body.mesh.materials = null; body.mesh.materials = null;
body.mesh.remove(); body.mesh.remove();
body.mesh = body.backupMesh; body.mesh = body.backupMesh;
body.backupMesh = null; body.backupMesh = null;
body.mesh.visible = true; body.mesh.visible = true;
body.mesh.updateInstance('visible'); body.mesh.updateInstance('visible');
body.mesh.updateInstance('position');
}
body.mesh.updateInstance('position');
}
if (body.orientation !== temp.orientation) { if (body.orientation !== temp.orientation) {
this.createCorner(body, temp); this.createCorner(body, temp);
} }
@ -1555,7 +1600,7 @@ GameLib.Event.Subscribe(
lives : 3, lives : 3,
getReady : false, getReady : false,
message : null, message : null,
gameOver : false gameOver : false
}; };
this.initializeGrid(); this.initializeGrid();