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