1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Noe Pedrazzini |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "ShipPart.h" |
---|
30 | |
---|
31 | #include <algorithm> |
---|
32 | |
---|
33 | #include "core/CoreIncludes.h" |
---|
34 | #include "core/GameMode.h" |
---|
35 | #include "core/XMLPort.h" |
---|
36 | #include "network/NetworkFunction.h" |
---|
37 | #include "Item.h" |
---|
38 | #include "worldentities/pawns/Pawn.h" |
---|
39 | #include "worldentities/pawns/ModularSpaceShip.h" |
---|
40 | #include "gametypes/Gametype.h" |
---|
41 | #include "worldentities/StaticEntity.h" |
---|
42 | #include "items/PartDestructionEvent.h" |
---|
43 | #include "chat/ChatManager.h" |
---|
44 | |
---|
45 | |
---|
46 | namespace orxonox |
---|
47 | { |
---|
48 | RegisterClass(ShipPart); |
---|
49 | |
---|
50 | ShipPart::ShipPart(Context* context) |
---|
51 | : Item(context), parent_(nullptr) |
---|
52 | { |
---|
53 | RegisterObject(ShipPart); |
---|
54 | this->eventExecution_ = true; |
---|
55 | this->healthMem_ = 100; |
---|
56 | } |
---|
57 | |
---|
58 | ShipPart::~ShipPart() |
---|
59 | { |
---|
60 | if (this->parent_) |
---|
61 | { |
---|
62 | // Remove this ShipPart from the parent. |
---|
63 | this->parent_->removeShipPart(this); |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | void ShipPart::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
68 | { |
---|
69 | SUPER(ShipPart, XMLPort, xmlelement, mode); |
---|
70 | |
---|
71 | XMLPortParam(ShipPart, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100); |
---|
72 | XMLPortParam(ShipPart, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200); |
---|
73 | XMLPortParam(ShipPart, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100); |
---|
74 | |
---|
75 | XMLPortParam(ShipPart, "damageabsorption", setDamageAbsorption, getDamageAbsorption, xmlelement, mode).defaultValues(0.5); |
---|
76 | |
---|
77 | XMLPortParamTemplate(ShipPart, "explosionposition", setExplosionPosition, getExplosionPosition, xmlelement, mode, Vector3); |
---|
78 | |
---|
79 | XMLPortObject(ShipPart, PartDestructionEvent, "destructionevents", addDestructionEvent, getDestructionEvent, xmlelement, mode); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | @brief |
---|
84 | Is called when the ShipPart dies. |
---|
85 | Executes PartDestructionEvents. |
---|
86 | Tells the ModularSpaceShip to remove this ShipPart. |
---|
87 | */ |
---|
88 | void ShipPart::death() |
---|
89 | { |
---|
90 | this->explode(); |
---|
91 | |
---|
92 | if(eventExecution_) |
---|
93 | { |
---|
94 | // Execute all destruction events |
---|
95 | for (unsigned int i = 0; i < this->eventList_.size(); i++) |
---|
96 | { |
---|
97 | this->getDestructionEvent(i)->execute(); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | this->destroyLater(); |
---|
102 | } |
---|
103 | |
---|
104 | void ShipPart::explode() |
---|
105 | { |
---|
106 | // BigExplosion* chunk = new BigExplosion(this->getContext()); |
---|
107 | // chunk->setPosition(this->parent_->getPosition() + this->parent_->getOrientation() * (this->explosionPosition_)); |
---|
108 | // //chunk->setPosition(this->parent_->getPosition() + this->parent_->getOrientation() * Vector3(this->entityList_[0]->getLocalInertia())); |
---|
109 | // chunk->setVelocity(this->parent_->getVelocity()); |
---|
110 | |
---|
111 | // this->explosionSound_->setPosition(this->parent_->getPosition()); |
---|
112 | // this->explosionSound_->play(); |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | @brief |
---|
117 | Add a StaticEntity to the ShipPart. |
---|
118 | @param engine |
---|
119 | A pointer to the StaticEntity to be added. |
---|
120 | */ |
---|
121 | void ShipPart::addEntity(StaticEntity* entity) |
---|
122 | { |
---|
123 | OrxAssert(entity != nullptr, "The Entity cannot be nullptr."); |
---|
124 | this->entityList_.push_back(entity); |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | @brief |
---|
129 | Get the i-th StaticEntity of the ShipPart. |
---|
130 | @return |
---|
131 | Returns a pointer to the i-the StaticEntity. nullptr if there is no StaticEntity with that index. |
---|
132 | */ |
---|
133 | StaticEntity* ShipPart::getEntity(unsigned int index) |
---|
134 | { |
---|
135 | if(this->entityList_.size() >= index) |
---|
136 | return nullptr; |
---|
137 | else |
---|
138 | return this->entityList_[index]; |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | @brief |
---|
143 | Check whether the ShipPart has a particular Entity. |
---|
144 | @param search |
---|
145 | A pointer to the Entity to be checked. |
---|
146 | */ |
---|
147 | bool ShipPart::hasEntity(StaticEntity* search) const |
---|
148 | { |
---|
149 | for(StaticEntity* entity : this->entityList_) |
---|
150 | { |
---|
151 | if(entity == search) |
---|
152 | return true; |
---|
153 | } |
---|
154 | return false; |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | @brief |
---|
159 | Add a PartDestructionEvent to the ShipPart. |
---|
160 | @param engine |
---|
161 | A pointer to the PartDestructionEvent to be added. |
---|
162 | */ |
---|
163 | void ShipPart::addDestructionEvent(PartDestructionEvent* event) |
---|
164 | { |
---|
165 | OrxAssert(event != nullptr, "The PartDestructionEvent cannot be nullptr."); |
---|
166 | event->setParent(this); |
---|
167 | this->eventList_.push_back(event); |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | @brief |
---|
172 | Get the i-th PartDestructionEvent of the ShipPart. |
---|
173 | @return |
---|
174 | Returns a pointer to the i-the PartDestructionEvent. nullptr if there is no PartDestructionEvent with that index. |
---|
175 | */ |
---|
176 | PartDestructionEvent* ShipPart::getDestructionEvent(unsigned int index) |
---|
177 | { |
---|
178 | if(this->eventList_.size() <= index) |
---|
179 | return nullptr; |
---|
180 | else |
---|
181 | return this->eventList_[index]; |
---|
182 | } |
---|
183 | |
---|
184 | void ShipPart::setParent(ModularSpaceShip* ship) |
---|
185 | { |
---|
186 | this->parent_ = ship; |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | @brief |
---|
191 | Handles a received hit. |
---|
192 | */ |
---|
193 | void ShipPart::handleHit(float damage, float healthdamage, float shielddamage, Pawn* originator) |
---|
194 | { |
---|
195 | if (parent_->getGametype() && parent_->getGametype()->allowPawnDamage(parent_, originator)) |
---|
196 | { |
---|
197 | if (shielddamage >= parent_->getShieldHealth()) |
---|
198 | { |
---|
199 | parent_->setShieldHealth(0); |
---|
200 | this->setHealth(this->health_ - (healthdamage + damage) * this->damageAbsorption_); |
---|
201 | parent_->setHealth(parent_->getHealth() - (healthdamage + damage) * (1 - this->damageAbsorption_)); |
---|
202 | } |
---|
203 | else |
---|
204 | { |
---|
205 | parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage); |
---|
206 | |
---|
207 | // remove remaining shieldAbsorpton-Part of damage from shield |
---|
208 | shielddamage = damage * parent_->getShieldAbsorption(); |
---|
209 | shielddamage = std::min(parent_->getShieldHealth(),shielddamage); |
---|
210 | parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage); |
---|
211 | |
---|
212 | // set remaining damage to health |
---|
213 | this->setHealth(this->health_ - ((damage - shielddamage) - healthdamage) * this->damageAbsorption_); |
---|
214 | parent_->setHealth(parent_->getHealth() - ((damage - shielddamage) - healthdamage) * (1- this->damageAbsorption_)); |
---|
215 | } |
---|
216 | } |
---|
217 | if (this->health_ < 0) |
---|
218 | this->death(); |
---|
219 | |
---|
220 | // (Ugly) Chatoutput of health, until a GUI for modularspaceships-shipparts is implemented. |
---|
221 | if ((this->health_ < 0.2 * this->maxHealth_) && (this->healthMem_ == 40)) |
---|
222 | { |
---|
223 | this->healthMem_ = 20; |
---|
224 | ChatManager::message("ShipPart " + this->getName() + " remaining health is 20%!"); |
---|
225 | return; |
---|
226 | } |
---|
227 | if ((this->health_ < 0.4 * this->maxHealth_) && (this->healthMem_ == 60)) |
---|
228 | { |
---|
229 | this->healthMem_ = 40; |
---|
230 | ChatManager::message("ShipPart " + this->getName() + " remaining health is 40%!"); |
---|
231 | return; |
---|
232 | } |
---|
233 | if ((this->health_ < 0.6 * this->maxHealth_) && (this->healthMem_ == 80)) |
---|
234 | { |
---|
235 | this->healthMem_ = 60; |
---|
236 | ChatManager::message("ShipPart " + this->getName() + " remaining health is 60%!"); |
---|
237 | return; |
---|
238 | } |
---|
239 | if ((this->health_ < 0.8 * this->maxHealth_) && (this->healthMem_ == 100)) |
---|
240 | { |
---|
241 | this->healthMem_ = 80; |
---|
242 | ChatManager::message("ShipPart " + this->getName() + " remaining health is 80%!"); |
---|
243 | return; |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | |
---|
248 | /** |
---|
249 | @brief |
---|
250 | Adds the ShipPart to the input SpaceShip. |
---|
251 | @param ship |
---|
252 | A pointer to the SpaceShip to which the ShipPart is added. |
---|
253 | */ |
---|
254 | /*void ShipPart::addToSpaceShip(ModularSpaceShip* ship) |
---|
255 | { |
---|
256 | this->parent_ = ship; |
---|
257 | |
---|
258 | if (ship) |
---|
259 | { |
---|
260 | this->parentID_ = ship->getObjectID(); |
---|
261 | if (!ship->hasShipPart(this)) |
---|
262 | ship->addShipPart(this); |
---|
263 | } |
---|
264 | }*/ |
---|
265 | |
---|
266 | } |
---|