-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpaceShip.cpp
282 lines (243 loc) · 6.37 KB
/
SpaceShip.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "SpaceShip.h"
#include "ShipModelInterface.h"
#include "ShipSystemBase.h"
#include "BasicShipModel.h"
#include "GameDefinitions.h"
#include "BusDataTypes.h"
#include "DataBus.h"
#include "DataBusConnection.h"
#include "StandardBusCommands.h"
#include "HotasSystemInterface.h"
#include "EngineSystemInterface.h"
#include "HudSystemInterface.h"
#include "HullSystemInterface.h"
#include "ShipSystemsManager.h"
#include "StationInterface.h"
#include "SolarSystemInterface.h"
#include "ObjectManager.h"
#include "Display.h"
#include <assert.h>
// Contact interface methods
void SpaceShip::ProcessImpulse(float impulse) {
hull_->ApplyImpact(impulse);
}
void SpaceShip::BeginContact(ContactInterface* object) {
if (object->Type() != 'S') return;
StationInterface *s = static_cast<StationInterface *>(object);
if (s != 0 && ship_anchored_ == false) {
station_iface_ = s;
}
}
void SpaceShip::EndContact(ContactInterface* object) {
StationInterface *s = static_cast<StationInterface *>(object);
if (s != 0 && ship_anchored_ == false) {
station_iface_ = 0;
}
}
char SpaceShip::Type() {
return 's';
}
SpaceShip::SpaceShip()
: model_(0)
, data_bus_(0)
, hud_(0)
, hotas_(0)
, engine_(0)
, radar_(0)
, jump_drive_(0)
, hull_(0)
, landing_gear_state_(0)
, station_iface_(0)
, ship_anchored_(false)
, destroyed_(false)
, map_x_(-86.5821)
, map_y_(-24.8058)
{
model_ = std::make_shared<BasicShipModel>();
OBJMGR.Set("model", model_);
data_bus_ = new DataBus();
hud_ = SYSTEMSMGR.getHudSystem();
hotas_ = SYSTEMSMGR.getHotasSystem();
engine_ = SYSTEMSMGR.getEngineSystem();
radar_ = SYSTEMSMGR.getRadarSystem();
jump_drive_ = SYSTEMSMGR.getJumpDriveSystem(map_x_, map_y_);
hull_ = SYSTEMSMGR.getHullSystem();
sensor_ = SYSTEMSMGR.getSensorSystem();
using std::placeholders::_1;
hull_->SetDestructionCallback(std::bind(&SpaceShip::OnDestroy, this));
}
SpaceShip::~SpaceShip() {
SYSTEMSMGR.Destroy();
delete data_bus_;
}
void SpaceShip::SetAngle(double angle) {
model_->SetAngle(angle);
}
double SpaceShip::GetAngle() {
return model_->GetAngle();
}
void SpaceShip::SetPosition(double x, double y) {
model_->SetPosition(x, y);
}
b2Vec2 const & SpaceShip::GetPosition() {
return model_->GetPosition();
}
void SpaceShip::SetMapPosition(const double & x, const double & y) {
map_x_ = x;
map_y_ = y;
}
// DEBUG
extern double dbg_map_x_;
extern double dbg_map_y_;
void SpaceShip::GetMapPosition(double & x, double & y) {
map_x_ = dbg_map_x_;
map_y_ = dbg_map_y_;
x = map_x_;
y = map_y_;
}
double SpaceShip::GetSpeed() {
return model_->GetSpeed();
}
double SpaceShip::Mass() {
double total_mass = 0.0;
if (engine_ != 0) {
total_mass = engine_->FuelMass();
}
total_mass += model_->GetMass();
return total_mass;
}
void SpaceShip::OnDestroy() {
destroyed_ = true;
hud_->Disable();
hotas_->Disable();
}
void SpaceShip::Init(b2World * world) {
// Init model
model_->Init(world, this);
sensor_->Init(data_bus_);
// Init engine system.
engine_->Init(data_bus_);
engine_->Mount( model_->GetEngineMount() );
radar_->Init(data_bus_);
jump_drive_->Init(data_bus_);
hud_->Init(data_bus_);
hotas_->Init(data_bus_);
hull_->Init(data_bus_);
}
void SpaceShip::Update(double delta_time) {
model_->Update(delta_time);
sensor_->Update(delta_time);
engine_->Update(delta_time);
radar_->Update(delta_time);
jump_drive_->Update(delta_time);
}
void SpaceShip::Render() {
model_->Render();
DISPLAY.UiMode();
hud_->Render();
if (station_iface_ != 0 && ship_anchored_) {
station_iface_->RenderUi();
}
}
void SpaceShip::RefuelRequest() {
if (station_iface_ != 0 && ship_anchored_) {
station_iface_->Refuel(engine_);
}
}
void SpaceShip::RepairRequest() {
if (station_iface_ != 0 && ship_anchored_) {
station_iface_->Repair(hull_);
}
}
void SpaceShip::HotasInput(int key, bool action) {
assert(hotas_ != 0);
switch(key) {
case GLFW_KEY_W: // main thruster
if (action == true) {
// enable thruster.
hotas_->SetThrottle(1.0);
}
else {
// disable thruster.
hotas_->SetThrottle(0.0);
}
break;
case GLFW_KEY_A: // right thruster
if (action == true) {
hotas_->SetSteering(-1.0);
}
else {
hotas_->SetSteering(0.0);
}
break;
case GLFW_KEY_D: // left thruster
if (action == true) {
hotas_->SetSteering(1.0);
}
else {
hotas_->SetSteering(0.0);
}
break;
case GLFW_KEY_S: // stop rotation
if (action == true) {
hotas_->Stabilize();
}
break;
case GLFW_KEY_J: // send jump request
if (action == true) {
hotas_->SendCommand(cmd__JUMP);
}
break;
case GLFW_KEY_F:
if (action == true) {
RefuelRequest();
}
break;
case GLFW_KEY_R:
if (action == true) {
RepairRequest();
}
break;
case GLFW_KEY_H:
if (action == true) {
if (ship_anchored_ == false) {
// Dock ship to station
ship_anchored_ = model_->Dock(station_iface_);
} else {
// Undock
ship_anchored_ = model_->Undock(station_iface_);
}
}
break;
case GLFW_KEY_G:
// TODO : (#147) pass landing gear command to landing gear 'system' through hotas.
// hotas_->ToggleLandingGear();
if (action) {
if (landing_gear_state_) {
landing_gear_state_ = 0;
} else {
landing_gear_state_ = 1;
}
// Control Landing gear on model.
model_->LandingGear(landing_gear_state_);
}
break;
#ifdef DISCONNECT_TEST
case GLFW_KEY_1: // hud
hud_->Disconnect();
break;
case GLFW_KEY_2: // ship
data_bus_->Disconnect("ship", bus_connection_);
break;
case GLFW_KEY_3: // engine
engine_->Disconnect();
break;
case GLFW_KEY_4: // hull
hull_->Disconnect();
break;
case GLFW_KEY_5: // radar
radar_->Disconnect();
break;
#endif // DISCONNECT_TEST
}
}