PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/js/DemoHelloWorld/DemoServerGame.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 123 lines | 54 code | 15 blank | 54 comment | 2 complexity | 01ab85bb161787e11351007758e8e8c4 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. require("../model/ImprovedNoise.js");
  19. DemoHelloWorld.DemoServerGame = function () {
  20. DemoHelloWorld.DemoServerGame.superclass.constructor.call(this);
  21. this.setGameDuration(DemoHelloWorld.Constants.GAME_DURATION);
  22. this.setupRandomField();
  23. return this;
  24. };
  25. DemoHelloWorld.DemoServerGame.prototype = {
  26. /**
  27. * Map RealtimeMultiplayerGame.Constants.CMDS to functions
  28. * If ServerNetChannel does not contain a function, it will check to see if it is a special function which the delegate wants to catch
  29. * If it is set, it will call that CMD on its delegate
  30. */
  31. setupCmdMap: function () {
  32. DemoHelloWorld.DemoServerGame.superclass.setupCmdMap.call(this);
  33. this.cmdMap[RealtimeMultiplayerGame.Constants.CMDS.PLAYER_UPDATE] = this.shouldUpdatePlayer;
  34. },
  35. /**
  36. * Randomly places some CircleEntities into game
  37. */
  38. setupRandomField: function () {
  39. //RealtimeMultiplayerGame.model.noise(10, 10, i/total)
  40. var total = DemoHelloWorld.Constants.MAX_CIRCLES;
  41. for (var i = 0; i < total; i++) {
  42. var radius = DemoHelloWorld.Constants.ENTITY_DEFAULT_RADIUS + Math.random() * 5;
  43. this.createCircleEntity(radius, this.getNextEntityID(), RealtimeMultiplayerGame.Constants.SERVER_SETTING.CLIENT_ID);
  44. }
  45. },
  46. /**
  47. * Helper method to create a single CircleEntity
  48. * @param {Number} aRadius
  49. * @param {Number} anEntityid
  50. * @param {Number} aClientid
  51. */
  52. createCircleEntity: function (aRadius, anEntityid, aClientid) {
  53. // Create the GameEntity
  54. var circleEntity = new DemoHelloWorld.CircleEntity(anEntityid, aClientid);
  55. circleEntity.entityType = DemoHelloWorld.Constants.ENTITY_TYPES.CIRCLE;
  56. circleEntity.radius = aRadius;
  57. circleEntity.position.set(Math.random() * DemoHelloWorld.Constants.GAME_WIDTH, Math.random() * DemoHelloWorld.Constants.GAME_HEIGHT);
  58. // Place the circle and collision circle into corresponding containers
  59. this.fieldController.addEntity(circleEntity);
  60. return circleEntity;
  61. },
  62. /**
  63. * Updates the game
  64. * Creates a WorldEntityDescription which it sends to NetChannel
  65. */
  66. tick: function () {
  67. // Do some fancy stuff for our own game
  68. // Loop through each entity and move it to the left
  69. this.fieldController.getEntities().forEach(function (key, entity) {
  70. entity.position.x -= entity.speed;
  71. if (entity.position.x < 0) { // reset
  72. entity.position.x = DemoHelloWorld.Constants.GAME_WIDTH;
  73. }
  74. }, this);
  75. // Note we call superclass's implementation after we're done
  76. DemoHelloWorld.DemoServerGame.superclass.tick.call(this);
  77. },
  78. /**
  79. * @inheritDoc
  80. */
  81. shouldAddPlayer: function (aClientid, data) {
  82. // A player has joined the game - do something fancy
  83. this.createCircleEntity(50, this.getNextEntityID(), aClientid);
  84. },
  85. /**
  86. * @inheritDoc
  87. */
  88. shouldUpdatePlayer: function (aClientid, data) {
  89. var entity = this.fieldController.getEntityWithid(data.payload.entityid);
  90. entity.input.deconstructInputBitmask(data.payload.input);
  91. },
  92. /**
  93. * @inheritDoc
  94. */
  95. shouldRemovePlayer: function (aClientid) {
  96. DemoHelloWorld.DemoServerGame.superclass.shouldRemovePlayer.call(this, aClientid);
  97. console.log("DEMO::REMOVEPLAYER");
  98. },
  99. /**
  100. * @inheritDoc
  101. */
  102. dealloc: function () {
  103. DemoHelloWorld.DemoServerGame.superclass.dealloc.call(this);
  104. }
  105. };
  106. // extend RealtimeMultiplayerGame.AbstractServerGame
  107. RealtimeMultiplayerGame.extend(DemoHelloWorld.DemoServerGame, RealtimeMultiplayerGame.AbstractServerGame, null);
  108. })()