/
DoorManager.cs
180 lines (171 loc) · 5.92 KB
/
DoorManager.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
169
170
171
172
173
174
175
176
177
178
179
180
using Sandbox.Game.EntityComponents;
using Sandbox.ModAPI.Ingame;
using Sandbox.ModAPI.Interfaces;
using SpaceEngineers.Game.ModAPI.Ingame;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System;
using VRage.Collections;
using VRage.Game.Components;
using VRage.Game.GUI.TextPanel;
using VRage.Game.ModAPI.Ingame.Utilities;
using VRage.Game.ModAPI.Ingame;
using VRage.Game.ObjectBuilders.Definitions;
using VRage.Game;
using VRage;
using VRageMath;
namespace IngameScript
{
partial class Program
{
public class DoorManager
{
int warningTime;
IMyBlockGroup hangarDoors;
List<IMyAirtightHangarDoor> doorList;
bool startDoorMove = false;
bool startPressurize = false;
bool startDepressurize = false;
State currentState = State.Idle;
DoorStatus currentDoorState;
SpeakerManager speakers;
AirManager airTanks;
[Flags] enum State
{
Idle = 0,
Warning = 1,
Depressurizing = 2,
Opening = 4,
Closing = 8
}
public DoorManager(SpeakerManager inSpeakers, AirManager inAirTanks)
{
hangarDoors = GridTerminalSystem.GetBlockGroupWithName(hangarDoorGroupName);
if (hangarDoors == null)
{
Echo("Hangar Doors group not found");
throw new ArgumentException("Cannot find Door group: " + hangarDoorGroupName);
}
doorList = new List<IMyAirtightHangarDoor>();
speakers = inSpeakers;
airTanks = inAirTanks;
}
public void CloseDoors()
{
if(currentState == State.Depressurizing)
{
airTanks.PressurizeBay();
}
else if (currentState != State.Warning)
{
startDoorMove = true;
}
else if (currentState == State.Warning)
{
speakers.StopWarning(WarningSource.DOOR);
}
currentState = State.Closing;
CheckState();
}
public void OpenDoors()
{
if (currentState == State.Idle)
{
currentState = State.Warning;
}
else if (currentState == State.Closing)
{
currentState = State.Opening;
startDoorMove = true;
}
CheckState();
}
public void ToggleDoors()
{
if (currentState.HasFlag(State.Warning | State.Depressurizing | State.Opening) || ((currentState == State.Idle) && CheckDoorState(DoorStatus.Open)))
{
CloseDoors();
}
else if (currentState.HasFlag(State.Closing) || ((currentState == State.Idle) && CheckDoorState(DoorStatus.Closed)))
{
OpenDoors();
}
}
public void CheckState()
{
bool processCompleted = false;
switch (currentState)
{
case State.Warning:
Echo("Warning");
speakers.StartWarning(WarningSource.DOOR, warningTime);
break;
case State.Depressurizing:
Echo("Depressurizing");
airTanks.DepressurizeBay(out processCompleted);
if (processCompleted)
{
currentState = State.Opening;
startDoorMove = true;
}
break;
case State.Opening:
Echo("Opening Doors");
ChangeBayDoors(DoorStatus.Open);
break;
case State.Closing:
Echo("Closing Doors");
startDoorMove = true;
break;
default:
break;
}
}
public void ChangeBayDoors(DoorStatus state)
{
string actionString;
if (state == DoorStatus.Closed)
{
actionString = "Open_Off";
}
else if (state == DoorStatus.Open)
{
actionString = "Open_On";
}
else
{
Echo("Invalid End Door State");
return;
}
hangarDoors.GetBlocksOfType<IMyAirtightHangarDoor>(doorList);
if (startDoorMove)
{
ITerminalAction action;
action = doorList.First().GetActionWithName(actionString);
doorList.ForEach((door) => action.Apply(door));
startDoorMove = false;
}
if (CheckDoorState(state))
{
currentState = State.Idle;
currentDoorState = state;
Echo("Finished");
}
}
public bool CheckDoorState(DoorStatus desiredState)
{
bool returnState = true;
foreach (IMyAirtightHangarDoor door in doorList)
{
if (door.Status != desiredState)
{
returnState = false;
}
}
return returnState
}
}
}
}