Advertisement
Guest User

ServiceProviderWrapper.cs

a guest
Apr 29th, 2015
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using UnityEngine;
  7.  
  8. namespace Assets
  9. {
  10.    /// <summary>
  11.    /// A wrapper to the serivice provider which provides some basic functionality in the absence of the SimplePlanes ServiceProvider
  12.    /// (to be used in the mod's Unity project while testing).
  13.    /// </summary>
  14.    public static class ServiceProviderWrapper
  15.    {
  16.       #region Static Fields
  17.  
  18.       /// <summary>
  19.       /// The mock cockpit transform to use when the cockpit isn't available (when testing in the mod's Unity scene).
  20.       /// </summary>
  21.       public static Transform MockCockpit;
  22.  
  23.       #endregion Static Fields
  24.  
  25.       #region Constructors
  26.  
  27.       /// <summary>
  28.       /// Initializes the <see cref="ServiceProviderWrapper"/> class.
  29.       /// </summary>
  30.       static ServiceProviderWrapper()
  31.       {
  32.          try
  33.          {
  34.             // See if the service provider is available...set it to enabled if the next line doesn't throw an exception.
  35.             var test = ServiceProvider.Instance.UserInterface.UIRootObject;
  36.  
  37.             ServiceProviderWrapper.IsServiceProviderAvailable = true;
  38.          }
  39.          catch
  40.          {
  41.             // Whelp, it threw an exception, so it must not be available.
  42.             ServiceProviderWrapper.IsServiceProviderAvailable = false;
  43.          }
  44.  
  45.          if(ServiceProviderWrapper.IsServiceProviderAvailable == false)
  46.          {
  47.             // You'll need to change "MyMockCockpit" to your real "dummy" cockpit for testing purposes.
  48.             ServiceProviderWrapper.MockCockpit = MyMockCockpit;
  49.          }
  50.       }
  51.  
  52.       #endregion Constructors
  53.  
  54.       #region Public Properties
  55.  
  56.       /// <summary>
  57.       /// Gets a value indicating whether the SimplePlanes service provider is enabled.
  58.       /// </summary>
  59.       /// <value>
  60.       /// <c>true</c> if this instance is enabled; otherwise, <c>false</c>.
  61.       /// </value>
  62.       public static bool IsServiceProviderAvailable
  63.       {
  64.          get;
  65.          private set;
  66.       }
  67.  
  68.       /// <summary>
  69.       /// Gets the main cockpit position.
  70.       /// </summary>
  71.       /// <value>
  72.       /// The main cockpit position.
  73.       /// </value>
  74.       public static Vector3 MainCockpitPosition
  75.       {
  76.          get
  77.          {
  78.             if (ServiceProviderWrapper.IsServiceProviderAvailable)
  79.             {
  80.                return ServiceProvider.Instance.PlayerAircraft.MainCockpitPosition;
  81.             }
  82.             else
  83.             {
  84.                return ServiceProviderWrapper.MockCockpit.position;
  85.             }
  86.          }
  87.       }
  88.  
  89.       /// <summary>
  90.       /// Gets the player aircraft velocity.
  91.       /// </summary>
  92.       /// <value>
  93.       /// The player aircraft velocity.
  94.       /// </value>
  95.       public static Vector3 PlayerAircraftVelocity
  96.       {
  97.          get
  98.          {
  99.             if (ServiceProviderWrapper.IsServiceProviderAvailable)
  100.             {
  101.                return ServiceProvider.Instance.PlayerAircraft.Velocity;
  102.             }
  103.             else
  104.             {
  105.                return ServiceProviderWrapper.MockCockpit.GetComponent<Rigidbody>().velocity;
  106.             }
  107.          }
  108.       }
  109.  
  110.       #endregion Public Properties
  111.  
  112.       #region Public Methods
  113.  
  114.       /// <summary>
  115.       /// Sets the main camera enabled.
  116.       /// </summary>
  117.       /// <param name="enabled">if set to <c>true</c> [enabled].</param>
  118.       public static void SetMainCameraEnabled(bool enabled)
  119.       {
  120.          if (ServiceProviderWrapper.IsServiceProviderAvailable)
  121.          {
  122.             ServiceProvider.Instance.GameCamera.SetMainCameraEnabled(enabled);
  123.          }
  124.          else
  125.          {
  126.             // Do nothing.
  127.          }
  128.       }
  129.  
  130.       #endregion Public Methods
  131.  
  132.       #region Internal Methods
  133.  
  134.       /// <summary>
  135.       /// Shows the status message.
  136.       /// </summary>
  137.       /// <param name="message">The message.</param>
  138.       internal static void ShowStatusMessage(string message)
  139.       {
  140.          if (ServiceProviderWrapper.IsServiceProviderAvailable)
  141.          {
  142.             ServiceProvider.Instance.GameWorld.ShowStatusMessage(message);
  143.          }
  144.          else
  145.          {
  146.             // Service provier isn't enabled, just log a Debug.Log message
  147.             Debug.LogFormat("Status message: {0}", message);
  148.          }
  149.       }
  150.  
  151.       #endregion Internal Methods
  152.    }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement