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

beta.r3js.org
-=yb4f310 2018-03-24 11:10:06 +01:00
parent 4956323ad7
commit 40311bb233
1 changed files with 123 additions and 193 deletions

View File

@ -157,8 +157,7 @@ this.state = {
exploding : false, exploding : false,
lives : 3, lives : 3,
getReady : false, getReady : false,
message : null, message : null
overrideOrientation : false
}; };
GameLib.CustomCode.prototype.displayHUD = function() { GameLib.CustomCode.prototype.displayHUD = function() {
@ -204,10 +203,10 @@ GameLib.CustomCode.prototype.displayHUD = function() {
this.canvasHUD.text('xtra', 420, 480, '12px Pixeled', '#ffffff'); this.canvasHUD.text('xtra', 420, 480, '12px Pixeled', '#ffffff');
this.canvasHUD.text('slow', 420, 500, '12px Pixeled', '#ffffff'); this.canvasHUD.text('slow', 420, 500, '12px Pixeled', '#ffffff');
if (this.state.message) { if (this.state.message) {
this.canvasHUD.text(this.state.message, 220, 250, '30px Pixeled', '#ffffff'); this.canvasHUD.text(this.state.message, 220, 250, '30px Pixeled', '#ffffff');
} }
this.canvasHUD.image(this.imagePowerupSpeed.instance, 50, 460, 48, 48); this.canvasHUD.image(this.imagePowerupSpeed.instance, 50, 460, 48, 48);
this.canvasHUD.image(this.imagePowerupLife.instance, 205, 460, 48, 48); this.canvasHUD.image(this.imagePowerupLife.instance, 205, 460, 48, 48);
this.canvasHUD.image(this.imagePowerupSlow.instance, 360, 460, 48, 48); this.canvasHUD.image(this.imagePowerupSlow.instance, 360, 460, 48, 48);
@ -235,19 +234,16 @@ GameLib.CustomCode.prototype.waitReload = function(delta) {
this.state.exploding = false; this.state.exploding = false;
this.state.getReady = true; this.state.getReady = true;
this.advanceSnake(this.speed * 2); window.setTimeout(
window.setTimeout(
function(){ function(){
this.state.message = null; this.state.message = null;
this.displayHUD(); this.displayHUD();
}.bind(this), }.bind(this),
5000 5000
); );
window.setTimeout( window.setTimeout(
function(){ function(){
this.state.message = "GO!"; this.state.message = "GO!";
@ -289,8 +285,6 @@ GameLib.CustomCode.prototype.waitReload = function(delta) {
GameLib.CustomCode.prototype.visualizeGrid = function () { GameLib.CustomCode.prototype.visualizeGrid = function () {
return;
if (this.noneMesh) { if (this.noneMesh) {
this.scene.instance.remove(this.noneMesh); this.scene.instance.remove(this.noneMesh);
} }
@ -650,8 +644,8 @@ GameLib.CustomCode.prototype.createGameObject = function(
gameObject.applyToMesh(true); gameObject.applyToMesh(true);
break; break;
case GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY: case GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY:
gameObject = new GameLib.CustomCode.SnakeBody( gameObject = new GameLib.CustomCode.SnakeBody(
type, type,
mesh, mesh,
position, position,
@ -661,9 +655,9 @@ GameLib.CustomCode.prototype.createGameObject = function(
isTail isTail
) )
gameObject.applyToMesh(true); gameObject.applyToMesh(true);
this.animation.meshes.push(gameObject.mesh); this.animation.meshes.push(gameObject.mesh);
break; break;
default: default:
throw new Error('unhandled object type: ' + objectType); throw new Error('unhandled object type: ' + objectType);
@ -839,27 +833,27 @@ GameLib.CustomCode.SnakeBody.prototype = Object.create(GameLib.CustomCode.GameOb
GameLib.CustomCode.SnakeBody.prototype.constructor = GameLib.CustomCode.GameObject; GameLib.CustomCode.SnakeBody.prototype.constructor = GameLib.CustomCode.GameObject;
GameLib.CustomCode.SnakeBody.prototype.dispose = function() { GameLib.CustomCode.SnakeBody.prototype.dispose = function() {
GameLib.CustomCode.GameObject.prototype.dispose.call(this); GameLib.CustomCode.GameObject.prototype.dispose.call(this);
if (this.backupMesh) { if (this.backupMesh) {
this.backupMesh.geometry = null; this.backupMesh.geometry = null;
this.backupMesh.materials = null; this.backupMesh.materials = null;
this.backupMesh.remove(); this.backupMesh.remove();
this.backupMesh = null; this.backupMesh = null;
} }
this.flip = 0; this.flip = 0;
this.orientaiton = 0; this.orientaiton = 0;
this.isTail = false; this.isTail = false;
} }
GameLib.CustomCode.SnakeBody.prototype.clone = function() { GameLib.CustomCode.SnakeBody.prototype.clone = function() {
return new GameLib.CustomCode.SnakeBody( return new GameLib.CustomCode.SnakeBody(
this.type, this.type,
this.mesh, this.mesh,
{ {
x : this.position.x, x : this.position.x,
y : this.position.y y : this.position.y
}, },
this.orientation, this.orientation,
this.flip, this.flip,
this.backupMesh, this.backupMesh,
@ -937,9 +931,9 @@ GameLib.CustomCode.prototype.initializeGrid = function() {
function(x) { function(x) {
x.map( x.map(
function(y) { function(y) {
if (y) { if (y) {
y.dispose(); y.dispose();
} }
} }
); );
} }
@ -1157,40 +1151,21 @@ GameLib.CustomCode.prototype.createCorner = function(body, temp) {
}.bind(this); }.bind(this);
GameLib.CustomCode.prototype.backup = function() { GameLib.CustomCode.prototype.backup = function() {
if (GameLib.Utils.UndefinedOrNull(this.backupSnake)) {
this.backupSnake = this.snake.map(
function(body) {
return body.clone();
}
);
this.backupOrientation = this.state.orientation;
}
this.backupPrevious = this.backupSnake.map(
function(body) {
return body.clone();
}
);
this.backupSnake = this.snake.map(
function(body) {
return body.clone();
}
);
this.backupPreviousOrientation = this.backupOrientation; this.backup = {
snake : this.snake.map(
this.backupOrientation = this.state.orientation; function(body) {
return body.clone();
this.state.overrideOrientation = true; }
),
orientation : this.state.orientation,
flip : this.state.flip
};
}.bind(this) }.bind(this)
GameLib.CustomCode.prototype.restore = function() { GameLib.CustomCode.prototype.restore = function() {
this.snake.map( this.snake.map(
function(body) { function(body) {
@ -1200,42 +1175,42 @@ GameLib.CustomCode.prototype.restore = function() {
*/ */
this.grid[body.position.x][body.position.y] = null; this.grid[body.position.x][body.position.y] = null;
var meshFound = false; var meshFound = false;
var backupMeshFound = false; var backupMeshFound = false;
this.backupPrevious.map( this.backup.snake.map(
function (backupBody) { function (backupBody) {
if ( if (
body.mesh === backupBody.mesh || body.mesh === backupBody.mesh ||
body.mesh === backupBody.backupMesh body.mesh === backupBody.backupMesh
) { ) {
meshFound = true; meshFound = true;
} }
if ( if (
body.backupMesh === backupBody.mesh || body.backupMesh === backupBody.mesh ||
body.backupMesh === backupBody.backupMesh body.backupMesh === backupBody.backupMesh
) { ) {
backupMeshFound = true; backupMeshFound = true;
} }
}.bind(this) }.bind(this)
); );
if (!meshFound) { if (!meshFound) {
body.mesh.geometry = null; body.mesh.geometry = null;
body.mesh.materials = null; body.mesh.materials = null;
body.mesh.remove(); body.mesh.remove();
body.mesh = null; body.mesh = null;
} }
if (!backupMeshFound) { if (!backupMeshFound) {
body.backupMesh.geometry = null; body.backupMesh.geometry = null;
body.backupMesh.materials = null; body.backupMesh.materials = null;
body.backupMesh.remove(); body.backupMesh.remove();
body.backupMesh = null; body.backupMesh = null;
} }
}.bind(this) }.bind(this)
) )
@ -1245,22 +1220,25 @@ GameLib.CustomCode.prototype.restore = function() {
this.initializeGrid(); this.initializeGrid();
/** /**
* Restore our backup snake * Restore our backup
*/ */
this.snake = this.backupPrevious.map( this.state.orientation = this.backup.orientation;
function(body) { this.state.flip = this.backup.flip;
return body.clone(); this.snake = this.backup.snake.map(
function(body) {
var clone = body.clone();
clone.applyToMesh();
return clone;
}.bind(this) }.bind(this)
); );
this.state.orientation = this.backupPreviousOrientation; this.backup();
this.visualizeGrid();
this.backupSnake = null;
this.backup();
this.visualizeGrid();
}.bind(this) }.bind(this)
/** /**
@ -1284,34 +1262,34 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) {
var backup = null; var backup = null;
var temp = null; var temp = null;
var advanced = false; var advanced = false;
this.snake.map( this.snake.map(
function(body, index) { function(body, index) {
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');
body.mesh.updateInstance('position');
}
if (index === 0) { if (index === 0) {
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');
body.mesh.updateInstance('position');
}
backup = { backup = {
position : { position : {
x : body.position.x, x : body.position.x,
@ -1330,42 +1308,16 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) {
this.grid[backup.position.x][backup.position.y] = new GameLib.CustomCode.GameObject(); this.grid[backup.position.x][backup.position.y] = new GameLib.CustomCode.GameObject();
if (!advanced) { if (!advanced) {
if (this.state.getReady) {
body.position.x = backup.position.x;
body.position.y = backup.position.y;
body.orientation = backup.orientation;
body.flip = backup.flip;
body.applyToMesh();
return;
}
this.explode(body.position); this.explode(body.position);
} else { } else {
if (this.state.overrideOrientation) {
this.backupPreviousOrientation = this.state.orientation;
this.backupOrientation = this.state.orientation;
this.state.overrideOrientation = false;
}
var gameObject = this.grid[body.position.x][body.position.y]; var gameObject = this.grid[body.position.x][body.position.y];
switch (gameObject.objectType) { switch (gameObject.objectType) {
case GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY : case GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY :
//f (body.orientation !== backup.orientation) {
//body.orientation = backup.orientation;
//body.flip = backup.flip;
//backup.orientation = this.state.orientation;
//backup.flip = this.state.flip;
// this.createCorner(body, backup);
// body.applyToMesh();
//}
if (!gameObject.isTail) { if (!gameObject.isTail) {
if (body.orientation !== backup.orientation) { if (body.orientation !== backup.orientation) {
@ -1487,28 +1439,6 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) {
*/ */
this.grid[body.position.x][body.position.y] = body; this.grid[body.position.x][body.position.y] = body;
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');
body.mesh.updateInstance('position');
}
if (body.orientation !== temp.orientation) { if (body.orientation !== temp.orientation) {
this.createCorner(body, temp); this.createCorner(body, temp);
} }
@ -1523,10 +1453,10 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) {
this.state.turning = false; this.state.turning = false;
if (this.state.eating) { if (this.state.eating) {
this.backup(); this.backup();
} }
this.state.eating = false; this.state.eating = false;
this.visualizeGrid(); this.visualizeGrid();
@ -1546,7 +1476,7 @@ GameLib.Event.Subscribe(
lives : 3, lives : 3,
getReady : false, getReady : false,
message : null, message : null,
overrideOrientation : false overrideOrientation : false
}; };
this.initializeGrid(); this.initializeGrid();