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