mirror of
https://github.com/PorktoberRevolution/ReStocked
synced 2024-09-01 17:34:42 +00:00
Finish overhauling animation module to support waiting states
• ISRU Animation module now will optionally wait for the current looping animation to complete before changing states, making transitions look more seamless • Better looping animation handling, instead of manually refreshing animations • Better documentation • Remove some debug code in HeatEffects
This commit is contained in:
parent
cf585360cb
commit
f5b81532bb
@ -75,6 +75,7 @@
|
||||
activeAnimationName = Run
|
||||
retractAnimationName = Retract
|
||||
needsEC = true
|
||||
waitForComplete = true
|
||||
}
|
||||
|
||||
MODULE
|
||||
|
Binary file not shown.
@ -59,7 +59,6 @@ namespace Restock
|
||||
|
||||
public void Start()
|
||||
{
|
||||
Debug.Log("Start()");
|
||||
if (base.vessel == null) return;
|
||||
|
||||
if (enableHeatEmissive)
|
||||
@ -101,9 +100,6 @@ namespace Restock
|
||||
{
|
||||
if (HighLogic.LoadedSceneIsEditor || HighLogic.LoadedSceneIsFlight) return;
|
||||
|
||||
Debug.Log("OnLoad()");
|
||||
Debug.Log(node.ToString());
|
||||
|
||||
renderers = base.part.FindModelComponents<Renderer>();
|
||||
|
||||
if (node.HasValue("excludedRenderer"))
|
||||
|
@ -28,16 +28,20 @@ namespace Restock
|
||||
// does this module need electric charge to be enabled?
|
||||
[KSPField] public bool needsEC = false;
|
||||
|
||||
public bool IsDeployed
|
||||
{
|
||||
get { return (CurrentState == State.Active || CurrentState == State.Deploying); }
|
||||
}
|
||||
// should the module wait until a current looping animation completes before changing state?
|
||||
[KSPField] public bool waitForComplete = false;
|
||||
|
||||
public bool IsDeployed => (CurrentState == State.InactiveWaiting ||
|
||||
CurrentState == State.Active ||
|
||||
CurrentState == State.Deploying);
|
||||
|
||||
private enum State
|
||||
{
|
||||
Inactive,
|
||||
InactiveWaiting,
|
||||
Deploying,
|
||||
Active,
|
||||
ActiveWaiting,
|
||||
Retracting
|
||||
}
|
||||
|
||||
@ -64,32 +68,31 @@ namespace Restock
|
||||
_activeAnimationPresent = (activeAnimationName != string.Empty);
|
||||
_inactiveAnimationPresent = (inactiveAnimationName != string.Empty);
|
||||
|
||||
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;
|
||||
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);
|
||||
|
||||
foreach (var a in base.part.FindModelAnimators()) a.Stop();
|
||||
|
||||
if (!HighLogic.LoadedSceneIsFlight) return;
|
||||
|
||||
if (ConvertersEnabled())
|
||||
{
|
||||
DeployStart(1000f);
|
||||
}
|
||||
else
|
||||
{
|
||||
RetractStart(1000f);
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnLoad(ConfigNode node)
|
||||
{
|
||||
if (!HighLogic.LoadedSceneIsFlight || base.vessel == null)
|
||||
{
|
||||
CurrentState = State.Inactive;
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsDeployed)
|
||||
{
|
||||
DeployEnd();
|
||||
}
|
||||
else
|
||||
{
|
||||
RetractEnd();
|
||||
}
|
||||
}
|
||||
|
||||
public void Update()
|
||||
@ -98,43 +101,51 @@ namespace Restock
|
||||
|
||||
try
|
||||
{
|
||||
if (needsEC && !CheatOptions.InfiniteElectricity)
|
||||
{
|
||||
var ecHash = PartResourceLibrary.ElectricityHashcode;
|
||||
base.vessel.GetConnectedResourceTotals(ecHash, out var ecAmount, out _, true);
|
||||
if (ecAmount < 0.1)
|
||||
{
|
||||
if (IsDeployed) RetractStart();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int enabledCount = 0;
|
||||
foreach (var m in _modules)
|
||||
{
|
||||
if (m.ModuleIsActive()) enabledCount++;
|
||||
}
|
||||
|
||||
switch (CurrentState)
|
||||
{
|
||||
case State.Active:
|
||||
if (enabledCount == 0)
|
||||
// System is inactive, and playing the inactive animation if present
|
||||
case State.Inactive:
|
||||
if (ConvertersEnabled())
|
||||
{
|
||||
if (waitForComplete)
|
||||
{
|
||||
DeployWait();
|
||||
}
|
||||
else
|
||||
{
|
||||
DeployStart();
|
||||
}
|
||||
else if (_activeAnimationPresent && !ActiveAnimation.IsPlaying(activeAnimationName))
|
||||
{
|
||||
PlayAnimation(ActiveAnimation, activeAnimationName);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
// System is inactive, but waiting for the animation to end before deploying
|
||||
case State.InactiveWaiting:
|
||||
if (!waitForComplete || !_inactiveAnimationPresent)
|
||||
{
|
||||
this.LogError(
|
||||
"Invalid state! waitForComplete not enabled or inactive animation not present.");
|
||||
CurrentState = State.Inactive;
|
||||
}
|
||||
else if (!ConvertersEnabled())
|
||||
{
|
||||
RetractEnd();
|
||||
}
|
||||
else if (!InactiveAnimation.IsPlaying(inactiveAnimationName))
|
||||
{
|
||||
DeployStart();
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
// System is deploying
|
||||
case State.Deploying:
|
||||
if (!_deployAnimationPresent)
|
||||
{
|
||||
this.LogError("Invalid state!");
|
||||
this.LogError("Invalid state! Deploying without an animation present.");
|
||||
CurrentState = State.Active;
|
||||
}
|
||||
if (enabledCount == 0)
|
||||
else if (!ConvertersEnabled())
|
||||
{
|
||||
RetractStart();
|
||||
}
|
||||
@ -142,26 +153,51 @@ namespace Restock
|
||||
{
|
||||
DeployEnd();
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case State.Inactive:
|
||||
if (enabledCount != 0)
|
||||
// System is active, and playing the active animation if present
|
||||
case State.Active:
|
||||
if (!ConvertersEnabled())
|
||||
{
|
||||
DeployStart();
|
||||
}
|
||||
else if (_inactiveAnimationPresent && !InactiveAnimation.IsPlaying(inactiveAnimationName))
|
||||
if (waitForComplete)
|
||||
{
|
||||
PlayAnimation(InactiveAnimation, inactiveAnimationName);
|
||||
RetractWait();
|
||||
}
|
||||
else
|
||||
{
|
||||
RetractStart();
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
// System is active, but waiting for the animation to finish before retracting
|
||||
case State.ActiveWaiting:
|
||||
if (!waitForComplete || !_activeAnimationPresent)
|
||||
{
|
||||
this.LogError("Invalid state! waitForComplete not enabled or active animation not present.");
|
||||
CurrentState = State.Active;
|
||||
}
|
||||
else if (ConvertersEnabled())
|
||||
{
|
||||
DeployEnd();
|
||||
}
|
||||
else if (!ActiveAnimation.IsPlaying(activeAnimationName))
|
||||
{
|
||||
RetractStart();
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
// System is retracting
|
||||
case State.Retracting:
|
||||
if (!_retractAnimationPresent && !_deployAnimationPresent)
|
||||
{
|
||||
this.LogError("Invalid state!");
|
||||
this.LogError("Invalid state! Retracting without an animation present.");
|
||||
CurrentState = State.Inactive;
|
||||
}
|
||||
if (enabledCount != 0)
|
||||
else if (ConvertersEnabled())
|
||||
{
|
||||
DeployStart();
|
||||
}
|
||||
@ -169,6 +205,7 @@ namespace Restock
|
||||
{
|
||||
RetractEnd();
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -181,12 +218,28 @@ namespace Restock
|
||||
}
|
||||
}
|
||||
|
||||
private void DeployStart()
|
||||
private void DeployWait()
|
||||
{
|
||||
if (_inactiveAnimationPresent){
|
||||
CurrentState = State.InactiveWaiting;
|
||||
PlayAnimation(InactiveAnimation, inactiveAnimationName, loop: false);
|
||||
}
|
||||
else
|
||||
{
|
||||
DeployStart();
|
||||
}
|
||||
}
|
||||
|
||||
private void DeployStart(float speed = 1f)
|
||||
{
|
||||
if (_deployAnimationPresent)
|
||||
{
|
||||
if (_retractAnimationPresent && RetractAnimation.IsPlaying(retractAnimationName))
|
||||
{
|
||||
RetractAnimation.Stop(retractAnimationName);
|
||||
}
|
||||
CurrentState = State.Deploying;
|
||||
PlayAnimation(DeployAnimation, deployAnimationName, deploySpeed);
|
||||
PlayAnimation(DeployAnimation, deployAnimationName, speed * deploySpeed);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -197,22 +250,41 @@ namespace Restock
|
||||
private void DeployEnd()
|
||||
{
|
||||
CurrentState = State.Active;
|
||||
|
||||
if (_activeAnimationPresent)
|
||||
{
|
||||
PlayAnimation(ActiveAnimation, activeAnimationName);
|
||||
PlayAnimation(ActiveAnimation, activeAnimationName, loop: true);
|
||||
}
|
||||
}
|
||||
|
||||
private void RetractStart()
|
||||
private void RetractWait()
|
||||
{
|
||||
if (_activeAnimationPresent)
|
||||
{
|
||||
CurrentState = State.ActiveWaiting;
|
||||
PlayAnimation(ActiveAnimation, activeAnimationName, loop: false);
|
||||
}
|
||||
else
|
||||
{
|
||||
RetractStart();
|
||||
}
|
||||
}
|
||||
|
||||
private void RetractStart(float speed = 1f)
|
||||
{
|
||||
if (_retractAnimationPresent)
|
||||
{
|
||||
if (_deployAnimationPresent && DeployAnimation.IsPlaying(deployAnimationName))
|
||||
{
|
||||
DeployAnimation.Stop(deployAnimationName);
|
||||
}
|
||||
CurrentState = State.Retracting;
|
||||
PlayAnimation(RetractAnimation, retractAnimationName, retractSpeed);
|
||||
} else if (_deployAnimationPresent)
|
||||
PlayAnimation(RetractAnimation, retractAnimationName, speed * retractSpeed);
|
||||
}
|
||||
else if (_deployAnimationPresent)
|
||||
{
|
||||
CurrentState = State.Retracting;
|
||||
PlayAnimation(DeployAnimation, deployAnimationName, retractSpeed * -1);
|
||||
PlayAnimation(DeployAnimation, deployAnimationName, speed * retractSpeed * -1);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -223,15 +295,42 @@ namespace Restock
|
||||
private void RetractEnd()
|
||||
{
|
||||
CurrentState = State.Inactive;
|
||||
|
||||
if (_inactiveAnimationPresent)
|
||||
{
|
||||
PlayAnimation(InactiveAnimation, inactiveAnimationName);
|
||||
PlayAnimation(InactiveAnimation, inactiveAnimationName, loop: true);
|
||||
}
|
||||
}
|
||||
|
||||
private void PlayAnimation(Animation anim, string name, float speed = 1f)
|
||||
private bool ConvertersEnabled()
|
||||
{
|
||||
if (needsEC && !CheatOptions.InfiniteElectricity)
|
||||
{
|
||||
var ecHash = PartResourceLibrary.ElectricityHashcode;
|
||||
base.vessel.GetConnectedResourceTotals(ecHash, out var ecAmount, out _, true);
|
||||
if (ecAmount < 0.1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var m in _modules)
|
||||
{
|
||||
if (m.ModuleIsActive())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void PlayAnimation(Animation anim, string name, float speed = 1f, bool loop = false)
|
||||
{
|
||||
var animState = anim[name];
|
||||
|
||||
if (animState.wrapMode != WrapMode.Loop)
|
||||
{
|
||||
if (speed < 0 && animState.time < Mathf.Epsilon)
|
||||
{
|
||||
animState.time = animState.length;
|
||||
@ -240,8 +339,12 @@ namespace Restock
|
||||
{
|
||||
animState.time = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
animState.speed = speed;
|
||||
animState.wrapMode = loop ? WrapMode.Loop : WrapMode.Once;
|
||||
|
||||
//if (!anim.IsPlaying(name))
|
||||
anim.Play(name);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user