/js/model/WorldEntityDescription.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs · JavaScript · 58 lines · 24 code · 9 blank · 25 comment · 0 complexity · d688e49c170a2bab234271ab55196f29 MD5 · raw file

  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. AbstractServerGame creates this each 'tick'
  11. -> NetChannel passes it to each Client
  12. -> Each client performs 'delta compression' to remove unchanged entities from being sent
  13. -> If ready, each client sends the customized WorldEntityDescription to it's connection
  14. Basic Usage:
  15. // Create a new world-entity-description, could be some room for optimization here but it only happens once per game loop anyway
  16. var worldEntityDescription = new WorldEntityDescription( this );
  17. this.netChannel.tick( this.gameClock, worldEntityDescription );
  18. */
  19. (function () {
  20. RealtimeMultiplayerGame.namespace("RealtimeMultiplayerGame.model");
  21. RealtimeMultiplayerGame.model.WorldEntityDescription = function (aGameInstance, allEntities) {
  22. this.gameClock = aGameInstance.getGameClock();
  23. this.gameTick = aGameInstance.getGameTick();
  24. this.allEntities = allEntities;
  25. // Ask each entity to create it's EntityDescriptionString
  26. this.entities = [];
  27. return this;
  28. };
  29. RealtimeMultiplayerGame.model.WorldEntityDescription.prototype = {
  30. entities: null,
  31. gameClock: 0,
  32. gameTick: 0,
  33. /**
  34. * Ask each entity to create it's entity description
  35. * Returns a single snapshot of the worlds current state as a '|' delimited string
  36. * @return {String} A '|' delmited string of the current world state
  37. */
  38. getEntityDescriptionAsString: function () {
  39. var len = this.allEntities.length;
  40. var fullDescriptionString = '';
  41. this.allEntities.forEach(function (key, entity) {
  42. var entityDescriptionString = entity.constructEntityDescription(this.gameTick);
  43. fullDescriptionString += "|" + entityDescriptionString;
  44. }, this);
  45. return fullDescriptionString;
  46. }
  47. }
  48. })();