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

beta.r3js.org
-=yb4f310 2018-03-13 12:13:07 +01:00
parent 5b86265f47
commit 2bb687727d
1 changed files with 29 additions and 28 deletions

View File

@ -52,6 +52,10 @@ this.snake = [];
this.grid = []; this.grid = [];
this.speed = GameLib.CustomCode.SPEED_INITIAL; this.speed = GameLib.CustomCode.SPEED_INITIAL;
this.advanceTime = 0; this.advanceTime = 0;
this.direction = {
x : -1,
y : 0
}
GameLib.CustomCode.prototype.createMaterial = function(image) { GameLib.CustomCode.prototype.createMaterial = function(image) {
var diffuseMap = new GameLib.D3.Texture.Image( var diffuseMap = new GameLib.D3.Texture.Image(
@ -99,8 +103,7 @@ this.meshBodyPatty = this.createGameMesh(this.imageBodyPatty);
GameLib.CustomCode.SnakeBody = function( GameLib.CustomCode.SnakeBody = function(
bodyType, bodyType,
mesh, mesh,
position, position
direction
) { ) {
if (GameLib.Utils.UndefinedOrNull(position)) { if (GameLib.Utils.UndefinedOrNull(position)) {
position = { position = {
@ -110,14 +113,6 @@ GameLib.CustomCode.SnakeBody = function(
} }
this.position = position; this.position = position;
if (GameLib.Utils.UndefinedOrNull(direction)) {
direction = {
x : -1,
y : 0
};
}
this.direction = direction;
if (GameLib.Utils.UndefinedOrNull(bodyType)) { if (GameLib.Utils.UndefinedOrNull(bodyType)) {
throw new Error('no body type specified'); throw new Error('no body type specified');
} }
@ -126,7 +121,7 @@ GameLib.CustomCode.SnakeBody = function(
if (GameLib.Utils.UndefinedOrNull(mesh)) { if (GameLib.Utils.UndefinedOrNull(mesh)) {
throw new Error('no mesh specified'); throw new Error('no mesh specified');
} }
this.mesh = mesh.clone(); this.mesh = mesh;
switch (this.bodyType) { switch (this.bodyType) {
case GameLib.CustomCode.BODY_TYPE_HEAD : case GameLib.CustomCode.BODY_TYPE_HEAD :
@ -147,14 +142,10 @@ GameLib.CustomCode.SnakeBody = function(
GameLib.CustomCode.SnakeBody.prototype.clone = function() { GameLib.CustomCode.SnakeBody.prototype.clone = function() {
return new GameLib.CustomCode.SnakeBody( return new GameLib.CustomCode.SnakeBody(
this.bodyType, this.bodyType,
this.mesh, this.mesh.clone(),
{ {
x : this.position.x, x : this.position.x,
y : this.position.y y : this.position.y
},
{
x : this.direction.x,
y : this.direction.y
} }
); );
} }
@ -169,9 +160,9 @@ GameLib.CustomCode.SnakeBody.prototype.applyPositionToMesh = function() {
this.mesh.updateInstance('position'); this.mesh.updateInstance('position');
} }
GameLib.CustomCode.SnakeBody.prototype.advance = function() { GameLib.CustomCode.SnakeBody.prototype.advance = function(direction) {
this.position.x += this.direction.x; this.position.x += direction.x;
this.position.y += this.direction.y; this.position.y += direction.y;
} }
GameLib.CustomCode.prototype.advanceSnake = function(delta) { GameLib.CustomCode.prototype.advanceSnake = function(delta) {
@ -185,25 +176,35 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) {
} }
var head = this.snake[0].clone(); var head = this.snake[0].clone();
head.advance(); head.advance(this.direction);
var oldHead = this.snake.shift(); var oldHead = this.snake.shift();
oldHead.mesh.geometry = null; oldHead.mesh.geometry = null;
oldHead.mesh.materials = null; oldHead.mesh.materials = null;
oldHead.mesh.remove(); oldHead.mesh.remove();
var oldTail = this.snake.pop(); var position = oldHead.position;
var snake = [head]; var snake = [head];
this.snake.map( this.snake.map(
function(body) { function(body) {
var tempPosition = {
x : body.position.x,
y : body.position.y
};
body.position.x = position.x;
body.position.y = position.y;
snake.push(body); snake.push(body);
position.x = tempPosition.x;
position.y = tempPosition.y;
} }
); );
snake.push(oldTail);
this.snake = snake; this.snake = snake;
}.bind(this); }.bind(this);