[10019] | 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" |
---|
[10023] | 39 | #include "worldentities/pawns/ModularSpaceShip.h" |
---|
[10019] | 40 | #include "gametypes/Gametype.h" |
---|
| 41 | #include "worldentities/StaticEntity.h" |
---|
[10052] | 42 | #include "items/PartDestructionEvent.h" |
---|
[10067] | 43 | #include "worldentities/BigExplosion.h" |
---|
| 44 | #include "chat/ChatManager.h" |
---|
[10019] | 45 | |
---|
| 46 | |
---|
| 47 | namespace orxonox |
---|
| 48 | { |
---|
| 49 | RegisterClass(ShipPart); |
---|
| 50 | |
---|
| 51 | ShipPart::ShipPart(Context* context) |
---|
[10624] | 52 | : Item(context), parent_(NULL) |
---|
[10019] | 53 | { |
---|
| 54 | RegisterObject(ShipPart); |
---|
[10624] | 55 | this->eventExecution_ = true; |
---|
[10071] | 56 | this->healthMem_ = 100; |
---|
[10019] | 57 | } |
---|
| 58 | |
---|
| 59 | ShipPart::~ShipPart() |
---|
| 60 | { |
---|
[10624] | 61 | if (this->parent_) |
---|
| 62 | { |
---|
| 63 | // Remove this ShipPart from the parent. |
---|
| 64 | this->parent_->removeShipPart(this); |
---|
| 65 | } |
---|
[10019] | 66 | } |
---|
| 67 | |
---|
[10023] | 68 | void ShipPart::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 69 | { |
---|
| 70 | SUPER(ShipPart, XMLPort, xmlelement, mode); |
---|
[10019] | 71 | |
---|
[10023] | 72 | XMLPortParam(ShipPart, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100); |
---|
| 73 | XMLPortParam(ShipPart, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200); |
---|
| 74 | XMLPortParam(ShipPart, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100); |
---|
| 75 | |
---|
| 76 | XMLPortParam(ShipPart, "damageabsorption", setDamageAbsorption, getDamageAbsorption, xmlelement, mode).defaultValues(0.5); |
---|
| 77 | |
---|
[10067] | 78 | XMLPortParamTemplate(ShipPart, "explosionposition", setExplosionPosition, getExplosionPosition, xmlelement, mode, Vector3); |
---|
| 79 | |
---|
[10052] | 80 | XMLPortObject(ShipPart, PartDestructionEvent, "destructionevents", addDestructionEvent, getDestructionEvent, xmlelement, mode); |
---|
[10023] | 81 | } |
---|
| 82 | |
---|
[10055] | 83 | /** |
---|
| 84 | @brief |
---|
| 85 | Is called when the ShipPart dies. |
---|
| 86 | Executes PartDestructionEvents. |
---|
| 87 | Tells the ModularSpaceShip to remove this ShipPart. |
---|
| 88 | */ |
---|
[10023] | 89 | void ShipPart::death() |
---|
| 90 | { |
---|
[10067] | 91 | this->explode(); |
---|
[10053] | 92 | |
---|
[10058] | 93 | if(eventExecution_) |
---|
[10053] | 94 | { |
---|
[10058] | 95 | // Execute all destruction events |
---|
| 96 | for (unsigned int i = 0; i < this->eventList_.size(); i++) |
---|
| 97 | { |
---|
| 98 | this->getDestructionEvent(i)->execute(); |
---|
| 99 | } |
---|
[10053] | 100 | } |
---|
| 101 | |
---|
[10624] | 102 | this->destroyLater(); |
---|
[10023] | 103 | } |
---|
| 104 | |
---|
[10067] | 105 | void ShipPart::explode() |
---|
| 106 | { |
---|
| 107 | BigExplosion* chunk = new BigExplosion(this->getContext()); |
---|
| 108 | chunk->setPosition(this->parent_->getPosition() + this->parent_->getOrientation() * (this->explosionPosition_)); |
---|
| 109 | //chunk->setPosition(this->parent_->getPosition() + this->parent_->getOrientation() * Vector3(this->entityList_[0]->getLocalInertia())); |
---|
| 110 | chunk->setVelocity(this->parent_->getVelocity()); |
---|
| 111 | |
---|
| 112 | // this->explosionSound_->setPosition(this->parent_->getPosition()); |
---|
| 113 | // this->explosionSound_->play(); |
---|
| 114 | } |
---|
| 115 | |
---|
[10019] | 116 | /** |
---|
| 117 | @brief |
---|
| 118 | Add a StaticEntity to the ShipPart. |
---|
| 119 | @param engine |
---|
| 120 | A pointer to the StaticEntity to be added. |
---|
| 121 | */ |
---|
| 122 | void ShipPart::addEntity(StaticEntity* entity) |
---|
| 123 | { |
---|
| 124 | OrxAssert(entity != NULL, "The Entity cannot be NULL."); |
---|
| 125 | this->entityList_.push_back(entity); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /** |
---|
| 129 | @brief |
---|
| 130 | Get the i-th StaticEntity of the ShipPart. |
---|
| 131 | @return |
---|
| 132 | Returns a pointer to the i-the StaticEntity. NULL if there is no StaticEntity with that index. |
---|
| 133 | */ |
---|
| 134 | StaticEntity* ShipPart::getEntity(unsigned int index) |
---|
| 135 | { |
---|
| 136 | if(this->entityList_.size() >= index) |
---|
| 137 | return NULL; |
---|
| 138 | else |
---|
| 139 | return this->entityList_[index]; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /** |
---|
| 143 | @brief |
---|
| 144 | Check whether the ShipPart has a particular Entity. |
---|
| 145 | @param engine |
---|
| 146 | A pointer to the Entity to be checked. |
---|
| 147 | */ |
---|
| 148 | bool ShipPart::hasEntity(StaticEntity* entity) const |
---|
| 149 | { |
---|
| 150 | for(unsigned int i = 0; i < this->entityList_.size(); i++) |
---|
| 151 | { |
---|
| 152 | if(this->entityList_[i] == entity) |
---|
| 153 | return true; |
---|
| 154 | } |
---|
| 155 | return false; |
---|
| 156 | } |
---|
| 157 | |
---|
[10052] | 158 | /** |
---|
| 159 | @brief |
---|
| 160 | Add a PartDestructionEvent to the ShipPart. |
---|
| 161 | @param engine |
---|
| 162 | A pointer to the PartDestructionEvent to be added. |
---|
| 163 | */ |
---|
[10053] | 164 | void ShipPart::addDestructionEvent(PartDestructionEvent* event) |
---|
[10052] | 165 | { |
---|
[10053] | 166 | OrxAssert(event != NULL, "The PartDestructionEvent cannot be NULL."); |
---|
| 167 | event->setParent(this); |
---|
| 168 | this->eventList_.push_back(event); |
---|
[10052] | 169 | } |
---|
| 170 | |
---|
| 171 | /** |
---|
| 172 | @brief |
---|
| 173 | Get the i-th PartDestructionEvent of the ShipPart. |
---|
| 174 | @return |
---|
| 175 | Returns a pointer to the i-the PartDestructionEvent. NULL if there is no PartDestructionEvent with that index. |
---|
| 176 | */ |
---|
| 177 | PartDestructionEvent* ShipPart::getDestructionEvent(unsigned int index) |
---|
| 178 | { |
---|
[10053] | 179 | if(this->eventList_.size() <= index) |
---|
[10052] | 180 | return NULL; |
---|
| 181 | else |
---|
| 182 | return this->eventList_[index]; |
---|
| 183 | } |
---|
| 184 | |
---|
[10019] | 185 | void ShipPart::setDamageAbsorption(float value) |
---|
| 186 | { |
---|
| 187 | this->damageAbsorption_ = value; |
---|
| 188 | } |
---|
| 189 | |
---|
[10023] | 190 | void ShipPart::setParent(ModularSpaceShip* ship) |
---|
| 191 | { |
---|
| 192 | this->parent_ = ship; |
---|
| 193 | } |
---|
| 194 | |
---|
[10019] | 195 | /** |
---|
| 196 | @brief |
---|
| 197 | Sets the health of the ShipPart. |
---|
| 198 | */ |
---|
| 199 | void ShipPart::setHealth(float health) |
---|
| 200 | { |
---|
| 201 | this->health_ = health; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | /** |
---|
| 205 | @brief |
---|
| 206 | Handles a received hit. |
---|
| 207 | */ |
---|
| 208 | void ShipPart::handleHit(float damage, float healthdamage, float shielddamage, Pawn* originator) |
---|
| 209 | { |
---|
| 210 | if (parent_->getGametype() && parent_->getGametype()->allowPawnDamage(parent_, originator)) |
---|
| 211 | { |
---|
| 212 | if (shielddamage >= parent_->getShieldHealth()) |
---|
| 213 | { |
---|
| 214 | parent_->setShieldHealth(0); |
---|
| 215 | this->setHealth(this->health_ - (healthdamage + damage) * this->damageAbsorption_); |
---|
| 216 | parent_->setHealth(parent_->getHealth() - (healthdamage + damage) * (1 - this->damageAbsorption_)); |
---|
| 217 | } |
---|
| 218 | else |
---|
| 219 | { |
---|
| 220 | parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage); |
---|
| 221 | |
---|
| 222 | // remove remaining shieldAbsorpton-Part of damage from shield |
---|
| 223 | shielddamage = damage * parent_->getShieldAbsorption(); |
---|
| 224 | shielddamage = std::min(parent_->getShieldHealth(),shielddamage); |
---|
| 225 | parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage); |
---|
| 226 | |
---|
| 227 | // set remaining damage to health |
---|
| 228 | this->setHealth(this->health_ - ((damage - shielddamage) - healthdamage) * this->damageAbsorption_); |
---|
| 229 | parent_->setHealth(parent_->getHealth() - ((damage - shielddamage) - healthdamage) * (1- this->damageAbsorption_)); |
---|
| 230 | } |
---|
| 231 | } |
---|
[10023] | 232 | if (this->health_ < 0) |
---|
[10624] | 233 | this->death(); |
---|
[10067] | 234 | |
---|
| 235 | // (Ugly) Chatoutput of health, until a GUI for modularspaceships-shipparts is implemented. |
---|
[10071] | 236 | if ((this->health_ < 0.2 * this->maxHealth_) && (this->healthMem_ == 40)) |
---|
[10067] | 237 | { |
---|
[10071] | 238 | this->healthMem_ = 20; |
---|
[10067] | 239 | ChatManager::message("ShipPart " + this->getName() + " remaining health is 20%!"); |
---|
| 240 | return; |
---|
| 241 | } |
---|
[10071] | 242 | if ((this->health_ < 0.4 * this->maxHealth_) && (this->healthMem_ == 60)) |
---|
[10067] | 243 | { |
---|
[10071] | 244 | this->healthMem_ = 40; |
---|
[10067] | 245 | ChatManager::message("ShipPart " + this->getName() + " remaining health is 40%!"); |
---|
| 246 | return; |
---|
| 247 | } |
---|
[10071] | 248 | if ((this->health_ < 0.6 * this->maxHealth_) && (this->healthMem_ == 80)) |
---|
[10067] | 249 | { |
---|
[10071] | 250 | this->healthMem_ = 60; |
---|
[10067] | 251 | ChatManager::message("ShipPart " + this->getName() + " remaining health is 60%!"); |
---|
| 252 | return; |
---|
| 253 | } |
---|
[10071] | 254 | if ((this->health_ < 0.8 * this->maxHealth_) && (this->healthMem_ == 100)) |
---|
[10067] | 255 | { |
---|
[10071] | 256 | this->healthMem_ = 80; |
---|
[10067] | 257 | ChatManager::message("ShipPart " + this->getName() + " remaining health is 80%!"); |
---|
| 258 | return; |
---|
| 259 | } |
---|
[10019] | 260 | } |
---|
| 261 | |
---|
| 262 | |
---|
| 263 | /** |
---|
| 264 | @brief |
---|
| 265 | Adds the ShipPart to the input SpaceShip. |
---|
| 266 | @param ship |
---|
| 267 | A pointer to the SpaceShip to which the ShipPart is added. |
---|
| 268 | */ |
---|
| 269 | /*void ShipPart::addToSpaceShip(ModularSpaceShip* ship) |
---|
| 270 | { |
---|
| 271 | this->parent_ = ship; |
---|
| 272 | |
---|
| 273 | if (ship) |
---|
| 274 | { |
---|
| 275 | this->parentID_ = ship->getObjectID(); |
---|
| 276 | if (!ship->hasShipPart(this)) |
---|
| 277 | ship->addShipPart(this); |
---|
| 278 | } |
---|
| 279 | }*/ |
---|
| 280 | |
---|
| 281 | } |
---|