[8489] | 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: |
---|
[8713] | 23 | * Sandro Sgier |
---|
[8489] | 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | @file ShrinkPickup.cc |
---|
| 32 | @brief Implementation of the HealthPickup class. |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | |
---|
[8375] | 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 | |
---|
[8489] | 44 | #include "worldentities/CameraPosition.h" |
---|
[8381] | 45 | |
---|
[8375] | 46 | namespace orxonox |
---|
| 47 | { |
---|
[9667] | 48 | RegisterClass(ShrinkPickup); |
---|
[8375] | 49 | |
---|
[8713] | 50 | /** |
---|
[8534] | 51 | @brief |
---|
| 52 | Constructor: Initializes the Pickup. |
---|
| 53 | */ |
---|
[9667] | 54 | ShrinkPickup::ShrinkPickup(Context* context) : Pickup(context) |
---|
[8375] | 55 | { |
---|
| 56 | RegisterObject(ShrinkPickup); |
---|
| 57 | |
---|
| 58 | this->initialize(); |
---|
[8713] | 59 | } |
---|
| 60 | |
---|
| 61 | ShrinkPickup::~ShrinkPickup() |
---|
| 62 | { |
---|
| 63 | |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | void ShrinkPickup::initialize(void) |
---|
| 67 | { |
---|
| 68 | this->addTarget(ClassIdentifier<Pawn>::getIdentifier()); |
---|
| 69 | |
---|
[8556] | 70 | this->shrinkFactor_ = 5.0f; |
---|
[8713] | 71 | this->shrinkDuration_ = 5.0f; |
---|
[8556] | 72 | this->duration_ = 5.0f; |
---|
[8713] | 73 | |
---|
[8556] | 74 | this->isActive_ = false; |
---|
[8713] | 75 | this->isShrinking_ = false; |
---|
[8556] | 76 | this->isTerminating_ = false; |
---|
| 77 | |
---|
[8713] | 78 | this->timeRemainig_ = 0.0f; |
---|
| 79 | this->currentFactor_ = 1.0f; |
---|
[8375] | 80 | } |
---|
| 81 | |
---|
[8534] | 82 | /** |
---|
| 83 | @brief |
---|
[8713] | 84 | Method for creating a ShrinkPickup object through XML. |
---|
[8534] | 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); |
---|
[8713] | 92 | XMLPortParam(ShrinkPickup, "shrinkDuration", setShrinkDuration, getShrinkDuration, xmlelement, mode); |
---|
[9348] | 93 | } |
---|
[8534] | 94 | |
---|
[9348] | 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; |
---|
[8534] | 107 | } |
---|
| 108 | |
---|
| 109 | /** |
---|
[9348] | 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 | /** |
---|
[8534] | 138 | @brief |
---|
[8713] | 139 | Prepares for shrinking. |
---|
[8534] | 140 | */ |
---|
[8713] | 141 | void ShrinkPickup::changedUsed(void) |
---|
[8534] | 142 | { |
---|
[8713] | 143 | SUPER(ShrinkPickup, changedUsed); |
---|
[8534] | 144 | |
---|
[8713] | 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 | } |
---|
[8534] | 153 | |
---|
[8713] | 154 | this->currentFactor_ = 1.0f; |
---|
| 155 | this->timeRemainig_ = this->shrinkDuration_; |
---|
[8534] | 156 | |
---|
[8713] | 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; |
---|
[8489] | 163 | } |
---|
[8375] | 164 | |
---|
[8713] | 165 | void ShrinkPickup::changedPickedUp(void) |
---|
[8489] | 166 | { |
---|
[8713] | 167 | SUPER(ShrinkPickup, changedPickedUp); |
---|
[9348] | 168 | |
---|
[8713] | 169 | if(!this->isPickedUp() && this->isActive_) |
---|
[8375] | 170 | { |
---|
[8713] | 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; |
---|
[8486] | 177 | |
---|
[8713] | 178 | float factor = 1.0f/this->currentFactor_; |
---|
[8489] | 179 | |
---|
[8713] | 180 | pawn->setScale3D(pawn->getScale3D()*factor); |
---|
| 181 | pawn->setMass(pawn->getMass()*factor); |
---|
[8489] | 182 | |
---|
[8713] | 183 | // Iterate over all camera positions and inversely move the camera to create a shrinking sensation. |
---|
| 184 | const std::list< SmartPtr<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; |
---|
[8489] | 205 | |
---|
[8713] | 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< SmartPtr<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 | } |
---|
[8375] | 223 | } |
---|
[8489] | 224 | } |
---|
[8375] | 225 | |
---|
[8554] | 226 | /** |
---|
[8534] | 227 | @brief |
---|
| 228 | Updates the scales of the ship. |
---|
| 229 | @param dt |
---|
| 230 | Time since last call. |
---|
| 231 | */ |
---|
[8489] | 232 | void ShrinkPickup::tick(float dt) |
---|
| 233 | { |
---|
[8713] | 234 | if(this->isActive_) |
---|
[8489] | 235 | { |
---|
[8713] | 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 | } |
---|
[8433] | 244 | |
---|
[8713] | 245 | this->timeRemainig_ -= dt; |
---|
[8433] | 246 | |
---|
[8713] | 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; |
---|
[8433] | 252 | |
---|
[8713] | 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< SmartPtr<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. |
---|
[8489] | 277 | { |
---|
[8713] | 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(); |
---|
[8381] | 281 | |
---|
[8713] | 282 | this->timeRemainig_ -= dt; |
---|
[8375] | 283 | |
---|
[8713] | 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; |
---|
[8489] | 289 | |
---|
[8713] | 290 | bool destroy = false; |
---|
[9348] | 291 | |
---|
[8713] | 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 | } |
---|
[8489] | 301 | |
---|
[8713] | 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< SmartPtr<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(); |
---|
[8489] | 318 | } |
---|
| 319 | } |
---|
| 320 | } |
---|
| 321 | |
---|
[8554] | 322 | /** |
---|
[8534] | 323 | @brief |
---|
| 324 | Initializes the termination. |
---|
| 325 | */ |
---|
[8489] | 326 | void ShrinkPickup::terminate(void) |
---|
| 327 | { |
---|
[8713] | 328 | this->setUsed(false); |
---|
[8489] | 329 | } |
---|
| 330 | |
---|
[8375] | 331 | Pawn* ShrinkPickup::carrierToPawnHelper(void) |
---|
| 332 | { |
---|
| 333 | PickupCarrier* carrier = this->getCarrier(); |
---|
[9348] | 334 | Pawn* pawn = orxonox_cast<Pawn*>(carrier); |
---|
[8375] | 335 | |
---|
| 336 | return pawn; |
---|
| 337 | } |
---|
| 338 | } |
---|