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

beta.r3js.org
-=yb4f310 2018-03-19 08:29:24 +01:00
parent a0a9c72e5f
commit 0f00831ae4
1 changed files with 119 additions and 123 deletions

View File

@ -69,7 +69,8 @@ this.advanceTime = 0;
*/ */
this.state = { this.state = {
orientation : 0, orientation : 0,
turning : false turning : false,
flip : false
}; };
GameLib.CustomCode.prototype.createMaterial = function(image) { GameLib.CustomCode.prototype.createMaterial = function(image) {
@ -126,16 +127,14 @@ this.meshPatty = this.createGameMesh(this.imagePatty);
* @param mesh * @param mesh
* @param position * @param position
* @param orientation * @param orientation
* @param bodyType * @param flip
* @param backup
* @constructor * @constructor
*/ */
GameLib.CustomCode.SnakeBody = function( GameLib.CustomCode.SnakeBody = function(
mesh, mesh,
position, position,
orientation, orientation,
bodyType, flip
backup
) { ) {
if (GameLib.Utils.UndefinedOrNull(mesh)) { if (GameLib.Utils.UndefinedOrNull(mesh)) {
@ -156,52 +155,50 @@ GameLib.CustomCode.SnakeBody = function(
orientation = GameLib.CustomCode.ORIENTATION_UP; orientation = GameLib.CustomCode.ORIENTATION_UP;
} }
this.orientation = orientation; this.orientation = orientation;
if (GameLib.Utils.UndefinedOrNull(bodyType)) { if (GameLib.Utils.UndefinedOrNull(flip)) {
bodyType = GameLib.CustomCode.BODY_TYPE_NORMAL; flip = false;
} }
this.bodyType = bodyType; this.flip = flip;
if (GameLib.Utils.UndefinedOrNull(backup)) { this.applyToMesh(flip);
backup = null;
}
this.backup = backup;
this.applyToMesh();
}; };
GameLib.CustomCode.SnakeBody.prototype.restoreBackup = function() { // 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.backup = null;
//
// /**
// * We don't restore the position because the position will have changed
// */
// }
if (!this.backup) { GameLib.CustomCode.SnakeBody.prototype.applyToMesh = function(flip) {
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; this.mesh.position.x = this.position.x + GameLib.CustomCode.GRID_OFFSET_X;
this.mesh.position.y = this.position.y + GameLib.CustomCode.GRID_OFFSET_Y; this.mesh.position.y = this.position.y + GameLib.CustomCode.GRID_OFFSET_Y;
this.mesh.rotation.z = this.orientation * Math.PI / 2; this.mesh.rotation.z = this.orientation * Math.PI / 2;
if (flip) {
this.mesh.rotation.z += Math.PI / 2;
}
/** /**
* TODO: We don't update instance position - animation should do this * TODO: We don't update instance position - animation should do this
*/ */
@ -233,98 +230,98 @@ GameLib.CustomCode.SnakeBody.prototype.advance = function(orientation) {
GameLib.CustomCode.prototype.advanceSnake = function(delta) { GameLib.CustomCode.prototype.advanceSnake = function(delta) {
this.advanceTime += delta; this.advanceTime += delta;
if (this.advanceTime > this.speed) { if (this.advanceTime > this.speed) {
this.advanceTime = 0; this.advanceTime = 0;
} else { } else {
return; return;
} }
var backup = null; var backup = null;
var temp = null; var temp = null;
this.snake.map( this.snake.map(
function(body, index) { function(body, index) {
if (index === 0) { if (index === 0) {
backup = { backup = {
position : { position : {
x : body.position.x, x : body.position.x,
y : body.position.y y : body.position.y
}, },
orientation : body.orientation orientation : body.orientation
}
body.advance(
this.state.orientation
);
//backup.orientation = body.orientation;
} }
if (index > 0) { body.advance(
this.state.orientation
);
temp = { //backup.orientation = body.orientation;
position : { }
x : body.position.x,
y : body.position.y
},
orientation : body.orientation
}
body.position.x = backup.position.x; if (index > 0) {
body.position.y = backup.position.y;
body.orientation = backup.orientation;
if (body.backupMesh) { temp = {
position : {
x : body.position.x,
y : body.position.y
},
orientation : body.orientation
}
body.position.x = backup.position.x;
body.position.y = backup.position.y;
body.orientation = backup.orientation;
if (body.backupMesh) {
/**
* We used to be a corner, change back
* @type {null}
*/
body.mesh.geometry = null;
body.mesh.materials = null;
body.mesh.remove();
body.mesh = body.backupMesh;
body.backupMesh = null;
body.mesh.visible = true;
body.mesh.updateInstance('visible');
}
if (
body.orientation !== this.snake[index - 1].orientation
) {
if ((index + 1) < this.snake.length) {
/** /**
* We used to be a corner, change back * Our orientation changed - we should make a corner
* @type {null}
*/ */
body.mesh.geometry = null; body.backupMesh = body.mesh;
body.mesh.materials = null; body.backupMesh.visible = false;
body.mesh.remove(); body.backupMesh.updateInstance('visible');
body.mesh = body.backupMesh; body.mesh = this.meshBreadCorner.clone();
} else {
body.backupMesh = null; body.orientation = this.snake[index - 1].orientation;
body.mesh.visible = true;
body.mesh.updateInstance('visible');
} }
if (
body.orientation !== this.snake[index - 1].orientation
) {
if ((index + 1) < this.snake.length) {
/**
* Our orientation changed - we should make a corner
*/
body.backupMesh = body.mesh;
body.backupMesh.visible = false;
body.backupMesh.updateInstance('visible');
body.mesh = this.meshBreadCorner.clone();
} else {
body.orientation = this.snake[index - 1].orientation;
}
}
backup = temp;
} }
body.applyToMesh(); backup = temp;
}
}.bind(this) body.applyToMesh(this.state.flip);
)
}.bind(this)
)
}.bind(this); }.bind(this);
@ -400,8 +397,7 @@ GameLib.Event.Subscribe(
x : 4, x : 4,
y : 1 y : 1
}, },
0, 0
GameLib.CustomCode.BODY_TYPE_TAIL
) )
]; ];
@ -410,13 +406,13 @@ 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();
} }
); );
} }
); );