Game.js 3.3 KB

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