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,39 +236,25 @@ 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
); );
body.applyToMesh();
return body;
} else { } else {
var temp = { body.position.x = this.snake[index - 1].position.x;
x : body.position.x, body.position.y = this.snake[index - 1].position.y;
y : body.position.y, body.orientation = this.snake[index - 1].orientation;
orientation : body.orientation,
bodyType : body.bodyType
};
body.position.x = backup.x;
body.position.y = backup.y;
body.orientation = backup.orientation;
if ( if (
index === 1 && index === 1 &&
@ -249,58 +262,59 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) {
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.position,
body.orientation,
GameLib.CustomCode.BODY_TYPE_CORNER,
body
);
body.backupMesh.visible = false; corner.applyToMesh();
body.backupMesh.updateInstance('visible'); return corner;
body.mesh = this.meshBreadCorner.clone();
body.bodyType = GameLib.CustomCode.BODY_TYPE_CORNER;
this.state.turning = false;
} else { } else {
if (body.bodyType === GameLib.CustomCode.BODY_TYPE_CORNER) { if (body.bodyType === GameLib.CustomCode.BODY_TYPE_CORNER) {
body.mesh.geometry = null; /**
body.mesh.materials = null; * First restore the backup before we start storing another corner
body.mesh.remove(); */
body.mesh = body.backupMesh; body.restoreBackup();
body.mesh.visible = true;
body.mesh.updateInstance('visible');
} else if (backup.bodyType === GameLib.CustomCode.BODY_TYPE_CORNER) { }
if (this.snake[index - 1].bodyType === GameLib.CustomCode.BODY_TYPE_CORNER) {
if (index === (this.snake.length - 1)) { if (index === (this.snake.length - 1)) {
/**
backup.bodyType = GameLib.CustomCode.BODY_TYPE_TAIL; * We're the tail - don't turn into a corner
*/
} else { return body;
body.backupMesh = body.mesh;
body.backupMesh.visible = false;
body.backupMesh.updateInstance('visible');
body.mesh = this.meshBreadCorner.clone();
} }
} var corner = new GameLib.CustomCode.SnakeBody(
this.meshBreadCorner,
body.position,
body.orientation,
GameLib.CustomCode.BODY_TYPE_CORNER,
body
);
body.bodyType = backup.bodyType; corner.applyToMesh();
}
backup.x = temp.x;
backup.y = temp.y;
backup.orientation = temp.orientation;
backup.bodyType = temp.bodyType
return corner;
} }
body.applyToMesh(); body.applyToMesh();
return body;
}
}
}.bind(this) }.bind(this)
); );
@ -394,7 +408,7 @@ GameLib.Event.Subscribe(
y.mesh.remove(); y.mesh.remove();
} }
); );
} }
); );
this.initializeGrid(); this.initializeGrid();