LightFlicker.js 816 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // a flickering light component
  2. var node = self.node;
  3. self.light = node.getComponent("PointLight2D");
  4. var baseRange = self.light.radius;
  5. var targetValue = baseRange;
  6. flicker = "mmmaaaammmaaaabcdefgabcdefg";
  7. var index = Math.random() * (flicker.length - 1);
  8. // make sure first update catches
  9. time = 100;
  10. self.light.radius = baseRange * flicker.charCodeAt(index)/255;
  11. function start() {
  12. }
  13. function update(timestep) {
  14. time += timestep;
  15. if (time > .025)
  16. {
  17. index++;
  18. time = 0.0;
  19. if (index >= flicker.length)
  20. index = 0;
  21. targetValue = baseRange * (flicker.charCodeAt(index)/255);
  22. }
  23. if (self.light.radius< targetValue)
  24. self.light.radius += timestep * 2;
  25. if (self.light.radius > targetValue)
  26. self.light.radius -= timestep * 2;
  27. self.light.softShadowLength = self.light.radius;
  28. }