/js/model/WorldEntityDescription.js
JavaScript | 58 lines | 24 code | 9 blank | 25 comment | 0 complexity | d688e49c170a2bab234271ab55196f29 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
1/** 2 File: 3 WorldEntityDescription.js 4 Created By: 5 Mario Gonzalez 6 Project: 7 RealtimeMultiplayerNodeJS 8 Abstract: 9 A WorldEntityDescription is a full description of the current world state. 10 11 AbstractServerGame creates this each 'tick' 12 -> NetChannel passes it to each Client 13 -> Each client performs 'delta compression' to remove unchanged entities from being sent 14 -> If ready, each client sends the customized WorldEntityDescription to it's connection 15 Basic Usage: 16 // Create a new world-entity-description, could be some room for optimization here but it only happens once per game loop anyway 17 var worldEntityDescription = new WorldEntityDescription( this ); 18 this.netChannel.tick( this.gameClock, worldEntityDescription ); 19 */ 20(function () { 21 RealtimeMultiplayerGame.namespace("RealtimeMultiplayerGame.model"); 22 23 RealtimeMultiplayerGame.model.WorldEntityDescription = function (aGameInstance, allEntities) { 24 this.gameClock = aGameInstance.getGameClock(); 25 this.gameTick = aGameInstance.getGameTick(); 26 this.allEntities = allEntities; 27 28 29 // Ask each entity to create it's EntityDescriptionString 30 this.entities = []; 31 32 33 return this; 34 }; 35 36 RealtimeMultiplayerGame.model.WorldEntityDescription.prototype = { 37 entities: null, 38 gameClock: 0, 39 gameTick: 0, 40 41 /** 42 * Ask each entity to create it's entity description 43 * Returns a single snapshot of the worlds current state as a '|' delimited string 44 * @return {String} A '|' delmited string of the current world state 45 */ 46 getEntityDescriptionAsString: function () { 47 var len = this.allEntities.length; 48 var fullDescriptionString = ''; 49 50 this.allEntities.forEach(function (key, entity) { 51 var entityDescriptionString = entity.constructEntityDescription(this.gameTick); 52 fullDescriptionString += "|" + entityDescriptionString; 53 }, this); 54 55 return fullDescriptionString; 56 } 57 } 58})();