| [10724] | 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 | *      Fabien Vultier | 
|---|
|  | 24 | *   Co-authors: | 
|---|
|  | 25 | *      ... | 
|---|
|  | 26 | * | 
|---|
|  | 27 | */ | 
|---|
|  | 28 |  | 
|---|
|  | 29 | /** | 
|---|
|  | 30 | @file BoostPickup.cc | 
|---|
|  | 31 | @brief Implementation of the BoostPickup class. | 
|---|
|  | 32 | */ | 
|---|
|  | 33 |  | 
|---|
|  | 34 | #include "BoostPickup.h" | 
|---|
|  | 35 |  | 
|---|
|  | 36 | #include <sstream> | 
|---|
|  | 37 | #include "core/CoreIncludes.h" | 
|---|
|  | 38 | #include "core/XMLPort.h" | 
|---|
|  | 39 |  | 
|---|
|  | 40 | #include "worldentities/pawns/SpaceShip.h" | 
|---|
|  | 41 |  | 
|---|
|  | 42 | namespace orxonox | 
|---|
|  | 43 | { | 
|---|
|  | 44 | RegisterClass(BoostPickup); | 
|---|
|  | 45 |  | 
|---|
|  | 46 | /** | 
|---|
|  | 47 | @brief | 
|---|
|  | 48 | Constructor. Registers the object and initializes the member variables. | 
|---|
|  | 49 | */ | 
|---|
|  | 50 | BoostPickup::BoostPickup(Context* context) : Pickup(context) | 
|---|
|  | 51 | { | 
|---|
|  | 52 | RegisterObject(BoostPickup); | 
|---|
|  | 53 |  | 
|---|
|  | 54 | this->initialize(); | 
|---|
|  | 55 | } | 
|---|
|  | 56 |  | 
|---|
|  | 57 | /** | 
|---|
|  | 58 | @brief | 
|---|
|  | 59 | Destructor. | 
|---|
|  | 60 | */ | 
|---|
|  | 61 | BoostPickup::~BoostPickup() | 
|---|
|  | 62 | { | 
|---|
|  | 63 | } | 
|---|
|  | 64 |  | 
|---|
|  | 65 | /** | 
|---|
|  | 66 | @brief | 
|---|
|  | 67 | Initializes the member variables. | 
|---|
|  | 68 | */ | 
|---|
|  | 69 | void BoostPickup::initialize(void) | 
|---|
|  | 70 | { | 
|---|
|  | 71 | this->boostRefill_ = 0.0f; | 
|---|
|  | 72 | //Defines who is allowed to pick up the pickup. | 
|---|
|  | 73 | this->addTarget(ClassIdentifier<SpaceShip>::getIdentifier()); | 
|---|
|  | 74 | } | 
|---|
|  | 75 |  | 
|---|
|  | 76 | /** | 
|---|
|  | 77 | @brief | 
|---|
|  | 78 | Method for creating a BoostPickup object through XML. | 
|---|
|  | 79 | */ | 
|---|
|  | 80 | void BoostPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) | 
|---|
|  | 81 | { | 
|---|
|  | 82 | SUPER(BoostPickup, XMLPort, xmlelement, mode); | 
|---|
|  | 83 |  | 
|---|
|  | 84 | XMLPortParam(BoostPickup, "boostrefill", setBoostRefill, getBoostRefill, xmlelement, mode); | 
|---|
|  | 85 | } | 
|---|
|  | 86 |  | 
|---|
|  | 87 | /** | 
|---|
|  | 88 | @brief | 
|---|
|  | 89 | Set the boost refill of this pickup. | 
|---|
|  | 90 | */ | 
|---|
|  | 91 | void BoostPickup::setBoostRefill(float boostRefill) | 
|---|
|  | 92 | { | 
|---|
|  | 93 | if(boostRefill >= 0.0f && boostRefill <= 1.0f) | 
|---|
|  | 94 | { | 
|---|
|  | 95 | this->boostRefill_ = boostRefill; | 
|---|
|  | 96 | } | 
|---|
|  | 97 | } | 
|---|
|  | 98 |  | 
|---|
|  | 99 | /** | 
|---|
|  | 100 | @brief | 
|---|
|  | 101 | Is called when the pickup has transisted from used to unused or the other way around. | 
|---|
|  | 102 | */ | 
|---|
|  | 103 | void BoostPickup::changedUsed(void) | 
|---|
|  | 104 | { | 
|---|
|  | 105 | SUPER(BoostPickup, changedUsed); | 
|---|
|  | 106 |  | 
|---|
|  | 107 | SpaceShip* ship = this->carrierToSpaceShipHelper(); | 
|---|
| [11071] | 108 | if(ship == nullptr) // If the PickupCarrier is no SpaceShip, then this pickup is useless and therefore is destroyed. | 
|---|
| [10724] | 109 | this->Pickupable::destroy(); | 
|---|
|  | 110 |  | 
|---|
|  | 111 | // If the pickup has transited to used. | 
|---|
|  | 112 | if(this->isUsed()) | 
|---|
|  | 113 | { | 
|---|
|  | 114 | float gainedBoostPower = ship->getInitialBoostPower() * boostRefill_; | 
|---|
|  | 115 | ship->gainBoostPower(gainedBoostPower); | 
|---|
|  | 116 |  | 
|---|
|  | 117 | // This will destroy the pickp | 
|---|
|  | 118 | this->setUsed(false); | 
|---|
|  | 119 | } | 
|---|
|  | 120 | else | 
|---|
|  | 121 | { | 
|---|
|  | 122 | this->Pickupable::destroy(); | 
|---|
|  | 123 | } | 
|---|
|  | 124 | } | 
|---|
|  | 125 |  | 
|---|
|  | 126 | /** | 
|---|
|  | 127 | @brief | 
|---|
|  | 128 | Helper to transform the PickupCarrier to a SpaceShip, and throw an error message if the conversion fails. | 
|---|
|  | 129 | @return | 
|---|
| [11071] | 130 | A pointer to the SpaceShip, or nullptr if the conversion failed. | 
|---|
| [10724] | 131 | */ | 
|---|
|  | 132 | SpaceShip* BoostPickup::carrierToSpaceShipHelper(void) | 
|---|
|  | 133 | { | 
|---|
|  | 134 | PickupCarrier* carrier = this->getCarrier(); | 
|---|
|  | 135 | SpaceShip* ship = orxonox_cast<SpaceShip*>(carrier); | 
|---|
|  | 136 |  | 
|---|
| [11071] | 137 | if(ship == nullptr) | 
|---|
| [10724] | 138 | { | 
|---|
|  | 139 | orxout(internal_error, context::pickups) << "Invalid PickupCarrier in BoostPickup." << endl; | 
|---|
|  | 140 | } | 
|---|
|  | 141 |  | 
|---|
|  | 142 | return ship; | 
|---|
|  | 143 | } | 
|---|
|  | 144 |  | 
|---|
|  | 145 |  | 
|---|
|  | 146 | } | 
|---|
|  | 147 |  | 
|---|