main.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // This script is the main entry point of the game
  2. // Require in the event loop handler
  3. // This is necessary if you want to take advantage of setTimeout/clearTimeout, setInterval/clearInterval, setImmediate/clearImmediate
  4. require('AtomicEventLoop');
  5. // it appears that scene needs to be stored in a global so it's not GC'd
  6. var scene;
  7. function createScene() {
  8. // create a 2D scene
  9. var scene = new Atomic.Scene();
  10. scene.createComponent("Octree");
  11. //craete camera node
  12. var cameraNode = scene.createChild("Camera");
  13. cameraNode.position = [0.0, 0.0, -10.0];
  14. //create camera component, and set it to ortho
  15. var camera = cameraNode.createComponent("Camera");
  16. camera.orthographic = true;
  17. camera.orthoSize = Atomic.graphics.height * Atomic.PIXEL_SIZE;
  18. //create a main viewport
  19. var viewport = new Atomic.Viewport(scene, camera);
  20. Atomic.renderer.setViewport(0, viewport);
  21. return scene;
  22. }
  23. // Set up the scene, create the star, and set some scheduled events via setTimeout and setInterval
  24. function main() {
  25. // create a 2D scene
  26. scene = createScene();
  27. // create the star node.
  28. var starNode = scene.createChild('Star');
  29. var star = starNode.createJSComponent('Components/Star.js');
  30. starNode.position2D = [0, 0];
  31. // reverse direction after 2 seconds
  32. setTimeout(function () {
  33. star.speed = -100;
  34. }, 2000);
  35. // start moving the star after 3 seconds
  36. setTimeout(function () {
  37. var currentX = 0,
  38. currentY = 0;
  39. starNode.position2D = [currentX, currentY];
  40. // every 5ms second move the star a little bit more in a diagonal
  41. // NOTE, you are not going to want to do animations this way,...this is just an example. Doing it this way ends up introducing a stutter
  42. var movementId = setInterval(function () {
  43. currentX += 0.05;
  44. currentY += 0.05;
  45. starNode.position2D = [currentX, currentY];
  46. // stop moving when we get in position
  47. if (currentX > 2.5 || currentY > 2.5) {
  48. clearInterval(movementId);
  49. // handle at the end of this update cycle
  50. setImmediate(function() {
  51. star.speed = 1000;
  52. });
  53. // set up something that we are going to immediately cancel so it doesn't happen
  54. var wonthappen = setImmediate(function() {
  55. star.speed = 100;
  56. });
  57. clearImmediate(wonthappen);
  58. }
  59. }, 5);
  60. }, 3000);
  61. }
  62. main();
  63. // we don't need an update handler here, but if we don't set one up, then main gets GC'd
  64. module.exports.update = function(timeStep) {};