2018-03-12 14:11:01 +01:00
|
|
|
if (this.parentEntity === data.entity) {
|
|
|
|
console.log('snake entity loaded');
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-12 14:46:14 +01:00
|
|
|
/**
|
|
|
|
* Defines
|
|
|
|
*/
|
|
|
|
GameLib.CustomCode.BODY_TYPE_HEAD = 0x1;
|
|
|
|
GameLib.CustomCode.BODY_TYPE_TAIL = 0x2;
|
|
|
|
|
|
|
|
GameLib.CustomCode.GRID_WIDTH = 11;
|
|
|
|
GameLib.CustomCode.GRID_HEIGHT = 11;
|
|
|
|
|
2018-03-12 15:30:14 +01:00
|
|
|
GameLib.CustomCode.GRID_OFFSET_X = 1;
|
|
|
|
GameLib.CustomCode.GRID_OFFSET_Y = 5;
|
|
|
|
|
|
|
|
GameLib.CustomCode.SPEED_INITIAL = 3;
|
|
|
|
|
2018-03-12 14:46:14 +01:00
|
|
|
/**
|
|
|
|
* Get runtime
|
|
|
|
*/
|
2018-03-12 14:11:01 +01:00
|
|
|
this.runtime = GameLib.Utils.GetRuntime();
|
|
|
|
|
2018-03-12 14:28:27 +01:00
|
|
|
/**
|
|
|
|
* Custom Code Components
|
|
|
|
*/
|
|
|
|
this.beforeRender = GameLib.EntityManager.Instance.findComponentById('zjq6ach3jt');
|
|
|
|
|
2018-03-12 14:11:01 +01:00
|
|
|
/**
|
|
|
|
* Geometries
|
|
|
|
*/
|
2018-03-12 14:12:20 +01:00
|
|
|
this.geometryBody = GameLib.EntityManager.Instance.findComponentById('8f5q7k5ozp');
|
2018-03-12 14:11:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Images
|
|
|
|
*/
|
|
|
|
this.imageEnd = GameLib.EntityManager.Instance.findComponentById('swkla3wpp');
|
|
|
|
this.imageBodyPatty = GameLib.EntityManager.Instance.findComponentById('01r51k9ptr');
|
|
|
|
this.imagePatty = GameLib.EntityManager.Instance.findComponentById('4lf0fw24su');
|
|
|
|
|
2018-03-12 14:19:14 +01:00
|
|
|
/**
|
|
|
|
* Other Objects (Scene)
|
|
|
|
*/
|
|
|
|
this.scene = GameLib.EntityManager.Instance.findComponentById('pllp034hsj');
|
|
|
|
|
2018-03-12 14:24:45 +01:00
|
|
|
/**
|
|
|
|
* Game objects
|
|
|
|
*/
|
2018-03-12 14:46:14 +01:00
|
|
|
this.snake = [];
|
2018-03-12 14:51:24 +01:00
|
|
|
this.grid = [];
|
2018-03-12 15:30:14 +01:00
|
|
|
this.speed = GameLib.CustomCode.SPEED_INITIAL;
|
|
|
|
this.advanceTime = 0;
|
|
|
|
|
2018-03-12 15:52:50 +01:00
|
|
|
GameLib.CustomCode.prototype.createMaterial = function(image) {
|
|
|
|
var diffuseMap = new GameLib.D3.Texture.Image(
|
|
|
|
this.runtime.graphics,
|
|
|
|
{
|
|
|
|
image : image
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return new GameLib.D3.Material.Phong(
|
|
|
|
this.runtime.graphics,
|
|
|
|
{
|
|
|
|
diffuseMap : diffuseMap
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
GameLib.CustomCode.prototype.createGameMesh = function(image) {
|
|
|
|
|
|
|
|
var mesh = new GameLib.D3.Mesh(
|
|
|
|
this.runtime.graphics,
|
|
|
|
{
|
|
|
|
geometry : this.geometryBody,
|
|
|
|
materials : [this.createMaterial(image)]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
this.scene.addClone(mesh);
|
|
|
|
|
|
|
|
return mesh;
|
|
|
|
|
|
|
|
}.bind(this)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create our objects
|
|
|
|
*/
|
|
|
|
this.meshEnd = this.createGameMesh(this.imageEnd);
|
2018-03-12 21:03:30 +01:00
|
|
|
this.meshEnd.useQuaternion = false;
|
2018-03-12 21:03:35 +01:00
|
|
|
this.meshEnd.rotation.z = Math.PI / 2;
|
2018-03-12 21:02:11 +01:00
|
|
|
this.meshEnd.updateInstance('rotation');
|
|
|
|
|
2018-03-12 15:52:50 +01:00
|
|
|
this.meshPatty = this.createGameMesh(this.imagePatty);
|
|
|
|
this.meshBodyPatty = this.createGameMesh(this.imageBodyPatty);
|
|
|
|
|
2018-03-12 15:30:14 +01:00
|
|
|
GameLib.CustomCode.SnakeBody = function(
|
2018-03-12 15:52:50 +01:00
|
|
|
bodyType,
|
2018-03-12 20:54:46 +01:00
|
|
|
mesh,
|
2018-03-12 15:30:14 +01:00
|
|
|
position,
|
2018-03-12 20:54:46 +01:00
|
|
|
direction
|
2018-03-12 15:30:14 +01:00
|
|
|
) {
|
2018-03-12 15:52:50 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(position)) {
|
|
|
|
position = {
|
|
|
|
x : Math.round(GameLib.CustomCode.GRID_WIDTH / 2),
|
|
|
|
y : Math.round(GameLib.CustomCode.GRID_HEIGHT / 2)
|
|
|
|
};
|
|
|
|
}
|
2018-03-12 15:30:14 +01:00
|
|
|
this.position = position;
|
2018-03-12 15:52:50 +01:00
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(direction)) {
|
|
|
|
direction = {
|
|
|
|
x : -1,
|
|
|
|
y : 0
|
|
|
|
};
|
|
|
|
}
|
2018-03-12 15:30:14 +01:00
|
|
|
this.direction = direction;
|
2018-03-12 15:52:50 +01:00
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(bodyType)) {
|
2018-03-12 20:54:46 +01:00
|
|
|
throw new Error('no body type specified');
|
2018-03-12 15:52:50 +01:00
|
|
|
}
|
|
|
|
this.bodyType = bodyType;
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(mesh)) {
|
2018-03-12 20:54:46 +01:00
|
|
|
throw new Error('no mesh specified');
|
2018-03-12 15:52:50 +01:00
|
|
|
}
|
2018-03-12 20:57:00 +01:00
|
|
|
this.mesh = mesh.clone();
|
2018-03-12 15:52:50 +01:00
|
|
|
|
2018-03-12 20:54:46 +01:00
|
|
|
switch (this.bodyType) {
|
|
|
|
case GameLib.CustomCode.BODY_TYPE_HEAD :
|
|
|
|
break;
|
|
|
|
case GameLib.CustomCode.BODY_TYPE_TAIL :
|
2018-03-12 21:01:10 +01:00
|
|
|
this.mesh.rotation.z = Math.PI;
|
|
|
|
this.mesh.updateInstance('rotation');
|
|
|
|
this.position.x += 1;
|
2018-03-12 20:57:33 +01:00
|
|
|
break;
|
2018-03-12 20:54:46 +01:00
|
|
|
default :
|
|
|
|
throw new Error('unreachable statement');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-03-12 15:52:50 +01:00
|
|
|
this.applyPositionToMesh();
|
2018-03-12 20:49:33 +01:00
|
|
|
};
|
2018-03-12 15:30:14 +01:00
|
|
|
|
|
|
|
GameLib.CustomCode.SnakeBody.prototype.clone = function() {
|
|
|
|
return new GameLib.CustomCode.SnakeBody(
|
2018-03-12 15:52:50 +01:00
|
|
|
this.bodyType,
|
2018-03-12 20:54:46 +01:00
|
|
|
this.mesh,
|
2018-03-12 15:30:14 +01:00
|
|
|
{
|
|
|
|
x : this.position.x,
|
|
|
|
y : this.position.y
|
|
|
|
},
|
|
|
|
{
|
|
|
|
x : this.direction.x,
|
|
|
|
y : this.direction.y
|
2018-03-12 20:54:46 +01:00
|
|
|
}
|
2018-03-12 15:30:14 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-12 15:52:50 +01:00
|
|
|
GameLib.CustomCode.SnakeBody.prototype.applyPositionToMesh = function() {
|
2018-03-12 15:30:14 +01:00
|
|
|
this.mesh.position.x = this.position.x + GameLib.CustomCode.GRID_OFFSET_X;
|
|
|
|
this.mesh.position.y = this.position.y + GameLib.CustomCode.GRID_OFFSET_Y;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: We don't update instance position - animation should do this
|
|
|
|
*/
|
|
|
|
this.mesh.updateInstance('position');
|
|
|
|
}
|
2018-03-12 14:40:09 +01:00
|
|
|
|
2018-03-12 15:52:50 +01:00
|
|
|
GameLib.CustomCode.SnakeBody.prototype.advance = function() {
|
|
|
|
this.position.x += this.direction.x;
|
|
|
|
this.position.y += this.direction.y;
|
|
|
|
}
|
|
|
|
|
2018-03-12 14:24:45 +01:00
|
|
|
GameLib.CustomCode.prototype.advanceSnake = function(delta) {
|
|
|
|
|
2018-03-12 15:30:14 +01:00
|
|
|
this.advanceTime += delta;
|
|
|
|
|
|
|
|
if (this.advanceTime > this.speed) {
|
|
|
|
this.advanceTime = 0;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var head = this.snake[0].clone();
|
|
|
|
head.advance();
|
|
|
|
|
|
|
|
this.snake = this.snake.reduce(
|
|
|
|
function(result, body, index) {
|
2018-03-12 15:56:49 +01:00
|
|
|
|
2018-03-12 15:30:14 +01:00
|
|
|
if ((index + 1) >= this.snake.length) {
|
|
|
|
/**
|
2018-03-12 15:56:49 +01:00
|
|
|
* Do nothing
|
2018-03-12 15:30:14 +01:00
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* Move the body over
|
|
|
|
*/
|
2018-03-12 15:36:08 +01:00
|
|
|
result.push(this.snake[index + 1]);
|
2018-03-12 15:30:14 +01:00
|
|
|
}
|
2018-03-12 15:36:08 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
|
2018-03-12 15:30:14 +01:00
|
|
|
}.bind(this),
|
2018-03-12 15:34:31 +01:00
|
|
|
[head]
|
2018-03-12 15:30:14 +01:00
|
|
|
)
|
|
|
|
|
2018-03-12 14:24:45 +01:00
|
|
|
}.bind(this);
|
|
|
|
|
2018-03-12 15:52:50 +01:00
|
|
|
/*
|
2018-03-12 14:51:24 +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++) {
|
|
|
|
this.grid[x][y] = {
|
|
|
|
mesh : null,
|
|
|
|
direction : {
|
|
|
|
x : 0,
|
|
|
|
y : 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}.bind(this);
|
2018-03-12 15:52:50 +01:00
|
|
|
*/
|
2018-03-12 14:11:01 +01:00
|
|
|
|
|
|
|
GameLib.Event.Subscribe(
|
|
|
|
GameLib.Event.GAME_START,
|
|
|
|
function() {
|
2018-03-12 14:24:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all existing snake parts
|
|
|
|
*/
|
|
|
|
this.snake.map(
|
|
|
|
function(body) {
|
|
|
|
body.geometry = null;
|
|
|
|
body.materials = null;
|
|
|
|
body.remove();
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-03-12 14:40:59 +01:00
|
|
|
this.snake = [
|
2018-03-12 20:49:33 +01:00
|
|
|
new GameLib.CustomCode.SnakeBody(
|
|
|
|
GameLib.CustomCode.BODY_TYPE_HEAD,
|
|
|
|
this.meshEnd
|
|
|
|
),
|
|
|
|
new GameLib.CustomCode.SnakeBody(
|
|
|
|
GameLib.CustomCode.BODY_TYPE_TAIL,
|
|
|
|
this.meshEnd
|
|
|
|
)
|
2018-03-12 14:40:59 +01:00
|
|
|
];
|
2018-03-12 14:24:45 +01:00
|
|
|
|
2018-03-12 14:51:24 +01:00
|
|
|
/**
|
|
|
|
* Cleanup grid
|
2018-03-12 20:50:27 +01:00
|
|
|
*
|
2018-03-12 14:46:14 +01:00
|
|
|
this.grid.map(
|
|
|
|
function(x) {
|
|
|
|
x.map(
|
|
|
|
function(y) {
|
|
|
|
y.mesh.geometry = null;
|
|
|
|
y.mesh.materials = null;
|
|
|
|
y.mesh.remove();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-03-12 14:51:24 +01:00
|
|
|
this.initializeGrid();
|
2018-03-12 20:50:27 +01:00
|
|
|
*/
|
2018-03-12 14:51:24 +01:00
|
|
|
|
2018-03-12 15:30:14 +01:00
|
|
|
/**
|
|
|
|
* Other Settings
|
|
|
|
*/
|
|
|
|
this.speed = GameLib.CustomCode.SPEED_INITIAL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Re-initialize our other custom code components
|
|
|
|
*/
|
2018-03-12 14:28:27 +01:00
|
|
|
this.beforeRender.initialized = false;
|
|
|
|
this.beforeRender.entityLoaded = this;
|
2018-03-12 14:25:46 +01:00
|
|
|
|
2018-03-12 14:28:27 +01:00
|
|
|
console.log('starting game snake');
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
GameLib.Event.Subscribe(
|
|
|
|
GameLib.Event.GAME_OVER,
|
|
|
|
function() {
|
|
|
|
|
|
|
|
this.beforeRender.entityLoaded = null;
|
|
|
|
this.beforeRender.initialized = false;
|
2018-03-12 14:25:46 +01:00
|
|
|
|
2018-03-12 14:11:01 +01:00
|
|
|
console.log('starting game snake');
|
2018-03-12 14:25:46 +01:00
|
|
|
}.bind(this)
|
2018-03-12 14:11:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
GameLib.Event.Emit(GameLib.Event.GAME_LOADED);
|
|
|
|
|
|
|
|
//@ sourceURL=entityLoaded.js
|