[6782] | 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 | * Lukas Gasser |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | @file DronePickup.cc |
---|
| 31 | @brief Implementation of the DronePickup class. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "DronePickup.h" |
---|
| 35 | |
---|
[7541] | 36 | #include <sstream> |
---|
[6782] | 37 | #include "core/CoreIncludes.h" |
---|
| 38 | #include "core/XMLPort.h" |
---|
| 39 | |
---|
[7541] | 40 | #include "controllers/DroneController.h" |
---|
| 41 | #include "pickup/PickupIdentifier.h" |
---|
[7547] | 42 | #include "worldentities/Drone.h" |
---|
[6782] | 43 | #include "worldentities/pawns/Pawn.h" |
---|
| 44 | |
---|
| 45 | namespace orxonox |
---|
| 46 | { |
---|
| 47 | |
---|
| 48 | CreateFactory(DronePickup); |
---|
[7127] | 49 | |
---|
[6782] | 50 | /** |
---|
| 51 | @brief |
---|
| 52 | Constructor. Registers the object and initializes the member variables. |
---|
| 53 | */ |
---|
| 54 | DronePickup::DronePickup(BaseObject* creator) : Pickup(creator) |
---|
| 55 | { |
---|
| 56 | RegisterObject(DronePickup); |
---|
[7127] | 57 | |
---|
[6782] | 58 | this->initialize(); |
---|
| 59 | } |
---|
[7127] | 60 | |
---|
[6782] | 61 | /** |
---|
| 62 | @brief |
---|
| 63 | Destructor. |
---|
| 64 | */ |
---|
| 65 | DronePickup::~DronePickup() |
---|
| 66 | { |
---|
[7127] | 67 | |
---|
[6782] | 68 | } |
---|
[7127] | 69 | |
---|
[6782] | 70 | /** |
---|
[7127] | 71 | @brief |
---|
[6782] | 72 | Initializes the member variables. |
---|
| 73 | */ |
---|
| 74 | void DronePickup::initialize(void) |
---|
| 75 | { |
---|
| 76 | this->addTarget(ClassIdentifier<Pawn>::getIdentifier()); |
---|
[9313] | 77 | this->setDurationType(pickupDurationType::once); |
---|
[6782] | 78 | this->droneTemplate_ = ""; |
---|
| 79 | } |
---|
[7127] | 80 | |
---|
[6782] | 81 | /** |
---|
| 82 | @brief |
---|
| 83 | Initializes the PickupIdentifier of this pickup. |
---|
| 84 | */ |
---|
| 85 | void DronePickup::initializeIdentifier(void) |
---|
| 86 | { |
---|
[9305] | 87 | this->pickupIdentifier_->addParameter("droneTemplate", this->getDroneTemplate()); |
---|
[6782] | 88 | } |
---|
[7127] | 89 | |
---|
[6782] | 90 | /** |
---|
| 91 | @brief |
---|
| 92 | Method for creating a DronePickup object through XML. |
---|
| 93 | */ |
---|
| 94 | void DronePickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) |
---|
| 95 | { |
---|
| 96 | SUPER(DronePickup, XMLPort, xmlelement, mode); |
---|
| 97 | XMLPortParam(DronePickup, "droneTemplate", setDroneTemplate, getDroneTemplate, xmlelement, mode); |
---|
[7127] | 98 | |
---|
[6782] | 99 | this->initializeIdentifier(); |
---|
| 100 | } |
---|
[7127] | 101 | |
---|
[7541] | 102 | /** |
---|
| 103 | @brief |
---|
| 104 | Set the droneTemplate. |
---|
| 105 | @param templatename |
---|
| 106 | The name of the Template to e set. |
---|
| 107 | */ |
---|
[9312] | 108 | void DronePickup::setDroneTemplate(const std::string& templatename){ |
---|
[6782] | 109 | droneTemplate_ = templatename; |
---|
[7127] | 110 | } |
---|
| 111 | |
---|
[7541] | 112 | /** |
---|
| 113 | @brief |
---|
| 114 | Get the name of the droneTemplate. |
---|
| 115 | @return |
---|
| 116 | Returns the name of the droneTemplate. |
---|
| 117 | */ |
---|
[6782] | 118 | const std::string& DronePickup::getDroneTemplate() const |
---|
| 119 | { |
---|
| 120 | return droneTemplate_; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | @brief |
---|
| 125 | Is called when the pickup has transited from used to unused or the other way around. |
---|
| 126 | */ |
---|
| 127 | void DronePickup::changedUsed(void) |
---|
| 128 | { |
---|
| 129 | SUPER(DronePickup, changedUsed); |
---|
[7127] | 130 | |
---|
[7541] | 131 | // If the pickup has transited to used. |
---|
[6782] | 132 | if(this->isUsed()) |
---|
| 133 | { |
---|
| 134 | |
---|
| 135 | Pawn* pawn = this->carrierToPawnHelper(); |
---|
[7541] | 136 | if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. |
---|
[7162] | 137 | this->Pickupable::destroy(); |
---|
[7127] | 138 | |
---|
[6782] | 139 | //Attach to pawn |
---|
[6891] | 140 | Drone* drone = new Drone(pawn->getCreator()); // this is neccessary because the projectiles fired need a valid creator for the particlespawner (when colliding against something) |
---|
[6782] | 141 | drone->addTemplate(this->getDroneTemplate()); |
---|
[6891] | 142 | |
---|
[6782] | 143 | Controller* controller = drone->getController(); |
---|
[9279] | 144 | DroneController* droneController = orxonox_cast<DroneController*>(controller); |
---|
[6782] | 145 | if(droneController != NULL) |
---|
| 146 | { |
---|
[6847] | 147 | droneController->setOwner(pawn); |
---|
[6782] | 148 | } |
---|
[7127] | 149 | |
---|
[6891] | 150 | Vector3 spawnPosition = pawn->getWorldPosition() + Vector3(30,0,-30); |
---|
| 151 | drone->setPosition(spawnPosition); |
---|
[7127] | 152 | |
---|
[7541] | 153 | // The pickup has been used up. |
---|
[6782] | 154 | this->setUsed(false); |
---|
| 155 | } |
---|
| 156 | else |
---|
| 157 | { |
---|
[7541] | 158 | // If either the pickup can only be used once or it is continuous and used up, it is destroyed upon setting it to unused. |
---|
[6782] | 159 | if(this->isOnce() || (this->isContinuous() )) |
---|
| 160 | { |
---|
[7162] | 161 | this->Pickupable::destroy(); |
---|
[6782] | 162 | } |
---|
| 163 | } |
---|
| 164 | } |
---|
[7127] | 165 | |
---|
[6782] | 166 | /** |
---|
| 167 | @brief |
---|
| 168 | Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails. |
---|
| 169 | @return |
---|
| 170 | A pointer to the Pawn, or NULL if the conversion failed. |
---|
| 171 | */ |
---|
| 172 | Pawn* DronePickup::carrierToPawnHelper(void) |
---|
| 173 | { |
---|
| 174 | PickupCarrier* carrier = this->getCarrier(); |
---|
[9279] | 175 | Pawn* pawn = orxonox_cast<Pawn*>(carrier); |
---|
[7127] | 176 | |
---|
[6782] | 177 | if(pawn == NULL) |
---|
| 178 | { |
---|
[8858] | 179 | orxout(internal_error, context::pickups) << "Invalid PickupCarrier in DronePickup." << endl; |
---|
[6782] | 180 | } |
---|
[7127] | 181 | |
---|
[6782] | 182 | return pawn; |
---|
| 183 | } |
---|
[7127] | 184 | |
---|
[6782] | 185 | /** |
---|
| 186 | @brief |
---|
| 187 | Creates a duplicate of the input OrxonoxClass. |
---|
| 188 | @param item |
---|
| 189 | A pointer to the Orxonox class. |
---|
| 190 | */ |
---|
| 191 | void DronePickup::clone(OrxonoxClass*& item) |
---|
| 192 | { |
---|
| 193 | if(item == NULL) |
---|
| 194 | item = new DronePickup(this); |
---|
[7127] | 195 | |
---|
[6782] | 196 | SUPER(DronePickup, clone, item); |
---|
[7127] | 197 | |
---|
[9279] | 198 | DronePickup* pickup = orxonox_cast<DronePickup*>(item); |
---|
[6782] | 199 | pickup->setDroneTemplate(this->getDroneTemplate()); |
---|
[7127] | 200 | |
---|
[6782] | 201 | pickup->initializeIdentifier(); |
---|
| 202 | } |
---|
| 203 | } |
---|