| 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 |  *      Benedict Simlinger | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 |     @file InvisiblePickup.cc | 
|---|
| 31 |     @brief Implementation of the InvisiblePickup class. | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #include "InvisiblePickup.h" | 
|---|
| 35 |  | 
|---|
| 36 | #include <sstream> | 
|---|
| 37 | //#include <OgreEntity.h> | 
|---|
| 38 | //#include <OgreAnimationState.h> | 
|---|
| 39 | #include "core/CoreIncludes.h" | 
|---|
| 40 | #include "core/XMLPort.h" | 
|---|
| 41 |  | 
|---|
| 42 | #include "pickup/PickupIdentifier.h" | 
|---|
| 43 | #include "worldentities/pawns/Pawn.h" | 
|---|
| 44 |  | 
|---|
| 45 | namespace orxonox | 
|---|
| 46 | { | 
|---|
| 47 |  | 
|---|
| 48 |     CreateFactory(InvisiblePickup); | 
|---|
| 49 |  | 
|---|
| 50 |     /** | 
|---|
| 51 |     @brief | 
|---|
| 52 |         Constructor. Registers the object and initializes the member variables. | 
|---|
| 53 |     */ | 
|---|
| 54 |     InvisiblePickup::InvisiblePickup(BaseObject* creator) : Pickup(creator) | 
|---|
| 55 |     { | 
|---|
| 56 |         RegisterObject(InvisiblePickup); | 
|---|
| 57 |         this->initialize(); | 
|---|
| 58 |     } | 
|---|
| 59 |  | 
|---|
| 60 |     /** | 
|---|
| 61 |     @brief | 
|---|
| 62 |         Destructor. | 
|---|
| 63 |     */ | 
|---|
| 64 |     InvisiblePickup::~InvisiblePickup() | 
|---|
| 65 |     { | 
|---|
| 66 |     } | 
|---|
| 67 |  | 
|---|
| 68 |     /** | 
|---|
| 69 |     @brief | 
|---|
| 70 |     Initializes the member variables. | 
|---|
| 71 |     */ | 
|---|
| 72 |     void InvisiblePickup::initialize(void) | 
|---|
| 73 |     { | 
|---|
| 74 |         this->duration_ = 0.0f; | 
|---|
| 75 |         // Defines who is allowed to pick up the pickup. | 
|---|
| 76 |         this->addTarget(ClassIdentifier<Pawn>::getIdentifier()); | 
|---|
| 77 |     } | 
|---|
| 78 |  | 
|---|
| 79 |     /** | 
|---|
| 80 |     @brief | 
|---|
| 81 |         Initializes the PickupIdentifier of this pickup. | 
|---|
| 82 |     */ | 
|---|
| 83 |     void InvisiblePickup::initializeIdentifier(void) | 
|---|
| 84 |     { | 
|---|
| 85 |         std::stringstream stream; | 
|---|
| 86 |         stream << this->getDuration(); | 
|---|
| 87 |         std::string type1 = "duration"; | 
|---|
| 88 |         std::string val1 = stream.str(); | 
|---|
| 89 |         this->pickupIdentifier_->addParameter(type1, val1); | 
|---|
| 90 |     } | 
|---|
| 91 |  | 
|---|
| 92 |     /** | 
|---|
| 93 |     @brief | 
|---|
| 94 |         Method for creating a HealthPickup object through XML. | 
|---|
| 95 |     */ | 
|---|
| 96 |     void InvisiblePickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) | 
|---|
| 97 |     { | 
|---|
| 98 |         SUPER(InvisiblePickup, XMLPort, xmlelement, mode); | 
|---|
| 99 |         XMLPortParam(InvisiblePickup, "duration", setDuration, getDuration, xmlelement, mode); | 
|---|
| 100 |  | 
|---|
| 101 |         this->initializeIdentifier(); | 
|---|
| 102 |     } | 
|---|
| 103 |  | 
|---|
| 104 |     /** | 
|---|
| 105 |     @brief | 
|---|
| 106 |         Is called when the pickup has transited from used to unused or the other way around. | 
|---|
| 107 |     */ | 
|---|
| 108 |     void InvisiblePickup::changedUsed(void) | 
|---|
| 109 |     { | 
|---|
| 110 |         SUPER(InvisiblePickup, changedUsed); | 
|---|
| 111 |  | 
|---|
| 112 |         // If the pickup has transited to used. | 
|---|
| 113 |         if (this->isUsed()) | 
|---|
| 114 |         { | 
|---|
| 115 |             // If its durationType is continuous, we set a Timer to be reminded, when the time has run out. | 
|---|
| 116 |             if(this->isContinuous()) | 
|---|
| 117 |             { | 
|---|
| 118 |                 if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f) | 
|---|
| 119 |                 { | 
|---|
| 120 |                     this->durationTimer_.unpauseTimer(); | 
|---|
| 121 |                 } | 
|---|
| 122 |                 else | 
|---|
| 123 |                 { | 
|---|
| 124 |                     this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&InvisiblePickup::pickupTimerCallback, this))); | 
|---|
| 125 |                 } | 
|---|
| 126 |             } | 
|---|
| 127 |  | 
|---|
| 128 |             this->setInvisible(true); | 
|---|
| 129 |  | 
|---|
| 130 |         } | 
|---|
| 131 |         else | 
|---|
| 132 |         { | 
|---|
| 133 |             this->setInvisible(false); | 
|---|
| 134 |  | 
|---|
| 135 |             // We destroy the pickup if either, the pickup has activationType immediate and durationType once or it has durationType continuous and the duration was exceeded. | 
|---|
| 136 |             if((!this->isContinuous() && this->isImmediate()) || (this->isContinuous() && !this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration())) | 
|---|
| 137 |             { | 
|---|
| 138 |                 this->Pickupable::destroy(); | 
|---|
| 139 |             } | 
|---|
| 140 |             // We pause the Timer if the pickup is continuous and the duration is not yet exceeded, | 
|---|
| 141 |             else if(this->isContinuous() && this->durationTimer_.isActive()) | 
|---|
| 142 |             { | 
|---|
| 143 |                 this->durationTimer_.pauseTimer(); | 
|---|
| 144 |             } | 
|---|
| 145 |         } | 
|---|
| 146 |     } | 
|---|
| 147 |  | 
|---|
| 148 |     /** | 
|---|
| 149 |     @brief | 
|---|
| 150 |         Creates a duplicate of the input OrxonoxClass. | 
|---|
| 151 |     @param item | 
|---|
| 152 |         A pointer to the Orxonox class. | 
|---|
| 153 |     */ | 
|---|
| 154 |     void InvisiblePickup::clone(OrxonoxClass*& item) | 
|---|
| 155 |     { | 
|---|
| 156 |         if(item == NULL) | 
|---|
| 157 |             item = new InvisiblePickup(this); | 
|---|
| 158 |  | 
|---|
| 159 |         SUPER(InvisiblePickup, clone, item); | 
|---|
| 160 |  | 
|---|
| 161 |         InvisiblePickup* pickup = dynamic_cast<InvisiblePickup*>(item); | 
|---|
| 162 |         pickup->setDuration(this->getDuration()); | 
|---|
| 163 |         pickup->initializeIdentifier(); | 
|---|
| 164 |     } | 
|---|
| 165 |  | 
|---|
| 166 |     /** | 
|---|
| 167 |     @brief | 
|---|
| 168 |         Sets the invisibility. | 
|---|
| 169 |     @param invisibility | 
|---|
| 170 |         The invisibility. | 
|---|
| 171 |     */ | 
|---|
| 172 |     bool InvisiblePickup::setInvisible(bool invisibility) | 
|---|
| 173 |     { | 
|---|
| 174 |         Pawn* pawn = this->carrierToPawnHelper(); | 
|---|
| 175 |         if(pawn == NULL) | 
|---|
| 176 |             return false; | 
|---|
| 177 |  | 
|---|
| 178 |         pawn->setVisible(!invisibility); | 
|---|
| 179 |         //TODO: Invisibility should imply radar invisibility as well, thus this should be solved in Pawn. | 
|---|
| 180 |         pawn->setRadarVisibility(!invisibility); | 
|---|
| 181 |  | 
|---|
| 182 | // Test to change Material at runtime! | 
|---|
| 183 |  | 
|---|
| 184 | //      Ogre::MaterialPtr mat = this->mesh_.getEntity()->getSubEntity(0)->getMaterial(); | 
|---|
| 185 | //      mat->setDiffuse(0.4, 0.3, 0.1, 0.1); | 
|---|
| 186 | //      mat->setAmbient(0.3, 0.7, 0.8); | 
|---|
| 187 | //      mat->setSpecular(0.5, 0.5, 0.5, 0.1); | 
|---|
| 188 | //      Ogre::SceneBlendType sbt = Ogre::SBT_ADD; | 
|---|
| 189 | // | 
|---|
| 190 | //      mat->setSceneBlending(sbt); | 
|---|
| 191 |  | 
|---|
| 192 |         return true; | 
|---|
| 193 |     } | 
|---|
| 194 |  | 
|---|
| 195 |     /** | 
|---|
| 196 |     @brief | 
|---|
| 197 |         Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails. | 
|---|
| 198 |     @return | 
|---|
| 199 |         A pointer to the Pawn, or NULL if the conversion failed. | 
|---|
| 200 |     */ | 
|---|
| 201 |     Pawn* InvisiblePickup::carrierToPawnHelper(void) | 
|---|
| 202 |     { | 
|---|
| 203 |         PickupCarrier* carrier = this->getCarrier(); | 
|---|
| 204 |         Pawn* pawn = dynamic_cast<Pawn*>(carrier); | 
|---|
| 205 |  | 
|---|
| 206 |         if(pawn == NULL) | 
|---|
| 207 |         { | 
|---|
| 208 |             COUT(1) << "Invalid PickupCarrier in InvisiblePickup." << std::endl; | 
|---|
| 209 |         } | 
|---|
| 210 |         return pawn; | 
|---|
| 211 |     } | 
|---|
| 212 |  | 
|---|
| 213 |     /** | 
|---|
| 214 |     @brief | 
|---|
| 215 |         Sets the time the InvisibilityPickup will last. | 
|---|
| 216 |     @param duration | 
|---|
| 217 |         The duration in seconds. | 
|---|
| 218 |     */ | 
|---|
| 219 |     void InvisiblePickup::setDuration(float duration) | 
|---|
| 220 |     { | 
|---|
| 221 |         if(duration >= 0.0f) | 
|---|
| 222 |         { | 
|---|
| 223 |             this->duration_ = duration; | 
|---|
| 224 |         } | 
|---|
| 225 |         else | 
|---|
| 226 |         { | 
|---|
| 227 |             COUT(1) << "Invalid duration in InvisiblePickup." << std::endl; | 
|---|
| 228 |             this->duration_ = 0.0f; | 
|---|
| 229 |         } | 
|---|
| 230 |     } | 
|---|
| 231 |  | 
|---|
| 232 |     /** | 
|---|
| 233 |     @brief | 
|---|
| 234 |         Helper method. Is called by the Timer as soon as it expires. | 
|---|
| 235 |     */ | 
|---|
| 236 |     void InvisiblePickup::pickupTimerCallback(void) | 
|---|
| 237 |     { | 
|---|
| 238 |         this->setUsed(false); | 
|---|
| 239 |     } | 
|---|
| 240 |  | 
|---|
| 241 | } | 
|---|