PageRenderTime 69ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/js/core/AbstractServerGame.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 95 lines | 41 code | 15 blank | 39 comment | 1 complexity | b3ddb6f3a07da4b958cc3cc77fe373bd 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. AbstractServerGame.js
  4. Created By:
  5. Mario Gonzalez
  6. Project:
  7. RealtimeMultiplayerNodeJS
  8. Abstract:
  9. This class is the base Game controller in RealtimeMultiplayerGame on the server side.
  10. It provides things such as dropping players, and contains a ServerNetChannel
  11. Basic Usage:
  12. [This class is not instantiated! - below is an example of using this class by extending it]
  13. (function(){
  14. MyGameClass = function() {
  15. return this;
  16. }
  17. RealtimeMultiplayerGame.extend(MyGameClass, RealtimeMultiplayerGame.AbstractServerGame, null);
  18. };
  19. */
  20. (function () {
  21. RealtimeMultiplayerGame.AbstractServerGame = function () {
  22. this.intervalFramerate += 6;
  23. RealtimeMultiplayerGame.AbstractServerGame.superclass.constructor.call(this);
  24. return this;
  25. };
  26. RealtimeMultiplayerGame.AbstractServerGame.prototype = {
  27. cmdMap: {}, // Map the CMD constants to functions
  28. nextEntityID: 0, // Incremented for everytime a new object is created
  29. // Methods
  30. setupNetChannel: function () {
  31. this.netChannel = new RealtimeMultiplayerGame.network.ServerNetChannel(this);
  32. },
  33. /**
  34. * Map RealtimeMultiplayerGame.Constants.CMDS to functions
  35. * If ServerNetChannel does not contain a function, it will check to see if it is a special function which the delegate wants to catch
  36. * If it is set, it will call that CMD on its delegate
  37. */
  38. setupCmdMap: function () {
  39. RealtimeMultiplayerGame.AbstractServerGame.superclass.setupCmdMap.call(this);
  40. // this.cmdMap[RealtimeMultiplayerGame.Constants.CMDS.PLAYER_UPDATE] = this.shouldUpdatePlayer;
  41. // These are left in as an example
  42. // this.cmdMap[RealtimeMultiplayerGame.Constants.CMDS.PLAYER_JOINED] = this.onPlayerJoined;
  43. // this.cmdMap[RealtimeMultiplayerGame.Constants.CMDS.PLAYER_DISCONNECT] = this.onPlayerDisconnect;
  44. },
  45. /**
  46. * Updates the gameworld
  47. * Creates a WorldEntityDescription which it sends to NetChannel
  48. */
  49. tick: function () {
  50. RealtimeMultiplayerGame.AbstractServerGame.superclass.tick.call(this);
  51. // Allow all entities to update their position
  52. this.fieldController.getEntities().forEach(function (key, entity) {
  53. entity.updatePosition(this.speedFactor, this.gameClock, this.gameTick);
  54. }, this);
  55. // Create a new world-entity-description,
  56. var worldEntityDescription = new RealtimeMultiplayerGame.model.WorldEntityDescription(this, this.fieldController.getEntities());
  57. this.netChannel.tick(this.gameClock, worldEntityDescription);
  58. if (this.gameClock > this.gameDuration) {
  59. this.shouldEndGame();
  60. }
  61. },
  62. shouldUpdatePlayer: function (client, data) {
  63. console.log("(AbstractServerGame)::onPlayerUpdate");
  64. },
  65. shouldRemovePlayer: function (clientid) {
  66. this.fieldController.removePlayer(clientid);
  67. },
  68. shouldEndGame: function () {
  69. console.log("(AbstractServerGame)::shouldEndGame");
  70. },
  71. ///// Accessors
  72. getNextEntityID: function () {
  73. return ++this.nextEntityID;
  74. }
  75. }
  76. // Extend RealtimeMultiplayerGame.AbstractGame
  77. RealtimeMultiplayerGame.extend(RealtimeMultiplayerGame.AbstractServerGame, RealtimeMultiplayerGame.AbstractGame, null);
  78. })()