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