if (this.parentEntity === data.entity) { console.log('snake entity loaded'); } else { return; } /** * Defines */ GameLib.CustomCode.BODY_TYPE_HEAD = 0x1; GameLib.CustomCode.BODY_TYPE_TAIL = 0x2; GameLib.CustomCode.GRID_WIDTH = 11; GameLib.CustomCode.GRID_HEIGHT = 11; /** * Get runtime */ this.runtime = GameLib.Utils.GetRuntime(); /** * Custom Code Components */ this.beforeRender = GameLib.EntityManager.Instance.findComponentById('zjq6ach3jt'); /** * Geometries */ this.geometryBody = GameLib.EntityManager.Instance.findComponentById('8f5q7k5ozp'); /** * Images */ this.imageEnd = GameLib.EntityManager.Instance.findComponentById('swkla3wpp'); this.imageBodyPatty = GameLib.EntityManager.Instance.findComponentById('01r51k9ptr'); this.imagePatty = GameLib.EntityManager.Instance.findComponentById('4lf0fw24su'); /** * Other Objects (Scene) */ this.scene = GameLib.EntityManager.Instance.findComponentById('pllp034hsj'); /** * Game objects */ this.snake = []; this.grid = []; GameLib.CustomCode.prototype.advanceSnake = function(delta) { }.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 }; if (this.snake.length > 0) { direction.x = this.snake[0].direction.x; direction.y = this.snake[0].direction.y; } return { mesh : body, direction : direction } }.bind(this); GameLib.CustomCode.prototype.createMaterial = function(image) { var diffuseMap = new GameLib.D3.Texture.Image( this.runtime.graphics, { image : image } ) return new GameLib.D3.Material.Phong( this.runtime.graphics, { diffuseMap : diffuseMap } ); }.bind(this); GameLib.CustomCode.prototype.createGameMesh = function(image) { var mesh = new GameLib.D3.Mesh( this.runtime.graphics, { geometry : this.geometryBody, materials : [this.createMaterial(image)] } ) this.scene.addClone(mesh); return mesh; }.bind(this) /** * Create our objects */ this.meshEnd = this.createGameMesh(this.imageEnd); this.meshPatty = this.createGameMesh(this.imagePatty); this.meshBodyPatty = this.createGameMesh(this.imageBodyPatty); GameLib.Event.Subscribe( GameLib.Event.GAME_START, function() { /** * Remove all existing snake parts */ this.snake.map( function(body) { body.geometry = null; body.materials = null; body.remove(); } ) this.snake = [ this.cloneBody(GameLib.CustomCode.BODY_TYPE_HEAD), this.cloneBody(GameLib.CustomCode.BODY_TYPE_TAIL) ]; /** * 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.beforeRender.initialized = false; this.beforeRender.entityLoaded = this; console.log('starting game snake'); }.bind(this) ); GameLib.Event.Subscribe( GameLib.Event.GAME_OVER, function() { this.beforeRender.entityLoaded = null; this.beforeRender.initialized = false; console.log('starting game snake'); }.bind(this) ) GameLib.Event.Emit(GameLib.Event.GAME_LOADED); //@ sourceURL=entityLoaded.js