2018-03-14 11:54:53 +01:00
|
|
|
if (this.parentEntity === data.entity) {
|
|
|
|
console.log('snake entity loaded');
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines
|
|
|
|
*/
|
|
|
|
GameLib.CustomCode.BODY_TYPE_NORMAL = 0x1;
|
|
|
|
GameLib.CustomCode.BODY_TYPE_TAIL = 0x2;
|
2018-03-15 11:25:17 +01:00
|
|
|
GameLib.CustomCode.BODY_TYPE_CORNER = 0x3;
|
2018-03-14 11:54:53 +01:00
|
|
|
|
2018-03-21 10:40:27 +01:00
|
|
|
GameLib.CustomCode.GRID_WIDTH = 11;
|
2018-03-21 10:43:05 +01:00
|
|
|
GameLib.CustomCode.GRID_HEIGHT = 10;
|
2018-03-14 11:54:53 +01:00
|
|
|
|
|
|
|
GameLib.CustomCode.GRID_OFFSET_X = 1;
|
2018-03-21 10:43:28 +01:00
|
|
|
GameLib.CustomCode.GRID_OFFSET_Y = 8;
|
2018-03-14 11:54:53 +01:00
|
|
|
|
2018-03-19 11:56:40 +01:00
|
|
|
GameLib.CustomCode.SPEED_INITIAL = 1;
|
2018-03-14 11:54:53 +01:00
|
|
|
|
|
|
|
GameLib.CustomCode.ORIENTATION_UP = 0;
|
|
|
|
GameLib.CustomCode.ORIENTATION_LEFT = 1;
|
|
|
|
GameLib.CustomCode.ORIENTATION_DOWN = 2;
|
|
|
|
GameLib.CustomCode.ORIENTATION_RIGHT = 3;
|
|
|
|
|
2018-03-20 15:07:58 +01:00
|
|
|
GameLib.CustomCode.FOOD_SPEED_INITIAL = 3;
|
|
|
|
GameLib.CustomCode.MAX_FOOD_ITEMS = 6;
|
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
GameLib.CustomCode.POWERUP_WAIT_TIME_MIN = 5;
|
|
|
|
GameLib.CustomCode.POWERUP_WAIT_TIME_MAX = 10;
|
|
|
|
GameLib.CustomCode.POWERUP_DURATION = 7;
|
|
|
|
GameLib.CustomCode.MAX_POWERUP_ITEMS = 3;
|
|
|
|
|
2018-03-20 15:26:31 +01:00
|
|
|
GameLib.CustomCode.FOOD_BACON = 0;
|
|
|
|
GameLib.CustomCode.FOOD_CHEESE = 1;
|
|
|
|
GameLib.CustomCode.FOOD_ONION = 2;
|
|
|
|
GameLib.CustomCode.FOOD_ONION_RING = 3;
|
|
|
|
GameLib.CustomCode.FOOD_PATTY = 4;
|
|
|
|
GameLib.CustomCode.FOOD_TOMATO = 5;
|
2018-03-20 15:07:58 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
GameLib.CustomCode.BODY_TYPE_BREAD_BACON = 0;
|
|
|
|
GameLib.CustomCode.BODY_TYPE_BREAD_CHEESE = 1;
|
|
|
|
GameLib.CustomCode.BODY_TYPE_BREAD_ONION = 2;
|
|
|
|
GameLib.CustomCode.BODY_TYPE_BREAD_ONION_RING = 3;
|
|
|
|
GameLib.CustomCode.BODY_TYPE_BREAD_PATTY = 4;
|
|
|
|
GameLib.CustomCode.BODY_TYPE_BREAD_HEAD = 5;
|
|
|
|
GameLib.CustomCode.BODY_TYPE_BREAD_TAIL = 6;
|
|
|
|
GameLib.CustomCode.BODY_TYPE_BREAD_CORNER = 7;
|
|
|
|
|
|
|
|
GameLib.CustomCode.OBJECT_TYPE_NONE = 0;
|
|
|
|
GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY = 1;
|
|
|
|
GameLib.CustomCode.OBJECT_TYPE_FOOD = 2;
|
|
|
|
GameLib.CustomCode.OBJECT_TYPE_POWERUP = 3;
|
2018-03-21 12:48:41 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
/**
|
|
|
|
* Get runtime
|
|
|
|
*/
|
|
|
|
this.runtime = GameLib.Utils.GetRuntime();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom Code Components
|
|
|
|
*/
|
|
|
|
this.beforeRender = GameLib.EntityManager.Instance.findComponentById('zjq6ach3jt');
|
|
|
|
this.keyUp = GameLib.EntityManager.Instance.findComponentById('306204wy29');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Geometries
|
|
|
|
*/
|
|
|
|
this.geometryBody = GameLib.EntityManager.Instance.findComponentById('8f5q7k5ozp');
|
|
|
|
|
2018-03-21 10:29:13 +01:00
|
|
|
GameLib.CustomCode.BODY_SCALE_X = this.geometryBody.width;
|
|
|
|
GameLib.CustomCode.BODY_SCALE_Y = this.geometryBody.height;
|
|
|
|
|
2018-03-21 11:39:57 +01:00
|
|
|
/**
|
|
|
|
* Materials
|
|
|
|
*/
|
|
|
|
this.materialTomato = GameLib.EntityManager.Instance.findComponentById('merwtvkm1t');
|
|
|
|
this.materialBacon = GameLib.EntityManager.Instance.findComponentById('x5ffcl6ojc');
|
|
|
|
this.materialCheese = GameLib.EntityManager.Instance.findComponentById('0rxqy5eddx');
|
|
|
|
this.materialOnionRing = GameLib.EntityManager.Instance.findComponentById('gz8zex5xug');
|
|
|
|
this.materialPatty = GameLib.EntityManager.Instance.findComponentById('f7y8nurbcv');
|
|
|
|
this.materialOnion = GameLib.EntityManager.Instance.findComponentById('im3tsuzwrp');
|
|
|
|
this.materialBreadBacon = GameLib.EntityManager.Instance.findComponentById('wn31yqi6lc');
|
|
|
|
this.materialBreadCheese = GameLib.EntityManager.Instance.findComponentById('i22ixqizge');
|
|
|
|
this.materialBreadOnion = GameLib.EntityManager.Instance.findComponentById('69sybcj08d');
|
|
|
|
this.materialBreadOnionRing = GameLib.EntityManager.Instance.findComponentById('kvesyjtr2v');
|
|
|
|
this.materialBreadPatty = GameLib.EntityManager.Instance.findComponentById('k6axym9bu5');
|
|
|
|
this.materialBreadHead = GameLib.EntityManager.Instance.findComponentById('heu4f7zzuh');
|
|
|
|
this.materialBreadTail = GameLib.EntityManager.Instance.findComponentById('mm2yq9rmpf');
|
|
|
|
this.materialBreadCorner = GameLib.EntityManager.Instance.findComponentById('ju9r1bw6cb');
|
2018-03-22 11:32:32 +01:00
|
|
|
this.materialPowerupSpeed = GameLib.EntityManager.Instance.findComponentById('fb32uutj9q');
|
|
|
|
this.materialPowerupLife = GameLib.EntityManager.Instance.findComponentById('6tld70a6zu');
|
|
|
|
this.materialPowerupSlow = GameLib.EntityManager.Instance.findComponentById('ulgntj8nta');
|
2018-03-14 11:54:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Other Objects (Scene)
|
|
|
|
*/
|
2018-03-19 10:23:48 +01:00
|
|
|
this.scene = GameLib.EntityManager.Instance.findComponentById('pllp034hsj');
|
|
|
|
this.animation = GameLib.EntityManager.Instance.findComponentById('8kb7utb2fn');
|
2018-03-19 11:08:26 +01:00
|
|
|
this.animationRotation = GameLib.EntityManager.Instance.findComponentById('z628kythyn');
|
2018-03-14 11:54:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Game objects
|
|
|
|
*/
|
|
|
|
this.snake = [];
|
2018-03-21 12:48:41 +01:00
|
|
|
this.grid = [[]];
|
2018-03-22 11:32:32 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
this.speed = GameLib.CustomCode.SPEED_INITIAL;
|
|
|
|
this.advanceTime = 0;
|
2018-03-22 11:32:32 +01:00
|
|
|
|
2018-03-21 12:48:41 +01:00
|
|
|
this.foodTime = 0;
|
|
|
|
this.foodSpeed = GameLib.CustomCode.FOOD_SPEED_INITIAL;
|
|
|
|
this.food = [];
|
2018-03-22 11:32:32 +01:00
|
|
|
|
|
|
|
this.powerupTime = 0;
|
|
|
|
this.nextPowerupTime = GameLib.Utils.GetRandomIntInclusive(
|
|
|
|
GameLib.CustomCode.POWERUP_WAIT_TIME_MIN,
|
|
|
|
GameLib.CustomCode.POWERUP_WAIT_TIME_MAX
|
|
|
|
);
|
2018-03-21 12:48:41 +01:00
|
|
|
this.powerups = [];
|
2018-03-14 11:54:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Orientation is 0, 1, 2 or 3, (up, left, down, right) -
|
|
|
|
* This is also the amount we need to multiply with PI to get the mesh rotation
|
|
|
|
* @type {{direction: {x: number, y: number}, orientation: number}}
|
|
|
|
*/
|
|
|
|
this.state = {
|
2018-03-15 11:36:32 +01:00
|
|
|
orientation : 0,
|
2018-03-19 08:29:24 +01:00
|
|
|
turning : false,
|
2018-03-19 09:37:42 +01:00
|
|
|
flip : 0
|
2018-03-14 11:54:53 +01:00
|
|
|
};
|
|
|
|
|
2018-03-20 14:34:55 +01:00
|
|
|
GameLib.CustomCode.prototype.visualizeGrid = function (color) {
|
|
|
|
|
|
|
|
if (this.starsMesh) {
|
|
|
|
this.scene.instance.remove(this.starsMesh);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.starsGeometry = new THREE.Geometry();
|
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
this.grid.map(
|
|
|
|
function(x, xIndex) {
|
|
|
|
x.map(
|
|
|
|
function(y, yIndex) {
|
|
|
|
this.starsGeometry.vertices.push(
|
|
|
|
new THREE.Vector3(
|
|
|
|
(xIndex * GameLib.CustomCode.BODY_SCALE_X) + GameLib.CustomCode.GRID_OFFSET_X,
|
|
|
|
(yIndex * GameLib.CustomCode.BODY_SCALE_Y) + GameLib.CustomCode.GRID_OFFSET_Y,
|
|
|
|
5
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}.bind(this)
|
|
|
|
)
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
2018-03-20 14:34:55 +01:00
|
|
|
var starsMaterial = new THREE.PointsMaterial({color: color, size: 0.1});
|
|
|
|
|
|
|
|
this.starsMesh = new THREE.Points(this.starsGeometry, starsMaterial);
|
|
|
|
|
|
|
|
this.scene.instance.add(this.starsMesh);
|
|
|
|
|
|
|
|
}.bind(this)
|
|
|
|
|
2018-03-21 11:42:32 +01:00
|
|
|
GameLib.CustomCode.prototype.createGameMesh = function(material, visible) {
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(visible)) {
|
|
|
|
visible = true;
|
|
|
|
}
|
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
var mesh = new GameLib.D3.Mesh(
|
|
|
|
this.runtime.graphics,
|
|
|
|
{
|
2018-03-21 10:23:46 +01:00
|
|
|
geometry : this.geometryBody,
|
2018-03-21 11:42:32 +01:00
|
|
|
materials : [material],
|
2018-03-22 11:32:32 +01:00
|
|
|
visible : visible,
|
|
|
|
useQuaternion : false
|
2018-03-14 11:54:53 +01:00
|
|
|
}
|
|
|
|
)
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
this.scene.addClone(mesh);
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
return mesh;
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
}.bind(this)
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
/**
|
|
|
|
* GameLib.CustomCode.Object
|
|
|
|
* @param objectType
|
|
|
|
* @param type
|
|
|
|
* @param mesh
|
|
|
|
* @param position
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.CustomCode.GameObject = function(
|
|
|
|
objectType,
|
|
|
|
type,
|
|
|
|
mesh,
|
2018-03-22 11:42:43 +01:00
|
|
|
position
|
2018-03-22 11:32:32 +01:00
|
|
|
) {
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(objectType)) {
|
|
|
|
objectType = GameLib.CustomCode.OBJECT_TYPE_NONE;
|
|
|
|
}
|
|
|
|
this.objectType = objectType;
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(type)) {
|
|
|
|
type = -1;
|
|
|
|
}
|
|
|
|
this.type = type;
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(mesh)) {
|
|
|
|
mesh = null;
|
|
|
|
}
|
|
|
|
this.mesh = mesh;
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(position)) {
|
|
|
|
position = null;
|
|
|
|
}
|
|
|
|
this.position = position;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disposes of the mesh object and resets this object to its defaults
|
|
|
|
*/
|
|
|
|
GameLib.CustomCode.GameObject.prototype.dispose = function(removeMesh) {
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(removeMesh)) {
|
|
|
|
removeMesh = true;
|
|
|
|
}
|
2018-03-22 11:42:43 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
if (this.mesh && removeMesh) {
|
|
|
|
this.mesh.geometry = null;
|
|
|
|
this.mesh.materials = null;
|
|
|
|
this.mesh.remove();
|
|
|
|
this.mesh = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.objectType = GameLib.CustomCode.OBJECT_TYPE_NONE;
|
|
|
|
|
|
|
|
this.type = -1;
|
|
|
|
|
|
|
|
this.position = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies the object position to the mesh taking into account the offset and scale of the body
|
|
|
|
* @param updateInstance
|
|
|
|
*/
|
|
|
|
GameLib.CustomCode.GameObject.prototype.applyToMesh = function(updateInstance) {
|
|
|
|
if (this.mesh) {
|
|
|
|
|
|
|
|
this.mesh.position.x = (this.position.x * GameLib.CustomCode.BODY_SCALE_X) + GameLib.CustomCode.GRID_OFFSET_X;
|
|
|
|
this.mesh.position.y = (this.position.y * GameLib.CustomCode.BODY_SCALE_Y) + GameLib.CustomCode.GRID_OFFSET_Y;
|
|
|
|
this.mesh.position.z = 0.1;
|
|
|
|
|
|
|
|
if (updateInstance) {
|
|
|
|
this.mesh.updateInstance('position');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scans through the grid, gets all empty positions, and chooses a random one
|
|
|
|
* @type {function(this:snakeEntityLoaded)}
|
|
|
|
*/
|
|
|
|
GameLib.CustomCode.prototype.getFreeGridPosition = function() {
|
|
|
|
|
|
|
|
var x = GameLib.Utils.GetRandomInt(0, GameLib.CustomCode.GRID_WIDTH);
|
|
|
|
var y = GameLib.Utils.GetRandomInt(0, GameLib.CustomCode.GRID_HEIGHT);
|
|
|
|
|
|
|
|
return {
|
|
|
|
x : x,
|
|
|
|
y : y
|
|
|
|
};
|
|
|
|
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @type {function(this:snakeEntityLoaded)}
|
|
|
|
*/
|
|
|
|
GameLib.CustomCode.prototype.createGameObject = function(
|
|
|
|
objectType,
|
|
|
|
type,
|
|
|
|
position,
|
|
|
|
orientation
|
|
|
|
) {
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(type)) {
|
|
|
|
type = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var mesh = null;
|
|
|
|
var array = null;
|
|
|
|
|
|
|
|
if (objectType === GameLib.CustomCode.OBJECT_TYPE_POWERUP) {
|
|
|
|
type = GameLib.Utils.GetRandomIntInclusive(0, 3);
|
|
|
|
switch (type) {
|
|
|
|
case GameLib.CustomCode.POWERUP_LIFE :
|
|
|
|
mesh = this.createGameMesh(this.materialPowerupLife);
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.POWERUP_SLOW :
|
|
|
|
mesh = this.createGameMesh(this.materialPowerupSlow);
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.POWERUP_SPEED :
|
|
|
|
mesh = this.createGameMesh(this.materialPowerupSpeed);
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
throw new Error('unhandled power up type : ' + type)
|
|
|
|
}
|
|
|
|
|
|
|
|
array = this.powerups;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (objectType === GameLib.CustomCode.OBJECT_TYPE_FOOD) {
|
|
|
|
type = GameLib.Utils.GetRandomIntInclusive(0, 5);
|
|
|
|
switch (foodType) {
|
|
|
|
case GameLib.CustomCode.FOOD_BACON :
|
|
|
|
mesh = this.createGameMesh(this.materialBacon);
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.FOOD_CHEESE :
|
|
|
|
mesh = this.createGameMesh(this.materialCheese);
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.FOOD_ONION :
|
|
|
|
mesh = this.createGameMesh(this.materialOnion);
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.FOOD_ONION_RING :
|
|
|
|
mesh = this.createGameMesh(this.materialOnionRing);
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.FOOD_PATTY :
|
|
|
|
mesh = this.createGameMesh(this.materialPatty);
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.FOOD_TOMATO :
|
|
|
|
mesh = this.createGameMesh(this.materialTomato);
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
throw new Error('unhandled food type : ' + type)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We apply the scale to the food meshes too - since they appear too big when normal
|
|
|
|
*/
|
|
|
|
mesh.scale.x = 0.8;
|
|
|
|
mesh.scale.y = 0.8;
|
|
|
|
mesh.updateInstance('scale');
|
|
|
|
|
|
|
|
array = this.food;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (objectType === GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY) {
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case GameLib.CustomCode.BODY_TYPE_BREAD_BACON :
|
|
|
|
mesh = this.createGameMesh(this.materialBreadBacon);
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.BODY_TYPE_BREAD_CHEESE :
|
|
|
|
mesh = this.createGameMesh(this.materialBreadCheese);
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.BODY_TYPE_BREAD_ONION :
|
|
|
|
mesh = this.createGameMesh(this.materialBreadOnion);
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.BODY_TYPE_BREAD_ONION_RING :
|
|
|
|
mesh = this.createGameMesh(this.materialBreadOnionRing);
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.BODY_TYPE_BREAD_PATTY :
|
|
|
|
mesh = this.createGameMesh(this.materialBreadPatty);
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.BODY_TYPE_BREAD_HEAD :
|
|
|
|
mesh = this.createGameMesh(this.materialBreadHead);
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.BODY_TYPE_BREAD_TAIL :
|
|
|
|
mesh = this.createGameMesh(this.materialBreadTail);
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.BODY_TYPE_BREAD_CORNER :
|
|
|
|
mesh = this.createGameMesh(this.materialBreadCorner);
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
throw new Error('unhandled body type : ' + type)
|
|
|
|
}
|
|
|
|
|
|
|
|
array = this.snake;
|
|
|
|
}
|
|
|
|
|
|
|
|
var gameObject = null;
|
|
|
|
|
|
|
|
switch (objectType) {
|
|
|
|
case GameLib.CustomCode.OBJECT_TYPE_FOOD:
|
|
|
|
case GameLib.CustomCode.OBJECT_TYPE_POWERUP:
|
|
|
|
gameObject = new GameLib.CustomCode.GameObject(
|
|
|
|
objectType,
|
|
|
|
type,
|
|
|
|
mesh,
|
2018-03-22 11:42:43 +01:00
|
|
|
position
|
2018-03-22 11:32:32 +01:00
|
|
|
)
|
|
|
|
gameObject.applyToMesh(true);
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY:
|
|
|
|
gameObject = new GameLib.CustomCode.SnakeBody(
|
|
|
|
type,
|
|
|
|
mesh,
|
|
|
|
position,
|
|
|
|
orientation
|
|
|
|
)
|
|
|
|
gameObject.applyToMesh(true);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error('unhandled object type: ' + objectType);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the grid with this object
|
|
|
|
*/
|
|
|
|
var currentGameObject = this.grid[gameObject.position.x][gameObject.position.y];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If there is a powerup at this position - remove it
|
|
|
|
*/
|
|
|
|
if (currentGameObject.objectType === GameLib.CustomCode.OBJECT_TYPE_POWERUP) {
|
|
|
|
this.powerups.splice(
|
|
|
|
this.powerups.indexof(currentGameObject),
|
|
|
|
1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If there is food at this position - remove it
|
|
|
|
*/
|
|
|
|
if (currentGameObject.objectType === GameLib.CustomCode.OBJECT_TYPE_FOOD) {
|
|
|
|
this.food.splice(
|
|
|
|
this.food.indexof(currentGameObject),
|
|
|
|
1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If there is an existing snake body at this position - explode!
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.grid[gameObject.position.x][gameObject.position.y].dispose();
|
|
|
|
this.grid[gameObject.position.x][gameObject.position.y] = gameObject;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save a reference to this object to the specific object array
|
|
|
|
*/
|
|
|
|
array.push(gameObject);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If we have too many objects - dispose of the first on in the queue (fifo)
|
|
|
|
*/
|
|
|
|
if (this.food.length > GameLib.CustomCode.MAX_FOOD_ITEMS) {
|
|
|
|
gameObject = this.food.shift();
|
|
|
|
this.grid[gameObject.position.x][gameObject.position.y].dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.powerups.length > GameLib.CustomCode.MAX_POWERUP_ITEMS_ITEMS) {
|
|
|
|
gameObject = this.powerups.shift();
|
|
|
|
this.grid[gameObject.position.x][gameObject.position.y].dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
GameLib.CustomCode.prototype.createPowerup = function(delta) {
|
|
|
|
|
|
|
|
this.powerupTime += delta;
|
|
|
|
|
|
|
|
if (this.powerupTime > this.nextPowerupTime) {
|
|
|
|
this.powerupTime = 0;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.createGameObject(
|
|
|
|
GameLib.CustomCode.OBJECT_TYPE_POWERUP,
|
|
|
|
null,
|
|
|
|
this.getFreeGridPosition()
|
|
|
|
);
|
|
|
|
|
|
|
|
}.bind(this);
|
|
|
|
|
2018-03-20 15:07:58 +01:00
|
|
|
GameLib.CustomCode.prototype.createFood = function(delta) {
|
|
|
|
|
|
|
|
this.foodTime += delta;
|
|
|
|
|
|
|
|
if (this.foodTime > this.foodSpeed) {
|
|
|
|
this.foodTime = 0;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
this.createGameObject(
|
|
|
|
GameLib.CustomCode.OBJECT_TYPE_FOOD,
|
|
|
|
null,
|
|
|
|
this.getFreeGridPosition()
|
|
|
|
);
|
|
|
|
|
2018-03-20 15:27:53 +01:00
|
|
|
}.bind(this);
|
2018-03-20 15:07:58 +01:00
|
|
|
|
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
/**
|
|
|
|
* GameLib.CustomCode.SnakeBody
|
2018-03-22 11:32:32 +01:00
|
|
|
* @param type
|
2018-03-14 11:54:53 +01:00
|
|
|
* @param mesh
|
|
|
|
* @param position
|
|
|
|
* @param orientation
|
2018-03-19 08:29:24 +01:00
|
|
|
* @param flip
|
2018-03-19 08:35:53 +01:00
|
|
|
* @param backupMesh
|
2018-03-22 11:32:32 +01:00
|
|
|
* @param isTail
|
2018-03-22 11:42:43 +01:00
|
|
|
* @property animation
|
2018-03-14 11:54:53 +01:00
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.CustomCode.SnakeBody = function(
|
2018-03-22 11:32:32 +01:00
|
|
|
type,
|
2018-03-14 11:54:53 +01:00
|
|
|
mesh,
|
|
|
|
position,
|
2018-03-15 11:25:17 +01:00
|
|
|
orientation,
|
2018-03-19 08:35:53 +01:00
|
|
|
flip,
|
2018-03-19 11:26:42 +01:00
|
|
|
backupMesh,
|
2018-03-22 11:32:32 +01:00
|
|
|
isTail
|
2018-03-14 11:54:53 +01:00
|
|
|
) {
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
GameLib.CustomCode.GameObject.call(
|
|
|
|
this,
|
|
|
|
GameLib.Object.OBJECT_TYPE_SNAKE_BODY,
|
|
|
|
type,
|
|
|
|
mesh,
|
2018-03-22 11:42:43 +01:00
|
|
|
position
|
2018-03-22 11:32:32 +01:00
|
|
|
);
|
2018-03-22 11:42:43 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(orientation)) {
|
|
|
|
orientation = GameLib.CustomCode.ORIENTATION_UP;
|
|
|
|
}
|
|
|
|
this.orientation = orientation;
|
2018-03-19 08:35:53 +01:00
|
|
|
|
2018-03-19 08:29:24 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(flip)) {
|
2018-03-19 09:37:42 +01:00
|
|
|
flip = 0;
|
2018-03-15 12:46:00 +01:00
|
|
|
}
|
2018-03-19 08:29:24 +01:00
|
|
|
this.flip = flip;
|
2018-03-15 12:46:00 +01:00
|
|
|
|
2018-03-19 08:35:53 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(backupMesh)) {
|
|
|
|
backupMesh = null;
|
|
|
|
}
|
|
|
|
this.backupMesh = backupMesh;
|
2018-03-19 10:08:33 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(isTail)) {
|
2018-03-19 11:26:42 +01:00
|
|
|
isTail = false;
|
|
|
|
}
|
|
|
|
this.isTail = isTail;
|
2018-03-22 11:32:32 +01:00
|
|
|
|
2018-03-19 10:08:33 +01:00
|
|
|
this.applyToMesh();
|
2018-03-22 11:42:43 +01:00
|
|
|
|
|
|
|
var animation = GameLib.EntityManager.Instance.findComponentById('8kb7utb2fn');
|
|
|
|
|
|
|
|
animation.meshes.push(this.mesh);
|
2018-03-14 11:54:53 +01:00
|
|
|
};
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
GameLib.CustomCode.SnakeBody.prototype = Object.create(GameLib.CustomCode.GameObject.prototype);
|
|
|
|
GameLib.CustomCode.SnakeBody.prototype.constructor = GameLib.CustomCode.GameObject;
|
|
|
|
|
|
|
|
GameLib.CustomCode.SnakeBody.prototype.applyToMesh = function(updateInstance) {
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
GameLib.CustomCode.GameObject.prototype.applyToMesh.call(this, updateInstance);
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
this.mesh.rotation.z = this.orientation * Math.PI / 2;
|
2018-03-19 08:35:53 +01:00
|
|
|
|
2018-03-19 09:37:42 +01:00
|
|
|
if (this.backupMesh) {
|
2018-03-19 10:08:33 +01:00
|
|
|
this.mesh.rotation.z += ((Math.PI / 2) * this.flip);
|
2018-03-22 11:32:32 +01:00
|
|
|
this.mesh.updateInstance('position');
|
|
|
|
this.mesh.updateInstance('rotation');
|
2018-03-19 08:29:24 +01:00
|
|
|
}
|
2018-03-19 08:35:53 +01:00
|
|
|
|
2018-03-21 12:48:41 +01:00
|
|
|
this.mesh.updateInstance('rotation');
|
2018-03-14 11:54:53 +01:00
|
|
|
}
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-19 10:08:33 +01:00
|
|
|
GameLib.CustomCode.SnakeBody.prototype.advance = function(orientation, flip) {
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
var crash = false;
|
|
|
|
|
|
|
|
switch (orientation) {
|
|
|
|
case GameLib.CustomCode.ORIENTATION_UP :
|
|
|
|
if ((this.position.y + 1) >= GameLib.CustomCode.GRID_HEIGHT) {
|
|
|
|
crash = true;
|
|
|
|
} else {
|
|
|
|
this.position.y += 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.ORIENTATION_DOWN :
|
|
|
|
if ((this.position.y - 1) < 0) {
|
|
|
|
crash = true;
|
|
|
|
} else {
|
|
|
|
this.position.y -= 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.ORIENTATION_LEFT :
|
|
|
|
if ((this.position.x - 1) < 0) {
|
|
|
|
crash = true;
|
|
|
|
} else {
|
|
|
|
this.position.x -= 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.ORIENTATION_RIGHT :
|
|
|
|
if ((this.position.x + 1) >= GameLib.CustomCode.GRID_WIDTH) {
|
|
|
|
crash = true;
|
|
|
|
} else {
|
|
|
|
this.position.x += 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
console.warn('unknown orientation');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crash) {
|
|
|
|
console.log('crashed!');
|
|
|
|
}
|
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
this.orientation = orientation;
|
2018-03-19 10:08:33 +01:00
|
|
|
|
|
|
|
this.flip = flip;
|
2018-03-22 11:32:32 +01:00
|
|
|
|
|
|
|
return !crash;
|
2018-03-14 11:54:53 +01:00
|
|
|
}
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-21 12:48:41 +01:00
|
|
|
GameLib.CustomCode.prototype.initializeGrid = function() {
|
|
|
|
|
|
|
|
this.grid = [];
|
|
|
|
|
|
|
|
for (var x = 0; x < GameLib.CustomCode.GRID_WIDTH; x++) {
|
|
|
|
this.grid[x] = [];
|
|
|
|
for (var y = 0; y < GameLib.CustomCode.GRID_HEIGHT; y++) {
|
2018-03-22 11:32:32 +01:00
|
|
|
this.grid[x][y] = new GameLib.CustomCode.GameObject();
|
2018-03-21 12:48:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}.bind(this);
|
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
// GameLib.CustomCode.prototype.rebuildGrid = function() {
|
|
|
|
//
|
|
|
|
// this.initializeGrid();
|
|
|
|
//
|
|
|
|
// /**
|
|
|
|
// * First add all the snake body parts
|
|
|
|
// */
|
|
|
|
// this.snake.map(
|
|
|
|
// function(body, index) {
|
|
|
|
// this.grid[body.position.x][body.position.y] = {
|
|
|
|
// occupied: true,
|
|
|
|
// type : GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY,
|
|
|
|
// index : index
|
|
|
|
// }
|
|
|
|
// }.bind(this)
|
|
|
|
// );
|
|
|
|
//
|
|
|
|
// /**
|
|
|
|
// * Then add all the food bits
|
|
|
|
// */
|
|
|
|
// this.food.map(
|
|
|
|
// function(food, index) {
|
|
|
|
// this.grid[food.position.x][food.position.y] = {
|
|
|
|
// occupied: true,
|
|
|
|
// type : GameLib.CustomCode.OBJECT_TYPE_FOOD,
|
|
|
|
// index : index
|
|
|
|
// }
|
|
|
|
// }.bind(this)
|
|
|
|
// );
|
|
|
|
//
|
|
|
|
// /**
|
|
|
|
// * And also all the power ups
|
|
|
|
// */
|
|
|
|
// this.powerups.map(
|
|
|
|
// function(powerup, index) {
|
|
|
|
// this.grid[powerup.position.x][powerup.position.y] = {
|
|
|
|
// occupied: true,
|
|
|
|
// type : GameLib.CustomCode.OBJECT_TYPE_POWERUP,
|
|
|
|
// index : index
|
|
|
|
// }
|
|
|
|
// }.bind(this)
|
|
|
|
// );
|
|
|
|
//
|
|
|
|
// }.bind(this);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Explode the snake
|
|
|
|
* @param position
|
|
|
|
*/
|
|
|
|
GameLib.CustomCode.prototype.explode = function(position) {
|
|
|
|
console.log('explode snake at position : ' + position.x + ', ' + position.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extend the snake with information from gameObject
|
|
|
|
* @param gameObject
|
|
|
|
*/
|
|
|
|
GameLib.CustomCode.prototype.extend = function(gameObject) {
|
|
|
|
console.log('extend');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Powerup the snake with information from gameObject
|
|
|
|
* @param gameObject
|
|
|
|
*/
|
|
|
|
GameLib.CustomCode.prototype.powerup = function(gameObject) {
|
|
|
|
console.log('powerup');
|
|
|
|
};
|
2018-03-21 12:48:41 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
/**
|
|
|
|
* Move the snake forward and do collision detection
|
|
|
|
* @type {function(this:snakeEntityLoaded)}
|
|
|
|
*/
|
2018-03-14 11:54:53 +01:00
|
|
|
GameLib.CustomCode.prototype.advanceSnake = function(delta) {
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-19 08:29:24 +01:00
|
|
|
this.advanceTime += delta;
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-19 08:29:24 +01:00
|
|
|
if (this.advanceTime > this.speed) {
|
|
|
|
this.advanceTime = 0;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-19 08:29:24 +01:00
|
|
|
var backup = null;
|
|
|
|
var temp = null;
|
2018-03-15 15:12:26 +01:00
|
|
|
|
2018-03-19 08:29:24 +01:00
|
|
|
this.snake.map(
|
|
|
|
function(body, index) {
|
2018-03-15 12:46:00 +01:00
|
|
|
|
2018-03-19 08:29:24 +01:00
|
|
|
if (index === 0) {
|
2018-03-15 15:12:26 +01:00
|
|
|
|
2018-03-19 08:29:24 +01:00
|
|
|
backup = {
|
|
|
|
position : {
|
|
|
|
x : body.position.x,
|
|
|
|
y : body.position.y
|
|
|
|
},
|
2018-03-19 10:08:33 +01:00
|
|
|
orientation : body.orientation,
|
|
|
|
flip : body.flip
|
2018-03-19 08:29:24 +01:00
|
|
|
}
|
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
var advanced = body.advance(
|
2018-03-19 10:08:33 +01:00
|
|
|
this.state.orientation,
|
2018-03-22 11:32:32 +01:00
|
|
|
this.state.flip,
|
|
|
|
this.grid
|
2018-03-19 08:29:24 +01:00
|
|
|
);
|
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
if (!advanced) {
|
2018-03-22 11:42:43 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
this.explode(body.position);
|
2018-03-22 11:42:43 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
} else {
|
2018-03-22 11:42:43 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
var gameObject = this.grid[body.position.x][body.position.y];
|
2018-03-22 11:42:43 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
switch (gameObject.objectType) {
|
|
|
|
case GameLib.CustomCode.OBJECT_TYPE_SNAKE_BODY :
|
|
|
|
this.explode(body.position);
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.OBJECT_TYPE_FOOD :
|
|
|
|
this.extend(gameObject);
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.OBJECT_TYPE_POWERUP:
|
|
|
|
this.powerup(gameObject);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/**
|
|
|
|
* Update the grid with the body
|
|
|
|
*/
|
|
|
|
this.grid[body.position.x][body.position.y] = body;
|
|
|
|
break;
|
|
|
|
}
|
2018-03-22 11:42:43 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
/**
|
|
|
|
* Food or powerup objects need to be destroyed before being replaced with a body object
|
|
|
|
*/
|
|
|
|
if (
|
|
|
|
gameObject.objectType === GameLib.CustomCode.OBJECT_TYPE_FOOD ||
|
2018-03-22 11:42:43 +01:00
|
|
|
gameObject.objectType === GameLib.CustomCode.OBJECT_TYPE_POWERUP
|
2018-03-22 11:32:32 +01:00
|
|
|
) {
|
2018-03-22 11:42:43 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
if (gameObject.objectType === GameLib.CustomCode.OBJECT_TYPE_FOOD) {
|
|
|
|
this.food.splice(
|
|
|
|
this.food.indexOf(gameObject),
|
|
|
|
1
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.powerups.splice(
|
|
|
|
this.powerups.indexOf(gameObject),
|
|
|
|
1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
gameObject.dispose();
|
2018-03-22 11:42:43 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
this.grid[body.position.x][body.position.y] = body;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-19 10:08:33 +01:00
|
|
|
backup.orientation = body.orientation;
|
|
|
|
backup.flip = body.flip;
|
2018-03-19 08:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (index > 0) {
|
2018-03-15 15:24:56 +01:00
|
|
|
|
2018-03-19 08:29:24 +01:00
|
|
|
temp = {
|
|
|
|
position : {
|
|
|
|
x : body.position.x,
|
|
|
|
y : body.position.y
|
|
|
|
},
|
2018-03-19 10:08:33 +01:00
|
|
|
orientation : body.orientation,
|
|
|
|
flip : body.flip
|
2018-03-15 15:01:32 +01:00
|
|
|
}
|
2018-03-15 12:46:00 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2018-03-19 08:29:24 +01:00
|
|
|
body.position.x = backup.position.x;
|
|
|
|
body.position.y = backup.position.y;
|
|
|
|
body.orientation = backup.orientation;
|
2018-03-19 10:08:33 +01:00
|
|
|
body.flip = backup.flip;
|
2018-03-15 12:46:00 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
/**
|
|
|
|
* Update the grid with our new body
|
|
|
|
*/
|
|
|
|
this.grid[body.position.x][body.position.y] = body;
|
2018-03-22 11:42:43 +01:00
|
|
|
|
2018-03-19 08:29:24 +01:00
|
|
|
if (body.backupMesh) {
|
|
|
|
/**
|
|
|
|
* We used to be a corner, change back
|
|
|
|
* @type {null}
|
|
|
|
*/
|
2018-03-19 10:52:57 +01:00
|
|
|
body.mesh.geometry = null;
|
2018-03-15 15:17:44 +01:00
|
|
|
|
2018-03-19 10:52:57 +01:00
|
|
|
body.mesh.materials = null;
|
2018-03-15 15:12:26 +01:00
|
|
|
|
2018-03-19 10:52:57 +01:00
|
|
|
body.mesh.remove();
|
2018-03-15 15:17:44 +01:00
|
|
|
|
2018-03-19 08:29:24 +01:00
|
|
|
body.mesh = body.backupMesh;
|
2018-03-15 15:17:44 +01:00
|
|
|
|
2018-03-19 08:29:24 +01:00
|
|
|
body.backupMesh = null;
|
2018-03-15 15:17:44 +01:00
|
|
|
|
2018-03-19 08:29:24 +01:00
|
|
|
body.mesh.visible = true;
|
2018-03-15 15:17:44 +01:00
|
|
|
|
2018-03-19 08:29:24 +01:00
|
|
|
body.mesh.updateInstance('visible');
|
2018-03-19 10:41:19 +01:00
|
|
|
|
|
|
|
body.mesh.updateInstance('position');
|
2018-03-22 11:32:32 +01:00
|
|
|
}
|
2018-03-15 15:17:44 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
if (body.orientation !== temp.orientation) {
|
2018-03-15 15:17:44 +01:00
|
|
|
|
2018-03-20 10:36:19 +01:00
|
|
|
if ((index ) < this.snake.length) {
|
2018-03-19 08:29:24 +01:00
|
|
|
/**
|
|
|
|
* Our orientation changed - we should make a corner
|
2018-03-22 11:32:32 +01:00
|
|
|
*/
|
|
|
|
body.orientation = temp.orientation;
|
|
|
|
|
|
|
|
body.applyToMesh();
|
|
|
|
|
|
|
|
body.orientation = backup.orientation;
|
|
|
|
|
2018-03-19 10:48:03 +01:00
|
|
|
body.backupMesh = body.mesh;
|
2018-03-22 11:32:32 +01:00
|
|
|
|
|
|
|
if (temp.orientation === GameLib.CustomCode.ORIENTATION_UP) {
|
|
|
|
body.backupMesh.position.y -= 0.5 * GameLib.CustomCode.BODY_SCALE_Y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temp.orientation === GameLib.CustomCode.ORIENTATION_DOWN) {
|
|
|
|
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_RIGHT) {
|
|
|
|
body.backupMesh.position.x -= 0.5 * GameLib.CustomCode.BODY_SCALE_X;
|
|
|
|
}
|
|
|
|
|
|
|
|
//body.backupMesh.visible = false;
|
2018-03-15 15:17:44 +01:00
|
|
|
|
2018-03-22 11:42:43 +01:00
|
|
|
//body.backupMesh.updateInstance('visible');
|
2018-03-22 11:32:32 +01:00
|
|
|
|
|
|
|
body.mesh = this.createGameMesh(this.materialBreadCorner);
|
|
|
|
|
|
|
|
body.mesh.position.z = 5;
|
|
|
|
|
|
|
|
body.mesh.updateInstance('position');
|
|
|
|
|
|
|
|
body.mesh.visible = true;
|
|
|
|
|
|
|
|
body.mesh.updateInstance('visible');
|
2018-03-19 11:04:20 +01:00
|
|
|
}
|
2018-03-22 11:32:32 +01:00
|
|
|
|
2018-03-19 10:08:33 +01:00
|
|
|
}
|
2018-03-22 11:32:32 +01:00
|
|
|
|
|
|
|
backup = temp;
|
2018-03-19 08:29:24 +01:00
|
|
|
}
|
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
//if (!body.isTail) {
|
|
|
|
|
2018-03-20 10:32:39 +01:00
|
|
|
body.applyToMesh();
|
2018-03-22 11:32:32 +01:00
|
|
|
//}
|
2018-03-15 15:12:26 +01:00
|
|
|
|
2018-03-19 08:29:24 +01:00
|
|
|
}.bind(this)
|
|
|
|
)
|
2018-03-15 15:12:26 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
this.state.turning = false;
|
2018-03-15 15:17:44 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
}.bind(this);
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
GameLib.Event.Subscribe(
|
|
|
|
GameLib.Event.GAME_START,
|
|
|
|
function() {
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
/**
|
|
|
|
* Remove all existing snake parts
|
|
|
|
*/
|
|
|
|
this.snake.map(
|
|
|
|
function(body) {
|
2018-03-22 11:42:43 +01:00
|
|
|
body.dispose();
|
2018-03-14 11:54:53 +01:00
|
|
|
}
|
|
|
|
)
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
this.food.map(
|
2018-03-21 12:48:41 +01:00
|
|
|
function(food) {
|
2018-03-22 11:42:43 +01:00
|
|
|
food.dispose();
|
2018-03-21 12:48:41 +01:00
|
|
|
}
|
|
|
|
)
|
2018-03-22 11:32:32 +01:00
|
|
|
|
|
|
|
this.powerups.map(
|
|
|
|
function(powerup) {
|
2018-03-22 11:42:43 +01:00
|
|
|
powerup.dispose();
|
2018-03-22 11:32:32 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
this.state = {
|
|
|
|
orientation : GameLib.CustomCode.ORIENTATION_UP
|
|
|
|
};
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
this.snake = [
|
|
|
|
new GameLib.CustomCode.SnakeBody(
|
2018-03-22 11:42:43 +01:00
|
|
|
GameLib.CustomCode.BODY_TYPE_BREAD_HEAD,
|
2018-03-14 11:54:53 +01:00
|
|
|
{
|
|
|
|
x : 4,
|
|
|
|
y : 4
|
|
|
|
},
|
2018-03-22 11:42:43 +01:00
|
|
|
GameLib.CustomCode.ORIENTATION_UP
|
2018-03-14 11:54:53 +01:00
|
|
|
),
|
|
|
|
new GameLib.CustomCode.SnakeBody(
|
2018-03-22 11:42:43 +01:00
|
|
|
GameLib.CustomCode.BODY_TYPE_BREAD_TAIL,
|
2018-03-14 11:54:53 +01:00
|
|
|
{
|
|
|
|
x : 4,
|
|
|
|
y : 3
|
|
|
|
},
|
2018-03-22 11:42:43 +01:00
|
|
|
GameLib.CustomCode.ORIENTATION_UP
|
2018-03-14 11:54:53 +01:00
|
|
|
)
|
|
|
|
];
|
2018-03-22 11:42:43 +01:00
|
|
|
|
2018-03-22 11:32:32 +01:00
|
|
|
this.initializeGrid();
|
2018-03-22 11:42:43 +01:00
|
|
|
//this.visualizeGrid();
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
/**
|
|
|
|
* Other Settings
|
|
|
|
*/
|
2018-03-22 11:32:32 +01:00
|
|
|
this.advanceTime = 0;
|
2018-03-14 11:54:53 +01:00
|
|
|
this.speed = GameLib.CustomCode.SPEED_INITIAL;
|
2018-03-22 11:32:32 +01:00
|
|
|
|
|
|
|
this.foodTime = 0;
|
|
|
|
this.foodSpeed = GameLib.CustomCode.FOOD_SPEED_INITIAL;
|
|
|
|
this.food = [];
|
|
|
|
|
|
|
|
this.powerupTime = 0;
|
|
|
|
this.nextPowerupTime = GameLib.Utils.GetRandomIntInclusive(
|
|
|
|
GameLib.CustomCode.POWERUP_WAIT_TIME_MIN,
|
|
|
|
GameLib.CustomCode.POWERUP_WAIT_TIME_MAX
|
|
|
|
);
|
|
|
|
this.powerups = [];
|
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
/**
|
|
|
|
* Re-initialize our other custom code components
|
|
|
|
*/
|
|
|
|
this.beforeRender.initialized = false;
|
|
|
|
this.beforeRender.entityLoaded = this;
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
this.keyUp.initialized = false;
|
|
|
|
this.keyUp.entityLoaded = this;
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
console.log('starting game snake');
|
|
|
|
}.bind(this)
|
|
|
|
);
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
GameLib.Event.Subscribe(
|
|
|
|
GameLib.Event.GAME_OVER,
|
|
|
|
function() {
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
this.keyUp.initialized = false;
|
|
|
|
this.keyUp.entityLoaded = null;
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
this.beforeRender.initialized = false;
|
|
|
|
this.beforeRender.entityLoaded = null;
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
console.log('starting game snake');
|
|
|
|
}.bind(this)
|
|
|
|
)
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
GameLib.Event.Emit(GameLib.Event.GAME_LOADED);
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
//@ sourceURL=entityLoaded.js
|