PageRenderTime 36ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/js/DemoHelloWorld/DemoView.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 103 lines | 45 code | 11 blank | 47 comment | 0 complexity | ec7146d08c9ec56bda1c4e1949221163 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. Version:
  20. 1.0
  21. */
  22. (function () {
  23. DemoHelloWorld.DemoView = function () {
  24. this.setupCAAT();
  25. this.setupStats();
  26. };
  27. DemoHelloWorld.DemoView.prototype = {
  28. // Properties
  29. caatDirector: null, // CAAT Director instance
  30. caatScene: null, // CAAT Scene instance
  31. stats: null, // Stats.js instance
  32. // Methods
  33. setupCAAT: function () {
  34. this.caatScene = new CAAT.Scene(); // Create a scene, all directors must have at least one scene - this is where all your stuff goes
  35. this.caatScene.create(); // Notice we call create when creating this, and ShapeActor below. Both are Actors
  36. this.caatScene.setFillStyle('#000000');
  37. this.caatDirector = new CAAT.Director().initialize(DemoHelloWorld.Constants.GAME_WIDTH, DemoHelloWorld.Constants.GAME_HEIGHT); // Create the director instance
  38. this.caatDirector.addScene(this.caatScene); // Immediately add the scene once it's created
  39. },
  40. /**
  41. * Updates our current view, passing along the current actual time (via Date().getTime());
  42. * @param {Number} gameClockReal The current actual time, according to the game
  43. */
  44. update: function (gameClockReal) {
  45. var delta = gameClockReal - this.caatDirector.timeline;
  46. this.caatDirector.render(delta);
  47. this.caatDirector.timeline = gameClockReal;
  48. },
  49. /**
  50. * Creates a Stats.js instance and adds it to the page
  51. */
  52. setupStats: function () {
  53. var container = document.createElement('div');
  54. this.stats = new Stats();
  55. this.stats.domElement.style.position = 'absolute';
  56. this.stats.domElement.style.top = '0px';
  57. container.appendChild(this.stats.domElement);
  58. document.body.appendChild(container);
  59. },
  60. /**
  61. * Add an entity from the view - For now this method definitely has some symantic issues because it's being passed a concrete view instead of figuring out what the view is
  62. * @param anEntityView
  63. */
  64. addEntity: function (anEntityView) {
  65. console.log("Adding Entity To CAAT", anEntityView);
  66. this.caatScene.addChild(anEntityView);
  67. },
  68. /**
  69. * Remove an entity from the view - For now this method definitely has some symantic issues because it's being passed a concrete view instead of figuring out what the view is
  70. * @param anEntityView
  71. */
  72. removeEntity: function (anEntityView) {
  73. console.log("Removing Entity From CAAT", anEntityView);
  74. this.caatScene.removeChild(anEntityView);
  75. },
  76. /**
  77. * Insert the CAATDirector canvas into an HTMLElement
  78. * @param {String} id An HTMLElement id
  79. */
  80. insertIntoHTMLElementWithId: function (id) {
  81. document.getElementById(id).appendChild(this.caatDirector.canvas);
  82. },
  83. /**
  84. * @inheritDoc
  85. */
  86. dealloc: function () {
  87. this.director.destroy();
  88. }
  89. };
  90. })();