Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using Sandbox.ModAPI;
  4. using Sisk.Utils.Profiler;
  5. using VRage.Game.ObjectBuilders;
  6. using VRageMath;
  7.  
  8. // ReSharper disable UsePatternMatching
  9. // ReSharper disable InlineOutVariableDeclaration
  10.  
  11. namespace AutoMcD.SmartRotors {
  12.     /// <summary>
  13.     ///     Track the sun direction.
  14.     /// </summary>
  15.     public class SunTracker {
  16.         private readonly Vector3 _baseSunDirection;
  17.         private readonly bool _enabled;
  18.         private readonly DateTime _offset = new DateTime(2081, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  19.         private readonly Vector3 _sunRotationAxis;
  20.  
  21.         /// <summary>
  22.         ///     Creates a new instance of <see cref="SunTracker" />.
  23.         /// </summary>
  24.         public SunTracker() {
  25.             var environment = MyAPIGateway.Session.GetSector().Environment;
  26.             var checkpoint = MyAPIGateway.Session.GetCheckpoint("null");
  27.  
  28.             Speed = 60f * MyAPIGateway.Session.SessionSettings.SunRotationIntervalMinutes;
  29.             _enabled = MyAPIGateway.Session.SessionSettings.EnableSunRotation;
  30.  
  31.             Vector3 sunDirectionNormalized;
  32.             Vector3.CreateFromAzimuthAndElevation(environment.SunAzimuth, environment.SunElevation, out sunDirectionNormalized);
  33.  
  34.             var weatherComponent = checkpoint.SessionComponents.OfType<MyObjectBuilder_SectorWeatherComponent>().FirstOrDefault();
  35.             if (weatherComponent != null && !weatherComponent.BaseSunDirection.IsZero) {
  36.                 _baseSunDirection = weatherComponent.BaseSunDirection;
  37.             }
  38.  
  39.             var cross = Vector3.Cross(Math.Abs(Vector3.Dot(sunDirectionNormalized, Vector3.Up)) > 0.95f ? Vector3.Cross(sunDirectionNormalized, Vector3.Left) : Vector3.Cross(sunDirectionNormalized, Vector3.Up), sunDirectionNormalized);
  40.             cross.Normalize();
  41.             _sunRotationAxis = cross;
  42.         }
  43.  
  44.         /// <summary>
  45.         ///     The elapsed game time.
  46.         /// </summary>
  47.         private TimeSpan ElapsedGameTime => MyAPIGateway.Session.GameDateTime - _offset;
  48.  
  49.         /// <summary>
  50.         ///     The speed of sun rotation.
  51.         /// </summary>
  52.         public float Speed { get; }
  53.  
  54.         /// <summary>
  55.         ///     Calculates the current sun direction.
  56.         /// </summary>
  57.         /// <returns>Returns the sun direction vector.</returns>
  58.         public Vector3D CalculateSunDirection() {
  59.             using (Mod.PROFILE ? Profiler.Measure(nameof(SunTracker), nameof(CalculateSunDirection)) : null) {
  60.                 if (_enabled) {
  61.                     var vector3 = Vector3D.Transform(_baseSunDirection, MatrixD.CreateFromAxisAngle(_sunRotationAxis, 6.283186f * (ElapsedGameTime.TotalSeconds / Speed)));
  62.                     vector3.Normalize();
  63.  
  64.                     return vector3;
  65.                 }
  66.  
  67.                 return _baseSunDirection;
  68.             }
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement