Guest User

KerBalloons 1.1 Source

a guest
May 14th, 2015
853
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6.  
  7. namespace KerBalloons
  8. {
  9.     public class ModuleBalloon : PartModule
  10.     {
  11.         //Variable declaration
  12.         //Inflate animation variables
  13.         [KSPField(isPersistant = false)]
  14.         public string inflateAnimation = "inflate";
  15.         public AnimationState[] inflateAnimStates;
  16.         public float balloonScale = 0;
  17.        
  18.         //Balloon up animation variables
  19.         [KSPField(isPersistant = false)]
  20.         public string upAnimation = "balloonUp";
  21.         public AnimationState[] upAnimStates;
  22.         public float upScale = 0;
  23.  
  24.         //Lift variables
  25.         [KSPField(isPersistant = false)]
  26.         public float targetBalloonLift = 0; //The maximum 'thrust' the balloon can produce (kN)
  27.         public float balloonLift = 0; //The current 'thrust' of the balloon (kN)
  28.        
  29.         //Inflation variables
  30.         [KSPField(isPersistant = false)]
  31.         public float minAtmoPressure = 0; //The minimum atmospheric pressure before the balloon bursts
  32.         public bool hasBurst = false; //Wether or not the balloon has burst
  33.         public bool isInflated = false; //Whether or not the balloon is curently inflated
  34.         public bool isInflating = false; //Whether or not the balloon is curently inflating
  35.         public bool isDeflated = false; //Whether or not the balloon has been deflated
  36.         public bool isDeflating = false; //Whether or not the balloon is curently deflating
  37.         public int burstCounter = 0;
  38.        
  39.         //Object variables
  40.         [KSPField(isPersistant = false)]
  41.         public string balloonObject = "Balloon";
  42.         [KSPField(isPersistant = false)]
  43.         public string balloonCapObject = "BalloonCap";
  44.         [KSPField(isPersistant = false)]
  45.         public string ropeScaleObject = "RopeScale";
  46.        
  47.         //Action groups
  48.         [KSPAction("Inflate Balloon")]
  49.         public void inflateAction(KSPActionParam param)
  50.         {
  51.             inflateBalloon();
  52.             Actions["inflateAction"].active = false;
  53.         }
  54.        
  55.         [KSPAction("Deflate Balloon")]
  56.         public void deflateAction(KSPActionParam param)
  57.         {
  58.             deflateBalloon();
  59.             Actions["deflateAction"].active = false;
  60.         }
  61.        
  62.         //On start
  63.         public override void OnStart(StartState state)
  64.         {
  65.             Debug.Log("KerBalloons Loaded!");
  66.            
  67.             //Inflate animation setup
  68.             Animation[] inflateAnimations = part.FindModelAnimators(inflateAnimation);
  69.             inflateAnimStates = new AnimationState[inflateAnimations.Length];
  70.             int i = 0;
  71.             foreach (Animation a in inflateAnimations)
  72.             {
  73.                 AnimationState aState = a[inflateAnimation];
  74.                 aState.speed = 0;
  75.                 aState.enabled = true;
  76.                 aState.layer = 1;
  77.                 a.Play(inflateAnimation);
  78.                 inflateAnimStates[i++] = aState;
  79.             }
  80.            
  81.             //Up animation setup
  82.             Animation[] upAnimations = part.FindModelAnimators(upAnimation);
  83.             upAnimStates = new AnimationState[upAnimations.Length];
  84.             i = 0;
  85.             foreach (Animation b in upAnimations)
  86.             {
  87.                 AnimationState bState = b[upAnimation];
  88.                 bState.speed = 0;
  89.                 bState.enabled = true;
  90.                 bState.layer = 1;
  91.                 b.Play(upAnimation);
  92.                 upAnimStates[i++] = bState;
  93.             }
  94.         }
  95.        
  96.        
  97.         //Continuously run
  98.         public void FixedUpdate()
  99.         {
  100.             if (HighLogic.LoadedSceneIsFlight)
  101.             {
  102.             if (hasBurst)
  103.             {
  104.                 burstCounter += 1;
  105.                 if (burstCounter > 10) {getChildGameObject(this.part.gameObject,"ParticleEmitterObject").GetComponent<KSPParticleEmitter>().particleEmitter.emit = false;}
  106.                 //this.part.rigidbody.centerOfMass = new Vector3(0,0.1f,0);
  107.             }
  108.             else
  109.             {
  110.                 getChildGameObject(this.part.gameObject,"ParticleEmitterScale").transform.localScale = GameObject.Find(balloonObject).transform.localScale;
  111.                 //this.part.rigidbody.centerOfMass = getChildGameObject(this.part.gameObject,"ParticleEmitterObject").transform.position;
  112.             }
  113.             //Balloon is inflated
  114.             if(isInflated)
  115.             {
  116.                 if (isInflating)
  117.                 {
  118.                     if (balloonLift > targetBalloonLift - (0.005f * targetBalloonLift))
  119.                     {
  120.                         balloonLift = targetBalloonLift;
  121.                         isInflating = false;
  122.                         Debug.Log("Balloon Inflated!");
  123.                     }
  124.                     else
  125.                     {
  126.                         balloonLift += 0.005f * targetBalloonLift;
  127.                         balloonScale = (balloonLift/targetBalloonLift)/2;
  128.                         if (balloonScale > 0.00005f * (float)Math.Pow((double)FlightGlobals.getStaticPressure()-105,2) + 0.45f) {balloonScale = 0.00005f * (float)Math.Pow((double)FlightGlobals.getStaticPressure()-105,2) + 0.45f;}
  129.                     }
  130.                 }
  131.                
  132.                 if (FlightGlobals.getStaticPressure() <= minAtmoPressure)
  133.                    
  134.                 {
  135.                     Debug.Log("Balloon has Burst!");
  136.                     this.part.Effect("burst");
  137.                     getChildGameObject(this.part.gameObject,"ParticleEmitterObject").GetComponent<KSPParticleEmitter>().particleEmitter.emit = true;
  138.                     balloonLift = balloonLift * 0.2f;
  139.                     hasBurst = true;
  140.                     isDeflated = true;
  141.                     Destroy(getChildGameObject(this.part.gameObject,balloonObject));
  142.                     Destroy(getChildGameObject(this.part.gameObject,ropeScaleObject));
  143.                     deflateBalloon();
  144.                 }
  145.                
  146.                 GameObject rotatePoint = getChildGameObject(this.part.gameObject,"rotatePoint");
  147.                 rotatePoint.transform.rotation = Quaternion.Slerp(rotatePoint.transform.rotation, Quaternion.LookRotation(Vector3.up,vessel.upAxis),balloonLift/5);
  148.                 this.part.rigidbody.AddForceAtPosition(vessel.upAxis * balloonLift,getChildGameObject(this.part.gameObject,"ParticleEmitterObject").transform.position); //Add balloon lift
  149.                
  150.             }
  151.  
  152.             //Balloon is deflated
  153.             else if (!isInflated)
  154.             {
  155.                 if (balloonLift > 0)
  156.                 {
  157.                     balloonLift -= 0.01f * targetBalloonLift;
  158.                     if (balloonLift < 0.01)
  159.                     {
  160.                         balloonLift = 0;
  161.                         isDeflated = true;
  162.                         isDeflating = false;
  163.                     }
  164.                 }
  165.                 balloonScale = (balloonLift/targetBalloonLift)/2;
  166.                 if (balloonScale > 0.00005f * (float)Math.Pow((double)FlightGlobals.getStaticPressure()-105,2) + 0.45f) {balloonScale = 0.00005f * (float)Math.Pow((double)FlightGlobals.getStaticPressure()-105,2) + 0.45f;}
  167.             }
  168.  
  169.             if(isInflated && !isInflating)
  170.             {
  171.                 if (FlightGlobals.getStaticPressure() <= 105)
  172.                 {
  173.                     balloonLift = ((-2.5f*targetBalloonLift)/100000) * (float)Math.Pow((double)FlightGlobals.getStaticPressure()-105, 2) + targetBalloonLift;
  174.                     balloonScale = (7.256f/20000) * (float)Math.Pow((double)FlightGlobals.getStaticPressure()-105,2) + 1;
  175.                 }
  176.                 else
  177.                 {
  178.                     balloonLift = targetBalloonLift;
  179.                     balloonScale = 1;
  180.                 }
  181.             }
  182.  
  183.             //Setting the rope scale
  184.             float animTime = balloonLift/targetBalloonLift/2;
  185.             if (animTime <= 0.5)
  186.             {
  187.                 getChildGameObject(this.part.gameObject,ropeScaleObject).transform.localScale = new Vector3(1,(balloonLift/targetBalloonLift)*100+1,1);
  188.                 upScale = balloonLift/targetBalloonLift;
  189.             }
  190.             updateAnim();
  191.             }
  192.         }
  193.        
  194.        
  195.         //Inflate balloon button pressed
  196.         [KSPEvent(active = true, guiActive = true, guiActiveEditor = false, guiActiveUnfocused = false, guiName = "Inflate Balloon")]
  197.         public void inflateBalloon()
  198.         {
  199.             if (!isInflated)
  200.             {
  201.                 if (FlightGlobals.getStaticPressure() > minAtmoPressure)
  202.                 {
  203.                     Debug.Log("Inflating Balloon!");
  204.                     this.part.Effect("inflate");
  205.                     isInflated = true;
  206.                     isInflating = true;
  207.                     Destroy(getChildGameObject(this.part.gameObject,balloonCapObject));
  208.                     Events["inflateBalloon"].active = false;
  209.                     Events["deflateBalloon"].active = true;
  210.                 }
  211.                 else
  212.                 {
  213.                     if (FlightGlobals.getStaticPressure () <= 0)
  214.                     {
  215.                         ScreenMessages.PostScreenMessage("Cannot inflate balloon in vacuum", 3, ScreenMessageStyle.UPPER_CENTER);
  216.                     }
  217.                     else
  218.                     {
  219.                         ScreenMessages.PostScreenMessage("Air pressure too low", 3, ScreenMessageStyle.UPPER_CENTER);
  220.                     }
  221.                 }
  222.             }
  223.         }
  224.        
  225.        
  226.         //Deflate baloon button pressed
  227.         [KSPEvent(active = false, guiActive = true, guiActiveEditor = false, guiActiveUnfocused = false, guiName = "Deflate Balloon")]
  228.         public void deflateBalloon()
  229.         {
  230.             if(isInflated)
  231.             {
  232.                 Debug.Log("Deflating Balloon!");
  233.                 if (!hasBurst) {this.part.Effect("deflate");}
  234.                 Events["deflateBalloon"].active = false;
  235.                 isInflated = false;
  236.                 isDeflating = true;
  237.             }
  238.         }
  239.        
  240.        
  241.        
  242.         public void updateAnim()
  243.         {
  244.             if (!hasBurst) //If the balloon hasn't burst
  245.             {
  246.                 foreach (AnimationState a in inflateAnimStates)
  247.                 {
  248.                     a.normalizedTime = balloonScale;
  249.                 }
  250.                
  251.                 foreach (AnimationState b in upAnimStates)
  252.                 {
  253.                     b.normalizedTime = upScale;
  254.                 }
  255.             }
  256.         }
  257.  
  258.         static public GameObject getChildGameObject(GameObject fromGameObject, string withName) {
  259.             Transform[] ts = fromGameObject.transform.GetComponentsInChildren<Transform>();
  260.             foreach (Transform t in ts) if (t.gameObject.name == withName) return t.gameObject;
  261.             return null;
  262.         }
  263.        
  264.     }
  265. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×