[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 "chat/ChatManager.h" |
---|
[10019] | 44 | |
---|
| 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
| 48 | RegisterClass(ShipPart); |
---|
| 49 | |
---|
| 50 | ShipPart::ShipPart(Context* context) |
---|
[11071] | 51 | : Item(context), parent_(nullptr) |
---|
[10019] | 52 | { |
---|
| 53 | RegisterObject(ShipPart); |
---|
[10624] | 54 | this->eventExecution_ = true; |
---|
[10071] | 55 | this->healthMem_ = 100; |
---|
[10019] | 56 | } |
---|
| 57 | |
---|
| 58 | ShipPart::~ShipPart() |
---|
| 59 | { |
---|
[10624] | 60 | if (this->parent_) |
---|
| 61 | { |
---|
| 62 | // Remove this ShipPart from the parent. |
---|
| 63 | this->parent_->removeShipPart(this); |
---|
| 64 | } |
---|
[10019] | 65 | } |
---|
| 66 | |
---|
[10023] | 67 | void ShipPart::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 68 | { |
---|
| 69 | SUPER(ShipPart, XMLPort, xmlelement, mode); |
---|
[10019] | 70 | |
---|
[10023] | 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 | |
---|
[10067] | 77 | XMLPortParamTemplate(ShipPart, "explosionposition", setExplosionPosition, getExplosionPosition, xmlelement, mode, Vector3); |
---|
| 78 | |
---|
[10052] | 79 | XMLPortObject(ShipPart, PartDestructionEvent, "destructionevents", addDestructionEvent, getDestructionEvent, xmlelement, mode); |
---|
[10023] | 80 | } |
---|
| 81 | |
---|
[10055] | 82 | /** |
---|
| 83 | @brief |
---|
| 84 | Is called when the ShipPart dies. |
---|
| 85 | Executes PartDestructionEvents. |
---|
| 86 | Tells the ModularSpaceShip to remove this ShipPart. |
---|
| 87 | */ |
---|
[10023] | 88 | void ShipPart::death() |
---|
| 89 | { |
---|
[10067] | 90 | this->explode(); |
---|
[10053] | 91 | |
---|
[10058] | 92 | if(eventExecution_) |
---|
[10053] | 93 | { |
---|
[10058] | 94 | // Execute all destruction events |
---|
| 95 | for (unsigned int i = 0; i < this->eventList_.size(); i++) |
---|
| 96 | { |
---|
| 97 | this->getDestructionEvent(i)->execute(); |
---|
| 98 | } |
---|
[10053] | 99 | } |
---|
| 100 | |
---|
[10624] | 101 | this->destroyLater(); |
---|
[10023] | 102 | } |
---|
| 103 | |
---|
[10067] | 104 | void ShipPart::explode() |
---|
| 105 | { |
---|
[11052] | 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()); |
---|
[10067] | 110 | |
---|
| 111 | // this->explosionSound_->setPosition(this->parent_->getPosition()); |
---|
| 112 | // this->explosionSound_->play(); |
---|
| 113 | } |
---|
| 114 | |
---|
[10019] | 115 | /** |
---|
| 116 | @brief |
---|
| 117 | Add a StaticEntity to the ShipPart. |
---|
[11099] | 118 | @param entity |
---|
[10019] | 119 | A pointer to the StaticEntity to be added. |
---|
| 120 | */ |
---|
| 121 | void ShipPart::addEntity(StaticEntity* entity) |
---|
| 122 | { |
---|
[11071] | 123 | OrxAssert(entity != nullptr, "The Entity cannot be nullptr."); |
---|
[10019] | 124 | this->entityList_.push_back(entity); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | @brief |
---|
| 129 | Get the i-th StaticEntity of the ShipPart. |
---|
| 130 | @return |
---|
[11071] | 131 | Returns a pointer to the i-the StaticEntity. nullptr if there is no StaticEntity with that index. |
---|
[10019] | 132 | */ |
---|
| 133 | StaticEntity* ShipPart::getEntity(unsigned int index) |
---|
| 134 | { |
---|
| 135 | if(this->entityList_.size() >= index) |
---|
[11071] | 136 | return nullptr; |
---|
[10019] | 137 | else |
---|
| 138 | return this->entityList_[index]; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | @brief |
---|
| 143 | Check whether the ShipPart has a particular Entity. |
---|
[11071] | 144 | @param search |
---|
[10019] | 145 | A pointer to the Entity to be checked. |
---|
| 146 | */ |
---|
[11071] | 147 | bool ShipPart::hasEntity(StaticEntity* search) const |
---|
[10019] | 148 | { |
---|
[11071] | 149 | for(StaticEntity* entity : this->entityList_) |
---|
[10019] | 150 | { |
---|
[11071] | 151 | if(entity == search) |
---|
[10019] | 152 | return true; |
---|
| 153 | } |
---|
| 154 | return false; |
---|
| 155 | } |
---|
| 156 | |
---|
[10052] | 157 | /** |
---|
| 158 | @brief |
---|
| 159 | Add a PartDestructionEvent to the ShipPart. |
---|
[11099] | 160 | @param event |
---|
[10052] | 161 | A pointer to the PartDestructionEvent to be added. |
---|
| 162 | */ |
---|
[10053] | 163 | void ShipPart::addDestructionEvent(PartDestructionEvent* event) |
---|
[10052] | 164 | { |
---|
[11071] | 165 | OrxAssert(event != nullptr, "The PartDestructionEvent cannot be nullptr."); |
---|
[10053] | 166 | event->setParent(this); |
---|
| 167 | this->eventList_.push_back(event); |
---|
[10052] | 168 | } |
---|
| 169 | |
---|
| 170 | /** |
---|
| 171 | @brief |
---|
| 172 | Get the i-th PartDestructionEvent of the ShipPart. |
---|
| 173 | @return |
---|
[11071] | 174 | Returns a pointer to the i-the PartDestructionEvent. nullptr if there is no PartDestructionEvent with that index. |
---|
[10052] | 175 | */ |
---|
| 176 | PartDestructionEvent* ShipPart::getDestructionEvent(unsigned int index) |
---|
| 177 | { |
---|
[10053] | 178 | if(this->eventList_.size() <= index) |
---|
[11071] | 179 | return nullptr; |
---|
[10052] | 180 | else |
---|
| 181 | return this->eventList_[index]; |
---|
| 182 | } |
---|
| 183 | |
---|
[10023] | 184 | void ShipPart::setParent(ModularSpaceShip* ship) |
---|
| 185 | { |
---|
| 186 | this->parent_ = ship; |
---|
| 187 | } |
---|
| 188 | |
---|
[10019] | 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 | } |
---|
[10023] | 217 | if (this->health_ < 0) |
---|
[10624] | 218 | this->death(); |
---|
[10067] | 219 | |
---|
| 220 | // (Ugly) Chatoutput of health, until a GUI for modularspaceships-shipparts is implemented. |
---|
[10071] | 221 | if ((this->health_ < 0.2 * this->maxHealth_) && (this->healthMem_ == 40)) |
---|
[10067] | 222 | { |
---|
[10071] | 223 | this->healthMem_ = 20; |
---|
[10067] | 224 | ChatManager::message("ShipPart " + this->getName() + " remaining health is 20%!"); |
---|
| 225 | return; |
---|
| 226 | } |
---|
[10071] | 227 | if ((this->health_ < 0.4 * this->maxHealth_) && (this->healthMem_ == 60)) |
---|
[10067] | 228 | { |
---|
[10071] | 229 | this->healthMem_ = 40; |
---|
[10067] | 230 | ChatManager::message("ShipPart " + this->getName() + " remaining health is 40%!"); |
---|
| 231 | return; |
---|
| 232 | } |
---|
[10071] | 233 | if ((this->health_ < 0.6 * this->maxHealth_) && (this->healthMem_ == 80)) |
---|
[10067] | 234 | { |
---|
[10071] | 235 | this->healthMem_ = 60; |
---|
[10067] | 236 | ChatManager::message("ShipPart " + this->getName() + " remaining health is 60%!"); |
---|
| 237 | return; |
---|
| 238 | } |
---|
[10071] | 239 | if ((this->health_ < 0.8 * this->maxHealth_) && (this->healthMem_ == 100)) |
---|
[10067] | 240 | { |
---|
[10071] | 241 | this->healthMem_ = 80; |
---|
[10067] | 242 | ChatManager::message("ShipPart " + this->getName() + " remaining health is 80%!"); |
---|
| 243 | return; |
---|
| 244 | } |
---|
[10019] | 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 | } |
---|