lights.nas 5.5 KB

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