Update: CC - Snake FS - Entity Loaded (21g30t1e75.js) 540 bytes modified
parent
f99160dc04
commit
1e2f98c6a0
158
21g30t1e75.js
158
21g30t1e75.js
|
@ -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
|
||||||
);
|
);
|
||||||
|
|
||||||
|
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;
|
if (
|
||||||
body.position.y = backup.y;
|
|
||||||
body.orientation = backup.orientation;
|
|
||||||
|
|
||||||
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.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();
|
} else {
|
||||||
|
|
||||||
body.bodyType = GameLib.CustomCode.BODY_TYPE_CORNER;
|
if (body.bodyType === GameLib.CustomCode.BODY_TYPE_CORNER) {
|
||||||
|
/**
|
||||||
|
* First restore the backup before we start storing another corner
|
||||||
|
*/
|
||||||
|
body.restoreBackup();
|
||||||
|
|
||||||
this.state.turning = false;
|
|
||||||
|
|
||||||
} else {
|
}
|
||||||
|
|
||||||
if (body.bodyType === GameLib.CustomCode.BODY_TYPE_CORNER) {
|
if (this.snake[index - 1].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)) {
|
if (index === (this.snake.length - 1)) {
|
||||||
|
/**
|
||||||
|
* We're the tail - don't turn into a corner
|
||||||
|
*/
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
backup.bodyType = GameLib.CustomCode.BODY_TYPE_TAIL;
|
var corner = new GameLib.CustomCode.SnakeBody(
|
||||||
|
this.meshBreadCorner,
|
||||||
|
body.position,
|
||||||
|
body.orientation,
|
||||||
|
GameLib.CustomCode.BODY_TYPE_CORNER,
|
||||||
|
body
|
||||||
|
);
|
||||||
|
|
||||||
} else {
|
corner.applyToMesh();
|
||||||
|
|
||||||
body.backupMesh = body.mesh;
|
return corner;
|
||||||
|
}
|
||||||
|
|
||||||
body.backupMesh.visible = false;
|
body.applyToMesh();
|
||||||
|
|
||||||
body.backupMesh.updateInstance('visible');
|
return body;
|
||||||
|
|
||||||
body.mesh = this.meshBreadCorner.clone();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
body.bodyType = backup.bodyType;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
backup.x = temp.x;
|
|
||||||
backup.y = temp.y;
|
|
||||||
backup.orientation = temp.orientation;
|
|
||||||
backup.bodyType = temp.bodyType
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
|
|
Loading…
Reference in New Issue