Light.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'atomic component';
  2. var halfWidth = Atomic.graphics.width * Atomic.PIXEL_SIZE * 0.5;
  3. var halfHeight = Atomic.graphics.height * Atomic.PIXEL_SIZE * 0.5;
  4. //Light component
  5. exports.component = function(self) {
  6. //Link to the current node
  7. var node = self.node;
  8. //Get PointLight2D component and set its color
  9. var light = node.getComponent("PointLight2D");
  10. light.color = [.1 + Math.random() * .9, .1 + Math.random() * .9, .1 + Math.random() * .9, 1];
  11. var x = -halfWidth + (halfWidth * 2) * Math.random();
  12. var y = -halfHeight + (halfHeight * 2) * Math.random();
  13. //Set position of the current node in the 2D space
  14. node.position2D = [x, y];
  15. var movex = -2 + (Math.random() * 4);
  16. var movey = -2 + (Math.random() * 4);
  17. // Update function calls one per each frame
  18. self.update = function(timeStep) {
  19. var prev = node.position2D;
  20. //translate node in 2D space on X and Y values
  21. node.translate2D([movex * timeStep, movey * timeStep]);
  22. var p = node.position2D;
  23. if (p[0] < -halfWidth || p[0] > halfWidth) {
  24. node.position2D = prev;
  25. movex = -movex;
  26. }
  27. if (p[1] < -halfHeight || p[1] > halfHeight) {
  28. node.position2D = prev;
  29. movey = -movey;
  30. }
  31. };
  32. };