Guest User

KerBalloons v0.1 - Source Code

a guest
Oct 17th, 2015
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. using KSP;
  8.  
  9. namespace KerBalloons
  10. {
  11.     public class ModuleKerBalloon : PartModule
  12.     {
  13.         [KSPField(isPersistant = false)]
  14.         public string CFGballoonObject = "Balloon";
  15.         [KSPField(isPersistant = false)]
  16.         public string CFGropeObject = "Rope";
  17.         [KSPField(isPersistant = false)]
  18.         public string CFGcapObject = "Cap";
  19.         [KSPField(isPersistant = false)]
  20.         public string CFGliftPointObject = "liftPoint";
  21.         [KSPField(isPersistant = false)]
  22.         public string CFGballoonPointObject = "balloonPoint";
  23.  
  24.         public GameObject balloonObject;
  25.         public GameObject ropeObject;
  26.         public GameObject capObject;
  27.         public GameObject liftPointObject;
  28.         public GameObject balloonPointObject;
  29.  
  30.         [KSPField(isPersistant = false)]
  31.         public float minAtmoPressure = 4;
  32.         [KSPField(isPersistant = false)]
  33.         public float maxAtmoPressure = 102;
  34.         [KSPField(isPersistant = false)]
  35.         public float minScale = 1;
  36.         [KSPField(isPersistant = false)]
  37.         public float maxScale = 5;
  38.         [KSPField(isPersistant = false)]
  39.         public float minLift = 0.5f;
  40.         [KSPField(isPersistant = false)]
  41.         public float maxLift = 1;
  42.         [KSPField(isPersistant = false)]
  43.         public string recommendedBody = "Kerbin";
  44.  
  45.         public bool isInflating;
  46.         public bool hasInflated;
  47.         public bool isInflated;
  48.         public bool isDeflating;
  49.         public bool hasBurst;
  50.         public float scaleInc;
  51.         public string bodyName;
  52.         [KSPField(isPersistant = false)]
  53.         public float bodyG;
  54.  
  55.         public override void OnStart(StartState state)
  56.         {
  57.             Debug.Log("KerBalloons Loaded");
  58.  
  59.             if (HighLogic.LoadedSceneIsFlight)
  60.             {
  61.                 balloonObject = getChildGameObject(this.part.gameObject, CFGballoonObject);
  62.                 ropeObject = getChildGameObject(this.part.gameObject, CFGropeObject);
  63.                 capObject = getChildGameObject(this.part.gameObject, CFGcapObject);
  64.                 liftPointObject = getChildGameObject(this.part.gameObject, CFGliftPointObject);
  65.                 balloonPointObject = getChildGameObject(this.part.gameObject, CFGballoonPointObject);
  66.                
  67.                 balloonObject.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
  68.             }
  69.         }
  70.  
  71.         public void FixedUpdate()
  72.         {
  73.             if (HighLogic.LoadedSceneIsFlight)
  74.             {
  75.                 float currentPressure = (float)FlightGlobals.getStaticPressure();
  76.                 if (hasInflated && !hasBurst)
  77.                 {
  78.                     if (isInflated)
  79.                     {
  80.                         float lift = getLift(currentPressure);
  81.                         rigidbody.AddForceAtPosition(vessel.upAxis * lift,liftPointObject.transform.position);
  82.  
  83.                         Vector3 scale = new Vector3(getScale(currentPressure), getScale(currentPressure), getScale(currentPressure));
  84.                         balloonObject.transform.localScale = scale;
  85.  
  86.                         ropeObject.transform.rotation = Quaternion.Slerp(ropeObject.transform.rotation, Quaternion.LookRotation(vessel.upAxis, vessel.upAxis), getLift(currentPressure) / 10);
  87.                         balloonObject.transform.rotation = Quaternion.Slerp(balloonObject.transform.rotation, Quaternion.LookRotation(vessel.upAxis, vessel.upAxis), getLift(currentPressure) / 8);
  88.  
  89.                         balloonObject.transform.position = balloonPointObject.transform.position;
  90.  
  91.                         if (currentPressure < minAtmoPressure) hasBurst = true;
  92.                     }
  93.                     else if (isDeflating)
  94.                     {
  95.                         if (scaleInc > 0)
  96.                         {
  97.                             scaleInc -= getScale(currentPressure) / 100;
  98.                             balloonObject.transform.localScale = new Vector3(scaleInc, scaleInc, scaleInc);
  99.  
  100.                             float progress = scaleInc / getScale(currentPressure);
  101.  
  102.                             float lift = getLift(currentPressure) * progress;
  103.                             rigidbody.AddForceAtPosition(vessel.upAxis * lift, liftPointObject.transform.position);
  104.  
  105.  
  106.                             ropeObject.transform.localScale = new Vector3(1, 1, progress);
  107.                             balloonObject.transform.position = balloonPointObject.transform.position;
  108.                         }
  109.                         else
  110.                         {
  111.                             isInflated = false;
  112.                             isDeflating = false;
  113.                         }
  114.                     }
  115.                 }
  116.                 else if (isInflating && !hasBurst)
  117.                 {
  118.                     if (scaleInc < getScale(currentPressure))
  119.                     {
  120.                         scaleInc += getScale(currentPressure)/200;
  121.                         balloonObject.transform.localScale = new Vector3(scaleInc, scaleInc, scaleInc);
  122.  
  123.                         float progress = scaleInc / getScale(currentPressure);
  124.  
  125.                         float lift = getLift(currentPressure) * progress;
  126.                         rigidbody.AddForceAtPosition(vessel.upAxis * lift, liftPointObject.transform.position);
  127.  
  128.                        
  129.  
  130.                         ropeObject.transform.rotation = Quaternion.Slerp(ropeObject.transform.rotation, Quaternion.LookRotation(vessel.upAxis, vessel.upAxis), getLift(currentPressure) / 10);
  131.                         balloonObject.transform.rotation = Quaternion.Slerp(balloonObject.transform.rotation, Quaternion.LookRotation(vessel.upAxis, vessel.upAxis), getLift(currentPressure) / 8);
  132.  
  133.                         ropeObject.transform.localScale = new Vector3(1, 1, progress);
  134.                         balloonObject.transform.position = balloonPointObject.transform.position;
  135.                     }
  136.                     else
  137.                     {
  138.                         hasInflated = true;
  139.                         isInflated = true;
  140.                         isInflating = false;
  141.                     }
  142.                 }
  143.                 else if (hasBurst && (isInflated || isInflating || isDeflating))
  144.                 {
  145.                     this.part.Effect("burst");
  146.                     isInflated = false;
  147.                     isInflating = false;
  148.                     isDeflating = false;
  149.                     Destroy(balloonObject);
  150.                     Destroy(ropeObject);
  151.                     Events["inflateBalloon"].active = false;
  152.                     Events["deflateBalloon"].active = false;
  153.                     Actions["inflateAction"].active = false;
  154.                     Actions["deflateAction"].active = false;
  155.                 }
  156.             }
  157.            
  158.         }
  159.  
  160.         [KSPEvent(active = true, guiActive = true, guiActiveEditor = false, guiActiveUnfocused = false, guiName = "Inflate Balloon")]
  161.         public void inflateBalloon()
  162.         {
  163.             if (!isInflated)
  164.             {
  165.                 if (FlightGlobals.getStaticPressure() > minAtmoPressure)
  166.                 {
  167.                     Debug.Log("Inflating Balloon!");
  168.                     this.part.Effect("inflate");
  169.                     isInflating = true;
  170.                     Destroy(capObject);
  171.                     Events["inflateBalloon"].active = false;
  172.                     Events["deflateBalloon"].active = true;
  173.                 }
  174.                 else
  175.                 {
  176.                     if (FlightGlobals.getStaticPressure() <= 0)
  177.                     {
  178.                         ScreenMessages.PostScreenMessage("Cannot inflate balloon in vacuum", 3, ScreenMessageStyle.UPPER_CENTER);
  179.                     }
  180.                     else if (FlightGlobals.getStaticPressure() < minAtmoPressure)
  181.                     {
  182.                         ScreenMessages.PostScreenMessage("Air pressure too low", 3, ScreenMessageStyle.UPPER_CENTER);
  183.                     }
  184.                     else if (FlightGlobals.getStaticPressure() > maxAtmoPressure)
  185.                     {
  186.                         ScreenMessages.PostScreenMessage("Air pressure too high", 3, ScreenMessageStyle.UPPER_CENTER);
  187.                     }
  188.                 }
  189.             }
  190.         }
  191.  
  192.         [KSPEvent(active = false, guiActive = true, guiActiveEditor = false, guiActiveUnfocused = false, guiName = "Deflate Balloon")]
  193.         public void deflateBalloon()
  194.         {
  195.             if (isInflated)
  196.             {
  197.                 Debug.Log("Deflating Balloon!");
  198.                 if (!hasBurst) { this.part.Effect("deflate"); }
  199.                 Events["deflateBalloon"].active = false;
  200.                 isInflated = false;
  201.                 isDeflating = true;
  202.             }
  203.         }
  204.  
  205.         [KSPAction("Inflate Balloon")]
  206.         public void inflateAction(KSPActionParam param)
  207.         {
  208.             inflateBalloon();
  209.             Actions["inflateAction"].active = false;
  210.         }
  211.  
  212.         [KSPAction("Deflate Balloon")]
  213.         public void deflateAction(KSPActionParam param)
  214.         {
  215.             deflateBalloon();
  216.             Actions["deflateAction"].active = false;
  217.         }
  218.  
  219.         public float getLift(float atmoPressure)
  220.         {
  221.             float coefficient = (minLift-maxLift) / Mathf.Pow(maxAtmoPressure,2);
  222.             float x = Mathf.Pow(atmoPressure - maxAtmoPressure - minAtmoPressure,2);
  223.             float yInt = maxLift;
  224.  
  225.             float lift = coefficient * x + yInt;
  226.             return lift;
  227.         }
  228.  
  229.         public float getScale(float atmoPressure)
  230.         {
  231.             float coefficient = (maxScale - minScale) / Mathf.Pow(maxAtmoPressure, 2);
  232.             float x = Mathf.Pow(atmoPressure - maxAtmoPressure - minAtmoPressure, 2);
  233.             float yInt = minScale;
  234.  
  235.             float scale = coefficient * x + yInt;
  236.             return scale;
  237.         }
  238.  
  239.         static public GameObject getChildGameObject(GameObject fromGameObject, string withName)
  240.         {
  241.             Transform[] ts = fromGameObject.transform.GetComponentsInChildren<Transform>();
  242.             foreach (Transform t in ts) if (t.gameObject.name == withName) return t.gameObject;
  243.             return null;
  244.         }
  245.  
  246.         public override string GetInfo()
  247.         {
  248.             //I know about FlightGlobal.Bodies() but for some reason when I use it in this function the game freezes on load
  249.             //Also it can't be put in OnStart() because that isn't called until the part is created
  250.             bodyName = recommendedBody;
  251.             if (recommendedBody == "Sun") bodyG = 17.1f;
  252.             if (recommendedBody == "Kerbin") bodyG = 9.81f;
  253.             if (recommendedBody == "Mun") bodyG = 1.63f;
  254.             if (recommendedBody == "Minmus") bodyG = 0.491f;
  255.             if (recommendedBody == "Moho") bodyG = 2.70f;
  256.             if (recommendedBody == "Eve") bodyG = 16.7f;
  257.             if (recommendedBody == "Duna") bodyG = 2.94f;
  258.             if (recommendedBody == "Ike") bodyG = 1.10f;
  259.             if (recommendedBody == "Jool") bodyG = 7.85f;
  260.             if (recommendedBody == "Laythe") bodyG = 7.85f;
  261.             if (recommendedBody == "Vall") bodyG = 2.31f;
  262.             if (recommendedBody == "Bop") bodyG = 0.589f;
  263.             if (recommendedBody == "Tylo") bodyG = 7.85f;
  264.             if (recommendedBody == "Gilly") bodyG = 0.049f;
  265.             if (recommendedBody == "Pol") bodyG = 0.373f;
  266.             if (recommendedBody == "Dres") bodyG = 1.13f;
  267.             if (recommendedBody == "Eeloo") bodyG = 1.69f;
  268.  
  269.             string moreInfoText;
  270.             moreInfoText = "Recommended Body: " + bodyName;
  271.             moreInfoText = moreInfoText + "\nMin pressure: " + minAtmoPressure.ToString() + "kPa";
  272.             moreInfoText = moreInfoText + "\nMax pressure: " + maxAtmoPressure.ToString() + "kPa";
  273.             moreInfoText = moreInfoText + "\nMax lift: " + maxLift.ToString() + "kN";
  274.             moreInfoText = moreInfoText + "\nMax payload " + "(" + bodyName + "):";
  275.             moreInfoText = moreInfoText + "\n  At max pressure: " + (Mathf.Floor((maxLift/bodyG)*1000)/1000).ToString() + "t";
  276.             moreInfoText = moreInfoText + "\n  At min pressure: " + (Mathf.Floor((minLift / bodyG) * 1000) / 1000).ToString() + "t";
  277.             return moreInfoText;
  278.         }
  279.  
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment