[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" |
---|
[10019] | 43 | |
---|
| 44 | |
---|
| 45 | namespace orxonox |
---|
| 46 | { |
---|
| 47 | RegisterClass(ShipPart); |
---|
| 48 | |
---|
| 49 | ShipPart::ShipPart(Context* context) |
---|
| 50 | : Item(context) |
---|
| 51 | { |
---|
| 52 | RegisterObject(ShipPart); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | ShipPart::~ShipPart() |
---|
| 56 | { |
---|
| 57 | |
---|
| 58 | } |
---|
| 59 | |
---|
[10023] | 60 | void ShipPart::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 61 | { |
---|
| 62 | SUPER(ShipPart, XMLPort, xmlelement, mode); |
---|
[10019] | 63 | |
---|
[10023] | 64 | XMLPortParam(ShipPart, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100); |
---|
| 65 | XMLPortParam(ShipPart, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200); |
---|
| 66 | XMLPortParam(ShipPart, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100); |
---|
| 67 | |
---|
| 68 | XMLPortParam(ShipPart, "damageabsorption", setDamageAbsorption, getDamageAbsorption, xmlelement, mode).defaultValues(0.5); |
---|
| 69 | |
---|
[10052] | 70 | XMLPortObject(ShipPart, PartDestructionEvent, "destructionevents", addDestructionEvent, getDestructionEvent, xmlelement, mode); |
---|
| 71 | |
---|
[10023] | 72 | /* |
---|
| 73 | XMLPortParam(ShipPart, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode).defaultValues(0); |
---|
| 74 | XMLPortParam(ShipPart, "initialshieldhealth", setInitialShieldHealth, getInitialShieldHealth, xmlelement, mode).defaultValues(0); |
---|
| 75 | XMLPortParam(ShipPart, "maxshieldhealth", setMaxShieldHealth, getMaxShieldHealth, xmlelement, mode).defaultValues(100); |
---|
| 76 | XMLPortParam(ShipPart, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode).defaultValues(0); |
---|
| 77 | |
---|
| 78 | XMLPortParam(ShipPart, "sShipPartparticlesource", setSShipPartParticleSource, getSShipPartParticleSource, xmlelement, mode); |
---|
| 79 | XMLPortParam(ShipPart, "sShipPartparticleduration", setSShipPartParticleDuration, getSShipPartParticleDuration, xmlelement, mode).defaultValues(3.0f); |
---|
| 80 | XMLPortParam(ShipPart, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7); |
---|
| 81 | |
---|
| 82 | XMLPortParam(ShipPart, "reloadrate", setReloadRate, getReloadRate, xmlelement, mode).defaultValues(0); |
---|
| 83 | XMLPortParam(ShipPart, "reloadwaittime", setReloadWaitTime, getReloadWaitTime, xmlelement, mode).defaultValues(1.0f); |
---|
| 84 | |
---|
| 85 | XMLPortParam(ShipPart, "explosionSound", setExplosionSound, getExplosionSound, xmlelement, mode); |
---|
| 86 | |
---|
| 87 | XMLPortParam ( RadarViewable, "radarname", setRadarName, getRadarName, xmlelement, mode ); |
---|
| 88 | */ |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void ShipPart::death() |
---|
| 92 | { |
---|
| 93 | this->parent_->removeShipPart(this); |
---|
[10033] | 94 | orxout() << this->getName() << " has died." << endl; |
---|
[10023] | 95 | } |
---|
| 96 | |
---|
[10019] | 97 | /** |
---|
| 98 | @brief |
---|
| 99 | Add a StaticEntity to the ShipPart. |
---|
| 100 | @param engine |
---|
| 101 | A pointer to the StaticEntity to be added. |
---|
| 102 | */ |
---|
| 103 | void ShipPart::addEntity(StaticEntity* entity) |
---|
| 104 | { |
---|
| 105 | OrxAssert(entity != NULL, "The Entity cannot be NULL."); |
---|
| 106 | this->entityList_.push_back(entity); |
---|
| 107 | //part->addToSpaceShip(this); //FIXME: (noep) add |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | /** |
---|
| 111 | @brief |
---|
| 112 | Get the i-th StaticEntity of the ShipPart. |
---|
| 113 | @return |
---|
| 114 | Returns a pointer to the i-the StaticEntity. NULL if there is no StaticEntity with that index. |
---|
| 115 | */ |
---|
| 116 | StaticEntity* ShipPart::getEntity(unsigned int index) |
---|
| 117 | { |
---|
| 118 | if(this->entityList_.size() >= index) |
---|
| 119 | return NULL; |
---|
| 120 | else |
---|
| 121 | return this->entityList_[index]; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | /** |
---|
| 125 | @brief |
---|
| 126 | Check whether the ShipPart has a particular Entity. |
---|
| 127 | @param engine |
---|
| 128 | A pointer to the Entity to be checked. |
---|
| 129 | */ |
---|
| 130 | bool ShipPart::hasEntity(StaticEntity* entity) const |
---|
| 131 | { |
---|
| 132 | for(unsigned int i = 0; i < this->entityList_.size(); i++) |
---|
| 133 | { |
---|
| 134 | if(this->entityList_[i] == entity) |
---|
| 135 | return true; |
---|
| 136 | } |
---|
| 137 | return false; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | void ShipPart::printEntities() |
---|
| 141 | { |
---|
| 142 | orxout() << "ShipPart " << this->getName() << " has the following entities assigned:" << endl; |
---|
| 143 | for(unsigned int j = 0; j < this->entityList_.size(); j++) |
---|
| 144 | { |
---|
| 145 | orxout() << " " << this->entityList_[j]->getName() << endl; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
[10052] | 149 | /** |
---|
| 150 | @brief |
---|
| 151 | Add a PartDestructionEvent to the ShipPart. |
---|
| 152 | @param engine |
---|
| 153 | A pointer to the PartDestructionEvent to be added. |
---|
| 154 | */ |
---|
| 155 | void ShipPart::addDestructionEvent(PartDestructionEvent* part) |
---|
| 156 | { |
---|
| 157 | OrxAssert(part != NULL, "The PartDestructionEvent cannot be NULL."); |
---|
| 158 | this->eventList_.push_back(part); |
---|
| 159 | //part->setParent(this); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | @brief |
---|
| 164 | Get the i-th PartDestructionEvent of the ShipPart. |
---|
| 165 | @return |
---|
| 166 | Returns a pointer to the i-the PartDestructionEvent. NULL if there is no PartDestructionEvent with that index. |
---|
| 167 | */ |
---|
| 168 | PartDestructionEvent* ShipPart::getDestructionEvent(unsigned int index) |
---|
| 169 | { |
---|
| 170 | if(this->eventList_.size() >= index) |
---|
| 171 | return NULL; |
---|
| 172 | else |
---|
| 173 | return this->eventList_[index]; |
---|
| 174 | } |
---|
| 175 | |
---|
[10019] | 176 | void ShipPart::setDamageAbsorption(float value) |
---|
| 177 | { |
---|
| 178 | this->damageAbsorption_ = value; |
---|
| 179 | } |
---|
| 180 | |
---|
[10023] | 181 | void ShipPart::setParent(ModularSpaceShip* ship) |
---|
| 182 | { |
---|
| 183 | this->parent_ = ship; |
---|
| 184 | } |
---|
| 185 | |
---|
[10019] | 186 | /** |
---|
| 187 | @brief |
---|
| 188 | Sets the health of the ShipPart. |
---|
| 189 | */ |
---|
| 190 | void ShipPart::setHealth(float health) |
---|
| 191 | { |
---|
| 192 | this->health_ = health; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | /** |
---|
| 196 | @brief |
---|
| 197 | Handles a received hit. |
---|
| 198 | */ |
---|
| 199 | void ShipPart::handleHit(float damage, float healthdamage, float shielddamage, Pawn* originator) |
---|
| 200 | { |
---|
| 201 | orxout() << "ShipPart " <<this->getName() << " is handling a hit!" << endl; |
---|
| 202 | if (parent_->getGametype() && parent_->getGametype()->allowPawnDamage(parent_, originator)) |
---|
| 203 | { |
---|
| 204 | if (shielddamage >= parent_->getShieldHealth()) |
---|
| 205 | { |
---|
| 206 | parent_->setShieldHealth(0); |
---|
| 207 | this->setHealth(this->health_ - (healthdamage + damage) * this->damageAbsorption_); |
---|
| 208 | parent_->setHealth(parent_->getHealth() - (healthdamage + damage) * (1 - this->damageAbsorption_)); |
---|
| 209 | } |
---|
| 210 | else |
---|
| 211 | { |
---|
| 212 | parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage); |
---|
| 213 | |
---|
| 214 | // remove remaining shieldAbsorpton-Part of damage from shield |
---|
| 215 | shielddamage = damage * parent_->getShieldAbsorption(); |
---|
| 216 | shielddamage = std::min(parent_->getShieldHealth(),shielddamage); |
---|
| 217 | parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage); |
---|
| 218 | |
---|
| 219 | // set remaining damage to health |
---|
| 220 | this->setHealth(this->health_ - ((damage - shielddamage) - healthdamage) * this->damageAbsorption_); |
---|
| 221 | parent_->setHealth(parent_->getHealth() - ((damage - shielddamage) - healthdamage) * (1- this->damageAbsorption_)); |
---|
| 222 | } |
---|
| 223 | } |
---|
[10023] | 224 | if (this->health_ < 0) |
---|
| 225 | this->death(); |
---|
| 226 | orxout() << "Health of ShipPart " << this->getName() << " is " << this->getHealth() << endl; |
---|
[10019] | 227 | } |
---|
| 228 | |
---|
| 229 | |
---|
| 230 | /** |
---|
| 231 | @brief |
---|
| 232 | Adds the ShipPart to the input SpaceShip. |
---|
| 233 | @param ship |
---|
| 234 | A pointer to the SpaceShip to which the ShipPart is added. |
---|
| 235 | */ |
---|
| 236 | /*void ShipPart::addToSpaceShip(ModularSpaceShip* ship) |
---|
| 237 | { |
---|
| 238 | this->parent_ = ship; |
---|
| 239 | |
---|
| 240 | if (ship) |
---|
| 241 | { |
---|
| 242 | this->parentID_ = ship->getObjectID(); |
---|
| 243 | if (!ship->hasShipPart(this)) |
---|
| 244 | ship->addShipPart(this); |
---|
| 245 | } |
---|
| 246 | }*/ |
---|
| 247 | |
---|
| 248 | } |
---|