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

beta.r3js.org
-=yb4f310 2018-03-12 15:30:14 +01:00
parent b8ebf39eb8
commit 262a85e751
1 changed files with 91 additions and 1 deletions

View File

@ -13,6 +13,11 @@ 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 = 3;
/**
* Get runtime
*/
@ -45,9 +50,75 @@ this.scene = GameLib.EntityManager.Instance.findComponentById('pllp034hsj');
*/
this.snake = [];
this.grid = [];
this.speed = GameLib.CustomCode.SPEED_INITIAL;
this.advanceTime = 0;
GameLib.CustomCode.SnakeBody = function(
mesh,
position,
direction
) {
this.mesh = mesh;
this.position = position;
this.direction = direction;
};
GameLib.CustomCode.SnakeBody.prototype.clone = function() {
return new GameLib.CustomCode.SnakeBody(
this.mesh,
{
x : this.position.x,
y : this.position.y
},
{
x : this.direction.x,
y : this.direction.y
}
);
}
GameLib.CustomCode.SnakeBody.prototype.advance = function() {
this.position.x += this.direction.x;
this.position.y += this.direction.y;
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');
}
GameLib.CustomCode.prototype.advanceSnake = function(delta) {
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) {
if ((index + 1) >= this.snake.length) {
/**
* do nothing
*/
} else {
/**
* Move the body over
*/
}
}.bind(this),
[body]
)
}.bind(this);
GameLib.CustomCode.prototype.initializeGrid = function() {
@ -92,14 +163,25 @@ GameLib.CustomCode.prototype.cloneBody = function(bodyType) {
y : 0
};
var position = {
x : Math.round(GameLib.CustomCode.GRID_WIDTH / 2),
y : Math.round(GameLib.CustomCode.GRID_HEIGHT / 2)
};
if (this.snake.length > 0) {
direction.x = this.snake[0].direction.x;
direction.y = this.snake[0].direction.y;
}
if (this.snake.length > 0) {
position.x = this.snake[0].position.x;
position.y = this.snake[0].position.y;
}
return {
mesh : body,
direction : direction
direction : direction,
position : position
}
}.bind(this);
@ -180,6 +262,14 @@ GameLib.Event.Subscribe(
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;