ReStocked/Source/Restock/ModuleRestockISRUAnimation.cs

248 lines
8.4 KiB
C#
Raw Normal View History

2019-08-15 07:09:18 +00:00
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Restock
{
public class ModuleRestockISRUAnimation : PartModule
2019-08-15 07:09:18 +00:00
{
2019-08-17 04:26:01 +00:00
// name of the deploy animation to use
2019-08-15 07:09:18 +00:00
[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 = "";
2019-08-17 04:26:01 +00:00
// speed to run the animation when deploying
[KSPField] public float deploySpeed = 1.0f;
2019-08-17 04:26:01 +00:00
// speed to run the animation when retracting
[KSPField] public float retractSpeed = 1.0f;
// 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); }
}
private enum State
{
Inactive,
Deploying,
Active,
Retracting
}
2019-08-15 07:09:18 +00:00
private Animation DeployAnimation { get; set; }
private Animation RetractAnimation { get; set; }
private Animation ActiveAnimation { get; set; }
private Animation InactiveAnimation { get; set; }
2019-08-15 07:09:18 +00:00
private State CurrentState { get; set; }
2019-08-15 07:09:18 +00:00
private bool _deployAnimationPresent = false;
private bool _retractAnimationPresent = false;
private bool _activeAnimationPresent = false;
private bool _inactiveAnimationPresent = false;
private List<BaseConverter> _modules;
2019-08-15 07:09:18 +00:00
public void Start()
{
_modules = base.part.FindModulesImplementing<BaseConverter>();
_deployAnimationPresent = (deployAnimationName != string.Empty);
_retractAnimationPresent = (retractAnimationName != string.Empty);
_activeAnimationPresent = (activeAnimationName != string.Empty);
_inactiveAnimationPresent = (inactiveAnimationName != string.Empty);
2019-08-15 07:09:18 +00:00
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();
2019-08-15 07:09:18 +00:00
}
public override void OnLoad(ConfigNode node)
{
if (!HighLogic.LoadedSceneIsFlight || base.vessel == null)
{
CurrentState = State.Inactive;
return;
}
if (IsDeployed)
2019-08-15 07:09:18 +00:00
{
DeployEnd();
2019-08-15 07:09:18 +00:00
}
else
{
RetractEnd();
2019-08-15 07:09:18 +00:00
}
}
2019-08-15 07:09:18 +00:00
public void Update()
{
if (!HighLogic.LoadedSceneIsFlight) return;
2019-08-15 07:09:18 +00:00
try
{
if (needsEC && !CheatOptions.InfiniteElectricity)
2019-08-15 07:09:18 +00:00
{
var ecHash = PartResourceLibrary.ElectricityHashcode;
base.vessel.GetConnectedResourceTotals(ecHash, out var ecAmount, out _, true);
if (ecAmount < 0.1)
{
if (IsDeployed) RetractStart();
2019-08-15 07:09:18 +00:00
return;
}
}
int enabledCount = 0;
foreach (var m in _modules)
{
if (m.ModuleIsActive()) enabledCount++;
}
switch (CurrentState)
{
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();
}
2019-08-15 07:09:18 +00:00
}
catch (Exception e)
{
this.LogException("Failed to update animation module", e);
}
}
private void DeployStart()
{
if (_deployAnimationPresent)
{
CurrentState = State.Deploying;
PlayAnimation(DeployAnimation, deployAnimationName, deploySpeed);
}
else
{
DeployEnd();
}
}
private void DeployEnd()
2019-08-15 07:09:18 +00:00
{
CurrentState = State.Active;
if (_activeAnimationPresent)
{
PlayAnimation(ActiveAnimation, activeAnimationName);
}
2019-08-15 07:09:18 +00:00
}
private void RetractStart()
2019-08-15 07:09:18 +00:00
{
if (_retractAnimationPresent)
{
CurrentState = State.Retracting;
PlayAnimation(RetractAnimation, retractAnimationName, retractSpeed);
} else if (_deployAnimationPresent)
{
CurrentState = State.Retracting;
PlayAnimation(DeployAnimation, deployAnimationName, retractSpeed * -1);
}
else
{
RetractEnd();
}
2019-08-15 07:09:18 +00:00
}
private void RetractEnd()
2019-08-15 07:09:18 +00:00
{
CurrentState = State.Inactive;
if (_inactiveAnimationPresent)
2019-08-15 07:09:18 +00:00
{
PlayAnimation(InactiveAnimation, inactiveAnimationName);
}
}
private void PlayAnimation(Animation anim, string name, float speed = 1f)
{
var animState = anim[name];
if (speed < 0 && animState.time < Mathf.Epsilon)
{
animState.time = animState.length;
2019-08-15 07:09:18 +00:00
}
else if (speed > 0 && animState.time > animState.length - Mathf.Epsilon)
{
animState.time = 0.0f;
}
animState.speed = speed;
anim.Play(name);
2019-08-15 07:09:18 +00:00
}
}
}