diff --git a/21g30t1e75.js b/21g30t1e75.js index 0cd3958..7732004 100644 --- a/21g30t1e75.js +++ b/21g30t1e75.js @@ -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;