LightFlicker.js 813 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. "atomic component";
  2. // a flickering light component
  3. exports.component = function(self){
  4. var node = self.node;
  5. self.light = node.getComponent("Light");
  6. var baseRange = 45;
  7. var targetValue = baseRange;
  8. //define a flicker pattern
  9. var flicker = "mmmaaaammmaaaabcdefgabcdefg";
  10. var index = Math.random() * (flicker.length - 1);
  11. // make sure first update catches
  12. var time = 100;
  13. self.update = function(timestep) {
  14. time += timestep;
  15. if (time > .05)
  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.range < targetValue)
  24. self.light.range += timestep * 10;
  25. if (self.light.range > targetValue)
  26. self.light.range -= timestep * 10;
  27. };
  28. };