/js/DemoCircles/DemoClientGame.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs · JavaScript · 144 lines · 75 code · 22 blank · 47 comment · 4 complexity · 973ea68d9227067151a2022af3ecfb1f MD5 · raw file

  1. /**
  2. File:
  3. DemoServerGame
  4. Created By:
  5. Mario Gonzalez
  6. Project:
  7. DemoApp
  8. Abstract:
  9. This is a concrete server instance of our game
  10. Basic Usage:
  11. DemoServerGame = new DemoApp.DemoServerGame();
  12. DemoServerGame.start();
  13. DemoServerGame.explodeEveryone();
  14. Version:
  15. 1.0
  16. */
  17. (function () {
  18. DemoApp.DemoClientGame = function () {
  19. DemoApp.DemoClientGame.superclass.constructor.call(this);
  20. this.startGameClock();
  21. return this;
  22. };
  23. DemoApp.DemoClientGame.prototype = {
  24. setupView: function () {
  25. this.view = new DemoApp.DemoView();
  26. this.view.insertIntoHTMLElementWithId("gamecontainer");
  27. DemoApp.DemoClientGame.superclass.setupView.call(this);
  28. },
  29. /**
  30. * @inheritDoc
  31. */
  32. tick: function () {
  33. DemoApp.DemoClientGame.superclass.tick.call(this);
  34. this.view.stats.update();
  35. this.view.update(this.gameClockReal);
  36. },
  37. /**
  38. * @inheritDoc
  39. */
  40. createEntityFromDesc: function (entityDesc) {
  41. var diameter = entityDesc.radius * 2;
  42. // Create a view via CAAT
  43. var aCircleView = new CAAT.ShapeActor();
  44. aCircleView.create();
  45. aCircleView.setSize(diameter, diameter);
  46. aCircleView.setFillStyle("#" + CAAT.Color.prototype.hsvToRgb((entityDesc.entityid * 15) % 360, 40, 99).toHex()); // Random color
  47. aCircleView.setLocation(entityDesc.x, entityDesc.y); // Place in the center of the screen, use the director's width/height
  48. var newEntity = null;
  49. var isOwnedByMe = entityDesc.clientid == this.netChannel.clientid;
  50. // If this is a player entity
  51. if (entityDesc.entityType & DemoApp.Constants.ENTITY_TYPES.PLAYER_ENTITY) {
  52. newEntity = new DemoApp.PlayerEntity(entityDesc.entityid, entityDesc.clientid);
  53. // If it is a player entity and it's my player entity - attach a KeyboardInputTrait to it
  54. if (isOwnedByMe) {
  55. newEntity.addTraitAndExecute(new RealtimeMultiplayerGame.controller.traits.KeyboardInputTrait());
  56. this.clientCharacter = newEntity;
  57. }
  58. } else {
  59. newEntity = new DemoApp.CircleEntity(entityDesc.entityid, entityDesc.clientid);
  60. }
  61. newEntity.position.set(entityDesc.x, entityDesc.y);
  62. newEntity.setView(aCircleView);
  63. this.fieldController.addEntity(newEntity);
  64. },
  65. /**
  66. * Called by the ClientNetChannel, it sends us an array containing tightly packed values and expects us to return a meaningful object
  67. * It is left up to each game to implement this function because only the game knows what it needs to send.
  68. * However the 4 example projects in RealtimeMultiplayerNodeJS offer should be used ans examples
  69. *
  70. * @param {Array} entityDescAsArray An array of tightly packed values
  71. * @return {Object} An object which will be returned to you later on tied to a specific entity
  72. */
  73. parseEntityDescriptionArray: function (entityDescAsArray) {
  74. var entityDescription = {};
  75. // It is left upto each game to implement this function because only the game knows what it needs to send.
  76. // However the 4 example projects in RealtimeMultiplayerNodeJS offer this an example
  77. entityDescription.entityid = entityDescAsArray[0];
  78. entityDescription.clientid = entityDescAsArray[1];
  79. entityDescription.entityType = +entityDescAsArray[2];
  80. entityDescription.x = +entityDescAsArray[3];
  81. entityDescription.y = +entityDescAsArray[4];
  82. entityDescription.radius = +entityDescAsArray[5];
  83. entityDescription.color = entityDescAsArray[6];
  84. return entityDescription;
  85. },
  86. /**
  87. * @inheritDoc
  88. */
  89. netChannelDidConnect: function (messageData) {
  90. DemoApp.DemoClientGame.superclass.netChannelDidConnect.call(this, messageData);
  91. DemoApp.DemoClientGame.prototype.log("DemoClientGame: Joining Game");
  92. this.joinGame("Player" + this.netChannel.getClientid()); // Automatically join the game with some name
  93. },
  94. /**
  95. * @inheritDoc
  96. */
  97. netChannelDidDisconnect: function (messageData) {
  98. DemoApp.DemoClientGame.superclass.netChannelDidDisconnect.call(this, messageData);
  99. DemoApp.DemoClientGame.prototype.log("DemoClientGame: netChannelDidDisconnect"); // Display disconnect
  100. },
  101. /**
  102. * This function logs something to the right panel
  103. * @param {Object} An object in the form of: { message: ['Client', 'domReady'] }
  104. */
  105. log: (function () {
  106. var message = function (message) {
  107. var el = document.createElement('p');
  108. el.innerHTML = '<b>' + esc(message) + ':</b> ';
  109. // Log if possible
  110. console.log(message);
  111. document.getElementsByTagName('aside')[0].appendChild(el);
  112. document.getElementsByTagName('aside')[0].scrollTop = 1000000;
  113. };
  114. var esc = function (msg) {
  115. return msg.replace(/</g, '&lt;').replace(/>/g, '&gt;');
  116. };
  117. return message;
  118. })()
  119. }
  120. // extend RealtimeMultiplayerGame.AbstractClientGame
  121. RealtimeMultiplayerGame.extend(DemoApp.DemoClientGame, RealtimeMultiplayerGame.AbstractClientGame, null);
  122. })()