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:42:38 +01:00
|
|
|
GameLib.CustomCode.GRID_OFFSET_Y = 7.5;
|
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-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-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-14 11:54:53 +01:00
|
|
|
/**
|
|
|
|
* Images
|
|
|
|
*/
|
|
|
|
this.imageBreadHead = GameLib.EntityManager.Instance.findComponentById('swkla3wpp');
|
|
|
|
this.imageBreadTail = GameLib.EntityManager.Instance.findComponentById('se6qlnmojd');
|
|
|
|
this.imageBreadPatty = GameLib.EntityManager.Instance.findComponentById('01r51k9ptr');
|
|
|
|
this.imageBreadCorner = GameLib.EntityManager.Instance.findComponentById('iljpuouaok');
|
2018-03-20 15:26:31 +01:00
|
|
|
|
|
|
|
this.imageBacon = GameLib.EntityManager.Instance.findComponentById('t4lb4hr4ue');
|
|
|
|
this.imageCheese = GameLib.EntityManager.Instance.findComponentById('y2611hx41r');
|
|
|
|
this.imageOnion = GameLib.EntityManager.Instance.findComponentById('g3z1wg2vt8');
|
|
|
|
this.imageOnionRing = GameLib.EntityManager.Instance.findComponentById('ztcyy0bgk5');
|
2018-03-14 11:54:53 +01:00
|
|
|
this.imagePatty = GameLib.EntityManager.Instance.findComponentById('4lf0fw24su');
|
2018-03-20 15:26:31 +01:00
|
|
|
this.imageTomato = GameLib.EntityManager.Instance.findComponentById('ptvwmo8l38');
|
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 = [];
|
|
|
|
this.grid = [];
|
|
|
|
this.speed = GameLib.CustomCode.SPEED_INITIAL;
|
|
|
|
this.advanceTime = 0;
|
2018-03-20 15:07:58 +01:00
|
|
|
this.foodTime = 0;
|
|
|
|
this.foodSpeed = GameLib.CustomCode.FOOD_SPEED_INITIAL;
|
|
|
|
this.foodItems = [];
|
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-20 14:41:09 +01:00
|
|
|
this.grid.map(
|
2018-03-20 14:42:06 +01:00
|
|
|
function(x, xIndex) {
|
|
|
|
x.map(
|
|
|
|
function(y, yIndex) {
|
2018-03-20 14:41:09 +01:00
|
|
|
this.starsGeometry.vertices.push(
|
|
|
|
new THREE.Vector3(
|
2018-03-21 10:31:29 +01:00
|
|
|
(xIndex * GameLib.CustomCode.BODY_SCALE_X) + GameLib.CustomCode.GRID_OFFSET_X,
|
|
|
|
(yIndex * GameLib.CustomCode.BODY_SCALE_Y) + GameLib.CustomCode.GRID_OFFSET_Y,
|
2018-03-20 14:41:09 +01:00
|
|
|
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-14 11:54:53 +01:00
|
|
|
GameLib.CustomCode.prototype.createMaterial = function(image) {
|
|
|
|
var diffuseMap = new GameLib.D3.Texture.Image(
|
|
|
|
this.runtime.graphics,
|
|
|
|
{
|
2018-03-19 17:39:28 +01:00
|
|
|
image : image,
|
|
|
|
repeat : {
|
|
|
|
x : 0.99,
|
|
|
|
y : 0.99
|
|
|
|
}
|
2018-03-14 11:54:53 +01:00
|
|
|
}
|
|
|
|
)
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
return new GameLib.D3.Material.Phong(
|
|
|
|
this.runtime.graphics,
|
|
|
|
{
|
2018-03-19 12:01:59 +01:00
|
|
|
diffuseMap : diffuseMap,
|
|
|
|
transparent : true
|
2018-03-14 11:54:53 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}.bind(this);
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-20 15:26:31 +01:00
|
|
|
GameLib.CustomCode.prototype.createGameMesh = function(image, visible) {
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-20 15:26:31 +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,
|
|
|
|
materials : [this.createMaterial(image)],
|
|
|
|
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-14 11:54:53 +01:00
|
|
|
/**
|
|
|
|
* Create our objects
|
|
|
|
*/
|
2018-03-21 10:35:52 +01:00
|
|
|
/*
|
2018-03-21 09:58:10 +01:00
|
|
|
this.meshBreadHead = this.createGameMesh(this.imageBreadHead, false);
|
2018-03-19 07:59:29 +01:00
|
|
|
this.meshBreadHead.useQuaternion = false;
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-21 09:58:10 +01:00
|
|
|
this.meshBreadTail = this.createGameMesh(this.imageBreadTail, false);
|
2018-03-19 07:59:29 +01:00
|
|
|
this.meshBreadTail.useQuaternion = false;
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-21 09:58:10 +01:00
|
|
|
this.meshBreadPatty = this.createGameMesh(this.imageBreadPatty, false);
|
2018-03-19 07:59:29 +01:00
|
|
|
this.meshBreadPatty.useQuaternion = false;
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-21 09:58:10 +01:00
|
|
|
this.meshBreadCorner = this.createGameMesh(this.imageBreadCorner, false);
|
2018-03-19 07:59:29 +01:00
|
|
|
this.meshBreadCorner.useQuaternion = false;
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-20 15:26:31 +01:00
|
|
|
this.meshBacon = this.createGameMesh(this.imageBacon, false);
|
|
|
|
this.meshCheese = this.createGameMesh(this.imageCheese, false);
|
|
|
|
this.meshOnion = this.createGameMesh(this.imageOnion, false);
|
|
|
|
this.meshOnionRing = this.createGameMesh(this.imageOnionRing, false);
|
|
|
|
this.meshPatty = this.createGameMesh(this.imagePatty, false);
|
|
|
|
this.meshTomato = this.createGameMesh(this.imageTomato, false);
|
2018-03-21 10:35:52 +01:00
|
|
|
*/
|
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-20 15:26:31 +01:00
|
|
|
var foodType = GameLib.Utils.GetRandomIntInclusive(0, 5);
|
|
|
|
|
|
|
|
var mesh = null;
|
2018-03-20 15:07:58 +01:00
|
|
|
|
2018-03-20 15:26:31 +01:00
|
|
|
switch (foodType) {
|
|
|
|
case GameLib.CustomCode.FOOD_BACON :
|
2018-03-21 10:35:52 +01:00
|
|
|
mesh = this.createGameMesh(this.imageBacon);
|
2018-03-20 15:26:31 +01:00
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.FOOD_CHEESE :
|
2018-03-21 10:35:52 +01:00
|
|
|
mesh = this.createGameMesh(this.imageCheese);
|
2018-03-20 15:26:31 +01:00
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.FOOD_ONION :
|
2018-03-21 10:35:52 +01:00
|
|
|
mesh = this.createGameMesh(this.imageOnion);
|
2018-03-20 15:26:31 +01:00
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.FOOD_ONION_RING :
|
2018-03-21 10:35:52 +01:00
|
|
|
mesh = this.createGameMesh(this.imageOnionRing);
|
2018-03-20 15:26:31 +01:00
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.FOOD_PATTY :
|
2018-03-21 10:35:52 +01:00
|
|
|
mesh = this.createGameMesh(this.imagePatty);
|
2018-03-20 15:26:31 +01:00
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.FOOD_TOMATO :
|
2018-03-21 10:35:52 +01:00
|
|
|
mesh = this.createGameMesh(this.imageTomato);
|
2018-03-20 15:26:31 +01:00
|
|
|
break;
|
|
|
|
default :
|
|
|
|
console.warn('unhandled food type');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
var positionX = GameLib.Utils.GetRandomInt(0, GameLib.CustomCode.GRID_WIDTH);
|
|
|
|
var positionY = GameLib.Utils.GetRandomInt(0, GameLib.CustomCode.GRID_HEIGHT);
|
|
|
|
|
2018-03-21 10:34:18 +01:00
|
|
|
mesh.position.x = (positionX * GameLib.CustomCode.BODY_SCALE_X) + GameLib.CustomCode.GRID_OFFSET_X;
|
|
|
|
mesh.position.y = (positionY * GameLib.CustomCode.BODY_SCALE_Y) + GameLib.CustomCode.GRID_OFFSET_Y;
|
2018-03-20 15:26:31 +01:00
|
|
|
|
|
|
|
mesh.visible = true;
|
|
|
|
|
|
|
|
mesh.updateInstance('position');
|
|
|
|
mesh.updateInstance('visible');
|
2018-03-20 15:07:58 +01:00
|
|
|
|
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
|
|
|
|
* @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-14 11:54:53 +01:00
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.CustomCode.SnakeBody = function(
|
|
|
|
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,
|
|
|
|
isTail
|
2018-03-14 11:54:53 +01:00
|
|
|
) {
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(mesh)) {
|
|
|
|
throw new Error('no mesh specified');
|
|
|
|
}
|
2018-03-21 10:23:46 +01:00
|
|
|
this.mesh = mesh;
|
2018-03-19 10:23:48 +01:00
|
|
|
|
2018-03-19 14:19:29 +01:00
|
|
|
this.mesh.position.z = GameLib.CustomCode.SnakeBody.LastZ;
|
|
|
|
GameLib.CustomCode.SnakeBody.LastZ += 0.1;
|
|
|
|
|
2018-03-19 14:21:10 +01:00
|
|
|
this.mesh.updateInstance('position');
|
2018-03-21 09:59:00 +01:00
|
|
|
this.mesh.updateInstance('visible');
|
2018-03-19 14:21:10 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(position)) {
|
|
|
|
position = {
|
|
|
|
x : Math.round(GameLib.CustomCode.GRID_WIDTH / 2),
|
|
|
|
y : Math.round(GameLib.CustomCode.GRID_HEIGHT / 2)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
this.position = position;
|
2018-03-14 11:53:15 +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-19 11:26:42 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(isTail)) {
|
|
|
|
isTail = false;
|
|
|
|
}
|
|
|
|
this.isTail = isTail;
|
|
|
|
|
2018-03-19 10:08:33 +01:00
|
|
|
this.applyToMesh();
|
2018-03-14 11:54:53 +01:00
|
|
|
};
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-19 14:19:29 +01:00
|
|
|
GameLib.CustomCode.SnakeBody.LastZ = 0;
|
|
|
|
|
2018-03-19 10:08:33 +01:00
|
|
|
GameLib.CustomCode.SnakeBody.prototype.applyToMesh = function() {
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-21 10:33:19 +01:00
|
|
|
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;
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-19 11:49:38 +01:00
|
|
|
//if ((this.mesh.rotation.z - (this.orientation * Math.PI / 2)) > Math.PI) {
|
|
|
|
// this.mesh.rotation.z -= Math.PI;
|
|
|
|
// this.mesh.updateInstance('rotation');
|
|
|
|
//}
|
2018-03-19 11:46:38 +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-19 10:50:39 +01:00
|
|
|
this.mesh.updateInstance('position');
|
2018-03-19 11:11:23 +01:00
|
|
|
this.mesh.updateInstance('rotation');
|
2018-03-19 08:29:24 +01:00
|
|
|
}
|
2018-03-19 08:35:53 +01:00
|
|
|
|
2018-03-14 11:53:15 +01:00
|
|
|
/**
|
2018-03-14 11:54:53 +01:00
|
|
|
* TODO: We don't update instance position - animation should do this
|
2018-03-14 11:53:15 +01:00
|
|
|
*/
|
2018-03-19 10:23:48 +01:00
|
|
|
// this.mesh.updateInstance('position');
|
2018-03-20 10:37:19 +01:00
|
|
|
//if (!this.isTail) {
|
2018-03-19 11:26:42 +01:00
|
|
|
this.mesh.updateInstance('rotation');
|
2018-03-20 10:37:19 +01:00
|
|
|
//}
|
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-20 14:29:47 +01:00
|
|
|
var crash = false;
|
|
|
|
|
|
|
|
switch (orientation) {
|
|
|
|
case GameLib.CustomCode.ORIENTATION_UP :
|
2018-03-20 14:36:44 +01:00
|
|
|
if ((this.position.y + 1) >= GameLib.CustomCode.GRID_HEIGHT) {
|
2018-03-20 14:29:47 +01:00
|
|
|
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 :
|
2018-03-20 14:36:44 +01:00
|
|
|
if ((this.position.x + 1) >= GameLib.CustomCode.GRID_WIDTH) {
|
2018-03-20 14:29:47 +01:00
|
|
|
crash = true;
|
|
|
|
} else {
|
|
|
|
this.position.x += 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
console.warn('unknown orientation');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!crash) {
|
|
|
|
console.log('check against body');
|
|
|
|
} else {
|
|
|
|
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-14 11:54:53 +01:00
|
|
|
}
|
2018-03-14 11:53:15 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
body.advance(
|
2018-03-19 10:08:33 +01:00
|
|
|
this.state.orientation,
|
|
|
|
this.state.flip
|
2018-03-19 08:29:24 +01:00
|
|
|
);
|
|
|
|
|
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-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-19 11:01:05 +01:00
|
|
|
|
2018-03-19 10:08:33 +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-15 15:17:44 +01:00
|
|
|
|
2018-03-19 08:29:24 +01:00
|
|
|
if (
|
2018-03-19 09:25:07 +01:00
|
|
|
body.orientation !== temp.orientation
|
2018-03-19 08:29:24 +01:00
|
|
|
) {
|
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-19 11:01:05 +01:00
|
|
|
*/
|
2018-03-19 11:02:45 +01:00
|
|
|
body.orientation = temp.orientation;
|
2018-03-19 11:01:05 +01:00
|
|
|
|
2018-03-19 10:52:35 +01:00
|
|
|
body.applyToMesh();
|
|
|
|
|
2018-03-19 11:02:45 +01:00
|
|
|
body.orientation = backup.orientation;
|
|
|
|
|
2018-03-19 10:48:03 +01:00
|
|
|
body.backupMesh = body.mesh;
|
2018-03-20 10:23:14 +01:00
|
|
|
|
|
|
|
if (temp.orientation === GameLib.CustomCode.ORIENTATION_UP) {
|
|
|
|
body.backupMesh.position.y -= 0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temp.orientation === GameLib.CustomCode.ORIENTATION_DOWN) {
|
|
|
|
body.backupMesh.position.y += 0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temp.orientation === GameLib.CustomCode.ORIENTATION_LEFT) {
|
|
|
|
body.backupMesh.position.x += 0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temp.orientation === GameLib.CustomCode.ORIENTATION_RIGHT) {
|
|
|
|
body.backupMesh.position.x -= 0.5;
|
|
|
|
}
|
2018-03-20 10:20:44 +01:00
|
|
|
|
|
|
|
//body.backupMesh.visible = false;
|
2018-03-15 15:17:44 +01:00
|
|
|
|
2018-03-20 10:12:12 +01:00
|
|
|
//body.backupMesh.updateInstance('visible');
|
2018-03-19 10:30:33 +01:00
|
|
|
|
2018-03-21 10:37:54 +01:00
|
|
|
body.mesh = this.createGameMesh(this.imageBreadCorner);
|
2018-03-20 08:40:39 +01:00
|
|
|
|
2018-03-20 08:41:08 +01:00
|
|
|
body.mesh.position.z = 5;
|
2018-03-20 08:40:39 +01:00
|
|
|
|
2018-03-20 08:40:55 +01:00
|
|
|
body.mesh.updateInstance('position');
|
2018-03-21 09:59:34 +01:00
|
|
|
|
|
|
|
body.mesh.visible = true;
|
|
|
|
|
|
|
|
body.mesh.updateInstance('visible');
|
2018-03-19 11:04:20 +01:00
|
|
|
}
|
2018-03-19 10:17:01 +01:00
|
|
|
|
2018-03-19 10:08:33 +01:00
|
|
|
}
|
2018-03-19 11:01:05 +01:00
|
|
|
|
|
|
|
backup = temp;
|
2018-03-19 08:29:24 +01:00
|
|
|
}
|
|
|
|
|
2018-03-20 10:32:39 +01:00
|
|
|
//if (!body.isTail) {
|
2018-03-20 10:29:10 +01:00
|
|
|
|
2018-03-20 10:32:39 +01:00
|
|
|
body.applyToMesh();
|
|
|
|
//}
|
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-19 10:11:53 +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-20 14:34:55 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
GameLib.CustomCode.prototype.initializeGrid = function() {
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
this.grid = [];
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
for (var x = 0; x < GameLib.CustomCode.GRID_WIDTH; x++) {
|
|
|
|
this.grid[x] = [];
|
|
|
|
for (var y = 0; y < GameLib.CustomCode.GRID_HEIGHT; y++) {
|
|
|
|
this.grid[x][y] = {
|
2018-03-20 14:34:55 +01:00
|
|
|
mesh : null
|
2018-03-14 11:54:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
}.bind(this);
|
2018-03-20 14:34:55 +01:00
|
|
|
|
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-19 10:17:01 +01:00
|
|
|
body.mesh.geometry = null;
|
|
|
|
body.mesh.materials = null;
|
|
|
|
body.mesh.remove();
|
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.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-21 10:23:46 +01:00
|
|
|
this.createGameMesh(this.imageBreadHead),
|
2018-03-14 11:54:53 +01:00
|
|
|
{
|
|
|
|
x : 4,
|
|
|
|
y : 4
|
|
|
|
},
|
|
|
|
0
|
|
|
|
),
|
|
|
|
new GameLib.CustomCode.SnakeBody(
|
2018-03-21 10:23:46 +01:00
|
|
|
this.createGameMesh(this.imageBreadPatty),
|
2018-03-14 11:54:53 +01:00
|
|
|
{
|
|
|
|
x : 4,
|
|
|
|
y : 3
|
|
|
|
},
|
|
|
|
0
|
|
|
|
),
|
|
|
|
new GameLib.CustomCode.SnakeBody(
|
2018-03-21 10:23:46 +01:00
|
|
|
this.createGameMesh(this.imageBreadPatty),
|
2018-03-14 11:54:53 +01:00
|
|
|
{
|
|
|
|
x : 4,
|
|
|
|
y : 2
|
|
|
|
},
|
|
|
|
0
|
|
|
|
),
|
|
|
|
new GameLib.CustomCode.SnakeBody(
|
2018-03-21 10:23:46 +01:00
|
|
|
this.createGameMesh(this.imageBreadTail),
|
2018-03-14 11:54:53 +01:00
|
|
|
{
|
2018-03-20 10:32:39 +01:00
|
|
|
x : 4,
|
|
|
|
y : 1
|
2018-03-14 11:54:53 +01:00
|
|
|
},
|
2018-03-19 11:27:19 +01:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
null,
|
|
|
|
true
|
2018-03-14 11:54:53 +01:00
|
|
|
)
|
|
|
|
];
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-19 11:53:19 +01:00
|
|
|
//this.snake[3].mesh.position.z += 1;
|
2018-03-19 11:52:30 +01:00
|
|
|
|
2018-03-19 10:26:24 +01:00
|
|
|
this.snake[0].mesh.updateInstance('position');
|
|
|
|
this.snake[1].mesh.updateInstance('position');
|
|
|
|
this.snake[2].mesh.updateInstance('position');
|
|
|
|
this.snake[3].mesh.updateInstance('position');
|
2018-03-19 10:25:31 +01:00
|
|
|
|
|
|
|
this.animation.meshes.push(this.snake[0].mesh);
|
|
|
|
this.animation.meshes.push(this.snake[1].mesh);
|
|
|
|
this.animation.meshes.push(this.snake[2].mesh);
|
2018-03-20 10:28:33 +01:00
|
|
|
|
2018-03-20 10:32:39 +01:00
|
|
|
//this.snake[2].mesh.instance.add(this.snake[3].mesh.instance);
|
|
|
|
this.animation.meshes.push(this.snake[3].mesh);
|
2018-03-19 10:25:31 +01:00
|
|
|
|
2018-03-20 10:37:19 +01:00
|
|
|
//this.animationRotation.meshes.push(this.snake[3].mesh);
|
2018-03-19 11:08:26 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
/**
|
|
|
|
* Cleanup grid
|
|
|
|
*
|
|
|
|
this.grid.map(
|
|
|
|
function(x) {
|
2018-03-19 08:29:24 +01:00
|
|
|
x.map(
|
2018-03-19 10:08:33 +01:00
|
|
|
function(y) {
|
|
|
|
y.mesh.geometry = null;
|
|
|
|
y.mesh.materials = null;
|
|
|
|
y.mesh.remove();
|
|
|
|
}
|
2018-03-19 08:29:24 +01:00
|
|
|
);
|
2018-03-15 12:46:00 +01:00
|
|
|
}
|
2018-03-14 11:54:53 +01:00
|
|
|
);
|
2018-03-20 14:34:55 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
this.initializeGrid();
|
2018-03-21 10:29:13 +01:00
|
|
|
this.visualizeGrid();
|
2018-03-14 11:53:15 +01:00
|
|
|
|
2018-03-14 11:54:53 +01:00
|
|
|
/**
|
|
|
|
* Other Settings
|
|
|
|
*/
|
2018-03-20 15:07:58 +01:00
|
|
|
this.advanceTime = 0;
|
2018-03-14 11:54:53 +01:00
|
|
|
this.speed = GameLib.CustomCode.SPEED_INITIAL;
|
2018-03-20 15:07:58 +01:00
|
|
|
this.foodTime = 0;
|
|
|
|
this.foodSpeed = GameLib.CustomCode.FOOD_SPEED_INITIAL;
|
|
|
|
this.foodItems = [];
|
|
|
|
|
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
|