Advertisement
Guest User

jetpack Kurt ground track screen

a guest
Jun 28th, 2022
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.32 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. // @kurtdekker
  7. //
  8. // Show ground track data for the last game, if possible.
  9. //
  10. // NOTE: if there is no LevelSnapshot available, this is detected in the
  11. // FlightDataLog scene and the button won't be visible.
  12.  
  13. public class FlightGroundTrack : MonoBehaviour
  14. {
  15.     const string s_Scene_FlightGroundTrack = "FlightGroundTrack";
  16.  
  17.     public static void Load( string _returnScene = null)
  18.     {
  19.         DSM.Statistics.TimesToGroundTrack.iValue++;
  20.  
  21.         suggest_orientation.LoadScene(
  22.             wantLandscape: true,
  23.             destinationScene: s_Scene_FlightGroundTrack,
  24.             finishAction: null);
  25.  
  26.         if (_returnScene != null)
  27.         {
  28.             FlightDataLog.SharedReturnScene = _returnScene;
  29.         }
  30.     }
  31.  
  32.     void DoDataLog()
  33.     {
  34.         FlightDataLog.Load();
  35.     }
  36.  
  37.     [Header("The actual backdrop LevelSnapshotter")]
  38.     public RawImage MapImage;
  39.  
  40.     [Header("Possible ticker marks.")]
  41.     public Image Tick2;
  42.     public Image Tick4;
  43.     public Image Tick8;
  44.  
  45.     [Header("Full extent of LevelSnapshot map view.")]
  46.     public RectTransform LowerLeft;
  47.     public RectTransform UpperRight;
  48.  
  49.     [Header("Template FlightDataEventIcon object.")]
  50.     public FlightDataEventIcon FlightDataEventIconTemplate;
  51.  
  52.     int index;
  53.  
  54.     // these are snapshots of the Datasacks, which cannot change once we're in here
  55.     Vector3 LevelSnapshotter_Position;
  56.     Vector3 LevelSnapshotter_ArealExtent;
  57.  
  58.     void Start()
  59.     {
  60.         Tick2.gameObject.SetActive(false);
  61.         Tick4.gameObject.SetActive(false);
  62.         Tick8.gameObject.SetActive(false);
  63.         FlightDataEventIconTemplate.gameObject.SetActive( false);
  64.  
  65.         var background = LevelSnapshotter.LoadSnapshot();
  66.  
  67.         if (background)
  68.         {
  69.             MapImage.texture = background;
  70.  
  71.             // copy for performance
  72.             LevelSnapshotter_Position = DSM.LevelSnapshotter.Position.v3Value;
  73.             LevelSnapshotter_ArealExtent = DSM.LevelSnapshotter.ArealExtent.v3Value;
  74.         }
  75.         else
  76.         {
  77.             // complain?
  78.         }
  79.  
  80.         index = 0;
  81.     }
  82.  
  83.     void UpdateRandomTickMarkTest()
  84.     {
  85.         var image = Tick8;
  86.  
  87.         var copy = Instantiate<Image>( image, image.transform.parent);
  88.  
  89.         copy.color = Random.ColorHSV();
  90.  
  91.         copy.transform.position = new Vector3(
  92.             Random.Range( LowerLeft.position.x, UpperRight.position.x),
  93.             Random.Range( LowerLeft.position.y, UpperRight.position.y),
  94.             0);
  95.  
  96.         copy.gameObject.SetActive( true);
  97.     }
  98.  
  99.     const float MinDistance = 2;
  100.  
  101.     Vector3 LastUIPosition;
  102.  
  103.     void CreateFlightDataEventIcon( Vector3 UIPosition, FlightDataEventFlags flags)
  104.     {
  105.         var icon = Instantiate<FlightDataEventIcon>( FlightDataEventIconTemplate, FlightDataEventIconTemplate.transform.parent);
  106.  
  107.         icon.transform.position = UIPosition;
  108.         icon.gameObject.SetActive( true);
  109.  
  110.         icon.InjectFlightDataEventFlags(flags);
  111.     }
  112.  
  113.     void UpdateRenderNextPoint()
  114.     {
  115.         while( true)
  116.         {
  117.             if (index >= FlightDataRecorder.Instance.NumPoints())
  118.             {
  119.                 return;
  120.             }
  121.  
  122.             var pt = FlightDataRecorder.Instance.GetDatapoint( index);
  123.             index++;
  124.  
  125.             var pos = pt.position;
  126.  
  127.             pos -= LevelSnapshotter_Position;
  128.  
  129.             // fraction of areal extent
  130.             float fx = Mathf.InverseLerp(
  131.                 -LevelSnapshotter_ArealExtent.x,
  132.                 LevelSnapshotter_ArealExtent.x,
  133.                 pos.x);
  134.             float fz = Mathf.InverseLerp(
  135.                 -LevelSnapshotter_ArealExtent.z,
  136.                 LevelSnapshotter_ArealExtent.z,
  137.                 pos.z);
  138.  
  139.             // now into UI space
  140.             float px = Mathf.Lerp( LowerLeft.position.x, UpperRight.position.x, fx);
  141.             float py = Mathf.Lerp( LowerLeft.position.y, UpperRight.position.y, fz);
  142.  
  143.             Vector3 UIPosition = new Vector3( px, py, 0);
  144.  
  145.             if (pt.flags != 0)
  146.             {
  147.                 CreateFlightDataEventIcon( UIPosition, pt.flags);
  148.             }
  149.  
  150.             if (Vector3.Distance( UIPosition, LastUIPosition) > MinDistance)
  151.             {
  152.                 LastUIPosition = UIPosition;
  153.  
  154.                 var image = Tick2;
  155.  
  156.                 var copy = Instantiate<Image>( image, image.transform.parent);
  157.  
  158.                 copy.color = new Color( 0.0f, 1.0f, 0.0f);
  159.  
  160.                 copy.transform.position = UIPosition;
  161.                 copy.gameObject.SetActive( true);
  162.  
  163.                 return;
  164.             }
  165.         }
  166.     }
  167.  
  168.     void Update()
  169.     {
  170.         UpdateRenderNextPoint();
  171.     }
  172.  
  173.     void OnUserInput( Datasack ds)
  174.     {
  175.         switch(ds.Value)
  176.         {
  177.         case "ButtonBack":
  178.             FlightDataLog.DoEscape();
  179.             break;
  180.         case "ButtonDataLog":
  181.             DoDataLog();
  182.             break;
  183.         }
  184.     }
  185.  
  186.     void OnEnable()
  187.     {
  188.         DSM.UserIntent.OnChanged += OnUserInput;
  189.     }
  190.  
  191.     void OnDisable()
  192.     {
  193.         DSM.UserIntent.OnChanged -= OnUserInput;
  194.     }
  195. }
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement