/js/BubbleDots/BubbleDotsClientGame.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs · JavaScript · 135 lines · 69 code · 20 blank · 46 comment · 3 complexity · ffd895aef61ca235d5bac127bfbce19e MD5 · raw file

  1. /**
  2. File:
  3. DemoServerGame
  4. Created By:
  5. Mario Gonzalez
  6. Project:
  7. BubbleDots
  8. Abstract:
  9. This is a concrete server instance of our game
  10. Basic Usage:
  11. DemoServerGame = new BubbleDots.DemoServerGame();
  12. DemoServerGame.start();
  13. DemoServerGame.explodeEveryone();
  14. Version:
  15. 1.0
  16. */
  17. (function () {
  18. BubbleDots.DemoClientGame = function () {
  19. BubbleDots.DemoClientGame.superclass.constructor.call(this);
  20. this.startGameClock();
  21. return this;
  22. };
  23. BubbleDots.DemoClientGame.prototype = {
  24. setupView: function (images) {
  25. this.view = new BubbleDots.DemoView(images);
  26. this.view.insertIntoHTMLElementWithId("gamecontainer");
  27. BubbleDots.DemoClientGame.superclass.setupView.call(this);
  28. },
  29. /**
  30. * @inheritDoc
  31. */
  32. tick: function () {
  33. BubbleDots.DemoClientGame.superclass.tick.call(this);
  34. this.view.stats.update();
  35. this.view.update(this.gameClockReal);
  36. this.view.textfield.setText("Ping: " + this.netChannel.getLatency());
  37. },
  38. /**
  39. * @inheritDoc
  40. */
  41. createEntityFromDesc: function (entityDesc) {
  42. // Create a new BubbleDots entity
  43. var newEntity = new BubbleDots.CircleEntity(entityDesc.entityid, entityDesc.clientid);
  44. newEntity.position.set(entityDesc.x, entityDesc.y);
  45. var entityView = this.view.createEntityView(entityDesc);
  46. newEntity.setView(entityView);
  47. this.fieldController.addEntity(newEntity);
  48. // Our own character
  49. if (entityDesc.clientid == this.netChannel.getClientid() && entityDesc.entityType & BubbleDots.Constants.ENTITY_TYPES.PLAYER_ENTITY) {
  50. this.setupClientPlayer(newEntity);
  51. this.view.setFocusCharacter(entityView);
  52. }
  53. },
  54. /**
  55. * Called when the player that represents this user is created
  56. * @param anEntity
  57. */
  58. setupClientPlayer: function (anEntity) {
  59. anEntity.addTraitAndExecute(new RealtimeMultiplayerGame.controller.traits.KeyboardInputTrait());
  60. this.clientCharacter = anEntity;
  61. },
  62. /**
  63. * @inheritDoc
  64. */
  65. netChannelDidConnect: function (messageData) {
  66. BubbleDots.DemoClientGame.superclass.netChannelDidConnect.call(this, messageData);
  67. BubbleDots.DemoClientGame.prototype.log("DemoClientGame: Joining Game");
  68. this.joinGame("Player" + this.netChannel.getClientid()); // Automatically join the game with some name
  69. },
  70. /**
  71. * @inheritDoc
  72. */
  73. netChannelDidDisconnect: function (messageData) {
  74. BubbleDots.DemoClientGame.superclass.netChannelDidDisconnect.call(this, messageData);
  75. BubbleDots.DemoClientGame.prototype.log("DemoClientGame: netChannelDidDisconnect"); // Display disconnect
  76. },
  77. /**
  78. * An array containing values received from the entity
  79. * @param {String} singleWorldUpdate
  80. */
  81. parseEntityDescriptionArray: function (entityDescAsArray) {
  82. var entityDescription = {};
  83. // It is up to the user to make sure that their objects are following a certain order
  84. // We do this because we need the performance of sending the tiniest strings possible
  85. entityDescription.entityid = entityDescAsArray[0];
  86. entityDescription.clientid = entityDescAsArray[1];
  87. entityDescription.entityType = +entityDescAsArray[2];
  88. entityDescription.x = +entityDescAsArray[3];
  89. entityDescription.y = +entityDescAsArray[4];
  90. entityDescription.scale = +entityDescAsArray[5];
  91. entityDescription.color = entityDescAsArray[6];
  92. return entityDescription;
  93. },
  94. /**
  95. * This function logs something to the right panel
  96. * @param {Object} An object in the form of: { message: ['Client', 'domReady'] }
  97. */
  98. log: (function () {
  99. var message = function (message) {
  100. var el = document.createElement('p');
  101. el.innerHTML = '<b>' + esc(message) + ':</b> ';
  102. // Log if possible
  103. console.log(message);
  104. document.getElementsByTagName('aside')[0].appendChild(el);
  105. document.getElementsByTagName('aside')[0].scrollTop = 1000000;
  106. };
  107. var esc = function (msg) {
  108. return msg.replace(/</g, '&lt;').replace(/>/g, '&gt;');
  109. };
  110. return message;
  111. })()
  112. };
  113. // extend RealtimeMultiplayerGame.AbstractClientGame
  114. RealtimeMultiplayerGame.extend(BubbleDots.DemoClientGame, RealtimeMultiplayerGame.AbstractClientGame, null);
  115. })();