LightFlicker.js 648 B

123456789101112131415161718192021222324252627282930313233343536
  1. // a flickering light component
  2. var node = self.node;
  3. self.light = node.getComponent("Light");
  4. var baseRange = 45;
  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. function update(timestep) {
  11. time += timestep;
  12. if (time > .05)
  13. {
  14. index++;
  15. time = 0.0;
  16. if (index >= flicker.length)
  17. index = 0;
  18. targetValue = baseRange * (flicker.charCodeAt(index)/255);
  19. }
  20. if (self.light.range < targetValue)
  21. self.light.range += timestep * 10;
  22. if (self.light.range > targetValue)
  23. self.light.range -= timestep * 10;
  24. }