Game.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.input.setMouseVisible(true);
  11. if (Atomic.platform == "Android") {
  12. this.renderer.reuseShadowMaps = false;
  13. this.renderer.shadowQuality = Atomic.SHADOWQUALITY_LOW_16BIT;
  14. }
  15. // root view
  16. this.uiView = new Atomic.UIView();
  17. }
  18. Game.prototype.init = function(start, update) {
  19. this.update = update;
  20. // register global to get at quickly
  21. __js_atomicgame_update = update;
  22. if (typeof(start) === "function")
  23. start();
  24. };
  25. Game.prototype.getSpriteSheet2D = function(xmlFile) {
  26. return this.cache.getResource("SpriteSheet2D", xmlFile);
  27. };
  28. Game.prototype.getSpriteSheet = Game.prototype.getSpriteSheet2D;
  29. Game.prototype.getSound = function(soundFile) {
  30. return this.cache.getResource("Sound", soundFile);
  31. };
  32. Game.prototype.getSprite2D = function(spriteFile) {
  33. return this.cache.getResource("Sprite2D", spriteFile);
  34. };
  35. Game.prototype.showDebugHud = function() {
  36. var uiStyle = this.cache.getResource("XMLFile", "UI/DefaultStyle.xml");
  37. var debugHud = this.engine.createDebugHud();
  38. debugHud.defaultStyle = uiStyle;
  39. debugHud.toggleAll();
  40. };
  41. Game.prototype.createScene2D = function() {
  42. var scene = new Atomic.Scene();
  43. scene.createComponent("Octree");
  44. var cameraNode = scene.createChild("Camera");
  45. cameraNode.position = [0.0, 0.0, -10.0];
  46. var camera = cameraNode.createComponent("Camera");
  47. camera.orthographic = true;
  48. camera.orthoSize = this.graphics.height * Atomic.PIXEL_SIZE;
  49. var viewport = null;
  50. if (Atomic.editor) {
  51. viewport = Atomic.editor.setView(scene, camera);
  52. } else {
  53. viewport = new Atomic.Viewport(scene, camera);
  54. this.renderer.setViewport(0, viewport);
  55. }
  56. this.scene = scene;
  57. this.cameraNode = cameraNode;
  58. this.camera = camera;
  59. this.viewport = viewport;
  60. return scene;
  61. };
  62. Game.prototype.dumpMetrics = function() {
  63. var metrics = Atomic.getVM().metrics;
  64. metrics.capture();
  65. print("--------------");
  66. print("Object Instances:");
  67. print("--------------");
  68. metrics.dump();
  69. print("--------------");
  70. print("Nodes:");
  71. print("--------------");
  72. metrics.dumpNodes();
  73. print("--------------");
  74. print("JS Components:");
  75. print("--------------");
  76. metrics.dumpJSComponents();
  77. };
  78. Game.prototype.createScene3D = function(filename) {
  79. var scene = new Atomic.Scene();
  80. // FIXME: Node should take a string name in constructor
  81. var cameraNode = new Atomic.Node();
  82. cameraNode.name = "Camera";
  83. cameraNode.position = [0.0, 0.0, -10.0];
  84. var camera = cameraNode.createComponent("Camera");
  85. this.cameraNode = cameraNode;
  86. this.camera = camera;
  87. if (typeof(filename) == "string")
  88. scene.loadXML(filename);
  89. else
  90. scene.createComponent("Octree");
  91. scene.addChild(cameraNode);
  92. var viewport = null;
  93. if (Atomic.editor) {
  94. viewport = Atomic.editor.setView(scene, camera);
  95. } else {
  96. viewport = new Atomic.Viewport(scene, camera);
  97. this.renderer.setViewport(0, viewport);
  98. }
  99. this.scene = scene;
  100. this.viewport = viewport;
  101. return scene;
  102. };
  103. Atomic.game = exports.game = new Game();