AtomicGame.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. Atomic.editor = null;
  2. function Game() {
  3. this.engine = Atomic.getEngine();
  4. this.cache = Atomic.getResourceCache();
  5. this.renderer = Atomic.getRenderer();
  6. this.graphics = Atomic.getGraphics();
  7. this.input = Atomic.getInput();
  8. this.input.setMouseVisible(true);
  9. if (Atomic.platform == "Android") {
  10. this.renderer.reuseShadowMaps = false;
  11. this.renderer.shadowQuality = Atomic.SHADOWQUALITY_LOW_16BIT;
  12. }
  13. // initialize the UI subsystem
  14. // TODO: this needs to take a config with default font
  15. // skin, etc
  16. Atomic.UI.__init();
  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. return scene;
  63. }
  64. Game.prototype.dumpMetrics = function() {
  65. var metrics = Atomic.getVM().metrics;
  66. metrics.capture();
  67. print("--------------");
  68. print("Object Instances:");
  69. print("--------------");
  70. metrics.dump();
  71. print("--------------");
  72. print("Nodes:");
  73. print("--------------");
  74. metrics.dumpNodes();
  75. print("--------------");
  76. print("JS Components:");
  77. print("--------------");
  78. metrics.dumpJSComponents();
  79. }
  80. Game.prototype.createScene3D = function(filename) {
  81. var scene = new Atomic.Scene();
  82. if (typeof(filename) == "string")
  83. scene.loadXML(filename)
  84. else
  85. scene.createComponent("Octree");
  86. var cameraNode = scene.createChild("Camera");
  87. cameraNode.position = [0.0, 0.0, -10.0];
  88. var camera = cameraNode.createComponent("Camera");
  89. var viewport = null;
  90. if (Atomic.editor) {
  91. viewport = Atomic.editor.setView(scene, camera);
  92. } else {
  93. viewport = new Atomic.Viewport(scene, camera);
  94. this.renderer.setViewport(0, viewport);
  95. }
  96. this.scene = scene;
  97. this.cameraNode = cameraNode;
  98. this.camera = camera;
  99. this.viewport = viewport;
  100. return scene;
  101. }
  102. Atomic.game = exports.game = new Game();