-
Notifications
You must be signed in to change notification settings - Fork 1
/
AircraftState.cs
168 lines (146 loc) · 6.02 KB
/
AircraftState.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using TMPro;
using System.Collections;
using UnityEngine;
using UnityEngine.Rendering;
namespace MiniRealisticAirways
{
public class AircraftState : MonoBehaviour
{
public static bool DisableStateOnTouchedDown(Aircraft aircraft)
{
return aircraft.direction == Aircraft.Direction.Inbound && aircraft.state == Aircraft.State.TouchedDown;
}
public static bool GetAircraftState(Aircraft aircraft, out AircraftState aircraftState)
{
aircraftState = aircraft.GetComponent<AircraftState>();
return aircraftState != null;
}
public static bool GetAircraftStates(Aircraft aircraft, out AircraftAltitude aircraftAltitude,
out AircraftSpeed aircraftSpeed, out AircraftType aircraftType)
{
aircraftAltitude = null;
aircraftSpeed = null;
aircraftType = null;
AircraftState aircraftState;
if (!GetAircraftState(aircraft, out aircraftState))
{
return false;
}
aircraftAltitude = aircraftState.aircraftAltitude_;
aircraftSpeed = aircraftState.aircraftSpeed_;
aircraftType = aircraftState.aircraftType_;
return aircraftAltitude != null && aircraftSpeed != null && aircraftType != null;
}
public void Initialize()
{
if (aircraft_ == null)
{
return;
}
// Initialize states.
aircraftAltitude_ = aircraft_.gameObject.AddComponent<AircraftAltitude>();
aircraftAltitude_.aircraft_ = aircraft_;
aircraftSpeed_ = aircraft_.gameObject.AddComponent<AircraftSpeed>();
aircraftSpeed_.aircraft_ = aircraft_;
aircraftType_ = aircraft_.gameObject.AddComponent<AircraftType>();
aircraftType_.aircraft_ = aircraft_;
}
public IEnumerator DelayDestoryCoroutine()
{
yield return new WaitForSeconds(5f);
aircraft_.ConditionalDestroy();
}
public bool IsAirborne()
{
if (aircraftAltitude_ == null)
{
return false;
}
return aircraftAltitude_.altitude_ > AltitudeLevel.Ground;
}
private void StartText(ref TMP_Text text, float fontSize, float x, float y, float z)
{
GameObject obj = Instantiate(new GameObject("Text"));
text = obj.AddComponent<TextMeshPro>();
text.fontSize = fontSize;
text.horizontalAlignment = HorizontalAlignmentOptions.Left;
text.verticalAlignment = VerticalAlignmentOptions.Top;
text.rectTransform.sizeDelta = new Vector2(2, 1);
obj.transform.SetParent(aircraft_.transform);
obj.transform.localPosition = new Vector3(x, y, z);
// make sorting layer of obj "Text"
SortingGroup sg = obj.AddComponent<SortingGroup>();
sg.sortingLayerName = "Text";
sg.sortingOrder = 1;
}
private void Start()
{
if (aircraft_ == null)
{
return;
}
StartText(ref altitudeText_, 2f, 0.4f, -3.6f, 5f);
StartText(ref speedText_, 2f, 2.5f, -3.6f, 5f);
StartText(ref altitudeLevelText_, 3.5f, 0.2f, -1.3f, 5f);
StartText(ref speedLevelText_, 3.5f, 4f, -3.3f, 5f);
StartText(ref fuelText_, 2.1f, 0.4f, -4.6f, 5f);
StartText(ref weightText_, 2.5f, 3f, -4.5f, 5f);
// Hacking on altitue level's text here.
altitudeLevelText_.transform.localScale = new Vector3(1.5f,
altitudeLevelText_.transform.localScale.y,
altitudeLevelText_.transform.localScale.z);
altitudeLevelText_.transform.rotation = Quaternion.AngleAxis(270, Vector3.back);
}
private void Update()
{
if (aircraft_ == null)
{
Destroy(gameObject);
return;
}
if (altitudeText_ == null || speedText_ == null || altitudeLevelText_ == null ||
speedLevelText_ == null || weightText_ == null || fuelText_ == null)
{
return;
}
if (!Plugin.showText_ || DisableStateOnTouchedDown(aircraft_))
{
altitudeText_.text = "";
speedText_.text = "";
altitudeLevelText_.text = "";
speedLevelText_.text = "";
fuelText_.text = "";
weightText_.text = "";
return;
}
AircraftAltitude aircraftAltitude;
AircraftSpeed aircraftSpeed;
AircraftType aircraftType;
if (!GetAircraftStates(aircraft_, out aircraftAltitude, out aircraftSpeed, out aircraftType))
{
return;
}
if (IsAirborne())
{
altitudeText_.text = "ALT: ";
speedText_.text = "SPD: ";
altitudeLevelText_.text = aircraftAltitude.ToString();
speedLevelText_.text = aircraftSpeed.ToString();
fuelText_.text = aircraftType.GetFuelString();
weightText_.text = aircraftType.weight_.ToString();
}
}
public AircraftAltitude aircraftAltitude_;
public AircraftSpeed aircraftSpeed_;
public AircraftType aircraftType_;
public Aircraft aircraft_;
public PlaceableWaypoint commandingWaypoint_;
public bool weatherAffected_ = false;
private TMP_Text altitudeText_;
private TMP_Text speedText_;
private TMP_Text altitudeLevelText_;
private TMP_Text speedLevelText_;
private TMP_Text fuelText_;
private TMP_Text weightText_;
}
}