| 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 | *      Sandro Sgier | 
|---|
| 24 | *   Co-authors: | 
|---|
| 25 | *      ... | 
|---|
| 26 | * | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | /** | 
|---|
| 31 | @file ShrinkPickup.cc | 
|---|
| 32 | @brief Implementation of the HealthPickup class. | 
|---|
| 33 | */ | 
|---|
| 34 |  | 
|---|
| 35 |  | 
|---|
| 36 | #include "ShrinkPickup.h" | 
|---|
| 37 |  | 
|---|
| 38 | #include <sstream> | 
|---|
| 39 | #include "core/CoreIncludes.h" | 
|---|
| 40 | #include "core/XMLPort.h" | 
|---|
| 41 |  | 
|---|
| 42 | #include "worldentities/pawns/Pawn.h" | 
|---|
| 43 |  | 
|---|
| 44 | #include "worldentities/CameraPosition.h" | 
|---|
| 45 |  | 
|---|
| 46 | namespace orxonox | 
|---|
| 47 | { | 
|---|
| 48 | RegisterClass(ShrinkPickup); | 
|---|
| 49 |  | 
|---|
| 50 | /** | 
|---|
| 51 | @brief | 
|---|
| 52 | Constructor: Initializes the Pickup. | 
|---|
| 53 | */ | 
|---|
| 54 | ShrinkPickup::ShrinkPickup(Context* context) : Pickup(context) | 
|---|
| 55 | { | 
|---|
| 56 | RegisterObject(ShrinkPickup); | 
|---|
| 57 |  | 
|---|
| 58 | this->initialize(); | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | ShrinkPickup::~ShrinkPickup() | 
|---|
| 62 | { | 
|---|
| 63 |  | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | void ShrinkPickup::initialize(void) | 
|---|
| 67 | { | 
|---|
| 68 | this->addTarget(ClassIdentifier<Pawn>::getIdentifier()); | 
|---|
| 69 |  | 
|---|
| 70 | this->shrinkFactor_ = 5.0f; | 
|---|
| 71 | this->shrinkDuration_ = 5.0f; | 
|---|
| 72 | this->duration_ = 5.0f; | 
|---|
| 73 |  | 
|---|
| 74 | this->isActive_ = false; | 
|---|
| 75 | this->isShrinking_ = false; | 
|---|
| 76 | this->isTerminating_ = false; | 
|---|
| 77 |  | 
|---|
| 78 | this->timeRemainig_ = 0.0f; | 
|---|
| 79 | this->currentFactor_ = 1.0f; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | /** | 
|---|
| 83 | @brief | 
|---|
| 84 | Method for creating a ShrinkPickup object through XML. | 
|---|
| 85 | */ | 
|---|
| 86 | void ShrinkPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) | 
|---|
| 87 | { | 
|---|
| 88 | SUPER(ShrinkPickup, XMLPort, xmlelement, mode); | 
|---|
| 89 |  | 
|---|
| 90 | XMLPortParam(ShrinkPickup, "shrinkFactor", setShrinkFactor, getShrinkFactor, xmlelement, mode); | 
|---|
| 91 | XMLPortParam(ShrinkPickup, "duration", setDuration, getDuration, xmlelement, mode); | 
|---|
| 92 | XMLPortParam(ShrinkPickup, "shrinkDuration", setShrinkDuration, getShrinkDuration, xmlelement, mode); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | /** | 
|---|
| 96 | @brief Sets the shrinking factor. | 
|---|
| 97 | @param factor The factor, needs to greater than 1. | 
|---|
| 98 | */ | 
|---|
| 99 | void ShrinkPickup::setShrinkFactor(float factor) | 
|---|
| 100 | { | 
|---|
| 101 | if(factor <= 1.0f) | 
|---|
| 102 | { | 
|---|
| 103 | orxout(internal_warning, context::pickups) << "Invalid shrinking factor in ShrinkPickup. Ignoring.." << endl; | 
|---|
| 104 | return; | 
|---|
| 105 | } | 
|---|
| 106 | this->shrinkFactor_ = factor; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | /** | 
|---|
| 110 | @brief Set the duration for which the ship remains shrunken. | 
|---|
| 111 | @param duration The duration, needs to be non-negative. | 
|---|
| 112 | */ | 
|---|
| 113 | void ShrinkPickup::setDuration(float duration) | 
|---|
| 114 | { | 
|---|
| 115 | if(duration < 0.0f) | 
|---|
| 116 | { | 
|---|
| 117 | orxout(internal_warning, context::pickups) << "Invalid duration in ShrinkPickup. Ignoring.." << endl; | 
|---|
| 118 | return; | 
|---|
| 119 | } | 
|---|
| 120 | this->duration_ = duration; | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | /** | 
|---|
| 124 | @brief Set the shrink duration. | 
|---|
| 125 | @param speed The shrink duration, needs to be positive. | 
|---|
| 126 | */ | 
|---|
| 127 | void ShrinkPickup::setShrinkDuration(float speed) | 
|---|
| 128 | { | 
|---|
| 129 | if(speed <= 0.0f) | 
|---|
| 130 | { | 
|---|
| 131 | orxout(internal_warning, context::pickups) << "Invalid shrink duration in ShrinkPickup. Ignoring.." << endl; | 
|---|
| 132 | return; | 
|---|
| 133 | } | 
|---|
| 134 | this->shrinkDuration_ = speed; | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | /** | 
|---|
| 138 | @brief | 
|---|
| 139 | Prepares for shrinking. | 
|---|
| 140 | */ | 
|---|
| 141 | void ShrinkPickup::changedUsed(void) | 
|---|
| 142 | { | 
|---|
| 143 | SUPER(ShrinkPickup, changedUsed); | 
|---|
| 144 |  | 
|---|
| 145 | if(this->isUsed()) | 
|---|
| 146 | { | 
|---|
| 147 | Pawn* pawn = this->carrierToPawnHelper(); | 
|---|
| 148 | if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. | 
|---|
| 149 | { | 
|---|
| 150 | this->Pickupable::destroy(); | 
|---|
| 151 | return; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | this->currentFactor_ = 1.0f; | 
|---|
| 155 | this->timeRemainig_ = this->shrinkDuration_; | 
|---|
| 156 |  | 
|---|
| 157 | this->isActive_ = true; // Start shrinking now. | 
|---|
| 158 | this->isShrinking_ = true; | 
|---|
| 159 | this->durationTimer_.setTimer(this->duration_, false, createExecutor(createFunctor(&ShrinkPickup::terminate, this))); //Set timer for termination. | 
|---|
| 160 | } | 
|---|
| 161 | if(!this->isUsed() && this->isActive_) | 
|---|
| 162 | this->isTerminating_ = true; | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | void ShrinkPickup::changedPickedUp(void) | 
|---|
| 166 | { | 
|---|
| 167 | SUPER(ShrinkPickup, changedPickedUp); | 
|---|
| 168 |  | 
|---|
| 169 | if(!this->isPickedUp() && this->isActive_) | 
|---|
| 170 | { | 
|---|
| 171 | if(this->isShrinking_ || this->isTerminating_) | 
|---|
| 172 | { | 
|---|
| 173 | //TODO: Deploy particle effect. | 
|---|
| 174 | Pawn* pawn = this->carrierToPawnHelper(); | 
|---|
| 175 | if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. | 
|---|
| 176 | return; | 
|---|
| 177 |  | 
|---|
| 178 | float factor = 1.0f/this->currentFactor_; | 
|---|
| 179 |  | 
|---|
| 180 | pawn->setScale3D(pawn->getScale3D()*factor); | 
|---|
| 181 | pawn->setMass(pawn->getMass()*factor); | 
|---|
| 182 |  | 
|---|
| 183 | // Iterate over all camera positions and inversely move the camera to create a shrinking sensation. | 
|---|
| 184 | const std::list< StrongPtr<CameraPosition> >& cameraPositions = pawn->getCameraPositions(); | 
|---|
| 185 | int size = cameraPositions.size(); | 
|---|
| 186 | for(int index = 0; index < size; index++) | 
|---|
| 187 | { | 
|---|
| 188 | CameraPosition* cameraPos = pawn->getCameraPosition(index); | 
|---|
| 189 | if(cameraPos == NULL) | 
|---|
| 190 | continue; | 
|---|
| 191 | cameraPos->setPosition(cameraPos->getPosition()/factor); | 
|---|
| 192 | } | 
|---|
| 193 | this->currentFactor_ = 1.0f; | 
|---|
| 194 | this->timeRemainig_ = this->shrinkDuration_; | 
|---|
| 195 | this->isActive_ = false; | 
|---|
| 196 | this->isShrinking_ = false; | 
|---|
| 197 | this->isTerminating_ = false; | 
|---|
| 198 | } | 
|---|
| 199 | else | 
|---|
| 200 | { | 
|---|
| 201 | //TODO: Deploy particle effect. | 
|---|
| 202 | Pawn* pawn = this->carrierToPawnHelper(); | 
|---|
| 203 | if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. | 
|---|
| 204 | return; | 
|---|
| 205 |  | 
|---|
| 206 | pawn->setScale3D(pawn->getScale3D()*this->shrinkFactor_); | 
|---|
| 207 | pawn->setMass(pawn->getMass()*this->shrinkFactor_); | 
|---|
| 208 |  | 
|---|
| 209 | // Iterate over all camera positions and inversely move the camera to create a shrinking sensation. | 
|---|
| 210 | const std::list< StrongPtr<CameraPosition> >& cameraPositions = pawn->getCameraPositions(); | 
|---|
| 211 | int size = cameraPositions.size(); | 
|---|
| 212 | for(int index = 0; index < size; index++) | 
|---|
| 213 | { | 
|---|
| 214 | CameraPosition* cameraPos = pawn->getCameraPosition(index); | 
|---|
| 215 | if(cameraPos == NULL) | 
|---|
| 216 | continue; | 
|---|
| 217 | cameraPos->setPosition(cameraPos->getPosition()/this->shrinkFactor_); | 
|---|
| 218 | } | 
|---|
| 219 | this->currentFactor_ = 1.0f; | 
|---|
| 220 | this->timeRemainig_ = this->shrinkDuration_; | 
|---|
| 221 | this->isActive_ = false; | 
|---|
| 222 | } | 
|---|
| 223 | } | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | /** | 
|---|
| 227 | @brief | 
|---|
| 228 | Updates the scales of the ship. | 
|---|
| 229 | @param dt | 
|---|
| 230 | Time since last call. | 
|---|
| 231 | */ | 
|---|
| 232 | void ShrinkPickup::tick(float dt) | 
|---|
| 233 | { | 
|---|
| 234 | if(this->isActive_) | 
|---|
| 235 | { | 
|---|
| 236 | if(this->isShrinking_)    // If the ship has not reached the target scale, continue shrinking | 
|---|
| 237 | { | 
|---|
| 238 | Pawn* pawn = this->carrierToPawnHelper(); | 
|---|
| 239 | if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. | 
|---|
| 240 | { | 
|---|
| 241 | this->Pickupable::destroy(); | 
|---|
| 242 | return; | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 245 | this->timeRemainig_ -= dt; | 
|---|
| 246 |  | 
|---|
| 247 | // Calculate the scaling factor by which the initial size would have to be scaled to have the current scale. | 
|---|
| 248 | float currentFactor = std::max(1 - (1-std::max(this->timeRemainig_, 0.0f)/this->shrinkDuration_)*(1-1/this->shrinkFactor_), 1/this->shrinkFactor_); | 
|---|
| 249 | // Calculate the factor by which the previous size has to be scaled to be the current scale. | 
|---|
| 250 | float factor = currentFactor/this->currentFactor_; | 
|---|
| 251 | this->currentFactor_ = currentFactor; | 
|---|
| 252 |  | 
|---|
| 253 | // Stop shrinking if the desired size is reached. | 
|---|
| 254 | if(this->timeRemainig_ <= 0.0f) | 
|---|
| 255 | { | 
|---|
| 256 | this->timeRemainig_ = this->shrinkDuration_; // Reset the time remaining for when we start to grow the ship again. | 
|---|
| 257 | this->currentFactor_ = 1/this->shrinkFactor_; | 
|---|
| 258 | this->isShrinking_ = false; | 
|---|
| 259 | } | 
|---|
| 260 |  | 
|---|
| 261 | pawn->setScale3D(pawn->getScale3D()*factor); | 
|---|
| 262 | pawn->setMass(pawn->getMass()*factor); | 
|---|
| 263 |  | 
|---|
| 264 | // Iterate over all camera positions and inversely move the camera to create a shrinking sensation. | 
|---|
| 265 | const std::list< StrongPtr<CameraPosition> >& cameraPositions = pawn->getCameraPositions(); | 
|---|
| 266 | int size = cameraPositions.size(); | 
|---|
| 267 | for(int index = 0; index < size; index++) | 
|---|
| 268 | { | 
|---|
| 269 | CameraPosition* cameraPos = pawn->getCameraPosition(index); | 
|---|
| 270 | if(cameraPos == NULL) | 
|---|
| 271 | continue; | 
|---|
| 272 | cameraPos->setPosition(cameraPos->getPosition()/factor); | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | } | 
|---|
| 276 | else if(this->isTerminating_)    // Grow until the ship reaches its default scale. | 
|---|
| 277 | { | 
|---|
| 278 | Pawn* pawn = this->carrierToPawnHelper(); | 
|---|
| 279 | if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. | 
|---|
| 280 | this->Pickupable::destroy(); | 
|---|
| 281 |  | 
|---|
| 282 | this->timeRemainig_ -= dt; | 
|---|
| 283 |  | 
|---|
| 284 | // Calculate the scaling factor by which the initial size would have to be scaled to have the current scale. | 
|---|
| 285 | float currentFactor = std::min(1/this->shrinkFactor_ + (1-std::max(this->timeRemainig_, 0.0f)/this->shrinkDuration_)*(1-1/this->shrinkFactor_), 1.0f); | 
|---|
| 286 | // Calculate the factor by which the previous size has to be scaled to be the current scale. | 
|---|
| 287 | float factor = currentFactor/this->currentFactor_; | 
|---|
| 288 | this->currentFactor_ = currentFactor; | 
|---|
| 289 |  | 
|---|
| 290 | bool destroy = false; | 
|---|
| 291 |  | 
|---|
| 292 | // Stop shrinking if the desired size is reached. | 
|---|
| 293 | if(this->timeRemainig_ <= 0.0f) | 
|---|
| 294 | { | 
|---|
| 295 | this->timeRemainig_ = shrinkDuration_; // Reset the time remaining for when we start to grow the ship again. | 
|---|
| 296 | this->currentFactor_ = 1.0f; | 
|---|
| 297 | this->isTerminating_ = false; | 
|---|
| 298 | this->isActive_ = false; | 
|---|
| 299 | destroy = true; | 
|---|
| 300 | } | 
|---|
| 301 |  | 
|---|
| 302 | pawn->setScale3D(pawn->getScale3D()*factor); | 
|---|
| 303 | pawn->setMass(pawn->getMass()*factor); | 
|---|
| 304 |  | 
|---|
| 305 | // Iterate over all camera positions and inversely move the camera to create a shrinking sensation. | 
|---|
| 306 | const std::list< StrongPtr<CameraPosition> >& cameraPositions = pawn->getCameraPositions(); | 
|---|
| 307 | int size = cameraPositions.size(); | 
|---|
| 308 | for(int index = 0; index < size; index++) | 
|---|
| 309 | { | 
|---|
| 310 | CameraPosition* cameraPos = pawn->getCameraPosition(index); | 
|---|
| 311 | if(cameraPos == NULL) | 
|---|
| 312 | continue; | 
|---|
| 313 | cameraPos->setPosition(cameraPos->getPosition()/factor); | 
|---|
| 314 | } | 
|---|
| 315 |  | 
|---|
| 316 | if(destroy) | 
|---|
| 317 | this->Pickupable::destroy(); | 
|---|
| 318 | } | 
|---|
| 319 | } | 
|---|
| 320 | } | 
|---|
| 321 |  | 
|---|
| 322 | /** | 
|---|
| 323 | @brief | 
|---|
| 324 | Initializes the termination. | 
|---|
| 325 | */ | 
|---|
| 326 | void ShrinkPickup::terminate(void) | 
|---|
| 327 | { | 
|---|
| 328 | this->setUsed(false); | 
|---|
| 329 | } | 
|---|
| 330 |  | 
|---|
| 331 | Pawn* ShrinkPickup::carrierToPawnHelper(void) | 
|---|
| 332 | { | 
|---|
| 333 | PickupCarrier* carrier = this->getCarrier(); | 
|---|
| 334 | Pawn* pawn = orxonox_cast<Pawn*>(carrier); | 
|---|
| 335 |  | 
|---|
| 336 | return pawn; | 
|---|
| 337 | } | 
|---|
| 338 | } | 
|---|