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_(NULL) |
---|
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 != NULL, "The Entity cannot be NULL."); |
---|
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. NULL 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 NULL; |
---|
137 | else |
---|
138 | return this->entityList_[index]; |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | @brief |
---|
143 | Check whether the ShipPart has a particular Entity. |
---|
144 | @param engine |
---|
145 | A pointer to the Entity to be checked. |
---|
146 | */ |
---|
147 | bool ShipPart::hasEntity(StaticEntity* entity) const |
---|
148 | { |
---|
149 | for(unsigned int i = 0; i < this->entityList_.size(); i++) |
---|
150 | { |
---|
151 | if(this->entityList_[i] == entity) |
---|
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 != NULL, "The PartDestructionEvent cannot be NULL."); |
---|
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. NULL 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 NULL; |
---|
180 | else |
---|
181 | return this->eventList_[index]; |
---|
182 | } |
---|
183 | |
---|
184 | void ShipPart::setDamageAbsorption(float value) |
---|
185 | { |
---|
186 | this->damageAbsorption_ = value; |
---|
187 | } |
---|
188 | |
---|
189 | void ShipPart::setParent(ModularSpaceShip* ship) |
---|
190 | { |
---|
191 | this->parent_ = ship; |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | @brief |
---|
196 | Sets the health of the ShipPart. |
---|
197 | */ |
---|
198 | void ShipPart::setHealth(float health) |
---|
199 | { |
---|
200 | this->health_ = health; |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | @brief |
---|
205 | Handles a received hit. |
---|
206 | */ |
---|
207 | void ShipPart::handleHit(float damage, float healthdamage, float shielddamage, Pawn* originator) |
---|
208 | { |
---|
209 | if (parent_->getGametype() && parent_->getGametype()->allowPawnDamage(parent_, originator)) |
---|
210 | { |
---|
211 | if (shielddamage >= parent_->getShieldHealth()) |
---|
212 | { |
---|
213 | parent_->setShieldHealth(0); |
---|
214 | this->setHealth(this->health_ - (healthdamage + damage) * this->damageAbsorption_); |
---|
215 | parent_->setHealth(parent_->getHealth() - (healthdamage + damage) * (1 - this->damageAbsorption_)); |
---|
216 | } |
---|
217 | else |
---|
218 | { |
---|
219 | parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage); |
---|
220 | |
---|
221 | // remove remaining shieldAbsorpton-Part of damage from shield |
---|
222 | shielddamage = damage * parent_->getShieldAbsorption(); |
---|
223 | shielddamage = std::min(parent_->getShieldHealth(),shielddamage); |
---|
224 | parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage); |
---|
225 | |
---|
226 | // set remaining damage to health |
---|
227 | this->setHealth(this->health_ - ((damage - shielddamage) - healthdamage) * this->damageAbsorption_); |
---|
228 | parent_->setHealth(parent_->getHealth() - ((damage - shielddamage) - healthdamage) * (1- this->damageAbsorption_)); |
---|
229 | } |
---|
230 | } |
---|
231 | if (this->health_ < 0) |
---|
232 | this->death(); |
---|
233 | |
---|
234 | // (Ugly) Chatoutput of health, until a GUI for modularspaceships-shipparts is implemented. |
---|
235 | if ((this->health_ < 0.2 * this->maxHealth_) && (this->healthMem_ == 40)) |
---|
236 | { |
---|
237 | this->healthMem_ = 20; |
---|
238 | ChatManager::message("ShipPart " + this->getName() + " remaining health is 20%!"); |
---|
239 | return; |
---|
240 | } |
---|
241 | if ((this->health_ < 0.4 * this->maxHealth_) && (this->healthMem_ == 60)) |
---|
242 | { |
---|
243 | this->healthMem_ = 40; |
---|
244 | ChatManager::message("ShipPart " + this->getName() + " remaining health is 40%!"); |
---|
245 | return; |
---|
246 | } |
---|
247 | if ((this->health_ < 0.6 * this->maxHealth_) && (this->healthMem_ == 80)) |
---|
248 | { |
---|
249 | this->healthMem_ = 60; |
---|
250 | ChatManager::message("ShipPart " + this->getName() + " remaining health is 60%!"); |
---|
251 | return; |
---|
252 | } |
---|
253 | if ((this->health_ < 0.8 * this->maxHealth_) && (this->healthMem_ == 100)) |
---|
254 | { |
---|
255 | this->healthMem_ = 80; |
---|
256 | ChatManager::message("ShipPart " + this->getName() + " remaining health is 80%!"); |
---|
257 | return; |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | /** |
---|
263 | @brief |
---|
264 | Adds the ShipPart to the input SpaceShip. |
---|
265 | @param ship |
---|
266 | A pointer to the SpaceShip to which the ShipPart is added. |
---|
267 | */ |
---|
268 | /*void ShipPart::addToSpaceShip(ModularSpaceShip* ship) |
---|
269 | { |
---|
270 | this->parent_ = ship; |
---|
271 | |
---|
272 | if (ship) |
---|
273 | { |
---|
274 | this->parentID_ = ship->getObjectID(); |
---|
275 | if (!ship->hasShipPart(this)) |
---|
276 | ship->addShipPart(this); |
---|
277 | } |
---|
278 | }*/ |
---|
279 | |
---|
280 | } |
---|