Advertisement
Whiplash141

Whip's Speed Based Lights v1

Dec 12th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. // Whip's Speed Based Lights v1 - 12/12/17
  2.  
  3. const string lightGroupName = "Thruster Lights"; //change this to whatever
  4. const double maxSpeed = 100;
  5. const float maxLightRadius = 160f;
  6. const float maxLightIntensity = 5f;
  7.  
  8. // No touchey
  9. List<IMyLightingBlock> lightList;
  10. const double oneOverMaxSpeed = 1.0 / maxSpeed;
  11.  
  12. Program()
  13. {
  14.     Runtime.UpdateFrequency = UpdateFrequency.Update10;
  15.     lightList = new List<IMyLightingBlock>();
  16. }
  17.  
  18. void Main(string argument, UpdateType updateType)
  19. {
  20.     if ((updateType & UpdateType.Update10) == 0)
  21.         return;
  22.  
  23.     Echo("WMI Speed Lights Online");
  24.     var lightGroup = GridTerminalSystem.GetBlockGroupWithName(lightGroupName);
  25.     if (lightGroup == null)
  26.     {
  27.         Echo($"> Error: No group named '{lightGroupName}' was found");
  28.         return;
  29.     }
  30.  
  31.     lightGroup.GetBlocksOfType(lightList);
  32.     if (lightList.Count == 0)
  33.     {
  34.         Echo($"> Error: No lights found in light group");
  35.         return;
  36.     }
  37.  
  38.     var controller = GetFirstBlockOfType<IMyShipController>();
  39.     if (controller == null)
  40.     {
  41.         Echo("> Error: No ship controller found");
  42.         return;
  43.     }
  44.  
  45.     var velocity = controller.GetShipVelocities().LinearVelocity;
  46.  
  47.     foreach(var block in lightList)
  48.     {
  49.         var lightDirection = block.WorldMatrix.Backward; //backward because we want the direction the light would "thrust" if it were a thruster
  50.         var forwardVelocity = velocity.Dot(lightDirection);
  51.         if (forwardVelocity < 1)
  52.         {
  53.             block.Enabled = false;
  54.             continue;
  55.         }
  56.         else
  57.             block.Enabled = true;
  58.  
  59.         var forwardVelocityProportion = forwardVelocity * oneOverMaxSpeed; //scale to get a proportion
  60.         block.Radius = maxLightRadius * (float)forwardVelocityProportion;
  61.         block.Intensity = maxLightIntensity * (float)forwardVelocityProportion;
  62.     }
  63.  
  64. }
  65.  
  66. T GetFirstBlockOfType<T>() where T : class, IMyTerminalBlock
  67. {
  68.     var blocks = new List<T>();
  69.     GridTerminalSystem.GetBlocksOfType(blocks);
  70.  
  71.     return blocks.Count > 0 ? blocks[0] : null;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement