-
Notifications
You must be signed in to change notification settings - Fork 32
/
KethaneDrillAnimator.cs
108 lines (95 loc) · 3.41 KB
/
KethaneDrillAnimator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Linq;
using UnityEngine;
namespace Kethane.PartModules
{
public class KethaneDrillAnimator : PartModule, IExtractorAnimator
{
[KSPField(isPersistant = false)]
public string DeployAnimation;
[KSPField(isPersistant = false)]
public string DrillAnimation;
[KSPField(isPersistant = true)]
public string State;
private AnimationState[] deployStates;
private AnimationState[] drillStates;
public override void OnStart(PartModule.StartState state)
{
deployStates = this.part.SetUpAnimation(DeployAnimation);
drillStates = this.part.SetUpAnimation(DrillAnimation);
if (CurrentState == ExtractorState.Deploying) { CurrentState = ExtractorState.Retracted; }
else if (CurrentState == ExtractorState.Retracting) { CurrentState = ExtractorState.Deployed; }
if (CurrentState == ExtractorState.Deployed)
{
foreach (var deployState in deployStates)
{
deployState.normalizedTime = 1;
}
}
foreach (var drillState in drillStates)
{
drillState.enabled = false;
drillState.wrapMode = WrapMode.Loop;
}
}
public ExtractorState CurrentState
{
get
{
try
{
return (ExtractorState)Enum.Parse(typeof(ExtractorState), State);
}
catch
{
CurrentState = ExtractorState.Retracted;
return CurrentState;
}
}
private set
{
State = Enum.GetName(typeof(ExtractorState), value);
}
}
public void Deploy()
{
if (CurrentState != ExtractorState.Retracted) { return; }
CurrentState = ExtractorState.Deploying;
}
public void Retract()
{
if (CurrentState != ExtractorState.Deployed) { return; }
CurrentState = ExtractorState.Retracting;
foreach (var state in drillStates)
{
state.enabled = false;
state.normalizedTime = 0;
state.speed = 0;
}
}
public void Update()
{
if (CurrentState == ExtractorState.Deploying && deployStates.All(s => s.normalizedTime >= 1))
{
CurrentState = ExtractorState.Deployed;
foreach (var state in drillStates)
{
state.enabled = true;
state.normalizedTime = 0;
state.speed = 1;
}
}
else if (CurrentState == ExtractorState.Retracting && deployStates.All(s => s.normalizedTime <= 0))
{
CurrentState = ExtractorState.Retracted;
}
foreach (var deployState in deployStates)
{
var time = Mathf.Clamp01(deployState.normalizedTime);
deployState.normalizedTime = time;
var speed = HighLogic.LoadedSceneIsEditor ? 1 - 10 * (time - 1) * time : 1;
deployState.speed = (CurrentState == ExtractorState.Deploying || CurrentState == ExtractorState.Deployed) ? speed : -speed;
}
}
}
}