PageRenderTime 42ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/js/DemoCircles/DemoServerGame.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 147 lines | 77 code | 23 blank | 47 comment | 1 complexity | 989d11ebd09461ae669ac270ed5fb68f 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. 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. require("../model/ImprovedNoise.js");
  19. DemoApp.DemoServerGame = function () {
  20. DemoApp.DemoServerGame.superclass.constructor.call(this);
  21. this.setGameDuration(DemoApp.Constants.GAME_DURATION);
  22. this.setupCollisionManager();
  23. this.setupRandomField();
  24. return this;
  25. };
  26. DemoApp.DemoServerGame.prototype = {
  27. collisionManager: null,
  28. /**
  29. * Map RealtimeMultiplayerGame.Constants.CMDS to functions
  30. * If ServerNetChannel does not contain a function, it will check to see if it is a special function which the delegate wants to catch
  31. * If it is set, it will call that CMD on its delegate
  32. */
  33. setupCmdMap: function () {
  34. DemoApp.DemoServerGame.superclass.setupCmdMap.call(this);
  35. this.cmdMap[RealtimeMultiplayerGame.Constants.CMDS.PLAYER_UPDATE] = this.shouldUpdatePlayer;
  36. },
  37. setupCollisionManager: function () {
  38. // Collision simulation
  39. this.collisionManager = new RealtimeMultiplayerGame.modules.circlecollision.CircleManager();
  40. this.collisionManager.setBounds(0, 0, DemoApp.Constants.GAME_WIDTH, DemoApp.Constants.GAME_HEIGHT);
  41. this.collisionManager.setNumberOfCollisionPasses(2);
  42. this.collisionManager.setNumberOfTargetingPasses(0);
  43. this.collisionManager.setCallback(this.onCollisionManagerCollision, this);
  44. },
  45. /**
  46. * Called when the collision manager detects a collision
  47. */
  48. onCollisionManagerCollision: function (ci, cj, v) {
  49. ci.delegate.tempColor();
  50. cj.delegate.tempColor();
  51. },
  52. /**
  53. * Randomly places some CircleEntities into game
  54. */
  55. setupRandomField: function () {
  56. //RealtimeMultiplayerGame.model.noise(10, 10, i/total)
  57. var total = DemoApp.Constants.MAX_CIRCLES;
  58. for (var i = 0; i < total; i++) {
  59. var radius = DemoApp.Constants.ENTITY_DEFAULT_RADIUS + Math.random() * 5;
  60. this.createCircleEntity(radius, this.getNextEntityID(), RealtimeMultiplayerGame.Constants.SERVER_SETTING.CLIENT_ID);
  61. }
  62. },
  63. /**
  64. * Helper method to create a single CircleEntity
  65. * @param {Number} aRadius
  66. * @param {Number} anEntityid
  67. * @param {Number} aClientid
  68. */
  69. createCircleEntity: function (aRadius, anEntityid, aClientid) {
  70. // Create a randomly sized circle, that will represent this entity in the collision manager
  71. var collisionCircle = new RealtimeMultiplayerGame.modules.circlecollision.PackedCircle();
  72. collisionCircle.setRadius(aRadius);
  73. // Create the GameEntity
  74. var circleEntity = new DemoApp.CircleEntity(anEntityid, aClientid);
  75. circleEntity.radius = aRadius;
  76. circleEntity.position.set(Math.random() * DemoApp.Constants.GAME_WIDTH, Math.random() * DemoApp.Constants.GAME_HEIGHT);
  77. circleEntity.setCollisionCircle(collisionCircle);
  78. // Place the circle and collision circle into corresponding containers
  79. this.collisionManager.addCircle(circleEntity.getCollisionCircle());
  80. this.fieldController.addEntity(circleEntity);
  81. circleEntity.entityType = DemoApp.Constants.ENTITY_TYPES.GENERIC_CIRCLE;
  82. return circleEntity;
  83. },
  84. createPlayerEntity: function (anEntityid, aClientid) {
  85. // Create the GameEntity
  86. var playerEntity = new DemoApp.PlayerEntity(anEntityid, aClientid);
  87. playerEntity.position.set(Math.random() * DemoApp.Constants.GAME_WIDTH, Math.random() * DemoApp.Constants.GAME_HEIGHT);
  88. var collisionCircle = new RealtimeMultiplayerGame.modules.circlecollision.PackedCircle();
  89. collisionCircle.setRadius(playerEntity.radius);
  90. playerEntity.setInput(new RealtimeMultiplayerGame.Input.Keyboard());
  91. playerEntity.setCollisionCircle(collisionCircle);
  92. // place player on field
  93. this.collisionManager.addCircle(playerEntity.getCollisionCircle());
  94. this.fieldController.addPlayer(playerEntity);
  95. return playerEntity;
  96. },
  97. /**
  98. * Updates the game
  99. * Creates a WorldEntityDescription which it sends to NetChannel
  100. */
  101. tick: function () {
  102. // Use both the BOUNDARY_WRAP_X flag, and the BOUNDARY_CONSTRAIN_Y flags as the rule
  103. var boundsRule = RealtimeMultiplayerGame.modules.circlecollision.CircleManager.prototype.BOUNDARY_WRAP_X;
  104. boundsRule |= RealtimeMultiplayerGame.modules.circlecollision.CircleManager.prototype.BOUNDARY_CONSTRAIN_Y;
  105. this.collisionManager.handleBoundaryForAllCircles(boundsRule);
  106. this.collisionManager.handleCollisions();
  107. // Note we call superclass's implementation after we're done
  108. DemoApp.DemoServerGame.superclass.tick.call(this);
  109. },
  110. shouldAddPlayer: function (aClientid, data) {
  111. this.createPlayerEntity(this.getNextEntityID(), aClientid);
  112. },
  113. shouldUpdatePlayer: function (aClientid, data) {
  114. var entity = this.fieldController.getEntityWithid(data.payload.entityid);
  115. entity.input.deconstructInputBitmask(data.payload.input);
  116. },
  117. shouldRemovePlayer: function (aClientid) {
  118. DemoApp.DemoServerGame.superclass.shouldRemovePlayer.call(this, aClientid);
  119. console.log("DEMO::REMOVEPLAYER");
  120. }
  121. };
  122. // extend RealtimeMultiplayerGame.AbstractServerGame
  123. RealtimeMultiplayerGame.extend(DemoApp.DemoServerGame, RealtimeMultiplayerGame.AbstractServerGame, null);
  124. })();