PageRenderTime 79ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/js/DemoBox2D/DemoBox2DView.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 145 lines | 68 code | 17 blank | 60 comment | 3 complexity | 5c05df2f7b588b4139b19dc89f98d376 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. DemoBox2DView.js
  4. Created By:
  5. Mario Gonzalez
  6. Project:
  7. RealtimeMultiplayerNodeJS
  8. Abstract:
  9. This class represents the view object in the Box2D Demo of RealtimeMultiplayerNodeJS
  10. It uses the CAAT canvas library to draw the entities
  11. Basic Usage:
  12. [Inside DemoBox2DClientGame]
  13. setupView: function() {
  14. this.view = new DemoBox2D.DemoView();
  15. this.view.insertIntoHTMLElementWithId( "gamecontainer" );
  16. DemoBox2D.DemoClientGame.superclass.setupView.call( this );
  17. }
  18. Version:
  19. 1.0
  20. */
  21. (function () {
  22. DemoBox2D.DemoView = function (aDelegate) {
  23. this.setDelegate(aDelegate);
  24. this.setupCAAT();
  25. this.setupMouseEvents();
  26. this.setupStats();
  27. };
  28. DemoBox2D.DemoView.prototype = {
  29. // Properties
  30. caatDirector: null, // CAAT Director instance
  31. caatScene: null, // CAAT Scene instance
  32. stats: null, // Stats.js instance
  33. delegate: null, // This is the object that will handle some MouseEvents we recieve from CAAT
  34. // Methods
  35. setupCAAT: function () {
  36. // Create a scene, all directors must have at least one scene - this is where all your stuff goes
  37. this.caatScene = new CAAT.Scene();
  38. this.caatScene.create();
  39. this.caatScene.setFillStyle('#000000');
  40. // Create the director instance, and immediately add the scene once it's created
  41. this.caatDirector = new CAAT.Director().initialize(DemoBox2D.Constants.GAME_WIDTH, DemoBox2D.Constants.GAME_HEIGHT);
  42. this.caatDirector.addScene(this.caatScene); //
  43. },
  44. /**
  45. * Setup MouseEvent which we will funnel to the delegate
  46. * @param delegate
  47. */
  48. setupMouseEvents: function (delegate) {
  49. var that = this;
  50. this.caatScene.mouseDown = function (mouseEvent) {
  51. that.delegate.onViewMouseDown(mouseEvent);
  52. };
  53. },
  54. /**
  55. * Updates our current view, passing along the current actual time (via Date().getTime());
  56. * @param {Number} gameClockReal The current actual time, according to the game
  57. */
  58. update: function (gameClockReal) {
  59. var delta = gameClockReal - this.caatDirector.timeline;
  60. this.caatDirector.render(delta);
  61. this.caatDirector.timeline = gameClockReal;
  62. },
  63. /**
  64. * Creates a Stats.js instance and adds it to the page
  65. */
  66. setupStats: function () {
  67. var container = document.createElement('div');
  68. this.stats = new Stats();
  69. this.stats.domElement.style.position = 'absolute';
  70. this.stats.domElement.style.top = '0px';
  71. container.appendChild(this.stats.domElement);
  72. document.body.appendChild(container);
  73. },
  74. /**
  75. * Creats / Adds an entity into canvas via CAAT
  76. * // TODO: Currently the entity is already created - however technically this is the function that should make it!
  77. * @param anEntityView
  78. */
  79. addEntity: function (anEntityView) {
  80. this.caatScene.addChild(anEntityView);
  81. anEntityView.mouseEnabled = false;
  82. },
  83. /**
  84. * Removes an entity via CAAT
  85. * @param anEntityView
  86. */
  87. removeEntity: function (anEntityView) {
  88. this.caatScene.removeChild(anEntityView);
  89. },
  90. /**
  91. * Insert the CAATDirector canvas into an HTMLElement
  92. * @param {String} id An HTMLElement id
  93. */
  94. insertIntoHTMLElementWithId: function (id) {
  95. document.getElementById(id).appendChild(this.caatDirector.canvas);
  96. },
  97. // Memory
  98. dealloc: function () {
  99. this.director.destroy();
  100. },
  101. // Accessors
  102. /**
  103. * Checks that an object contains the required methods and sets it as the delegate for this DemoBox2D.DemoView instance
  104. * @param {DemoBox2D.DemoViewDelegateProtocol} aDelegate A delegate that conforms to DemoBox2D.DemoViewDelegateProtocol
  105. */
  106. setDelegate: function (aDelegate) {
  107. var theInterface = DemoBox2D.DemoViewDelegateProtocol;
  108. for (var member in theInterface) {
  109. if ((typeof aDelegate[member] != typeof theInterface[member])) {
  110. console.log("object failed to implement interface member " + member);
  111. return false;
  112. }
  113. }
  114. // Checks passed
  115. this.delegate = aDelegate;
  116. }
  117. };
  118. // Override
  119. RealtimeMultiplayerGame.extend(DemoBox2D.DemoView, RealtimeMultiplayerGame.AbstractGameView, null);
  120. /**
  121. * This is the object that will handle some MouseEvents we recieve from CAAT
  122. * This is strictly specific to our Box2D demo, and is pretty bare but seems symantically correct following the structure RealtimeMultiplayerNodeJS has in place
  123. */
  124. DemoBox2D.DemoViewDelegateProtocol = {
  125. onViewMouseDown: function (mouseEvent) {
  126. }
  127. };
  128. })();