@ -8,6 +8,13 @@ namespace Aerostats
{
public class ModuleBalloonCase : PartModule
{
public enum BalloonState
{
Packed ,
Deployed ,
Detached
}
/// <summary>
/// Maximum rate at which gas can be injected in the balloon to fill it, in m^3/s at stp
/// </summary>
@ -43,11 +50,17 @@ namespace Aerostats
public float MinimumFillQuantity = 1 0.0f ;
/// <summary>
/// Length of spring that can not be extended (no force applied under this length)
/// Length of spring at rest (no force applied under this length)
/// </summary>
[KSPField(isPersistant = false, guiActive = false)]
public float SpringRestLength = 1 0.0f ;
/// <summary>
/// Length at which the spring breaks
/// </summary>
[KSPField(isPersistant = false, guiActive = false)]
public float SpringBreakLength = 5 0.0f ;
/// <summary>
/// Force applied by the spring between the balloon and the vessel, proportional to spring extension, in newton per meter
/// </summary>
@ -57,9 +70,8 @@ namespace Aerostats
[KSPField(isPersistant = false, guiActive = true)]
public string Status ;
private bool Deployed ;
private bool Destroyed ;
[KSPField(isPersistant = true, guiActive = false)]
public BalloonState State ;
/// <summary>
/// Gas quantity currently inside the balloon
@ -76,18 +88,18 @@ namespace Aerostats
[KSPEvent(guiActive = true, active = true, externalToEVAOnly = false, guiActiveUnfocused = true, guiName = "Deploy balloon", unfocusedRange = 5)]
public void Deploy ( )
{
if ( Destroyed | | Deploy ed)
if ( State ! = BalloonState . Pack ed)
return ;
Deployed = true ;
State = BalloonState . Deployed ;
UnityEngine . Debug . Log ( "Aerostats: staged " ) ;
Util . PostDebugScreenMessage ( "staged " ) ;
UnityEngine . Debug . Log ( "Aerostats: deploying " ) ;
Util . PostDebugScreenMessage ( "deploying " ) ;
AvailablePart avPart = PartLoader . Instance . parts . Single ( p = > p . name = = "heliumBalloon" ) ;
var balloonPart = ( Part ) UnityEngine . Object . Instantiate ( avPart . partPrefab ) ;
var balloonObject = balloonPart . gameObject ;
balloonObject . transform . position = part . Rigidbody . position + part . Rigidbody . transform . up * 3 .0f;
balloonObject . transform . position = part . Rigidbody . position + part . Rigidbody . transform . up * 1 .0f;
balloonObject . transform . rotation = part . Rigidbody . rotation ;
balloonPart . SetHierarchyRoot ( balloonPart ) ;
balloonPart . vessel = vessel ;
@ -103,7 +115,7 @@ namespace Aerostats
Balloon . part . vessel . vesselName = vessel . vesselName + " balloon" ;
float initialGasQuantity = Mathf . Min ( 0.1f , part . RequestResource ( "Helium" , MinimumFillQuantity ) ) ;
Balloon . Initialize ( this , part . rigidbody . velocity , GasDensity , initialGasQuantity ) ;
Balloon . Initialize ( this , ( Vector3d ) part . rigidbody . velocity + Krakensbane . GetFrameVelocity ( ) , GasDensity , initialGasQuantity ) ;
LiftingGasQuantity = Balloon . LiftingGasQuantity ;
@ -120,18 +132,19 @@ namespace Aerostats
[KSPEvent(guiActive = true, active = true, externalToEVAOnly = false, guiActiveUnfocused = true, guiName = "Cut rope", unfocusedRange = 5)]
public void Cut ( )
{
if ( Destroyed | | ! Deployed )
if ( State ! = BalloonState . Deployed )
return ;
if ( Balloon ! = null )
{
Balloon . OnDetached ( ) ;
Balloon = null ;
}
if ( Spring ! = null )
Destroy ( Spring ) ;
Spring = null ;
part . OnJustAboutToBeDestroyed - = OnPartDestroyed ;
Destroyed = true ;
State = BalloonState . Detached ;
}
public override void OnStart ( PartModule . StartState state )
@ -149,7 +162,10 @@ namespace Aerostats
private void FixedUpdate ( )
{
if ( Destroyed )
if ( State = = BalloonState . Deployed & & Balloon = = null )
Cut ( ) ;
if ( State = = BalloonState . Detached )
{
Status = "separated" ;
VolumeStatus = "-" ;
@ -161,7 +177,7 @@ namespace Aerostats
Deploy ( ) ;
}
if ( Deployed )
if ( State = = BalloonState . Deployed )
{
float currentMaxQuantity = Balloon . GetCurrentMaxQuantity ( ) ;
@ -203,7 +219,12 @@ namespace Aerostats
float springLength = springVec . magnitude ;
float springForceMag = 0 ;
var balloonAttachPoint = Balloon . rigidbody . position - Balloon . transform . up . normalized * Balloon . Radius ;
if ( springLength > restLength )
if ( springLength > SpringBreakLength )
{
Cut ( ) ;
FlightLogger . eventLog . Add ( "The balloon cable broke due to extreme extension" ) ;
}
else if ( springLength > restLength )
{
float tensingLength = springLength - restLength ;
springForceMag = tensingLength * SpringHardness ;
@ -222,11 +243,12 @@ namespace Aerostats
private void LateUpdate ( )
{
if ( Destroyed )
if ( State = = BalloonState . Deployed & & Balloon = = null )
Cut ( ) ;
if ( State ! = BalloonState . Deployed )
return ;
if ( Deployed )
{
var balloonAttachPoint = Balloon . rigidbody . position - Balloon . transform . up . normalized * Balloon . Radius ;
Spring . SetPosition ( 0 , part . transform . position ) ;
@ -234,4 +256,3 @@ namespace Aerostats
}
}
}
}