PageRenderTime 21ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/js/BubbleDots/BubbleDotsServerGame.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 170 lines | 82 code | 25 blank | 63 comment | 4 complexity | c91e3bad0753dff1127038a9e28c22bb 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. 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. require("../model/ImprovedNoise.js");
  19. require("./lib/color.js");
  20. require("./lib/Tween.js");
  21. BubbleDots.DemoServerGame = function () {
  22. BubbleDots.DemoServerGame.superclass.constructor.call(this);
  23. this.setGameDuration(BubbleDots.Constants.GAME_DURATION);
  24. this.setupCollisionManager();
  25. this.setupRandomField();
  26. return this;
  27. };
  28. BubbleDots.DemoServerGame.prototype = {
  29. collisionManager: null,
  30. /**
  31. * Map RealtimeMultiplayerGame.Constants.CMDS to functions
  32. * If ServerNetChannel does not contain a function, it will check to see if it is a special function which the delegate wants to catch
  33. * If it is set, it will call that CMD on its delegate
  34. */
  35. setupCmdMap: function () {
  36. BubbleDots.DemoServerGame.superclass.setupCmdMap.call(this);
  37. this.cmdMap[RealtimeMultiplayerGame.Constants.CMDS.PLAYER_UPDATE] = this.shouldUpdatePlayer;
  38. },
  39. /**
  40. * Sets up the collision manager
  41. */
  42. setupCollisionManager: function () {
  43. // Collision simulation
  44. this.collisionManager = new RealtimeMultiplayerGame.modules.circlecollision.CircleManager();
  45. this.collisionManager.setBounds(0, 0, BubbleDots.Constants.GAME_WIDTH, BubbleDots.Constants.GAME_HEIGHT);
  46. this.collisionManager.setNumberOfCollisionPasses(2);
  47. this.collisionManager.setNumberOfTargetingPasses(0);
  48. this.collisionManager.setCallback(this.onCollisionManagerCollision, this);
  49. },
  50. /**
  51. * Called when the collision manager detects a collision
  52. */
  53. onCollisionManagerCollision: function (ci, cj, v) {
  54. ci.delegate.onCollision(ci.delegate, cj.delegate, v.clone());
  55. cj.delegate.onCollision(ci.delegate, cj.delegate, v.clone());
  56. },
  57. /**
  58. * Randomly places some CircleEntities into game
  59. */
  60. setupRandomField: function () {
  61. //RealtimeMultiplayerGame.model.noise(10, 10, i/total)
  62. var total = BubbleDots.Constants.MAX_CIRCLES;
  63. for (var i = 0; i < total; i++) {
  64. var radius = BubbleDots.Constants.ENTITY_DEFAULT_RADIUS;
  65. var entity = this.createEntity(BubbleDots.CircleEntity, radius, this.getNextEntityID(), RealtimeMultiplayerGame.Constants.SERVER_SETTING.CLIENT_ID);
  66. // Randomly make the object 'food' or 'poison'
  67. if (i % 5 === 0) {
  68. entity.addTraitAndExecute(new BubbleDots.traits.PoisonTrait());
  69. } else {
  70. entity.addTraitAndExecute(new BubbleDots.traits.FoodTrait());
  71. }
  72. // entity.addTraitAndExecute( new BubbleDots.traits.PerlinNoiseTrait() );
  73. }
  74. },
  75. /**
  76. * Helper method to create a single CircleEntity
  77. * @param {Number} aRadius
  78. * @param {Number} anEntityid
  79. * @param {Number} aClientid
  80. */
  81. createEntity: function (aBubbleDotEntityConstructor, aRadius, anEntityid, aClientid) {
  82. // Create the GameEntity
  83. var circleEntity = new aBubbleDotEntityConstructor(anEntityid, aClientid);
  84. circleEntity.position.set(Math.random() * BubbleDots.Constants.GAME_WIDTH * 20, Math.random() * BubbleDots.Constants.GAME_HEIGHT);
  85. // Create a randomly sized circle, that will represent this entity in the collision manager
  86. var collisionCircle = new RealtimeMultiplayerGame.modules.circlecollision.PackedCircle();
  87. circleEntity.setCollisionCircle(collisionCircle);
  88. circleEntity.setRadius(aRadius);
  89. // Place the circle and collision circle into corresponding containers
  90. this.collisionManager.addCircle(circleEntity.getCollisionCircle());
  91. this.fieldController.addEntity(circleEntity);
  92. return circleEntity;
  93. },
  94. /**
  95. * @inheritDoc
  96. */
  97. tick: function () {
  98. this.collisionManager.handleCollisions();
  99. BubbleDots.lib.TWEEN.update();
  100. // var boundaryRule = RealtimeMultiplayerGame.modules.circlecollision.CircleManager.prototype.BOUNDARY_CONSTRAIN_Y;
  101. // var that = this;
  102. // this.fieldController.getPlayers().forEach(function(key, value) {
  103. // this.collisionManager.handleBoundaryForCircle( value.getCollisionCircle(), boundaryRule );
  104. // }, this);
  105. // Note we call superclass's implementation after we're done
  106. BubbleDots.DemoServerGame.superclass.tick.call(this);
  107. },
  108. /**
  109. * @inheritDoc
  110. */
  111. shouldAddPlayer: function (aClientid, data) {
  112. var center = new RealtimeMultiplayerGame.model.Point(BubbleDots.Constants.GAME_WIDTH / 2, BubbleDots.Constants.GAME_HEIGHT / 2);
  113. var playerEntity = this.createEntity(BubbleDots.PlayerEntity, BubbleDots.Constants.ENTITY_DEFAULT_RADIUS, this.getNextEntityID(), aClientid);
  114. playerEntity.position = center.clone();
  115. playerEntity.getCollisionCircle().setPosition(center.clone());
  116. playerEntity.setInput(new RealtimeMultiplayerGame.Input.Keyboard());
  117. playerEntity.setColor("4");
  118. playerEntity.addTraitAndExecute(new BubbleDots.traits.GravityTrait());
  119. // Set the boundary trait and the rule it will use
  120. var boundaryTrait = new BubbleDots.traits.BoundaryTrait(this.collisionManager);
  121. boundaryTrait.setBoundaryRule(RealtimeMultiplayerGame.modules.circlecollision.CircleManager.prototype.BOUNDARY_CONSTRAIN_Y);
  122. playerEntity.addTraitAndExecute(boundaryTrait);
  123. this.fieldController.addPlayer(playerEntity);
  124. },
  125. /**
  126. * @inheritDoc
  127. */
  128. shouldUpdatePlayer: function (aClientid, data) {
  129. var entity = this.fieldController.getEntityWithid(data.payload.entityid);
  130. entity.input.deconstructInputBitmask(data.payload.input);
  131. },
  132. /**
  133. * @inheritDoc
  134. */
  135. shouldRemovePlayer: function (aClientid) {
  136. var entity = this.fieldController.getPlayerWithid(aClientid);
  137. if (entity) {
  138. this.collisionManager.removeCircle(entity.getCollisionCircle());
  139. }
  140. BubbleDots.DemoServerGame.superclass.shouldRemovePlayer.call(this, aClientid);
  141. }
  142. };
  143. // extend RealtimeMultiplayerGame.AbstractServerGame
  144. RealtimeMultiplayerGame.extend(BubbleDots.DemoServerGame, RealtimeMultiplayerGame.AbstractServerGame, null);
  145. })();