Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- namespace KerBalloons
- {
- public class ModuleBalloon : PartModule
- {
- //Variable declaration
- //Inflate animation variables
- [KSPField(isPersistant = false)]
- public string inflateAnimation = "inflate";
- public AnimationState[] inflateAnimStates;
- public float balloonScale = 0;
- //Balloon up animation variables
- [KSPField(isPersistant = false)]
- public string upAnimation = "balloonUp";
- public AnimationState[] upAnimStates;
- public float upScale = 0;
- //Lift variables
- [KSPField(isPersistant = false)]
- public float targetBalloonLift = 0; //The maximum 'thrust' the balloon can produce (kN)
- public float balloonLift = 0; //The current 'thrust' of the balloon (kN)
- //Inflation variables
- [KSPField(isPersistant = false)]
- public float minAtmoPressure = 0; //The minimum atmospheric pressure before the balloon bursts
- public bool hasBurst = false; //Wether or not the balloon has burst
- public bool isInflated = false; //Whether or not the balloon is curently inflated
- public bool isInflating = false; //Whether or not the balloon is curently inflating
- public bool isDeflated = false; //Whether or not the balloon has been deflated
- public bool isDeflating = false; //Whether or not the balloon is curently deflating
- public int burstCounter = 0;
- //Object variables
- [KSPField(isPersistant = false)]
- public string balloonObject = "Balloon";
- [KSPField(isPersistant = false)]
- public string balloonCapObject = "BalloonCap";
- [KSPField(isPersistant = false)]
- public string ropeScaleObject = "RopeScale";
- //Action groups
- [KSPAction("Inflate Balloon")]
- public void inflateAction(KSPActionParam param)
- {
- inflateBalloon();
- Actions["inflateAction"].active = false;
- }
- [KSPAction("Deflate Balloon")]
- public void deflateAction(KSPActionParam param)
- {
- deflateBalloon();
- Actions["deflateAction"].active = false;
- }
- //On start
- public override void OnStart(StartState state)
- {
- Debug.Log("KerBalloons Loaded!");
- //Inflate animation setup
- Animation[] inflateAnimations = part.FindModelAnimators(inflateAnimation);
- inflateAnimStates = new AnimationState[inflateAnimations.Length];
- int i = 0;
- foreach (Animation a in inflateAnimations)
- {
- AnimationState aState = a[inflateAnimation];
- aState.speed = 0;
- aState.enabled = true;
- aState.layer = 1;
- a.Play(inflateAnimation);
- inflateAnimStates[i++] = aState;
- }
- //Up animation setup
- Animation[] upAnimations = part.FindModelAnimators(upAnimation);
- upAnimStates = new AnimationState[upAnimations.Length];
- i = 0;
- foreach (Animation b in upAnimations)
- {
- AnimationState bState = b[upAnimation];
- bState.speed = 0;
- bState.enabled = true;
- bState.layer = 1;
- b.Play(upAnimation);
- upAnimStates[i++] = bState;
- }
- }
- //Continuously run
- public void FixedUpdate()
- {
- if (HighLogic.LoadedSceneIsFlight)
- {
- if (hasBurst)
- {
- burstCounter += 1;
- if (burstCounter > 10) {getChildGameObject(this.part.gameObject,"ParticleEmitterObject").GetComponent<KSPParticleEmitter>().particleEmitter.emit = false;}
- //this.part.rigidbody.centerOfMass = new Vector3(0,0.1f,0);
- }
- else
- {
- getChildGameObject(this.part.gameObject,"ParticleEmitterScale").transform.localScale = GameObject.Find(balloonObject).transform.localScale;
- //this.part.rigidbody.centerOfMass = getChildGameObject(this.part.gameObject,"ParticleEmitterObject").transform.position;
- }
- //Balloon is inflated
- if(isInflated)
- {
- if (isInflating)
- {
- if (balloonLift > targetBalloonLift - (0.005f * targetBalloonLift))
- {
- balloonLift = targetBalloonLift;
- isInflating = false;
- Debug.Log("Balloon Inflated!");
- }
- else
- {
- balloonLift += 0.005f * targetBalloonLift;
- balloonScale = (balloonLift/targetBalloonLift)/2;
- 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;}
- }
- }
- if (FlightGlobals.getStaticPressure() <= minAtmoPressure)
- {
- Debug.Log("Balloon has Burst!");
- this.part.Effect("burst");
- getChildGameObject(this.part.gameObject,"ParticleEmitterObject").GetComponent<KSPParticleEmitter>().particleEmitter.emit = true;
- balloonLift = balloonLift * 0.2f;
- hasBurst = true;
- isDeflated = true;
- Destroy(getChildGameObject(this.part.gameObject,balloonObject));
- Destroy(getChildGameObject(this.part.gameObject,ropeScaleObject));
- deflateBalloon();
- }
- GameObject rotatePoint = getChildGameObject(this.part.gameObject,"rotatePoint");
- rotatePoint.transform.rotation = Quaternion.Slerp(rotatePoint.transform.rotation, Quaternion.LookRotation(Vector3.up,vessel.upAxis),balloonLift/5);
- this.part.rigidbody.AddForceAtPosition(vessel.upAxis * balloonLift,getChildGameObject(this.part.gameObject,"ParticleEmitterObject").transform.position); //Add balloon lift
- }
- //Balloon is deflated
- else if (!isInflated)
- {
- if (balloonLift > 0)
- {
- balloonLift -= 0.01f * targetBalloonLift;
- if (balloonLift < 0.01)
- {
- balloonLift = 0;
- isDeflated = true;
- isDeflating = false;
- }
- }
- balloonScale = (balloonLift/targetBalloonLift)/2;
- 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;}
- }
- if(isInflated && !isInflating)
- {
- if (FlightGlobals.getStaticPressure() <= 105)
- {
- balloonLift = ((-2.5f*targetBalloonLift)/100000) * (float)Math.Pow((double)FlightGlobals.getStaticPressure()-105, 2) + targetBalloonLift;
- balloonScale = (7.256f/20000) * (float)Math.Pow((double)FlightGlobals.getStaticPressure()-105,2) + 1;
- }
- else
- {
- balloonLift = targetBalloonLift;
- balloonScale = 1;
- }
- }
- //Setting the rope scale
- float animTime = balloonLift/targetBalloonLift/2;
- if (animTime <= 0.5)
- {
- getChildGameObject(this.part.gameObject,ropeScaleObject).transform.localScale = new Vector3(1,(balloonLift/targetBalloonLift)*100+1,1);
- upScale = balloonLift/targetBalloonLift;
- }
- updateAnim();
- }
- }
- //Inflate balloon button pressed
- [KSPEvent(active = true, guiActive = true, guiActiveEditor = false, guiActiveUnfocused = false, guiName = "Inflate Balloon")]
- public void inflateBalloon()
- {
- if (!isInflated)
- {
- if (FlightGlobals.getStaticPressure() > minAtmoPressure)
- {
- Debug.Log("Inflating Balloon!");
- this.part.Effect("inflate");
- isInflated = true;
- isInflating = true;
- Destroy(getChildGameObject(this.part.gameObject,balloonCapObject));
- Events["inflateBalloon"].active = false;
- Events["deflateBalloon"].active = true;
- }
- else
- {
- if (FlightGlobals.getStaticPressure () <= 0)
- {
- ScreenMessages.PostScreenMessage("Cannot inflate balloon in vacuum", 3, ScreenMessageStyle.UPPER_CENTER);
- }
- else
- {
- ScreenMessages.PostScreenMessage("Air pressure too low", 3, ScreenMessageStyle.UPPER_CENTER);
- }
- }
- }
- }
- //Deflate baloon button pressed
- [KSPEvent(active = false, guiActive = true, guiActiveEditor = false, guiActiveUnfocused = false, guiName = "Deflate Balloon")]
- public void deflateBalloon()
- {
- if(isInflated)
- {
- Debug.Log("Deflating Balloon!");
- if (!hasBurst) {this.part.Effect("deflate");}
- Events["deflateBalloon"].active = false;
- isInflated = false;
- isDeflating = true;
- }
- }
- public void updateAnim()
- {
- if (!hasBurst) //If the balloon hasn't burst
- {
- foreach (AnimationState a in inflateAnimStates)
- {
- a.normalizedTime = balloonScale;
- }
- foreach (AnimationState b in upAnimStates)
- {
- b.normalizedTime = upScale;
- }
- }
- }
- static public GameObject getChildGameObject(GameObject fromGameObject, string withName) {
- Transform[] ts = fromGameObject.transform.GetComponentsInChildren<Transform>();
- foreach (Transform t in ts) if (t.gameObject.name == withName) return t.gameObject;
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement