Update: CC - Snake FS - Entity Loaded (21g30t1e75.js) 540 bytes modified
parent
f99160dc04
commit
1e2f98c6a0
144
21g30t1e75.js
144
21g30t1e75.js
|
@ -108,16 +108,12 @@ GameLib.CustomCode.prototype.createGameMesh = function(image) {
|
|||
* Create our objects
|
||||
*/
|
||||
this.meshBreadHead = this.createGameMesh(this.imageBreadHead);
|
||||
this.meshBreadHead.useQuaternion = false;
|
||||
|
||||
this.meshBreadTail = this.createGameMesh(this.imageBreadTail);
|
||||
this.meshBreadTail.useQuaternion = false;
|
||||
|
||||
this.meshBreadPatty = this.createGameMesh(this.imageBreadPatty);
|
||||
this.meshBreadPatty.useQuaternion = false;
|
||||
|
||||
this.meshBreadCorner = this.createGameMesh(this.imageBreadCorner);
|
||||
this.meshBreadCorner.useQuaternion = false;
|
||||
|
||||
this.meshPatty = this.createGameMesh(this.imagePatty);
|
||||
|
||||
|
@ -127,19 +123,22 @@ this.meshPatty = this.createGameMesh(this.imagePatty);
|
|||
* @param position
|
||||
* @param orientation
|
||||
* @param bodyType
|
||||
* @param backup
|
||||
* @constructor
|
||||
*/
|
||||
GameLib.CustomCode.SnakeBody = function(
|
||||
mesh,
|
||||
position,
|
||||
orientation,
|
||||
bodyType
|
||||
bodyType,
|
||||
backup
|
||||
) {
|
||||
|
||||
if (GameLib.Utils.UndefinedOrNull(mesh)) {
|
||||
throw new Error('no mesh specified');
|
||||
}
|
||||
this.mesh = mesh.clone();
|
||||
this.mesh.useQuaternion = false;
|
||||
|
||||
if (GameLib.Utils.UndefinedOrNull(position)) {
|
||||
position = {
|
||||
|
@ -159,11 +158,39 @@ GameLib.CustomCode.SnakeBody = function(
|
|||
}
|
||||
this.bodyType = bodyType;
|
||||
|
||||
this.backupMesh = null;
|
||||
if (GameLib.Utils.UndefinedOrNull(backup)) {
|
||||
backup = null;
|
||||
}
|
||||
this.backup = backup;
|
||||
|
||||
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() {
|
||||
|
||||
this.mesh.position.x = this.position.x + GameLib.CustomCode.GRID_OFFSET_X;
|
||||
|
@ -209,39 +236,25 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) {
|
|||
return;
|
||||
}
|
||||
|
||||
var backup = {
|
||||
x : 0,
|
||||
y : 0,
|
||||
orientation : GameLib.CustomCode.ORIENTATION_UP,
|
||||
bodyType : GameLib.CustomCode.BODY_TYPE_NORMAL
|
||||
};
|
||||
this.snake = this.snake.map(
|
||||
|
||||
this.snake.map(
|
||||
function(body, index) {
|
||||
|
||||
if (index === 0) {
|
||||
|
||||
backup.x = body.position.x;
|
||||
backup.y = body.position.y;
|
||||
backup.orientation = body.orientation;
|
||||
backup.bodyType = body.bodyType;
|
||||
|
||||
body.advance(
|
||||
this.state.orientation
|
||||
);
|
||||
|
||||
body.applyToMesh();
|
||||
|
||||
return body;
|
||||
|
||||
} else {
|
||||
|
||||
var temp = {
|
||||
x : body.position.x,
|
||||
y : body.position.y,
|
||||
orientation : body.orientation,
|
||||
bodyType : body.bodyType
|
||||
};
|
||||
|
||||
body.position.x = backup.x;
|
||||
body.position.y = backup.y;
|
||||
body.orientation = backup.orientation;
|
||||
body.position.x = this.snake[index - 1].position.x;
|
||||
body.position.y = this.snake[index - 1].position.y;
|
||||
body.orientation = this.snake[index - 1].orientation;
|
||||
|
||||
if (
|
||||
index === 1 &&
|
||||
|
@ -249,58 +262,59 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) {
|
|||
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');
|
||||
|
||||
body.mesh = this.meshBreadCorner.clone();
|
||||
|
||||
body.bodyType = GameLib.CustomCode.BODY_TYPE_CORNER;
|
||||
|
||||
this.state.turning = false;
|
||||
return corner;
|
||||
|
||||
} 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) {
|
||||
/**
|
||||
* First restore the backup before we start storing another corner
|
||||
*/
|
||||
body.restoreBackup();
|
||||
|
||||
|
||||
}
|
||||
|
||||
if (this.snake[index - 1].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;
|
||||
|
||||
body.backupMesh.updateInstance('visible');
|
||||
|
||||
body.mesh = this.meshBreadCorner.clone();
|
||||
/**
|
||||
* We're the tail - don't turn into a corner
|
||||
*/
|
||||
return body;
|
||||
}
|
||||
|
||||
}
|
||||
var corner = new GameLib.CustomCode.SnakeBody(
|
||||
this.meshBreadCorner,
|
||||
body.position,
|
||||
body.orientation,
|
||||
GameLib.CustomCode.BODY_TYPE_CORNER,
|
||||
body
|
||||
);
|
||||
|
||||
body.bodyType = backup.bodyType;
|
||||
|
||||
}
|
||||
|
||||
backup.x = temp.x;
|
||||
backup.y = temp.y;
|
||||
backup.orientation = temp.orientation;
|
||||
backup.bodyType = temp.bodyType
|
||||
corner.applyToMesh();
|
||||
|
||||
return corner;
|
||||
}
|
||||
|
||||
body.applyToMesh();
|
||||
|
||||
return body;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}.bind(this)
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in New Issue