/
ModulePropellerPitchCtrl.cs
93 lines (84 loc) · 3.09 KB
/
ModulePropellerPitchCtrl.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using UnityEngine;
using System.Text.RegularExpressions;
namespace PropellerPithAndRotorTorque
{
public class ModulePropellerPitchCtrl : PartModule
{
[UI_FloatRange(stepIncrement = 0.5f, maxValue = 45f, minValue = 0f, affectSymCounterparts = UI_Scene.All)]
[KSPAxisField(incrementalSpeed = 100f, guiFormat = "F1", isPersistant = true, axisMode = KSPAxisMode.Incremental, guiActive = true, guiActiveEditor = true, guiName = "Target AOA")]
public float fanTargetAOA = 5f;
public ModuleControlSurface ModuleCS;
private PPARTMgr mgr;
[KSPField(isPersistant = true)]
private bool Enabled = false;
public void Init(PPARTMgr m)
{
if (m == null) return;
ModuleCS = part.FindModuleImplementing<ModuleControlSurface>();
if (ModuleCS == null) { Destroy(this); return; }
var l = part.FindModulesImplementing<ModulePropellerPitchCtrl>();
for (int i = 1; i < l.Count; Destroy(l[i++])) ;//Destroy duplicate modules (idk why there's duplicate when I play modded game)
mgr = m;
mgr.RegisterModuleCS(ModuleCS, fanTargetAOA, this);
mgr.PitchCtrlEnabled = Enabled;
}
/// <summary>
/// This is called on revert flight, and would cause problem if I initialize plugin here
/// </summary>
/// <param name="node"></param>
public override void OnLoad(ConfigNode node)
{
//if (vessel != null)
// PPARTMgr.OnModuleLoading(vessel);//This won't be reached when lunching a vessel
}
public override void OnSave(ConfigNode node)
{
base.OnSave(node);
if (mgr != null)
{
Enabled = mgr.PitchCtrlEnabled;
}
}
public override void OnStart(StartState state)
{
base.OnStart(state);
base.Fields["fanTargetAOA"].OnValueModified += OnTargetAOAChanged;
}
protected void OnTargetAOAChanged(object field)
{
if (mgr != null)
{
if (fanTargetAOA != mgr.targetAOA)
{
mgr.oldTargetAOA = mgr.targetAOA;
mgr.targetAOA = fanTargetAOA;
}
}
}
public void FixedUpdate()
{
if (vessel != null)
if (!PPARTMgr.IsPPARTMgrInitialized(vessel))
{
Debug.Log("[PropellerPithAndRotorTorque]: Initializing plugin from part");
PPARTMgr.OnModuleLoading(vessel);
}
if (mgr == null) return;
if (fanTargetAOA != mgr.targetAOA)
{
Fields["fanTargetAOA"].SetValue(mgr.targetAOA, this);
}
}
void OnDestroy()
{
base.Fields["fanTargetAOA"].OnValueModified -= OnTargetAOAChanged;
}
}
}