-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDragonRocketship.cpp
187 lines (170 loc) · 4.95 KB
/
DragonRocketship.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
#include "DragonRocketship.h"
DragonRocketship::DragonRocketship(string n) : Rocketship(n, 'd')
{
cargo = nullptr;
spacecraft = nullptr;
rocket = nullptr;
}
DragonRocketship::~DragonRocketship()
{
if (cargo != nullptr)
{
if (spacecraft != nullptr)
for (int i = 0; i < spacecraft->getCapacity(); i++)
cargo[i] = nullptr;
delete[] cargo;
}
}
void DragonRocketship::Launch(Station *ss)
{
this->countdown();
rocket->turnOn();
this->attachToStation(ss);
this->dropCargo();
}
Rocketship *DragonRocketship::clone()
{
DragonRocketship *temp = new DragonRocketship(this->getName());
temp->attachSpacecraft((this->getSpacecraft())->clone());
temp->attachRocket((this->getRocket())->clone());
if (this->getCargo() != nullptr)
{
vector<Cargo *> c;
for (int i = 0; i < spacecraft->getCapacity() - 1; i++)
{
if (cargo[i] != nullptr)
c.push_back(this->getCargo()[i]->clone());
}
temp->attachCargo(c);
}
return temp;
}
void DragonRocketship::attachSpacecraft(Spacecraft *s)
{
spacecraft = s;
}
void DragonRocketship::attachRocket(FalconRocket *r)
{
rocket = r;
}
void DragonRocketship::attachCargo(vector<Cargo *> cvect)
{
cargo = new Cargo *[spacecraft->getCapacity()];
int toFill = cvect.size();
if (toFill > spacecraft->getCapacity())
{
cout << "There is not enough space on the " << this->getName() << " for all the cargo" << endl;
toFill = spacecraft->getCapacity(); // only fill the available space
}
for (int i = 0; i < toFill; i++)
cargo[i] = cvect[i];
}
void DragonRocketship::dropCargo()
{
Station *station = this->getStation();
if (cargo[0] == nullptr)
cout << "There is no Cargo to unload" << endl;
for (int i = 0; i < spacecraft->getCapacity(); i++)
{
if (cargo[i] != nullptr)
{
station->receiveCargo(cargo[i]);
cargo[i] = nullptr;
}
}
}
int DragonRocketship::getRockets()
{
return rocket->getEngine()->EngineCount();
}
bool DragonRocketship::testFire()
{
return rocket->getEngine()->testFire();
}
bool DragonRocketship::testLoading()
{
return cargo == NULL ? false : true;
}
void DragonRocketship::startLanding(Station *base)
{
Station *s = this->getStation();
cout << "Landing for " << this->getName() << " initialised" << endl;
bool loopBack;
loopBack = false;
string response;
string addOn;
vector<Cargo *> temp;
do
{
loopBack = false;
cout << "Do you want to load " << addOn << "equipment from the space station? (Y/N):";
cin >> response;
if (response == "Y")
{
if (!s->equipment.empty())
{
int index;
int amount;
cout << "Choose an index from the list:" << endl;
s->printEquipment();
cin >> index;
while (!cin.good() || index <= 0)
{
cin.clear();
cin.ignore(15, '\n');
cout << "Please enter a valid index: ";
cin >> index;
}
cout << "How many " << s->equipment.at(index - 1).first->getName() << " do you want to load";
cin >> amount;
while (!cin.good() || amount <= 0)
{
cin.clear();
cin.ignore(15, '\n');
cout << "Please enter a valid amount: ";
cin >> amount;
}
pair<Cargo *, int> p = s->loadEquipment(index - 1, amount);
if (p.second < 0)
cout << "Please enter a valid index." << endl;
else
{
for (int i = 0; i < p.second - 1; i++)
{
temp.push_back(p.first->clone());
}
temp.push_back(p.first);
}
loopBack = true;
addOn = "more ";
}
else
{
cout << "No equipment to load" << endl;
loopBack = false;
}
}
else if (response != "N")
{
cout << "Please input only Y or N." << endl;
loopBack = true;
}
else
loopBack = false;
} while (loopBack);
this->attachCargo(temp);
cout << this->getName() << " begins its journey back to earth" << endl;
this->attachToStation(base);
}
Spacecraft *DragonRocketship::getSpacecraft()
{
return spacecraft;
}
FalconRocket *DragonRocketship::getRocket()
{
return rocket;
}
Cargo **DragonRocketship::getCargo()
{
return cargo;
}