Lights.nas 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #Universal light systems by Tomaskom
  2. var lightsPath = "lightpack/"; #path to the property node, where all internal values are placed
  3. #list of switches for lights - if you don't intend to use some light, assign it nil value instead, like whateverSwitch = nil; and you don't need to care about anything else
  4. var navSwitch = "/controls/switches/nav-lights-switch";
  5. var beaconSwitch = "/controls/switches/beacon-switch";
  6. var strobeSwitch = "/controls/switches/strobe-switch";
  7. var landingSwitch = "/controls/switches/landing-lights-switch";
  8. var searchSwitch = "/controls/switches/search-lights-switch";
  9. var taxiSwitch = nil;
  10. var probeSwitch = nil;
  11. var whiteSwitch = "/controls/switches/nav-lights-switch";
  12. #switch this from 1 to 0 if you want to use advanced cyclical fading animation of the the nav lights instead of being stable on when the switch is on
  13. navStillOn = 1;
  14. #switch this from 0 to 1 if you want to bind the landing and taxi lights to the landing gear
  15. gearBind = 0;
  16. #### NAV LIGHTS ####
  17. #class for a periodic fade in/out animation - for flashing, use rather standard aircraft.light.new(), as in Beacon and Strobe section
  18. var lightCycle = {
  19. #constructor
  20. new: func(propSwitch, propOut) {
  21. m = { parents: [lightCycle] };
  22. props.globals.initNode(propOut, 0, "DOUBLE");
  23. props.globals.initNode(propSwitch, 1, "BOOL");
  24. m.fadeIn = 0.4; #fade in time
  25. m.fadeOut = 0.4; #fade out time
  26. m.stayOn = 1.5; #stable on period
  27. m.stayOff = 1; #stable off period
  28. m.turnOff = 0.12; #fade out time when turned off
  29. m.phase = 0; #phase to be run on next timer call: 0 -> fade in, 1 -> stay on, 2 -> fade out, 3 -> stay off
  30. m.cycleTimer = maketimer(0.1, func {
  31. if(getprop(propSwitch)) {
  32. if(m.phase == 0) {
  33. interpolate(propOut, 1, m.fadeIn);
  34. m.phase = 1;
  35. m.cycleTimer.restart(m.fadeIn);
  36. }
  37. else if(m.phase == 1){
  38. m.phase = 2;
  39. m.cycleTimer.restart(m.stayOn);
  40. }
  41. else if(m.phase == 2){
  42. interpolate(propOut, 0, m.fadeOut);
  43. m.phase = 3;
  44. m.cycleTimer.restart(m.fadeOut);
  45. }
  46. else if(m.phase == 3){
  47. m.phase = 0;
  48. m.cycleTimer.restart(m.stayOff);
  49. }
  50. }
  51. else {
  52. interpolate(propOut, 0, m.turnOff); #kills any currently ongoing interpolation
  53. m.phase = 0;
  54. }
  55. });
  56. m.cycleTimer.singleShot = 1;
  57. if(propSwitch==nil) {
  58. m.listen = nil;
  59. return m;
  60. }
  61. m.listen = setlistener(propSwitch, func{m.cycleTimer.restart(0);}); #handle switch changes
  62. m.cycleTimer.restart(0); #start the looping
  63. return m;
  64. },
  65. #destructor
  66. del: func {
  67. if(me.listen!=nil) removelistener(me.listen);
  68. me.cycleTimer.stop();
  69. },
  70. };
  71. #By default, the switch property is initialized to 1 (only if no value is already assigned). Don't change the class implementation! To override this, set the property manually. You don't need to care if any other code already does it for you.
  72. var navLights = nil;
  73. if(!navStillOn) {
  74. navLights = lightCycle.new(navSwitch, lightsPath~"nav-lights-intensity");
  75. ### Uncomment and tune those to customize times ###
  76. #navLights.fadeIn = 0.4; #fade in time
  77. #navLights.fadeOut = 0.4; #fade out time
  78. #navLights.stayOn = 3; #stable on period
  79. #navLights.stayOff = 0.6; #stable off period
  80. #navLights.turnOff = 0.12; #fade out time when turned off
  81. }
  82. ### BEACON ###
  83. var beacon = nil;
  84. if(beaconSwitch!=nil) {
  85. props.globals.initNode(beaconSwitch, 1, "BOOL");
  86. beacon = aircraft.light.new(lightsPath~"beacon-state",
  87. [0.0, 1.0], beaconSwitch);
  88. }
  89. ### STROBE ###
  90. var strobe = nil;
  91. if(strobeSwitch!=nil) {
  92. props.globals.initNode(strobeSwitch, 1, "BOOL");
  93. strobe = aircraft.light.new(lightsPath~"strobe-state",
  94. [0.0, 0.87], strobeSwitch);
  95. }
  96. ### LIGHT FADING ###
  97. #class for controlling fade in/out behavior - propIn is a control property (handled as a boolean) and propOut is interpolated
  98. #all light brightness animations in xmls depend on propOut (Rembrandt brightness, material emission, flares transparency, ...)
  99. var lightFadeInOut = {
  100. #constructor
  101. new: func(propSwitch, propOut) {
  102. m = { parents: [lightFadeInOut] };
  103. m.fadeIn = 0.3; #some sane defaults
  104. m.fadeOut = 0.4;
  105. if(propSwitch==nil) {
  106. m.listen = nil;
  107. return m;
  108. }
  109. props.globals.initNode(propSwitch, 1, "BOOL");
  110. m.isOn = getprop(propSwitch);
  111. props.globals.initNode(propOut, m.isOn, "DOUBLE");
  112. m.listen = setlistener(propSwitch,
  113. func {
  114. if(m.isOn and !getprop(propSwitch)) {
  115. interpolate(propOut, 0, m.fadeOut);
  116. m.isOn = 0;
  117. }
  118. if(!m.isOn and getprop(propSwitch)) {
  119. interpolate(propOut, 1, m.fadeIn);
  120. m.isOn = 1;
  121. }
  122. }
  123. );
  124. return m;
  125. },
  126. #destructor
  127. del: func {
  128. if(me.listen!=nil) removelistener(me.listen);
  129. },
  130. };
  131. fadeLanding = lightFadeInOut.new(landingSwitch, lightsPath~"landing-lights-intensity");
  132. fadeSearch = lightFadeInOut.new(searchSwitch, lightsPath~"search-lights-intensity");
  133. fadeTaxi = lightFadeInOut.new(taxiSwitch, lightsPath~"taxi-light-intensity");
  134. fadeProbe = lightFadeInOut.new(probeSwitch, lightsPath~"probe-light-intensity");
  135. fadeWhite = lightFadeInOut.new(whiteSwitch, lightsPath~"white-light-intensity");
  136. if(navStillOn) {
  137. navLights = lightFadeInOut.new(navSwitch, lightsPath~"nav-lights-intensity");
  138. navLights.fadeIn = 0.1;
  139. navLights.fadeOut = 0.12;
  140. }
  141. #manipulate times if defaults don't fit your needs:
  142. #fadeLanding.fadeIn = 0.5;
  143. #fadeLanding.fadeOut = 0.8;
  144. #enable binding to gear
  145. if(gearBind) {
  146. setlistener("/controls/gear/gear-down", func {
  147. gearDown = getprop("/controls/gear/gear-down");
  148. if(gearDown) {
  149. if(landingSwitch!=nil) setprop(landingSwitch, 1);
  150. if(taxiSwitch!=nil) setprop(taxiSwitch, 1);
  151. }
  152. else{
  153. if(landingSwitch!=nil) setprop(landingSwitch, 0);
  154. if(taxiSwitch!=nil) setprop(taxiSwitch, 0);
  155. }
  156. });
  157. }
  158. print("Lightpack light system initialized");