main.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. var cameraNode = scene.createChild("Camera");
  12. cameraNode.position = [0.0, 0.0, -10.0];
  13. var camera = cameraNode.createComponent("Camera");
  14. camera.orthographic = true;
  15. camera.orthoSize = Atomic.graphics.height * Atomic.PIXEL_SIZE;
  16. var viewport = null;
  17. viewport = new Atomic.Viewport(scene, camera);
  18. Atomic.renderer.setViewport(0, viewport);
  19. return scene;
  20. }
  21. // Set up the scene, create the star, and set some scheduled events via setTimeout and setInterval
  22. function main() {
  23. // create a 2D scene
  24. scene = createScene();
  25. // create the star node.
  26. var starNode = scene.createChild('Star');
  27. var star = starNode.createJSComponent('Components/Star.js');
  28. starNode.position2D = [0, 0];
  29. // reverse direction after 2 seconds
  30. setTimeout(function () {
  31. star.speed = -100;
  32. }, 2000);
  33. // start moving the star after 3 seconds
  34. setTimeout(function () {
  35. var currentX = 0,
  36. currentY = 0;
  37. starNode.position2D = [currentX, currentY];
  38. // every 5ms second move the star a little bit more in a diagonal
  39. // 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
  40. var movementId = setInterval(function () {
  41. currentX += 0.05;
  42. currentY += 0.05;
  43. starNode.position2D = [currentX, currentY];
  44. // stop moving when we get in position
  45. if (currentX > 2.5 || currentY > 2.5) {
  46. clearInterval(movementId);
  47. // handle at the end of this update cycle
  48. setImmediate(function() {
  49. star.speed = 1000;
  50. });
  51. // set up something that we are going to immediately cancel so it doesn't happen
  52. var wonthappen = setImmediate(function() {
  53. star.speed = 100;
  54. });
  55. clearImmediate(wonthappen);
  56. }
  57. }, 5);
  58. }, 3000);
  59. }
  60. main();
  61. // we don't need an update handler here, but if we don't set one up, then main gets GC'd
  62. module.exports.update = function(timeStep) {};