Game.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* global __js_atomicgame_update */
  2. Atomic.editor = null;
  3. function Game() {
  4. this.engine = Atomic.getEngine();
  5. this.cache = Atomic.getResourceCache();
  6. this.renderer = Atomic.getRenderer();
  7. this.graphics = Atomic.getGraphics();
  8. this.input = Atomic.getInput();
  9. this.jsid = -1; // joystick, rumble start off
  10. this.xsim = 0.0;
  11. this.ysim = 0.0;
  12. this.input.setMouseVisible(true);
  13. if (Atomic.platform == "Android") {
  14. this.renderer.reuseShadowMaps = false;
  15. this.renderer.shadowQuality = Atomic.SHADOWQUALITY_LOW_16BIT;
  16. }
  17. // root view
  18. this.uiView = new Atomic.UIView();
  19. }
  20. Game.prototype.init = function(start, update) {
  21. this.update = update;
  22. // register global to get at quickly
  23. __js_atomicgame_update = update;
  24. if (typeof(start) === "function")
  25. start();
  26. };
  27. Game.prototype.getSpriteSheet2D = function(xmlFile) {
  28. return this.cache.getResource("SpriteSheet2D", xmlFile);
  29. };
  30. Game.prototype.getSpriteSheet = Game.prototype.getSpriteSheet2D;
  31. Game.prototype.getSound = function(soundFile) {
  32. return this.cache.getResource("Sound", soundFile);
  33. };
  34. Game.prototype.getSprite2D = function(spriteFile) {
  35. return this.cache.getResource("Sprite2D", spriteFile);
  36. };
  37. Game.prototype.showDebugHud = function() {
  38. var uiStyle = this.cache.getResource("XMLFile", "UI/DefaultStyle.xml");
  39. var debugHud = this.engine.createDebugHud();
  40. debugHud.defaultStyle = uiStyle;
  41. debugHud.toggleAll();
  42. };
  43. Game.prototype.createScene2D = function() {
  44. var scene = new Atomic.Scene();
  45. scene.createComponent("Octree");
  46. var cameraNode = scene.createChild("Camera");
  47. cameraNode.position = [0.0, 0.0, -10.0];
  48. var camera = cameraNode.createComponent("Camera");
  49. camera.orthographic = true;
  50. camera.orthoSize = this.graphics.height * Atomic.PIXEL_SIZE;
  51. var viewport = null;
  52. if (Atomic.editor) {
  53. viewport = Atomic.editor.setView(scene, camera);
  54. } else {
  55. viewport = new Atomic.Viewport(scene, camera);
  56. this.renderer.setViewport(0, viewport);
  57. }
  58. this.scene = scene;
  59. this.cameraNode = cameraNode;
  60. this.camera = camera;
  61. this.viewport = viewport;
  62. if ( Atomic.input.numJoysticks > 0 ) {
  63. this.jsid = 0
  64. scene.subscribeToEvent("JoystickConnected", function(ev) {
  65. this.jsid = ev.JoystickID; // get the joystick id for future calls.
  66. });
  67. // have the joystick drive the UI
  68. scene.subscribeToEvent("JoystickButtonDown", function(ev) {
  69. if ( ev.Button == Atomic.CONTROLLER_BUTTON_X
  70. || ev.Button == Atomic.CONTROLLER_BUTTON_LEFTSTICK )
  71. {
  72. Atomic.input.joystickSimulateMouseButton(1); // mouse button 1 press
  73. }
  74. });
  75. scene.subscribeToEvent("JoystickAxisMove", function(ev) {
  76. var joyLookDeadZone = 0.05;
  77. var joyMoveDistance = 1.7;
  78. if ( ev.Button < 2 )
  79. {
  80. var look1 = 0.0
  81. var look2 = 0.0;
  82. if ( ev.Button == 0) look1 = ev.Position;
  83. if ( ev.Button == 1) look2 = ev.position;
  84. if (look1 < -joyLookDeadZone || look1 > joyLookDeadZone) // has a value other than 0
  85. this.xsim += joyMoveDistance * look1;
  86. else this.xsim = 0;
  87. if (look2 < -joyLookDeadZone || look2 > joyLookDeadZone)
  88. this.ysim += joyMoveDistance * look2;
  89. else this.ysim = 0;
  90. }
  91. });
  92. }
  93. scene.subscribeToEvent("SceneUpdate", function(ev) {
  94. if ( Atomic.input.numJoysticks > 0 && Atomic.input.isMouseVisible()) {
  95. if ( this.xsim == undefined || this.ysim == undefined ) return;
  96. if ( this.xsim != 0.0 || this.ysim != 0.0 )
  97. {
  98. var nowx = Atomic.input.mousePosition; // ui.cursor.screenPosition; // readonysim
  99. Atomic.input.joystickSimulateMouseMove( nowx[0] + this.xsim, nowx[1] + this.ysim);
  100. }
  101. }
  102. });
  103. return scene;
  104. };
  105. Game.prototype.dumpMetrics = function() {
  106. var metrics = Atomic.getVM().metrics;
  107. metrics.capture();
  108. print("--------------");
  109. print("Object Instances:");
  110. print("--------------");
  111. metrics.dump();
  112. print("--------------");
  113. print("Nodes:");
  114. print("--------------");
  115. metrics.dumpNodes();
  116. print("--------------");
  117. print("JS Components:");
  118. print("--------------");
  119. metrics.dumpJSComponents();
  120. };
  121. Game.prototype.createScene3D = function(filename) {
  122. var scene = new Atomic.Scene();
  123. // FIXME: Node should take a string name in constructor
  124. var cameraNode = new Atomic.Node();
  125. cameraNode.name = "Camera";
  126. cameraNode.position = [0.0, 0.0, -10.0];
  127. var camera = cameraNode.createComponent("Camera");
  128. this.cameraNode = cameraNode;
  129. this.camera = camera;
  130. if (typeof(filename) == "string")
  131. scene.loadXML(filename);
  132. else
  133. scene.createComponent("Octree");
  134. scene.addChild(cameraNode);
  135. var viewport = null;
  136. if (Atomic.editor) {
  137. viewport = Atomic.editor.setView(scene, camera);
  138. } else {
  139. viewport = new Atomic.Viewport(scene, camera);
  140. this.renderer.setViewport(0, viewport);
  141. }
  142. this.scene = scene;
  143. this.viewport = viewport;
  144. return scene;
  145. };
  146. Atomic.game = exports.game = new Game();