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

beta.r3js.org
-=yb4f310 2018-03-15 12:46:00 +01:00
parent f99160dc04
commit 1e2f98c6a0
1 changed files with 102 additions and 88 deletions

View File

@ -108,16 +108,12 @@ GameLib.CustomCode.prototype.createGameMesh = function(image) {
* Create our objects * Create our objects
*/ */
this.meshBreadHead = this.createGameMesh(this.imageBreadHead); this.meshBreadHead = this.createGameMesh(this.imageBreadHead);
this.meshBreadHead.useQuaternion = false;
this.meshBreadTail = this.createGameMesh(this.imageBreadTail); this.meshBreadTail = this.createGameMesh(this.imageBreadTail);
this.meshBreadTail.useQuaternion = false;
this.meshBreadPatty = this.createGameMesh(this.imageBreadPatty); this.meshBreadPatty = this.createGameMesh(this.imageBreadPatty);
this.meshBreadPatty.useQuaternion = false;
this.meshBreadCorner = this.createGameMesh(this.imageBreadCorner); this.meshBreadCorner = this.createGameMesh(this.imageBreadCorner);
this.meshBreadCorner.useQuaternion = false;
this.meshPatty = this.createGameMesh(this.imagePatty); this.meshPatty = this.createGameMesh(this.imagePatty);
@ -127,19 +123,22 @@ this.meshPatty = this.createGameMesh(this.imagePatty);
* @param position * @param position
* @param orientation * @param orientation
* @param bodyType * @param bodyType
* @param backup
* @constructor * @constructor
*/ */
GameLib.CustomCode.SnakeBody = function( GameLib.CustomCode.SnakeBody = function(
mesh, mesh,
position, position,
orientation, orientation,
bodyType bodyType,
backup
) { ) {
if (GameLib.Utils.UndefinedOrNull(mesh)) { if (GameLib.Utils.UndefinedOrNull(mesh)) {
throw new Error('no mesh specified'); throw new Error('no mesh specified');
} }
this.mesh = mesh.clone(); this.mesh = mesh.clone();
this.mesh.useQuaternion = false;
if (GameLib.Utils.UndefinedOrNull(position)) { if (GameLib.Utils.UndefinedOrNull(position)) {
position = { position = {
@ -159,11 +158,39 @@ GameLib.CustomCode.SnakeBody = function(
} }
this.bodyType = bodyType; this.bodyType = bodyType;
this.backupMesh = null; if (GameLib.Utils.UndefinedOrNull(backup)) {
backup = null;
}
this.backup = backup;
this.applyToMesh(); this.applyToMesh();
}; };
GameLib.CustomCode.SnakeBody.prototype.restoreBackup = function() {
if (!this.backup) {
console.warn('no backup to restore');
return;
}
this.mesh.geometry = null;
this.mesh.materials = null;
this.mesh.remove();
this.mesh = this.backup.mesh;
this.mesh.visible = true;
this.mesh.updateInstance('visible');
this.orientation = this.backup.orientation;
this.bodyType = this.backup.bodyType;
this.backup = null;
/**
* We don't restore the position because the position will have changed
*/
}
GameLib.CustomCode.SnakeBody.prototype.applyToMesh = function() { GameLib.CustomCode.SnakeBody.prototype.applyToMesh = function() {
this.mesh.position.x = this.position.x + GameLib.CustomCode.GRID_OFFSET_X; this.mesh.position.x = this.position.x + GameLib.CustomCode.GRID_OFFSET_X;
@ -209,98 +236,85 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) {
return; return;
} }
var backup = { this.snake = this.snake.map(
x : 0,
y : 0,
orientation : GameLib.CustomCode.ORIENTATION_UP,
bodyType : GameLib.CustomCode.BODY_TYPE_NORMAL
};
this.snake.map(
function(body, index) { function(body, index) {
if (index === 0) { if (index === 0) {
backup.x = body.position.x;
backup.y = body.position.y;
backup.orientation = body.orientation;
backup.bodyType = body.bodyType;
body.advance( body.advance(
this.state.orientation this.state.orientation
); );
} else { body.applyToMesh();
var temp = { return body;
x : body.position.x,
y : body.position.y, } else {
orientation : body.orientation,
bodyType : body.bodyType
};
body.position.x = backup.x; body.position.x = this.snake[index - 1].position.x;
body.position.y = backup.y; body.position.y = this.snake[index - 1].position.y;
body.orientation = backup.orientation; body.orientation = this.snake[index - 1].orientation;
if ( if (
index === 1 && index === 1 &&
this.state.turning && this.state.turning &&
body.bodyType !== GameLib.CustomCode.BODY_TYPE_TAIL body.bodyType !== GameLib.CustomCode.BODY_TYPE_TAIL
) { ) {
body.backupMesh = body.mesh; var corner = new GameLib.CustomCode.SnakeBody(
this.meshBreadCorner,
body.backupMesh.visible = false; body.position,
body.orientation,
body.backupMesh.updateInstance('visible'); GameLib.CustomCode.BODY_TYPE_CORNER,
body
body.mesh = this.meshBreadCorner.clone(); );
corner.applyToMesh();
body.bodyType = GameLib.CustomCode.BODY_TYPE_CORNER; return corner;
this.state.turning = false;
} else { } else {
if (body.bodyType === GameLib.CustomCode.BODY_TYPE_CORNER) {
body.mesh.geometry = null;
body.mesh.materials = null;
body.mesh.remove();
body.mesh = body.backupMesh;
body.mesh.visible = true;
body.mesh.updateInstance('visible');
} else if (backup.bodyType === GameLib.CustomCode.BODY_TYPE_CORNER) {
if (index === (this.snake.length - 1)) {
backup.bodyType = GameLib.CustomCode.BODY_TYPE_TAIL;
} else {
body.backupMesh = body.mesh;
body.backupMesh.visible = false; if (body.bodyType === GameLib.CustomCode.BODY_TYPE_CORNER) {
/**
* First restore the backup before we start storing another corner
*/
body.restoreBackup();
body.backupMesh.updateInstance('visible');
}
if (this.snake[index - 1].bodyType === GameLib.CustomCode.BODY_TYPE_CORNER) {
body.mesh = this.meshBreadCorner.clone(); if (index === (this.snake.length - 1)) {
} /**
* We're the tail - don't turn into a corner
} */
return body;
body.bodyType = backup.bodyType; }
} var corner = new GameLib.CustomCode.SnakeBody(
this.meshBreadCorner,
backup.x = temp.x; body.position,
backup.y = temp.y; body.orientation,
backup.orientation = temp.orientation; GameLib.CustomCode.BODY_TYPE_CORNER,
backup.bodyType = temp.bodyType body
);
corner.applyToMesh();
return corner;
}
body.applyToMesh();
return body;
}
} }
body.applyToMesh();
}.bind(this) }.bind(this)
); );
@ -387,14 +401,14 @@ GameLib.Event.Subscribe(
* *
this.grid.map( this.grid.map(
function(x) { function(x) {
x.map( x.map(
function(y) { function(y) {
y.mesh.geometry = null; y.mesh.geometry = null;
y.mesh.materials = null; y.mesh.materials = null;
y.mesh.remove(); y.mesh.remove();
} }
); );
} }
); );
this.initializeGrid(); this.initializeGrid();