| 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 |  | 
|---|
| 36 | #include <sstream> | 
|---|
| 37 | #include "core/CoreIncludes.h" | 
|---|
| 38 | #include "core/XMLPort.h" | 
|---|
| 39 |  | 
|---|
| 40 | #include "controllers/DroneController.h" | 
|---|
| 41 | #include "worldentities/Drone.h" | 
|---|
| 42 | #include "worldentities/pawns/Pawn.h" | 
|---|
| 43 |  | 
|---|
| 44 | namespace orxonox | 
|---|
| 45 | { | 
|---|
| 46 |  | 
|---|
| 47 |     RegisterClass(DronePickup); | 
|---|
| 48 |  | 
|---|
| 49 |     /** | 
|---|
| 50 |     @brief | 
|---|
| 51 |         Constructor. Registers the object and initializes the member variables. | 
|---|
| 52 |     */ | 
|---|
| 53 |     DronePickup::DronePickup(Context* context) : Pickup(context) | 
|---|
| 54 |     { | 
|---|
| 55 |         RegisterObject(DronePickup); | 
|---|
| 56 |  | 
|---|
| 57 |         this->initialize(); | 
|---|
| 58 |     } | 
|---|
| 59 |  | 
|---|
| 60 |     /** | 
|---|
| 61 |     @brief | 
|---|
| 62 |         Destructor. | 
|---|
| 63 |     */ | 
|---|
| 64 |     DronePickup::~DronePickup() | 
|---|
| 65 |     { | 
|---|
| 66 |  | 
|---|
| 67 |     } | 
|---|
| 68 |  | 
|---|
| 69 |     /** | 
|---|
| 70 |     @brief | 
|---|
| 71 |         Initializes the member variables. | 
|---|
| 72 |     */ | 
|---|
| 73 |     void DronePickup::initialize(void) | 
|---|
| 74 |     { | 
|---|
| 75 |         this->addTarget(ClassIdentifier<Pawn>::getIdentifier()); | 
|---|
| 76 |         this->setDurationType(pickupDurationType::once); | 
|---|
| 77 |         this->droneTemplate_ = ""; | 
|---|
| 78 |     } | 
|---|
| 79 |  | 
|---|
| 80 |     /** | 
|---|
| 81 |     @brief | 
|---|
| 82 |         Method for creating a DronePickup object through XML. | 
|---|
| 83 |     */ | 
|---|
| 84 |     void DronePickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) | 
|---|
| 85 |     { | 
|---|
| 86 |         SUPER(DronePickup, XMLPort, xmlelement, mode); | 
|---|
| 87 |         XMLPortParam(DronePickup, "droneTemplate", setDroneTemplate, getDroneTemplate, xmlelement, mode); | 
|---|
| 88 |     } | 
|---|
| 89 |  | 
|---|
| 90 |     /** | 
|---|
| 91 |     @brief | 
|---|
| 92 |         Set the droneTemplate. | 
|---|
| 93 |     @param templatename | 
|---|
| 94 |         The name of the Template to e set. | 
|---|
| 95 |     */ | 
|---|
| 96 |     void DronePickup::setDroneTemplate(const std::string& templatename){ | 
|---|
| 97 |         droneTemplate_ = templatename; | 
|---|
| 98 |     } | 
|---|
| 99 |  | 
|---|
| 100 |     /** | 
|---|
| 101 |     @brief | 
|---|
| 102 |         Get the name of the droneTemplate. | 
|---|
| 103 |     @return | 
|---|
| 104 |         Returns the name of the droneTemplate. | 
|---|
| 105 |     */ | 
|---|
| 106 |     const std::string& DronePickup::getDroneTemplate() const | 
|---|
| 107 |     { | 
|---|
| 108 |         return droneTemplate_; | 
|---|
| 109 |     } | 
|---|
| 110 |  | 
|---|
| 111 |     /** | 
|---|
| 112 |     @brief | 
|---|
| 113 |         Is called when the pickup has transited from used to unused or the other way around. | 
|---|
| 114 |     */ | 
|---|
| 115 |     void DronePickup::changedUsed(void) | 
|---|
| 116 |     { | 
|---|
| 117 |         SUPER(DronePickup, changedUsed); | 
|---|
| 118 |  | 
|---|
| 119 |         // If the pickup has transited to used. | 
|---|
| 120 |         if(this->isUsed()) | 
|---|
| 121 |         { | 
|---|
| 122 |  | 
|---|
| 123 |                 Pawn* pawn = this->carrierToPawnHelper(); | 
|---|
| 124 |                 if(pawn == nullptr) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. | 
|---|
| 125 |                     this->Pickupable::destroy(); | 
|---|
| 126 |  | 
|---|
| 127 |                 //Attach to pawn | 
|---|
| 128 |                 Drone* drone = new Drone(pawn->getContext()); // this is neccessary because the projectiles fired need a valid creator for the particlespawner (when colliding against something) | 
|---|
| 129 |                 drone->addTemplate(this->getDroneTemplate()); | 
|---|
| 130 |  | 
|---|
| 131 |                 Controller* controller = drone->getController(); | 
|---|
| 132 |                 DroneController* droneController = orxonox_cast<DroneController*>(controller); | 
|---|
| 133 |                 if(droneController != nullptr) | 
|---|
| 134 |                 { | 
|---|
| 135 |                     droneController->setOwner(pawn); | 
|---|
| 136 |                 } | 
|---|
| 137 |  | 
|---|
| 138 |                 Vector3 spawnPosition = pawn->getWorldPosition() + Vector3(30,0,-30); | 
|---|
| 139 |                 drone->setPosition(spawnPosition); | 
|---|
| 140 |  | 
|---|
| 141 |                 // The pickup has been used up. | 
|---|
| 142 |                 this->setUsed(false); | 
|---|
| 143 |         } | 
|---|
| 144 |         else | 
|---|
| 145 |         { | 
|---|
| 146 |             // If either the pickup can only be used once or it is continuous and used up, it is destroyed upon setting it to unused. | 
|---|
| 147 |             if(this->isOnce() || (this->isContinuous() )) | 
|---|
| 148 |             { | 
|---|
| 149 |                 this->Pickupable::destroy(); | 
|---|
| 150 |             } | 
|---|
| 151 |         } | 
|---|
| 152 |     } | 
|---|
| 153 |  | 
|---|
| 154 |     /** | 
|---|
| 155 |     @brief | 
|---|
| 156 |         Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails. | 
|---|
| 157 |     @return | 
|---|
| 158 |         A pointer to the Pawn, or nullptr if the conversion failed. | 
|---|
| 159 |     */ | 
|---|
| 160 |     Pawn* DronePickup::carrierToPawnHelper(void) | 
|---|
| 161 |     { | 
|---|
| 162 |         PickupCarrier* carrier = this->getCarrier(); | 
|---|
| 163 |         Pawn* pawn = orxonox_cast<Pawn*>(carrier); | 
|---|
| 164 |  | 
|---|
| 165 |         if(pawn == nullptr) | 
|---|
| 166 |         { | 
|---|
| 167 |             orxout(internal_error, context::pickups) << "Invalid PickupCarrier in DronePickup." << endl; | 
|---|
| 168 |         } | 
|---|
| 169 |  | 
|---|
| 170 |         return pawn; | 
|---|
| 171 |     } | 
|---|
| 172 | } | 
|---|