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 = {
orientation : 0,
turning : false
turning : false,
flip : false
};
GameLib.CustomCode.prototype.createMaterial = function(image) {
@ -126,16 +127,14 @@ this.meshPatty = this.createGameMesh(this.imagePatty);
* @param mesh
* @param position
* @param orientation
* @param bodyType
* @param backup
* @param flip
* @constructor
*/
GameLib.CustomCode.SnakeBody = function(
mesh,
position,
orientation,
bodyType,
backup
flip
) {
if (GameLib.Utils.UndefinedOrNull(mesh)) {
@ -156,52 +155,50 @@ GameLib.CustomCode.SnakeBody = function(
orientation = GameLib.CustomCode.ORIENTATION_UP;
}
this.orientation = orientation;
if (GameLib.Utils.UndefinedOrNull(bodyType)) {
bodyType = GameLib.CustomCode.BODY_TYPE_NORMAL;
if (GameLib.Utils.UndefinedOrNull(flip)) {
flip = false;
}
this.bodyType = bodyType;
this.flip = flip;
if (GameLib.Utils.UndefinedOrNull(backup)) {
backup = null;
}
this.backup = backup;
this.applyToMesh();
this.applyToMesh(flip);
};
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) {
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(flip) {
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.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
*/
@ -233,98 +230,98 @@ GameLib.CustomCode.SnakeBody.prototype.advance = function(orientation) {
GameLib.CustomCode.prototype.advanceSnake = function(delta) {
this.advanceTime += delta;
this.advanceTime += delta;
if (this.advanceTime > this.speed) {
this.advanceTime = 0;
} else {
return;
}
if (this.advanceTime > this.speed) {
this.advanceTime = 0;
} else {
return;
}
var backup = null;
var temp = null;
var backup = null;
var temp = null;
this.snake.map(
function(body, index) {
this.snake.map(
function(body, index) {
if (index === 0) {
if (index === 0) {
backup = {
position : {
x : body.position.x,
y : body.position.y
},
orientation : body.orientation
}
body.advance(
this.state.orientation
);
//backup.orientation = body.orientation;
backup = {
position : {
x : body.position.x,
y : body.position.y
},
orientation : body.orientation
}
if (index > 0) {
body.advance(
this.state.orientation
);
temp = {
position : {
x : body.position.x,
y : body.position.y
},
orientation : body.orientation
}
//backup.orientation = body.orientation;
}
body.position.x = backup.position.x;
body.position.y = backup.position.y;
body.orientation = backup.orientation;
if (index > 0) {
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
* @type {null}
* Our orientation changed - we should make a corner
*/
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.backupMesh = null;
body.mesh.visible = true;
body.mesh.updateInstance('visible');
body.mesh = this.meshBreadCorner.clone();
} else {
body.orientation = this.snake[index - 1].orientation;
}
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);
@ -400,8 +397,7 @@ GameLib.Event.Subscribe(
x : 4,
y : 1
},
0,
GameLib.CustomCode.BODY_TYPE_TAIL
0
)
];
@ -410,13 +406,13 @@ GameLib.Event.Subscribe(
*
this.grid.map(
function(x) {
x.map(
function(y) {
y.mesh.geometry = null;
y.mesh.materials = null;
y.mesh.remove();
}
);
x.map(
function(y) {
y.mesh.geometry = null;
y.mesh.materials = null;
y.mesh.remove();
}
);
}
);