LightFlicker.js 790 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // a flickering light component
  2. 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 update(timestep) {
  12. time += timestep;
  13. if (time > .025)
  14. {
  15. index++;
  16. time = 0.0;
  17. if (index >= flicker.length)
  18. index = 0;
  19. targetValue = baseRange * (flicker.charCodeAt(index)/255);
  20. }
  21. if (self.light.radius< targetValue)
  22. self.light.radius += timestep * 2;
  23. if (self.light.radius > targetValue)
  24. self.light.radius -= timestep * 2;
  25. self.light.softShadowLength = self.light.radius;
  26. }