overhaul ModuleRestockISRUAnimation to support more animation states

Getting closer to making this a drop-in replacement for ModuleAnimationGroup in all cases!
pull/653/head
Andrew Cassidy 5 years ago
parent 019af2af2a
commit cf585360cb
No known key found for this signature in database
GPG Key ID: 963017B38FD477A1

@ -19,6 +19,7 @@
{ {
name = ModuleRestockISRUAnimation name = ModuleRestockISRUAnimation
deployAnimationName = heater deployAnimationName = heater
needsEC = true
} }
MODULE MODULE
@ -71,6 +72,9 @@
{ {
name = ModuleRestockISRUAnimation name = ModuleRestockISRUAnimation
deployAnimationName = Deploy deployAnimationName = Deploy
activeAnimationName = Run
retractAnimationName = Retract
needsEC = true
} }
MODULE MODULE

@ -9,27 +9,68 @@ namespace Restock
// name of the deploy animation to use // name of the deploy animation to use
[KSPField] public string deployAnimationName = ""; [KSPField] public string deployAnimationName = "";
// name of the retract animation to use
// will default to using the deploy animation in reverse
[KSPField] public string retractAnimationName = "";
// name of the active animation to use
[KSPField] public string activeAnimationName = "";
// name of the inactive animation to use
[KSPField] public string inactiveAnimationName = "";
// speed to run the animation when deploying // speed to run the animation when deploying
[KSPField] public float deploySpeed = 1.0f; [KSPField] public float deploySpeed = 1.0f;
// speed to run the animation when retracting // speed to run the animation when retracting
[KSPField] public float retractSpeed = 1.0f; [KSPField] public float retractSpeed = 1.0f;
public bool isDeployed = false; // does this module need electric charge to be enabled?
[KSPField] public bool needsEC = false;
public Animation DeployAnimation { get; private set; } public bool IsDeployed
{
get { return (CurrentState == State.Active || CurrentState == State.Deploying); }
}
private enum State
{
Inactive,
Deploying,
Active,
Retracting
}
private List<BaseConverter> _modules; private Animation DeployAnimation { get; set; }
private Animation RetractAnimation { get; set; }
private Animation ActiveAnimation { get; set; }
private Animation InactiveAnimation { get; set; }
private State CurrentState { get; set; }
private bool _deployAnimationPresent = false; private bool _deployAnimationPresent = false;
private bool _retractAnimationPresent = false;
private bool _activeAnimationPresent = false;
private bool _inactiveAnimationPresent = false;
private List<BaseConverter> _modules;
public void Start() public void Start()
{ {
_modules = base.part.FindModulesImplementing<BaseConverter>(); _modules = base.part.FindModulesImplementing<BaseConverter>();
_deployAnimationPresent = (deployAnimationName != string.Empty); _deployAnimationPresent = (deployAnimationName != string.Empty);
_retractAnimationPresent = (retractAnimationName != string.Empty);
_activeAnimationPresent = (activeAnimationName != string.Empty);
_inactiveAnimationPresent = (inactiveAnimationName != string.Empty);
DeployAnimation = ((_deployAnimationPresent) ? base.part.FindModelAnimators(deployAnimationName)[0] : null); DeployAnimation = ((_deployAnimationPresent) ? base.part.FindModelAnimators(deployAnimationName)[0] : null);
RetractAnimation = ((_retractAnimationPresent) ? base.part.FindModelAnimators(retractAnimationName)[0] : null);
ActiveAnimation = ((_activeAnimationPresent) ? base.part.FindModelAnimators(activeAnimationName)[0] : null);
InactiveAnimation = ((_inactiveAnimationPresent) ? base.part.FindModelAnimators(inactiveAnimationName)[0] : null);
CurrentState = State.Inactive;
foreach (var a in base.part.FindModelAnimators()) a.Stop(); foreach (var a in base.part.FindModelAnimators()) a.Stop();
} }
@ -37,32 +78,33 @@ namespace Restock
{ {
if (!HighLogic.LoadedSceneIsFlight || base.vessel == null) if (!HighLogic.LoadedSceneIsFlight || base.vessel == null)
{ {
isDeployed = false; CurrentState = State.Inactive;
return; return;
} }
if (isDeployed) if (IsDeployed)
{ {
PlayDeployAnimation(1000); DeployEnd();
} }
else else
{ {
PlayDeployAnimation(-1000); RetractEnd();
} }
} }
public void Update() public void Update()
{ {
if (!HighLogic.LoadedSceneIsFlight) return; if (!HighLogic.LoadedSceneIsFlight) return;
try try
{ {
if (!CheatOptions.InfiniteElectricity) if (needsEC && !CheatOptions.InfiniteElectricity)
{ {
var ecHash = PartResourceLibrary.ElectricityHashcode; var ecHash = PartResourceLibrary.ElectricityHashcode;
base.vessel.GetConnectedResourceTotals(ecHash, out var ecAmount, out _, true); base.vessel.GetConnectedResourceTotals(ecHash, out var ecAmount, out _, true);
if (ecAmount < 0.1) if (ecAmount < 0.1)
{ {
if (isDeployed) RetractModule(); if (IsDeployed) RetractStart();
return; return;
} }
} }
@ -73,8 +115,65 @@ namespace Restock
if (m.ModuleIsActive()) enabledCount++; if (m.ModuleIsActive()) enabledCount++;
} }
if (isDeployed && enabledCount == 0) RetractModule(); switch (CurrentState)
else if (!isDeployed && enabledCount != 0) DeployModule(); {
case State.Active:
if (enabledCount == 0)
{
DeployStart();
}
else if (_activeAnimationPresent && !ActiveAnimation.IsPlaying(activeAnimationName))
{
PlayAnimation(ActiveAnimation, activeAnimationName);
}
break;
case State.Deploying:
if (!_deployAnimationPresent)
{
this.LogError("Invalid state!");
CurrentState = State.Active;
}
if (enabledCount == 0)
{
RetractStart();
}
else if (!DeployAnimation.IsPlaying(deployAnimationName))
{
DeployEnd();
}
break;
case State.Inactive:
if (enabledCount != 0)
{
DeployStart();
}
else if (_inactiveAnimationPresent && !InactiveAnimation.IsPlaying(inactiveAnimationName))
{
PlayAnimation(InactiveAnimation, inactiveAnimationName);
}
break;
case State.Retracting:
if (!_retractAnimationPresent && !_deployAnimationPresent)
{
this.LogError("Invalid state!");
CurrentState = State.Inactive;
}
if (enabledCount != 0)
{
DeployStart();
}
else if (!RetractAnimation.IsPlaying(retractAnimationName))
{
RetractEnd();
}
break;
default:
throw new ArgumentOutOfRangeException();
}
} }
catch (Exception e) catch (Exception e)
{ {
@ -82,35 +181,68 @@ namespace Restock
} }
} }
public void DeployModule() private void DeployStart()
{
if (_deployAnimationPresent)
{
CurrentState = State.Deploying;
PlayAnimation(DeployAnimation, deployAnimationName, deploySpeed);
}
else
{
DeployEnd();
}
}
private void DeployEnd()
{ {
isDeployed = true; CurrentState = State.Active;
PlayDeployAnimation(1 * deploySpeed); if (_activeAnimationPresent)
{
PlayAnimation(ActiveAnimation, activeAnimationName);
}
} }
public void RetractModule() private void RetractStart()
{ {
isDeployed = false; if (_retractAnimationPresent)
PlayDeployAnimation(-1 * retractSpeed); {
CurrentState = State.Retracting;
PlayAnimation(RetractAnimation, retractAnimationName, retractSpeed);
} else if (_deployAnimationPresent)
{
CurrentState = State.Retracting;
PlayAnimation(DeployAnimation, deployAnimationName, retractSpeed * -1);
}
else
{
RetractEnd();
}
} }
private void PlayDeployAnimation(float speed) private void RetractEnd()
{ {
if (_deployAnimationPresent) CurrentState = State.Inactive;
if (_inactiveAnimationPresent)
{ {
var deployAnimationState = DeployAnimation[deployAnimationName]; PlayAnimation(InactiveAnimation, inactiveAnimationName);
if (speed < 0 && deployAnimationState.time < Mathf.Epsilon) }
{ }
deployAnimationState.time = deployAnimationState.length;
}
else if (speed > 0 && deployAnimationState.time > deployAnimationState.length - Mathf.Epsilon)
{
deployAnimationState.time = 0.0f;
}
deployAnimationState.speed = speed; private void PlayAnimation(Animation anim, string name, float speed = 1f)
DeployAnimation.Play(deployAnimationName); {
var animState = anim[name];
if (speed < 0 && animState.time < Mathf.Epsilon)
{
animState.time = animState.length;
} }
else if (speed > 0 && animState.time > animState.length - Mathf.Epsilon)
{
animState.time = 0.0f;
}
animState.speed = speed;
anim.Play(name);
} }
} }
} }
Loading…
Cancel
Save