From 1e2f98c6a0fedad7c4523eb75107f44b12e4201a Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Thu, 15 Mar 2018 12:46:00 +0100 Subject: [PATCH] Update: CC - Snake FS - Entity Loaded (21g30t1e75.js) 540 bytes modified --- 21g30t1e75.js | 190 +++++++++++++++++++++++++++----------------------- 1 file changed, 102 insertions(+), 88 deletions(-) diff --git a/21g30t1e75.js b/21g30t1e75.js index 4d557eb..bf894e4 100644 --- a/21g30t1e75.js +++ b/21g30t1e75.js @@ -108,16 +108,12 @@ GameLib.CustomCode.prototype.createGameMesh = function(image) { * Create our objects */ this.meshBreadHead = this.createGameMesh(this.imageBreadHead); -this.meshBreadHead.useQuaternion = false; this.meshBreadTail = this.createGameMesh(this.imageBreadTail); -this.meshBreadTail.useQuaternion = false; this.meshBreadPatty = this.createGameMesh(this.imageBreadPatty); -this.meshBreadPatty.useQuaternion = false; this.meshBreadCorner = this.createGameMesh(this.imageBreadCorner); -this.meshBreadCorner.useQuaternion = false; this.meshPatty = this.createGameMesh(this.imagePatty); @@ -127,19 +123,22 @@ this.meshPatty = this.createGameMesh(this.imagePatty); * @param position * @param orientation * @param bodyType + * @param backup * @constructor */ GameLib.CustomCode.SnakeBody = function( mesh, position, orientation, - bodyType + bodyType, + backup ) { if (GameLib.Utils.UndefinedOrNull(mesh)) { throw new Error('no mesh specified'); } this.mesh = mesh.clone(); + this.mesh.useQuaternion = false; if (GameLib.Utils.UndefinedOrNull(position)) { position = { @@ -159,11 +158,39 @@ GameLib.CustomCode.SnakeBody = function( } this.bodyType = bodyType; - this.backupMesh = null; - + if (GameLib.Utils.UndefinedOrNull(backup)) { + backup = null; + } + this.backup = backup; + this.applyToMesh(); }; +GameLib.CustomCode.SnakeBody.prototype.restoreBackup = function() { + + if (!this.backup) { + console.warn('no backup to restore'); + return; + } + + this.mesh.geometry = null; + this.mesh.materials = null; + this.mesh.remove(); + + this.mesh = this.backup.mesh; + this.mesh.visible = true; + this.mesh.updateInstance('visible'); + + this.orientation = this.backup.orientation; + this.bodyType = this.backup.bodyType; + + this.backup = null; + + /** + * We don't restore the position because the position will have changed + */ +} + GameLib.CustomCode.SnakeBody.prototype.applyToMesh = function() { this.mesh.position.x = this.position.x + GameLib.CustomCode.GRID_OFFSET_X; @@ -209,98 +236,85 @@ GameLib.CustomCode.prototype.advanceSnake = function(delta) { return; } - var backup = { - x : 0, - y : 0, - orientation : GameLib.CustomCode.ORIENTATION_UP, - bodyType : GameLib.CustomCode.BODY_TYPE_NORMAL - }; - - this.snake.map( + this.snake = this.snake.map( + function(body, index) { if (index === 0) { - backup.x = body.position.x; - backup.y = body.position.y; - backup.orientation = body.orientation; - backup.bodyType = body.bodyType; - body.advance( this.state.orientation ); - } else { + body.applyToMesh(); - var temp = { - x : body.position.x, - y : body.position.y, - orientation : body.orientation, - bodyType : body.bodyType - }; + return body; + + } else { - body.position.x = backup.x; - body.position.y = backup.y; - body.orientation = backup.orientation; - - if ( - index === 1 && - this.state.turning && + body.position.x = this.snake[index - 1].position.x; + body.position.y = this.snake[index - 1].position.y; + body.orientation = this.snake[index - 1].orientation; + + if ( + index === 1 && + this.state.turning && body.bodyType !== GameLib.CustomCode.BODY_TYPE_TAIL ) { - body.backupMesh = body.mesh; - - body.backupMesh.visible = false; - - body.backupMesh.updateInstance('visible'); - - body.mesh = this.meshBreadCorner.clone(); + var corner = new GameLib.CustomCode.SnakeBody( + this.meshBreadCorner, + body.position, + body.orientation, + GameLib.CustomCode.BODY_TYPE_CORNER, + body + ); + + corner.applyToMesh(); - body.bodyType = GameLib.CustomCode.BODY_TYPE_CORNER; - - this.state.turning = false; + return corner; - } else { - - if (body.bodyType === GameLib.CustomCode.BODY_TYPE_CORNER) { - body.mesh.geometry = null; - body.mesh.materials = null; - body.mesh.remove(); - body.mesh = body.backupMesh; - body.mesh.visible = true; - body.mesh.updateInstance('visible'); - } else if (backup.bodyType === GameLib.CustomCode.BODY_TYPE_CORNER) { - - if (index === (this.snake.length - 1)) { - - backup.bodyType = GameLib.CustomCode.BODY_TYPE_TAIL; - - } else { - - body.backupMesh = body.mesh; + } else { - body.backupMesh.visible = false; + if (body.bodyType === GameLib.CustomCode.BODY_TYPE_CORNER) { + /** + * First restore the backup before we start storing another corner + */ + body.restoreBackup(); - body.backupMesh.updateInstance('visible'); + + } + + if (this.snake[index - 1].bodyType === GameLib.CustomCode.BODY_TYPE_CORNER) { - body.mesh = this.meshBreadCorner.clone(); - } - - } - - body.bodyType = backup.bodyType; - - } - - backup.x = temp.x; - backup.y = temp.y; - backup.orientation = temp.orientation; - backup.bodyType = temp.bodyType - + if (index === (this.snake.length - 1)) { + /** + * We're the tail - don't turn into a corner + */ + return body; + } + + var corner = new GameLib.CustomCode.SnakeBody( + this.meshBreadCorner, + body.position, + body.orientation, + GameLib.CustomCode.BODY_TYPE_CORNER, + body + ); + + corner.applyToMesh(); + + return corner; + } + + body.applyToMesh(); + + return body; + + } + } - - body.applyToMesh(); + }.bind(this) ); @@ -387,14 +401,14 @@ GameLib.Event.Subscribe( * this.grid.map( function(x) { - x.map( - function(y) { - y.mesh.geometry = null; - y.mesh.materials = null; - y.mesh.remove(); - } - ); - } + x.map( + function(y) { + y.mesh.geometry = null; + y.mesh.materials = null; + y.mesh.remove(); + } + ); +} ); this.initializeGrid();