From 536f7c1bc9ea137742906bcbf75525d6f96255a7 Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Mon, 12 Mar 2018 15:52:50 +0100 Subject: [PATCH] Update: CC - Snake FS - Entity Loaded (21g30t1e75.js) 59 bytes modified --- 21g30t1e75.js | 275 +++++++++++++++++++++++++------------------------- 1 file changed, 136 insertions(+), 139 deletions(-) diff --git a/21g30t1e75.js b/21g30t1e75.js index 32848c8..13a88e7 100644 --- a/21g30t1e75.js +++ b/21g30t1e75.js @@ -53,143 +53,6 @@ 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 - */ - result.push(this.snake[index + 1]); - } - - return result; - - }.bind(this), - [head] - ) - -}.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.CustomCode.prototype.cloneBody = function(bodyType) { - - var mesh = null; - - switch (bodyType) { - case (GameLib.CustomCode.BODY_TYPE_HEAD) : - mesh = this.meshEnd.clone(); - break; - case (GameLib.CustomCode.BODY_TYPE_TAIL) : - mesh = this.meshEnd.clone(); - mesh.rotation.z = Math.PI; - mesh.updateInstance('rotation'); - break; - default: - console.warn('unhandled body type : ' + bodyType); - break; - } - - var direction = { - x : -1, - 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 new GameLib.CustomCode.SnakeBody( - mesh, - position, - direction - ); - -}.bind(this); - GameLib.CustomCode.prototype.createMaterial = function(image) { var diffuseMap = new GameLib.D3.Texture.Image( this.runtime.graphics, @@ -229,6 +92,140 @@ this.meshEnd = this.createGameMesh(this.imageEnd); this.meshPatty = this.createGameMesh(this.imagePatty); this.meshBodyPatty = this.createGameMesh(this.imageBodyPatty); +GameLib.CustomCode.SnakeBody = function( + bodyType, + position, + direction, + mesh +) { + 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(direction)) { + direction = { + x : -1, + y : 0 + }; + } + this.direction = direction; + + if (GameLib.Utils.UndefinedOrNull(bodyType)) { + bodyType = GameLib.CustomCode.BODY_TYPE_HEAD; + } + this.bodyType = bodyType; + + if (GameLib.Utils.UndefinedOrNull(mesh)) { + + switch (this.bodyType) { + case GameLib.CustomCode.BODY_TYPE_HEAD : + mesh = this.meshEnd; + break; + case GameLib.CustomCode.BODY_TYPE_TAIL : + mesh = this.meshEnd.clone(); + mesh.rotation.z = Math.PI; + mesh.updateInstance('rotation'); + position.x += 1; + default : + throw new Error('body type not specified and no mesh'); + break; + } + + } + this.mesh = mesh; + + this.applyPositionToMesh(); +}; + +GameLib.CustomCode.SnakeBody.prototype.clone = function() { + return new GameLib.CustomCode.SnakeBody( + this.bodyType, + { + x : this.position.x, + y : this.position.y + }, + { + x : this.direction.x, + y : this.direction.y + }, + this.mesh + ); +} + +GameLib.CustomCode.SnakeBody.prototype.applyPositionToMesh = 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; + + /** + * TODO: We don't update instance position - animation should do this + */ + this.mesh.updateInstance('position'); +} + +GameLib.CustomCode.SnakeBody.prototype.advance = function() { + this.position.x += this.direction.x; + this.position.y += this.direction.y; +} + +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 + */ + result.push(this.snake[index + 1]); + } + + return result; + + }.bind(this), + [head] + ) + +}.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() { @@ -245,8 +242,8 @@ GameLib.Event.Subscribe( ) this.snake = [ - this.cloneBody(GameLib.CustomCode.BODY_TYPE_HEAD), - this.cloneBody(GameLib.CustomCode.BODY_TYPE_TAIL) + new GameLib.CustomCode.SnakeBody(GameLib.CustomCode.BODY_TYPE_HEAD), + new GameLib.CustomCode.SnakeBody(GameLib.CustomCode.BODY_TYPE_TAIL) ]; /**