328 lines
6.3 KiB
JavaScript
328 lines
6.3 KiB
JavaScript
if (this.parentEntity === data.entity) {
|
|
console.log('snake entity loaded');
|
|
} else {
|
|
return;
|
|
}
|
|
|
|
/**
|
|
* Defines
|
|
*/
|
|
GameLib.CustomCode.BODY_TYPE_HEAD = 0x1;
|
|
GameLib.CustomCode.BODY_TYPE_TAIL = 0x2;
|
|
|
|
GameLib.CustomCode.GRID_WIDTH = 11;
|
|
GameLib.CustomCode.GRID_HEIGHT = 11;
|
|
|
|
GameLib.CustomCode.GRID_OFFSET_X = 1;
|
|
GameLib.CustomCode.GRID_OFFSET_Y = 5;
|
|
|
|
GameLib.CustomCode.SPEED_INITIAL = 1;
|
|
|
|
/**
|
|
* 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');
|
|
|
|
/**
|
|
* Images
|
|
*/
|
|
this.imageEnd = GameLib.EntityManager.Instance.findComponentById('swkla3wpp');
|
|
this.imageBodyPatty = GameLib.EntityManager.Instance.findComponentById('01r51k9ptr');
|
|
this.imagePatty = GameLib.EntityManager.Instance.findComponentById('4lf0fw24su');
|
|
|
|
/**
|
|
* Other Objects (Scene)
|
|
*/
|
|
this.scene = GameLib.EntityManager.Instance.findComponentById('pllp034hsj');
|
|
|
|
/**
|
|
* Game objects
|
|
*/
|
|
this.snake = [];
|
|
this.grid = [];
|
|
this.speed = GameLib.CustomCode.SPEED_INITIAL;
|
|
this.advanceTime = 0;
|
|
this.direction = {
|
|
x : -1,
|
|
y : 0
|
|
}
|
|
this.rotation = Math.PI / 2;
|
|
|
|
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);
|
|
this.meshEnd.useQuaternion = false;
|
|
|
|
this.meshPatty = this.createGameMesh(this.imagePatty);
|
|
this.meshBodyPatty = this.createGameMesh(this.imageBodyPatty);
|
|
|
|
GameLib.CustomCode.SnakeBody = function(
|
|
bodyType,
|
|
mesh,
|
|
position,
|
|
rotation
|
|
) {
|
|
if (GameLib.Utils.UndefinedOrNull(bodyType)) {
|
|
throw new Error('no body type specified');
|
|
}
|
|
this.bodyType = bodyType;
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(mesh)) {
|
|
throw new Error('no mesh specified');
|
|
}
|
|
this.mesh = mesh.clone();
|
|
|
|
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;
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(rotation)) {
|
|
rotation = Math.PI / 2;
|
|
}
|
|
this.rotation = rotation;
|
|
|
|
switch (this.bodyType) {
|
|
case GameLib.CustomCode.BODY_TYPE_HEAD :
|
|
break;
|
|
case GameLib.CustomCode.BODY_TYPE_TAIL :
|
|
this.rotation += Math.PI;
|
|
this.position.x += 1;
|
|
break;
|
|
default :
|
|
throw new Error('unreachable statement');
|
|
break;
|
|
}
|
|
|
|
this.applyToMesh();
|
|
};
|
|
|
|
/*
|
|
GameLib.CustomCode.SnakeBody.prototype.clone = function() {
|
|
return new GameLib.CustomCode.SnakeBody(
|
|
this.bodyType,
|
|
this.mesh,
|
|
{
|
|
x : this.position.x,
|
|
y : this.position.y
|
|
}
|
|
);
|
|
}
|
|
*/
|
|
GameLib.CustomCode.SnakeBody.prototype.applyToMesh = function() {
|
|
|
|
this.mesh.position.x = this.position.x + GameLib.CustomCode.GRID_OFFSET_X;
|
|
this.mesh.position.y = this.position.y + GameLib.CustomCode.GRID_OFFSET_Y;
|
|
|
|
this.mesh.rotation.z = this.rotation;
|
|
|
|
/**
|
|
* TODO: We don't update instance position - animation should do this
|
|
*/
|
|
this.mesh.updateInstance('position');
|
|
this.mesh.updateInstance('rotation');
|
|
}
|
|
|
|
GameLib.CustomCode.SnakeBody.prototype.advance = function(direction, rotation) {
|
|
this.position.x += direction.x;
|
|
this.position.y += direction.y;
|
|
|
|
this.rotation = rotation;
|
|
}
|
|
|
|
GameLib.CustomCode.prototype.advanceSnake = function(delta) {
|
|
|
|
this.advanceTime += delta;
|
|
|
|
if (this.advanceTime > this.speed) {
|
|
this.advanceTime = 0;
|
|
} else {
|
|
return;
|
|
}
|
|
|
|
var position = {x:0, y:0};
|
|
|
|
this.snake.map(
|
|
function(body, index) {
|
|
|
|
if (index === 0) {
|
|
|
|
position.x = body.position.x;
|
|
position.y = body.position.y;
|
|
|
|
body.advance(this.direction, this.rotation);
|
|
|
|
} else {
|
|
|
|
var tempPosition = {
|
|
x : body.position.x,
|
|
y : body.position.y
|
|
};
|
|
|
|
body.position.x = position.x;
|
|
body.position.y = position.y;
|
|
|
|
position.x = tempPosition.x;
|
|
position.y = tempPosition.y;
|
|
|
|
}
|
|
|
|
body.applyToMesh();
|
|
}.bind(this)
|
|
);
|
|
|
|
}.bind(this);
|
|
|
|
/*
|
|
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);
|
|
*/
|
|
|
|
GameLib.Event.Subscribe(
|
|
GameLib.Event.GAME_START,
|
|
function() {
|
|
|
|
/**
|
|
* Remove all existing snake parts
|
|
*/
|
|
this.snake.map(
|
|
function(body) {
|
|
body.geometry = null;
|
|
body.materials = null;
|
|
body.remove();
|
|
}
|
|
)
|
|
|
|
this.direction = {
|
|
x : -1,
|
|
y : 0
|
|
}
|
|
this.rotation = Math.PI / 2;
|
|
|
|
this.snake = [
|
|
new GameLib.CustomCode.SnakeBody(
|
|
GameLib.CustomCode.BODY_TYPE_HEAD,
|
|
this.meshEnd
|
|
),
|
|
new GameLib.CustomCode.SnakeBody(
|
|
GameLib.CustomCode.BODY_TYPE_TAIL,
|
|
this.meshEnd
|
|
)
|
|
];
|
|
|
|
/**
|
|
* Cleanup grid
|
|
*
|
|
this.grid.map(
|
|
function(x) {
|
|
x.map(
|
|
function(y) {
|
|
y.mesh.geometry = null;
|
|
y.mesh.materials = null;
|
|
y.mesh.remove();
|
|
}
|
|
);
|
|
}
|
|
);
|
|
|
|
this.initializeGrid();
|
|
*/
|
|
|
|
/**
|
|
* Other Settings
|
|
*/
|
|
this.speed = GameLib.CustomCode.SPEED_INITIAL;
|
|
|
|
/**
|
|
* Re-initialize our other custom code components
|
|
*/
|
|
this.beforeRender.initialized = false;
|
|
this.beforeRender.entityLoaded = this;
|
|
|
|
this.keyUp.initialized = false;
|
|
this.keyUp.entityLoaded = this;
|
|
|
|
console.log('starting game snake');
|
|
}.bind(this)
|
|
);
|
|
|
|
GameLib.Event.Subscribe(
|
|
GameLib.Event.GAME_OVER,
|
|
function() {
|
|
|
|
this.keyUp.initialized = false;
|
|
this.keyUp.entityLoaded = null;
|
|
|
|
this.beforeRender.initialized = false;
|
|
this.beforeRender.entityLoaded = null;
|
|
|
|
console.log('starting game snake');
|
|
}.bind(this)
|
|
)
|
|
|
|
GameLib.Event.Emit(GameLib.Event.GAME_LOADED);
|
|
|
|
//@ sourceURL=entityLoaded.js
|