-
Notifications
You must be signed in to change notification settings - Fork 4
/
ModuleAnimation2Value.cs
91 lines (79 loc) · 2.91 KB
/
ModuleAnimation2Value.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
using System;
using System.Reflection;
using UnityEngine;
using KSP;
namespace ModuleAnimation2Value
{
public class ModuleAnimation2Value : PartModule
{
[KSPField(isPersistant = false)]
public string valueModule = "";
[KSPField(isPersistant = false)]
public string valueName = "";
[KSPField(isPersistant = false)]
public string animationName = "";
[KSPField(isPersistant = false)]
public FloatCurve valueCurve = new FloatCurve();
protected Animation[] anims;
protected PartModule module;
protected Type moduleType;
protected FieldInfo field;
protected PropertyInfo property;
public override void OnStart(PartModule.StartState state)
{
if (state != PartModule.StartState.Editor)
{
anims = part.FindModelAnimators(animationName);
if ((anims == null) || (anims.Length == 0))
{
print("ModuleAnimation2Value - animation not found: " + animationName);
}
moduleType = part.GetType();
if (valueModule != "")
{
if (part.Modules.Contains(valueModule))
{
module = part.Modules[valueModule];
moduleType = module.GetType();
}
else
{
print("ModuleAnimation2Value - module not found: " + valueModule);
}
}
field = moduleType.GetField(valueName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
if (field == null)
{
property = moduleType.GetProperty(valueName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
if (property == null)
{
print("ModuleAnimation2Value - field/property not found: " + valueName);
}
}
}
base.OnStart(state);
}
public void Update()
{
if (FlightGlobals.ready)
{
if ((anims != null) && (anims.Length > 0) && ((field != null) || (property != null)))
{
object target = part;
if (module != null)
{
target = module;
}
if (field != null)
{
field.SetValue(target, valueCurve.Evaluate(anims[0][animationName].normalizedTime));
}
else
{
property.SetValue(target, valueCurve.Evaluate(anims[0][animationName].normalizedTime), null);
}
}
}
}
}
}