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

beta.r3js.org
-=yb4f310 2018-03-23 08:40:51 +01:00
parent c39dc83dda
commit cc02305c6e
1 changed files with 170 additions and 168 deletions

View File

@ -136,8 +136,8 @@ this.state = {
turning : false,
flip : 0,
eating : false,
exploding : false,
lives : 2
exploding : false,
lives : 2
};
GameLib.CustomCode.prototype.visualizeGrid = function () {
@ -333,25 +333,25 @@ GameLib.CustomCode.GameObject.prototype.applyToMesh = function(updateInstance) {
*/
GameLib.CustomCode.prototype.getFreeGridPosition = function() {
var positions = [];
this.grid.map(
function(row, x) {
row.map(
function(object, y) {
if (object.objectType === GameLib.CustomCode.OBJECT_TYPE_NONE) {
positions.push(
{
x : x,
y : y
}
)
}
}
);
}
);
var positions = [];
this.grid.map(
function(row, x) {
row.map(
function(object, y) {
if (object.objectType === GameLib.CustomCode.OBJECT_TYPE_NONE) {
positions.push(
{
x : x,
y : y
}
)
}
}
);
}
);
var index = GameLib.Utils.GetRandomInt(0, positions.length);
return positions[index];
@ -531,7 +531,7 @@ GameLib.CustomCode.prototype.createGameObject = function(
if (currentGameObject.objectType === GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY) {
console.warn('should not reach this code if collision detection is perfect');
this.explode(currentGameObject.position);
return;
return gameObject;
}
this.grid[gameObject.position.x][gameObject.position.y].dispose();
@ -750,16 +750,16 @@ GameLib.CustomCode.prototype.initializeGrid = function() {
*/
GameLib.CustomCode.prototype.explode = function(position) {
console.log('explode snake at position : ' + position.x + ', ' + position.y);
this.state.exploding = true;
this.state.lives -= 1;
this.particleEnginePickle.position.x = (position.x * GameLib.CustomCode.BODY_SCALE_X) + GameLib.CustomCode.GRID_OFFSET_X;
this.particleEnginePickle.position.y = (position.y * GameLib.CustomCode.BODY_SCALE_Y) + GameLib.CustomCode.GRID_OFFSET_Y;
this.particleEnginePickle.updateInstance('position');
this.particleEnginePickle.enabled = true;
this.state.exploding = true;
this.state.lives -= 1;
this.particleEnginePickle.position.x = (position.x * GameLib.CustomCode.BODY_SCALE_X) + GameLib.CustomCode.GRID_OFFSET_X;
this.particleEnginePickle.position.y = (position.y * GameLib.CustomCode.BODY_SCALE_Y) + GameLib.CustomCode.GRID_OFFSET_Y;
this.particleEnginePickle.updateInstance('position');
this.particleEnginePickle.enabled = true;
}
/**
@ -779,10 +779,10 @@ GameLib.CustomCode.prototype.extend = function(gameObject, position, orientation
body = this.createGameObject(
GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY,
GameLib.CustomCode.BODY_TYPE_BREAD_BACON,
{
x : position.x,
y : position.y
},
{
x : position.x,
y : position.y
},
orientation
)
break;
@ -791,9 +791,9 @@ GameLib.CustomCode.prototype.extend = function(gameObject, position, orientation
GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY,
GameLib.CustomCode.BODY_TYPE_BREAD_CHEESE,
{
x : position.x,
y : position.y
},
x : position.x,
y : position.y
},
orientation
)
break;
@ -802,9 +802,9 @@ GameLib.CustomCode.prototype.extend = function(gameObject, position, orientation
GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY,
GameLib.CustomCode.BODY_TYPE_BREAD_ONION,
{
x : position.x,
y : position.y
},
x : position.x,
y : position.y
},
orientation
)
break;
@ -813,9 +813,9 @@ GameLib.CustomCode.prototype.extend = function(gameObject, position, orientation
GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY,
GameLib.CustomCode.BODY_TYPE_BREAD_ONION_RING,
{
x : position.x,
y : position.y
},
x : position.x,
y : position.y
},
orientation
)
break;
@ -824,9 +824,9 @@ GameLib.CustomCode.prototype.extend = function(gameObject, position, orientation
GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY,
GameLib.CustomCode.BODY_TYPE_BREAD_PATTY,
{
x : position.x,
y : position.y
},
x : position.x,
y : position.y
},
orientation
)
break;
@ -835,9 +835,9 @@ GameLib.CustomCode.prototype.extend = function(gameObject, position, orientation
GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY,
GameLib.CustomCode.BODY_TYPE_BREAD_TOMATO,
{
x : position.x,
y : position.y
},
x : position.x,
y : position.y
},
orientation
)
break;
@ -845,7 +845,7 @@ GameLib.CustomCode.prototype.extend = function(gameObject, position, orientation
throw new Error('unhandled food type: ' + gameObject.type);
}
/*
/*
if (orientation === GameLib.CustomCode.ORIENTATION_UP) {
body.mesh.position.y -= 0.5 * GameLib.CustomCode.BODY_SCALE_Y;
}
@ -870,7 +870,7 @@ GameLib.CustomCode.prototype.extend = function(gameObject, position, orientation
/**
* Food or powerup objects need to be destroyed before being replaced with a body object
*/
*/
this.food.splice(
this.food.indexOf(gameObject),
1
@ -879,7 +879,7 @@ GameLib.CustomCode.prototype.extend = function(gameObject, position, orientation
gameObject.dispose();
this.grid[body.position.x][body.position.y] = body;
}.bind(this);
/**
@ -895,47 +895,47 @@ GameLib.CustomCode.prototype.powerup = function(gameObject) {
);
gameObject.dispose();
}.bind(this);
GameLib.CustomCode.prototype.createCorner = function(body, temp) {
var backupOrientation = body.orientation;
body.orientation = temp.orientation;
var backupOrientation = body.orientation;
body.applyToMesh();
body.orientation = temp.orientation;
body.orientation = backupOrientation;
body.applyToMesh();
body.backupMesh = body.mesh;
body.orientation = backupOrientation;
if (temp.orientation === GameLib.CustomCode.ORIENTATION_UP) {
body.backupMesh.position.y -= 0.5 * GameLib.CustomCode.BODY_SCALE_Y;
}
body.backupMesh = body.mesh;
if (temp.orientation === GameLib.CustomCode.ORIENTATION_DOWN) {
body.backupMesh.position.y += 0.5 * GameLib.CustomCode.BODY_SCALE_Y;
}
if (temp.orientation === GameLib.CustomCode.ORIENTATION_UP) {
body.backupMesh.position.y -= 0.5 * GameLib.CustomCode.BODY_SCALE_Y;
}
if (temp.orientation === GameLib.CustomCode.ORIENTATION_LEFT) {
body.backupMesh.position.x += 0.5 * GameLib.CustomCode.BODY_SCALE_X;
}
if (temp.orientation === GameLib.CustomCode.ORIENTATION_DOWN) {
body.backupMesh.position.y += 0.5 * GameLib.CustomCode.BODY_SCALE_Y;
}
if (temp.orientation === GameLib.CustomCode.ORIENTATION_RIGHT) {
body.backupMesh.position.x -= 0.5 * GameLib.CustomCode.BODY_SCALE_X;
}
if (temp.orientation === GameLib.CustomCode.ORIENTATION_LEFT) {
body.backupMesh.position.x += 0.5 * GameLib.CustomCode.BODY_SCALE_X;
}
body.mesh = this.createGameMesh(this.materialBreadCorner);
if (temp.orientation === GameLib.CustomCode.ORIENTATION_RIGHT) {
body.backupMesh.position.x -= 0.5 * GameLib.CustomCode.BODY_SCALE_X;
}
body.mesh.position.z = 5;
body.mesh = this.createGameMesh(this.materialBreadCorner);
body.mesh.updateInstance('position');
body.mesh.position.z = 5;
body.mesh.visible = true;
body.mesh.updateInstance('position');
body.mesh.visible = true;
body.mesh.updateInstance('visible');
body.mesh.updateInstance('visible');
}.bind(this);
/**
@ -943,7 +943,7 @@ GameLib.CustomCode.prototype.createCorner = function(body, temp) {
* @type {function(this:snakeEntityLoaded)}
*/
GameLib.CustomCode.prototype.advanceSnake = function(delta) {
this.advanceTime += delta;
if (this.advanceTime > this.speed) {
@ -975,40 +975,44 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) {
this.state.flip,
this.grid
);
this.grid[backup.position.x][backup.position.y] = new GameLib.CustomCode.GameObject();
if (!advanced) {
this.explode(body.position);
this.grid[backup.position.x][backup.position.y] = new GameLib.CustomCode.GameObject();
} else {
if (!advanced) {
var gameObject = this.grid[body.position.x][body.position.y];
this.explode(body.position);
switch (gameObject.objectType) {
case GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY :
this.explode(body.position);
break;
case GameLib.CustomCode.OBJECT_TYPE_FOOD :
this.extend(gameObject, backup.position, backup.orientation);
break;
case GameLib.CustomCode.OBJECT_TYPE_POWERUP:
this.powerup(gameObject);
break;
default:
break;
}
}
this.grid[body.position.x][body.position.y] = body
this.visualizeGrid();
backup.orientation = body.orientation;
} else {
var gameObject = this.grid[body.position.x][body.position.y];
switch (gameObject.objectType) {
case GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY :
this.explode(body.position);
break;
case GameLib.CustomCode.OBJECT_TYPE_FOOD :
this.extend(gameObject, backup.position, backup.orientation);
break;
case GameLib.CustomCode.OBJECT_TYPE_POWERUP:
this.powerup(gameObject);
break;
default:
break;
}
}
this.grid[body.position.x][body.position.y] = body
this.visualizeGrid();
backup.orientation = body.orientation;
backup.flip = body.flip;
} else {
temp = {
if (this.state.exploding === true) {
return;
}
temp = {
position : {
x : body.position.x,
y : body.position.y
@ -1016,64 +1020,64 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) {
orientation : body.orientation,
flip : body.flip
}
if (this.state.eating === true) {
if ((body.orientation !== backup.orientation) &&
(index === 1)) {
body.orientation = backup.orientation;
body.flip = backup.flip;
this.createCorner(body, temp);
body.applyToMesh();
}
return;
}
/**
* Create a new empty object at the current grid position (free it up)
*/
this.grid[body.position.x][body.position.y] = new GameLib.CustomCode.GameObject();
/**
* Assign the new location to the body
*/
body.position.x = backup.position.x;
body.position.y = backup.position.y;
body.orientation = backup.orientation;
body.flip = backup.flip;
if (this.state.eating === true) {
/**
* Update the grid with our new 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;
if ((body.orientation !== backup.orientation) &&
(index === 1)) {
body.orientation = backup.orientation;
body.flip = backup.flip;
this.createCorner(body, temp);
body.applyToMesh();
}
body.mesh.materials = null;
return;
}
body.mesh.remove();
/**
* Create a new empty object at the current grid position (free it up)
*/
this.grid[body.position.x][body.position.y] = new GameLib.CustomCode.GameObject();
body.mesh = body.backupMesh;
/**
* Assign the new location to the body
*/
body.position.x = backup.position.x;
body.position.y = backup.position.y;
body.orientation = backup.orientation;
body.flip = backup.flip;
body.backupMesh = null;
/**
* Update the grid with our new body
*/
this.grid[body.position.x][body.position.y] = body;
body.mesh.visible = true;
if (body.backupMesh) {
/**
* We used to be a corner, change back
* @type {null}
*/
body.mesh.geometry = null;
body.mesh.updateInstance('visible');
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) {
this.createCorner(body, temp);
}
body.mesh.updateInstance('position');
}
if (body.orientation !== temp.orientation) {
this.createCorner(body, temp);
}
backup = temp;
}
@ -1085,9 +1089,7 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) {
this.state.turning = false;
this.state.eating = false;
this.visualizeGrid();
}.bind(this);
@ -1117,14 +1119,14 @@ GameLib.Event.Subscribe(
}
);
this.state = {
orientation : 0,
turning : false,
flip : 0,
eating : false,
exploding : false,
lives : 2
};
this.state = {
orientation : 0,
turning : false,
flip : 0,
eating : false,
exploding : false,
lives : 2
};
this.initializeGrid();
this.visualizeGrid();