/js/DemoCircles/DemoView.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs · JavaScript · 96 lines · 45 code · 12 blank · 39 comment · 0 complexity · 617f37112ae6131c241e9f4051d7f120 MD5 · raw file

  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. DemoApp.DemoView = function () {
  24. this.setupCAAT();
  25. this.setupStats();
  26. };
  27. DemoApp.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(DemoApp.Constants.GAME_WIDTH, DemoApp.Constants.GAME_HEIGHT); // Create the director instance
  38. this.caatDirector.addScene(this.caatScene); // Immediately add the scene once it's created
  39. // Start the render loop, with at 60FPS
  40. // this.caatDirector.loop(60);
  41. },
  42. /**
  43. * Updates our current view, passing along the current actual time (via Date().getTime());
  44. * @param {Number} gameClockReal The current actual time, according to the game
  45. */
  46. update: function (gameClockReal) {
  47. var delta = gameClockReal - this.caatDirector.timeline;
  48. this.caatDirector.render(delta);
  49. this.caatDirector.timeline = gameClockReal;
  50. },
  51. /**
  52. * Creates a Stats.js instance and adds it to the page
  53. */
  54. setupStats: function () {
  55. var container = document.createElement('div');
  56. this.stats = new Stats();
  57. this.stats.domElement.style.position = 'absolute';
  58. this.stats.domElement.style.top = '0px';
  59. container.appendChild(this.stats.domElement);
  60. document.body.appendChild(container);
  61. },
  62. addEntity: function (anEntityView) {
  63. console.log("Adding Entity To CAAT", anEntityView);
  64. this.caatScene.addChild(anEntityView);
  65. },
  66. removeEntity: function (anEntityView) {
  67. console.log("Removing Entity From CAAT", anEntityView);
  68. this.caatScene.removeChild(anEntityView);
  69. },
  70. /**
  71. * Insert the CAATDirector canvas into an HTMLElement
  72. * @param {String} id An HTMLElement id
  73. */
  74. insertIntoHTMLElementWithId: function (id) {
  75. document.getElementById(id).appendChild(this.caatDirector.canvas);
  76. },
  77. // Memory
  78. dealloc: function () {
  79. this.director.destroy();
  80. }
  81. };
  82. })();