PageRenderTime 25ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/js/DemoHelloWorld/DemoClientGame.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 152 lines | 72 code | 21 blank | 59 comment | 0 complexity | 567b5203fa213f57dd47aecc9e8152f9 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. DemoServerGame
  4. Created By:
  5. Mario Gonzalez
  6. Project:
  7. DemoHelloWorld
  8. Abstract:
  9. This is a concrete server instance of our game
  10. Basic Usage:
  11. DemoServerGame = new DemoHelloWorld.DemoServerGame();
  12. DemoServerGame.start();
  13. DemoServerGame.explodeEveryone();
  14. Version:
  15. 1.0
  16. */
  17. (function () {
  18. DemoHelloWorld.DemoClientGame = function () {
  19. DemoHelloWorld.DemoClientGame.superclass.constructor.call(this);
  20. this.startGameClock();
  21. return this;
  22. };
  23. DemoHelloWorld.DemoClientGame.prototype = {
  24. setupView: function () {
  25. this.view = new DemoHelloWorld.DemoView();
  26. this.view.insertIntoHTMLElementWithId("gamecontainer");
  27. DemoHelloWorld.DemoClientGame.superclass.setupView.call(this);
  28. },
  29. /**
  30. * @inheritDoc
  31. */
  32. tick: function () {
  33. DemoHelloWorld.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. // Create the entity and add it to the fieldcontroller
  49. var newEntity = new DemoHelloWorld.CircleEntity(entityDesc.entityid, entityDesc.clientid);
  50. newEntity.position.set(entityDesc.x, entityDesc.y);
  51. newEntity.setView(aCircleView);
  52. this.fieldController.addEntity(newEntity);
  53. },
  54. /**
  55. * Called by the ClientNetChannel, it sends us an array containing tightly packed values and expects us to return a meaningful object
  56. * It is left up to each game to implement this function because only the game knows what it needs to send.
  57. * However the 4 example projects in RealtimeMultiplayerNodeJS offer should be used ans examples
  58. *
  59. * @param {Array} entityDescAsArray An array of tightly packed values
  60. * @return {Object} An object which will be returned to you later on tied to a specific entity
  61. */
  62. parseEntityDescriptionArray: function (entityDescAsArray) {
  63. var entityDescription = {};
  64. // It is left upto each game to implement this function because only the game knows what it needs to send.
  65. // However the 4 example projects in RealtimeMultiplayerNodeJS offer this an example
  66. entityDescription.entityid = +entityDescAsArray[0];
  67. entityDescription.clientid = +entityDescAsArray[1];
  68. entityDescription.entityType = +entityDescAsArray[2];
  69. entityDescription.x = +entityDescAsArray[3];
  70. entityDescription.y = +entityDescAsArray[4];
  71. entityDescription.radius = +entityDescAsArray[5];
  72. return entityDescription;
  73. },
  74. /**
  75. * @inheritDoc
  76. */
  77. netChannelDidConnect: function (messageData) {
  78. DemoHelloWorld.DemoClientGame.superclass.netChannelDidConnect.call(this, messageData);
  79. this.joinGame("Player" + this.netChannel.getClientid()); // Automatically join the game with some name
  80. },
  81. /**
  82. * @inheritDoc
  83. */
  84. netChannelDidReceiveMessage: function (messageData) {
  85. DemoHelloWorld.DemoClientGame.superclass.netChannelDidReceiveMessage.call(this, messageData);
  86. // Do some stuff here
  87. },
  88. /**
  89. * @inheritDoc
  90. */
  91. netChannelDidDisconnect: function (messageData) {
  92. DemoHelloWorld.DemoClientGame.superclass.netChannelDidDisconnect.call(this, messageData);
  93. // Do some stuff here
  94. },
  95. /**
  96. * @inheritDoc
  97. */
  98. joinGame: function (aNickname) {
  99. // This is called when the user has decided to join the game, in our HelloWorld example this happens automatically
  100. // In a regular game situation, this might happen after a user picked their chracter for example
  101. this.nickname = aNickname;
  102. // Create a 'join' message and queue it in ClientNetChannel
  103. this.netChannel.addMessageToQueue(true, RealtimeMultiplayerGame.Constants.CMDS.PLAYER_JOINED, { nickname: this.nickname });
  104. },
  105. /**
  106. * @inheritDoc
  107. */
  108. log: (function () {
  109. var message = function (message) {
  110. var el = document.createElement('p');
  111. el.innerHTML = '<b>' + esc(message) + ':</b> ';
  112. // Log if possible
  113. console.log(message);
  114. document.getElementsByTagName('aside')[0].appendChild(el);
  115. document.getElementsByTagName('aside')[0].scrollTop = 1000000;
  116. };
  117. var esc = function (msg) {
  118. return msg.replace(/</g, '&lt;').replace(/>/g, '&gt;');
  119. };
  120. return message;
  121. })(),
  122. /**
  123. * Deallocate memory from your game, and let the superclass do the same
  124. */
  125. dealloc: function () {
  126. DemoHelloWorld.DemoClientGame.superclass.dealloc.call(this);
  127. }
  128. };
  129. // extend RealtimeMultiplayerGame.AbstractClientGame
  130. RealtimeMultiplayerGame.extend(DemoHelloWorld.DemoClientGame, RealtimeMultiplayerGame.AbstractClientGame, null);
  131. })()