if (this.parentEntity === data.entity) { console.log('snake entity loaded'); } else { return; } /** * Defines */ GameLib.CustomCode.BODY_TYPE_NORMAL = 0x1; GameLib.CustomCode.BODY_TYPE_TAIL = 0x2; GameLib.CustomCode.BODY_TYPE_CORNER = 0x3; GameLib.CustomCode.GRID_WIDTH = 11; GameLib.CustomCode.GRID_HEIGHT = 10; GameLib.CustomCode.GRID_OFFSET_X = 1; GameLib.CustomCode.GRID_OFFSET_Y = 8; GameLib.CustomCode.SPEED_INITIAL = 1; GameLib.CustomCode.ORIENTATION_UP = 0; GameLib.CustomCode.ORIENTATION_LEFT = 1; GameLib.CustomCode.ORIENTATION_DOWN = 2; GameLib.CustomCode.ORIENTATION_RIGHT = 3; GameLib.CustomCode.FOOD_SPEED_INITIAL = 3; GameLib.CustomCode.MAX_FOOD_ITEMS = 6; GameLib.CustomCode.FOOD_BACON = 0; GameLib.CustomCode.FOOD_CHEESE = 1; GameLib.CustomCode.FOOD_ONION = 2; GameLib.CustomCode.FOOD_ONION_RING = 3; GameLib.CustomCode.FOOD_PATTY = 4; GameLib.CustomCode.FOOD_TOMATO = 5; /** * Get runtime */ this.runtime = GameLib.Utils.GetRuntime(); /** * Custom Code Components */ this.beforeRender = GameLib.EntityManager.Instance.findComponentById('zjq6ach3jt'); this.keyUp = GameLib.EntityManager.Instance.findComponentById('306204wy29'); /** * Geometries */ this.geometryBody = GameLib.EntityManager.Instance.findComponentById('8f5q7k5ozp'); GameLib.CustomCode.BODY_SCALE_X = this.geometryBody.width; GameLib.CustomCode.BODY_SCALE_Y = this.geometryBody.height; /** * Images * this.imageBreadHead = GameLib.EntityManager.Instance.findComponentById('swkla3wpp'); this.imageBreadTail = GameLib.EntityManager.Instance.findComponentById('se6qlnmojd'); this.imageBreadPatty = GameLib.EntityManager.Instance.findComponentById('01r51k9ptr'); this.imageBreadCorner = GameLib.EntityManager.Instance.findComponentById('iljpuouaok'); this.imageBacon = GameLib.EntityManager.Instance.findComponentById('t4lb4hr4ue'); this.imageCheese = GameLib.EntityManager.Instance.findComponentById('y2611hx41r'); this.imageOnion = GameLib.EntityManager.Instance.findComponentById('g3z1wg2vt8'); this.imageOnionRing = GameLib.EntityManager.Instance.findComponentById('ztcyy0bgk5'); this.imagePatty = GameLib.EntityManager.Instance.findComponentById('4lf0fw24su'); this.imageTomato = GameLib.EntityManager.Instance.findComponentById('ptvwmo8l38'); */ /** * Materials */ this.materialTomato = GameLib.EntityManager.Instance.findComponentById('merwtvkm1t'); this.materialBacon = GameLib.EntityManager.Instance.findComponentById('x5ffcl6ojc'); this.materialCheese = GameLib.EntityManager.Instance.findComponentById('0rxqy5eddx'); this.materialOnionRing = GameLib.EntityManager.Instance.findComponentById('gz8zex5xug'); this.materialPatty = GameLib.EntityManager.Instance.findComponentById('f7y8nurbcv'); this.materialOnion = GameLib.EntityManager.Instance.findComponentById('im3tsuzwrp'); this.materialBreadBacon = GameLib.EntityManager.Instance.findComponentById('wn31yqi6lc'); this.materialBreadCheese = GameLib.EntityManager.Instance.findComponentById('i22ixqizge'); this.materialBreadOnion = GameLib.EntityManager.Instance.findComponentById('69sybcj08d'); this.materialBreadOnionRing = GameLib.EntityManager.Instance.findComponentById('kvesyjtr2v'); this.materialBreadPatty = GameLib.EntityManager.Instance.findComponentById('k6axym9bu5'); this.materialBreadHead = GameLib.EntityManager.Instance.findComponentById('heu4f7zzuh'); this.materialBreadTail = GameLib.EntityManager.Instance.findComponentById('mm2yq9rmpf'); this.materialBreadCorner = GameLib.EntityManager.Instance.findComponentById('ju9r1bw6cb'); /** * Other Objects (Scene) */ this.scene = GameLib.EntityManager.Instance.findComponentById('pllp034hsj'); this.animation = GameLib.EntityManager.Instance.findComponentById('8kb7utb2fn'); this.animationRotation = GameLib.EntityManager.Instance.findComponentById('z628kythyn'); /** * Game objects */ this.snake = []; this.grid = []; this.speed = GameLib.CustomCode.SPEED_INITIAL; this.advanceTime = 0; this.foodTime = 0; this.foodSpeed = GameLib.CustomCode.FOOD_SPEED_INITIAL; this.foodItems = []; /** * Orientation is 0, 1, 2 or 3, (up, left, down, right) - * This is also the amount we need to multiply with PI to get the mesh rotation * @type {{direction: {x: number, y: number}, orientation: number}} */ this.state = { orientation : 0, turning : false, flip : 0 }; GameLib.CustomCode.prototype.visualizeGrid = function (color) { if (this.starsMesh) { this.scene.instance.remove(this.starsMesh); } this.starsGeometry = new THREE.Geometry(); this.grid.map( function(x, xIndex) { x.map( function(y, yIndex) { this.starsGeometry.vertices.push( new THREE.Vector3( (xIndex * GameLib.CustomCode.BODY_SCALE_X) + GameLib.CustomCode.GRID_OFFSET_X, (yIndex * GameLib.CustomCode.BODY_SCALE_Y) + GameLib.CustomCode.GRID_OFFSET_Y, 5 ) ); }.bind(this) ) }.bind(this) ); var starsMaterial = new THREE.PointsMaterial({color: color, size: 0.1}); this.starsMesh = new THREE.Points(this.starsGeometry, starsMaterial); this.scene.instance.add(this.starsMesh); }.bind(this) GameLib.CustomCode.prototype.createGameMesh = function(material, visible) { if (GameLib.Utils.UndefinedOrNull(visible)) { visible = true; } var mesh = new GameLib.D3.Mesh( this.runtime.graphics, { geometry : this.geometryBody, materials : [material], visible : visible, useQuaternion : false } ) this.scene.addClone(mesh); return mesh; }.bind(this) GameLib.CustomCode.prototype.createFood = function(delta) { this.foodTime += delta; if (this.foodTime > this.foodSpeed) { this.foodTime = 0; } else { return; } var foodType = GameLib.Utils.GetRandomIntInclusive(0, 5); var mesh = null; switch (foodType) { case GameLib.CustomCode.FOOD_BACON : mesh = this.createGameMesh(this.materialBacon); break; case GameLib.CustomCode.FOOD_CHEESE : mesh = this.createGameMesh(this.materialCheese); break; case GameLib.CustomCode.FOOD_ONION : mesh = this.createGameMesh(this.materialOnion); break; case GameLib.CustomCode.FOOD_ONION_RING : mesh = this.createGameMesh(this.materialOnionRing); break; case GameLib.CustomCode.FOOD_PATTY : mesh = this.createGameMesh(this.materialPatty); break; case GameLib.CustomCode.FOOD_TOMATO : mesh = this.createGameMesh(this.materialTomato); break; default : console.warn('unhandled food type'); break; } var positionX = GameLib.Utils.GetRandomInt(0, GameLib.CustomCode.GRID_WIDTH); var positionY = GameLib.Utils.GetRandomInt(0, GameLib.CustomCode.GRID_HEIGHT); mesh.position.x = (positionX * GameLib.CustomCode.BODY_SCALE_X) + GameLib.CustomCode.GRID_OFFSET_X; mesh.position.y = (positionY * GameLib.CustomCode.BODY_SCALE_Y) + GameLib.CustomCode.GRID_OFFSET_Y; /** * We apply the scale to the mesh too - since they appear too big when normal */ mesh.scale.x = GameLib.CustomCode.BODY_SCALE_X; mesh.scale.y = GameLib.CustomCode.BODY_SCALE_Y; mesh.visible = true; mesh.updateInstance('position'); //mesh.updateInstance('scale'); mesh.updateInstance('visible'); }.bind(this); /** * GameLib.CustomCode.SnakeBody * @param mesh * @param position * @param orientation * @param flip * @param backupMesh * @constructor */ GameLib.CustomCode.SnakeBody = function( mesh, position, orientation, flip, backupMesh, isTail ) { if (GameLib.Utils.UndefinedOrNull(mesh)) { throw new Error('no mesh specified'); } this.mesh = mesh; this.mesh.position.z = GameLib.CustomCode.SnakeBody.LastZ; GameLib.CustomCode.SnakeBody.LastZ += 0.1; this.mesh.updateInstance('position'); this.mesh.updateInstance('visible'); 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(orientation)) { orientation = GameLib.CustomCode.ORIENTATION_UP; } this.orientation = orientation; if (GameLib.Utils.UndefinedOrNull(flip)) { flip = 0; } this.flip = flip; if (GameLib.Utils.UndefinedOrNull(backupMesh)) { backupMesh = null; } this.backupMesh = backupMesh; if (GameLib.Utils.UndefinedOrNull(isTail)) { isTail = false; } this.isTail = isTail; this.applyToMesh(); }; GameLib.CustomCode.SnakeBody.LastZ = 0; GameLib.CustomCode.SnakeBody.prototype.applyToMesh = function() { this.mesh.position.x = (this.position.x * GameLib.CustomCode.BODY_SCALE_X) + GameLib.CustomCode.GRID_OFFSET_X; this.mesh.position.y = (this.position.y * GameLib.CustomCode.BODY_SCALE_Y) + GameLib.CustomCode.GRID_OFFSET_Y; //if ((this.mesh.rotation.z - (this.orientation * Math.PI / 2)) > Math.PI) { // this.mesh.rotation.z -= Math.PI; // this.mesh.updateInstance('rotation'); //} this.mesh.rotation.z = this.orientation * Math.PI / 2; if (this.backupMesh) { this.mesh.rotation.z += ((Math.PI / 2) * this.flip); this.mesh.updateInstance('position'); this.mesh.updateInstance('rotation'); } /** * TODO: We don't update instance position - animation should do this */ // this.mesh.updateInstance('position'); //if (!this.isTail) { this.mesh.updateInstance('rotation'); //} } GameLib.CustomCode.SnakeBody.prototype.advance = function(orientation, flip) { 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; } GameLib.CustomCode.prototype.advanceSnake = function(delta) { this.advanceTime += delta; if (this.advanceTime > this.speed) { this.advanceTime = 0; } else { return; } var backup = null; var temp = null; this.snake.map( function(body, index) { if (index === 0) { backup = { position : { x : body.position.x, y : body.position.y }, orientation : body.orientation, flip : body.flip } body.advance( this.state.orientation, this.state.flip ); backup.orientation = body.orientation; backup.flip = body.flip; } if (index > 0) { temp = { position : { x : body.position.x, y : body.position.y }, orientation : body.orientation, flip : body.flip } body.position.x = backup.position.x; body.position.y = backup.position.y; body.orientation = backup.orientation; body.flip = backup.flip; if (body.backupMesh) { /** * We used to be a corner, change back * @type {null} */ body.mesh.geometry = null; body.mesh.materials = null; body.mesh.remove(); body.mesh = body.backupMesh; body.backupMesh = null; body.mesh.visible = true; body.mesh.updateInstance('visible'); body.mesh.updateInstance('position'); } if ( body.orientation !== temp.orientation ) { if ((index ) < this.snake.length) { /** * Our orientation changed - we should make a corner */ body.orientation = temp.orientation; body.applyToMesh(); body.orientation = backup.orientation; body.backupMesh = body.mesh; if (temp.orientation === GameLib.CustomCode.ORIENTATION_UP) { body.backupMesh.position.y -= 0.5 * GameLib.CustomCode.BODY_SCALE_Y; } if (temp.orientation === GameLib.CustomCode.ORIENTATION_DOWN) { body.backupMesh.position.y += 0.5 * GameLib.CustomCode.BODY_SCALE_Y; } if (temp.orientation === GameLib.CustomCode.ORIENTATION_LEFT) { body.backupMesh.position.x += 0.5 * GameLib.CustomCode.BODY_SCALE_X; } if (temp.orientation === GameLib.CustomCode.ORIENTATION_RIGHT) { body.backupMesh.position.x -= 0.5 * GameLib.CustomCode.BODY_SCALE_X; } //body.backupMesh.visible = false; //body.backupMesh.updateInstance('visible'); body.mesh = this.createGameMesh(this.materialBreadCorner); body.mesh.position.z = 5; body.mesh.updateInstance('position'); body.mesh.visible = true; body.mesh.updateInstance('visible'); } } backup = temp; } //if (!body.isTail) { body.applyToMesh(); //} }.bind(this) ) this.state.turning = false; }.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 } } } }.bind(this); GameLib.Event.Subscribe( GameLib.Event.GAME_START, function() { /** * Remove all existing snake parts */ this.snake.map( function(body) { body.mesh.geometry = null; body.mesh.materials = null; body.mesh.remove(); } ) this.state = { orientation : GameLib.CustomCode.ORIENTATION_UP }; this.snake = [ new GameLib.CustomCode.SnakeBody( this.createGameMesh(this.materialBreadHead), { x : 4, y : 4 }, 0 ), new GameLib.CustomCode.SnakeBody( this.createGameMesh(this.materialBreadPatty), { x : 4, y : 3 }, 0 ), new GameLib.CustomCode.SnakeBody( this.createGameMesh(this.materialBreadPatty), { x : 4, y : 2 }, 0 ), new GameLib.CustomCode.SnakeBody( this.createGameMesh(this.materialBreadTail), { x : 4, y : 1 }, 0, 0, null, true ) ]; //this.snake[3].mesh.position.z += 1; this.snake[0].mesh.updateInstance('position'); this.snake[1].mesh.updateInstance('position'); this.snake[2].mesh.updateInstance('position'); this.snake[3].mesh.updateInstance('position'); this.animation.meshes.push(this.snake[0].mesh); this.animation.meshes.push(this.snake[1].mesh); this.animation.meshes.push(this.snake[2].mesh); //this.snake[2].mesh.instance.add(this.snake[3].mesh.instance); this.animation.meshes.push(this.snake[3].mesh); //this.animationRotation.meshes.push(this.snake[3].mesh); /** * Cleanup grid * this.grid.map( function(x) { x.map( function(y) { y.mesh.geometry = null; y.mesh.materials = null; y.mesh.remove(); } ); } ); */ this.initializeGrid(); //this.visualizeGrid(); /** * Other Settings */ this.advanceTime = 0; this.speed = GameLib.CustomCode.SPEED_INITIAL; this.foodTime = 0; this.foodSpeed = GameLib.CustomCode.FOOD_SPEED_INITIAL; this.foodItems = []; /** * Re-initialize our other custom code components */ this.beforeRender.initialized = false; this.beforeRender.entityLoaded = this; this.keyUp.initialized = false; this.keyUp.entityLoaded = this; console.log('starting game snake'); }.bind(this) ); GameLib.Event.Subscribe( GameLib.Event.GAME_OVER, function() { this.keyUp.initialized = false; this.keyUp.entityLoaded = null; this.beforeRender.initialized = false; this.beforeRender.entityLoaded = null; console.log('starting game snake'); }.bind(this) ) GameLib.Event.Emit(GameLib.Event.GAME_LOADED); //@ sourceURL=entityLoaded.js