diff --git a/21g30t1e75.js b/21g30t1e75.js index d381b9e..5efc7f2 100644 --- a/21g30t1e75.js +++ b/21g30t1e75.js @@ -220,22 +220,49 @@ GameLib.CustomCode.SnakeBody.prototype.applyToMesh = function() { GameLib.CustomCode.SnakeBody.prototype.advance = function(orientation, flip) { - if (orientation === GameLib.CustomCode.ORIENTATION_UP) { - this.position.y += 1; - } - - if (orientation === GameLib.CustomCode.ORIENTATION_DOWN) { - this.position.y -= 1; - } - - if (orientation === GameLib.CustomCode.ORIENTATION_LEFT) { - this.position.x -= 1; - } - - if (orientation === GameLib.CustomCode.ORIENTATION_RIGHT) { - this.position.x += 1; - } - + var crash = false; + + switch (orientation) { + case GameLib.CustomCode.ORIENTATION_UP : + if ((this.position.y + 1) > GameLib.CustomCode.GRID_HEIGHT) { + crash = true; + } else { + this.position.y += 1; + } + break; + case GameLib.CustomCode.ORIENTATION_DOWN : + if ((this.position.y - 1) < 0) { + crash = true; + } else { + this.position.y -= 1; + } + break; + case GameLib.CustomCode.ORIENTATION_LEFT : + if ((this.position.x - 1) < 0) { + crash = true; + } else { + this.position.x -= 1; + } + break; + case GameLib.CustomCode.ORIENTATION_RIGHT : + if ((this.position.x + 1) > GameLib.CustomCode.GRID_WIDTH) { + crash = true; + } else { + this.position.x += 1; + } + break; + default : + console.warn('unknown orientation'); + break; + } + + if (!crash) { + console.log('check against body'); + } else { + console.log('crashed!'); + } + + this.orientation = orientation; this.flip = flip;