Light.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. exports.component = function(self) {
  5. var node = self.node;
  6. var light = node.getComponent("PointLight2D");
  7. light.color = [.1 + Math.random() * .9, .1 + Math.random() * .9, .1 + Math.random() * .9, 1];
  8. var x = -halfWidth + (halfWidth * 2) * Math.random();
  9. var y = -halfHeight + (halfHeight * 2) * Math.random();
  10. node.position2D = [x, y];
  11. var movex = -2 + (Math.random() * 4);
  12. var movey = -2 + (Math.random() * 4);
  13. // Update
  14. self.update = function(timeStep) {
  15. var prev = node.position2D;
  16. node.translate2D([movex * timeStep, movey * timeStep]);
  17. var p = node.position2D;
  18. if (p[0] < -halfWidth || p[0] > halfWidth) {
  19. node.position2D = prev;
  20. movex = -movex;
  21. }
  22. if (p[1] < -halfHeight || p[1] > halfHeight) {
  23. node.position2D = prev;
  24. movey = -movey;
  25. }
  26. }
  27. }