Changeset 8713 for code/trunk/src/modules/pickup/items/ShrinkPickup.cc
- Timestamp:
- Jun 27, 2011, 10:45:15 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/modules/pickup/items/ShrinkPickup.cc
r8706 r8713 21 21 * 22 22 * Author: 23 * Damian 'Mozork' Frick23 * Sandro Sgier 24 24 * Co-authors: 25 25 * ... … … 43 43 #include "worldentities/pawns/Pawn.h" 44 44 45 #include "weaponsystem/WeaponSlot.h"46 #include "weaponsystem/Weapon.h"47 45 #include "worldentities/CameraPosition.h" 48 46 … … 51 49 CreateFactory(ShrinkPickup); 52 50 53 51 /** 54 52 @brief 55 53 Constructor: Initializes the Pickup. … … 60 58 61 59 this->initialize(); 60 } 61 62 ShrinkPickup::~ShrinkPickup() 63 { 64 65 } 66 67 void ShrinkPickup::initialize(void) 68 { 69 this->addTarget(ClassIdentifier<Pawn>::getIdentifier()); 70 62 71 this->shrinkFactor_ = 5.0f; 63 this->shrink Speed_ = 5.0f;72 this->shrinkDuration_ = 5.0f; 64 73 this->duration_ = 5.0f; 74 65 75 this->isActive_ = false; 76 this->isShrinking_ = false; 66 77 this->isTerminating_ = false; 67 78 68 this->size_ = 0; 69 this->defaultCameraPos_ = 0.0f; 70 this->defaultScale_ = Vector3::UNIT_SCALE; 71 this->actualScale_ = Vector3::UNIT_SCALE; 72 this->smallScale_ = Vector3::UNIT_SCALE; 73 this->defaultMass_ = 1.0f; 74 this->actualMass_ = 1.0f; 75 this->smallMass_ = 1.0f; 76 this->pawn_ = NULL; 77 } 78 79 ShrinkPickup::~ShrinkPickup() 80 { 81 79 this->timeRemainig_ = 0.0f; 80 this->currentFactor_ = 1.0f; 82 81 } 83 82 … … 97 96 98 97 stream.clear(); 99 stream << this->getShrink Speed();98 stream << this->getShrinkDuration(); 100 99 std::string val3 = stream.str(); 101 std::string type3 = "shrink Speed";100 std::string type3 = "shrinkDuration"; 102 101 this->pickupIdentifier_->addParameter(type3, val3); 103 102 } … … 105 104 /** 106 105 @brief 107 Method for creating a Shrink hPickup object through XML.106 Method for creating a ShrinkPickup object through XML. 108 107 */ 109 108 void ShrinkPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) … … 113 112 XMLPortParam(ShrinkPickup, "shrinkFactor", setShrinkFactor, getShrinkFactor, xmlelement, mode); 114 113 XMLPortParam(ShrinkPickup, "duration", setDuration, getDuration, xmlelement, mode); 115 XMLPortParam(ShrinkPickup, "shrink Speed", setShrinkSpeed, getShrinkSpeed, xmlelement, mode);114 XMLPortParam(ShrinkPickup, "shrinkDuration", setShrinkDuration, getShrinkDuration, xmlelement, mode); 116 115 117 116 this->initializeIdentifier(); … … 120 119 /** 121 120 @brief 122 Sets the shrinking factor. 123 @param factor 124 The factor. 125 */ 126 void ShrinkPickup::setShrinkFactor(float factor) 127 { 128 this->shrinkFactor_ = factor; 129 } 130 131 /** 132 @brief 133 Sets the duration. 134 @param duration 135 The duration. 136 */ 137 void ShrinkPickup::setDuration(float duration) 138 { 139 this->duration_ = duration; 140 } 141 142 /** 143 @brief 144 Sets the shrinking speed. 145 @param speed 146 The speed. 147 */ 148 void ShrinkPickup::setShrinkSpeed(float speed) 149 { 150 this->shrinkSpeed_ = speed; 151 } 152 153 void ShrinkPickup::initialize(void) 154 { 155 this->addTarget(ClassIdentifier<Pawn>::getIdentifier()); 156 } 157 158 /** 159 @brief 160 Prepares for shrinking (collecting several informations). 121 Prepares for shrinking. 161 122 */ 162 123 void ShrinkPickup::changedUsed(void) … … 166 127 if(this->isUsed()) 167 128 { 168 this->pawn_ = this->carrierToPawnHelper(); 169 if(this->pawn_ == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. 129 Pawn* pawn = this->carrierToPawnHelper(); 130 if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. 131 { 170 132 this->Pickupable::destroy(); 171 172 //Collect scaling information. 173 this->defaultScale_ = this->pawn_->getScale3D(); 174 this->defaultMass_ = this->pawn_->getMass(); 175 176 this->smallScale_ = this->defaultScale_ / this->shrinkFactor_; 177 this->smallMass_ = this->defaultMass_ / this->shrinkFactor_; 178 179 this->actualScale_ = this->defaultScale_; 180 this->actualMass_ = this->defaultMass_; 181 182 this->cameraPositions_ = this->pawn_->getCameraPositions(); 183 this->size_ = this->cameraPositions_.size(); 184 this->isActive_ = true; //start shrinking now. 185 this->durationTimer_.setTimer(this->duration_, false, createExecutor(createFunctor(&ShrinkPickup::terminate, this))); //Set timer for termination. 133 return; 134 } 135 136 this->currentFactor_ = 1.0f; 137 this->timeRemainig_ = this->shrinkDuration_; 138 139 this->isActive_ = true; // Start shrinking now. 140 this->isShrinking_ = true; 141 this->durationTimer_.setTimer(this->duration_, false, createExecutor(createFunctor(&ShrinkPickup::terminate, this))); //Set timer for termination. 142 } 143 if(!this->isUsed() && this->isActive_) 144 this->isTerminating_ = true; 145 } 146 147 void ShrinkPickup::changedPickedUp(void) 148 { 149 SUPER(ShrinkPickup, changedPickedUp); 150 151 if(!this->isPickedUp() && this->isActive_) 152 { 153 if(this->isShrinking_ || this->isTerminating_) 154 { 155 //TODO: Deploy particle effect. 156 Pawn* pawn = this->carrierToPawnHelper(); 157 if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. 158 return; 159 160 float factor = 1.0f/this->currentFactor_; 161 162 pawn->setScale3D(pawn->getScale3D()*factor); 163 pawn->setMass(pawn->getMass()*factor); 164 165 // Iterate over all camera positions and inversely move the camera to create a shrinking sensation. 166 const std::list< SmartPtr<CameraPosition> >& cameraPositions = pawn->getCameraPositions(); 167 int size = cameraPositions.size(); 168 for(int index = 0; index < size; index++) 169 { 170 CameraPosition* cameraPos = pawn->getCameraPosition(index); 171 if(cameraPos == NULL) 172 continue; 173 cameraPos->setPosition(cameraPos->getPosition()/factor); 174 } 175 this->currentFactor_ = 1.0f; 176 this->timeRemainig_ = this->shrinkDuration_; 177 this->isActive_ = false; 178 this->isShrinking_ = false; 179 this->isTerminating_ = false; 180 } 181 else 182 { 183 //TODO: Deploy particle effect. 184 Pawn* pawn = this->carrierToPawnHelper(); 185 if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. 186 return; 187 188 pawn->setScale3D(pawn->getScale3D()*this->shrinkFactor_); 189 pawn->setMass(pawn->getMass()*this->shrinkFactor_); 190 191 // Iterate over all camera positions and inversely move the camera to create a shrinking sensation. 192 const std::list< SmartPtr<CameraPosition> >& cameraPositions = pawn->getCameraPositions(); 193 int size = cameraPositions.size(); 194 for(int index = 0; index < size; index++) 195 { 196 CameraPosition* cameraPos = pawn->getCameraPosition(index); 197 if(cameraPos == NULL) 198 continue; 199 cameraPos->setPosition(cameraPos->getPosition()/this->shrinkFactor_); 200 } 201 this->currentFactor_ = 1.0f; 202 this->timeRemainig_ = this->shrinkDuration_; 203 this->isActive_ = false; 204 } 186 205 } 187 206 } … … 195 214 void ShrinkPickup::tick(float dt) 196 215 { 197 if(this->isActive_ == true && this->actualScale_ > this->smallScale_) //if the ship has not reached the target scale, continue shrinking216 if(this->isActive_) 198 217 { 199 float factor = 1 + dt*this->shrinkSpeed_; 200 201 this->actualScale_ /= factor; 202 this->actualMass_ /= factor; 203 204 this->pawn_->setScale3D(this->actualScale_); 205 this->pawn_->setMass(this->actualMass_); 206 207 for(int index = 0; index < this->size_; index++) 208 { 209 CameraPosition* cameraPos = this->pawn_->getCameraPosition(index); 210 if(cameraPos == NULL) 211 continue; 212 cameraPos->setPosition(cameraPos->getPosition()*factor); 218 if(this->isShrinking_) // If the ship has not reached the target scale, continue shrinking 219 { 220 Pawn* pawn = this->carrierToPawnHelper(); 221 if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. 222 { 223 this->Pickupable::destroy(); 224 return; 225 } 226 227 this->timeRemainig_ -= dt; 228 229 // Calculate the scaling factor by which the initial size would have to be scaled to have the current scale. 230 float currentFactor = std::max(1 - (1-std::max(this->timeRemainig_, 0.0f)/this->shrinkDuration_)*(1-1/this->shrinkFactor_), 1/this->shrinkFactor_); 231 // Calculate the factor by which the previous size has to be scaled to be the current scale. 232 float factor = currentFactor/this->currentFactor_; 233 this->currentFactor_ = currentFactor; 234 235 // Stop shrinking if the desired size is reached. 236 if(this->timeRemainig_ <= 0.0f) 237 { 238 this->timeRemainig_ = this->shrinkDuration_; // Reset the time remaining for when we start to grow the ship again. 239 this->currentFactor_ = 1/this->shrinkFactor_; 240 this->isShrinking_ = false; 241 } 242 243 pawn->setScale3D(pawn->getScale3D()*factor); 244 pawn->setMass(pawn->getMass()*factor); 245 246 // Iterate over all camera positions and inversely move the camera to create a shrinking sensation. 247 const std::list< SmartPtr<CameraPosition> >& cameraPositions = pawn->getCameraPositions(); 248 int size = cameraPositions.size(); 249 for(int index = 0; index < size; index++) 250 { 251 CameraPosition* cameraPos = pawn->getCameraPosition(index); 252 if(cameraPos == NULL) 253 continue; 254 cameraPos->setPosition(cameraPos->getPosition()/factor); 255 } 256 257 } 258 else if(this->isTerminating_) // Grow until the ship reaches its default scale. 259 { 260 Pawn* pawn = this->carrierToPawnHelper(); 261 if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. 262 this->Pickupable::destroy(); 263 264 this->timeRemainig_ -= dt; 265 266 // Calculate the scaling factor by which the initial size would have to be scaled to have the current scale. 267 float currentFactor = std::min(1/this->shrinkFactor_ + (1-std::max(this->timeRemainig_, 0.0f)/this->shrinkDuration_)*(1-1/this->shrinkFactor_), 1.0f); 268 // Calculate the factor by which the previous size has to be scaled to be the current scale. 269 float factor = currentFactor/this->currentFactor_; 270 this->currentFactor_ = currentFactor; 271 272 bool destroy = false; 273 274 // Stop shrinking if the desired size is reached. 275 if(this->timeRemainig_ <= 0.0f) 276 { 277 this->timeRemainig_ = shrinkDuration_; // Reset the time remaining for when we start to grow the ship again. 278 this->currentFactor_ = 1.0f; 279 this->isTerminating_ = false; 280 this->isActive_ = false; 281 destroy = true; 282 } 283 284 pawn->setScale3D(pawn->getScale3D()*factor); 285 pawn->setMass(pawn->getMass()*factor); 286 287 // Iterate over all camera positions and inversely move the camera to create a shrinking sensation. 288 const std::list< SmartPtr<CameraPosition> >& cameraPositions = pawn->getCameraPositions(); 289 int size = cameraPositions.size(); 290 for(int index = 0; index < size; index++) 291 { 292 CameraPosition* cameraPos = pawn->getCameraPosition(index); 293 if(cameraPos == NULL) 294 continue; 295 cameraPos->setPosition(cameraPos->getPosition()/factor); 296 } 297 298 if(destroy) 299 this->Pickupable::destroy(); 213 300 } 214 301 } 215 else this->isActive_ = false;216 217 if(this->isTerminating_ == true && this->actualScale_ < this->defaultScale_) //grow until the ship reaches its default scale.218 {219 float factor = 1 + dt*this->shrinkSpeed_;220 221 this->actualScale_ *= factor;222 this->actualMass_ *= factor;223 224 this->pawn_->setScale3D(this->actualScale_);225 this->pawn_->setMass(this->actualMass_);226 227 for(int index = 0; index < this->size_; index++)228 {229 CameraPosition* cameraPos = this->pawn_->getCameraPosition(index);230 if(cameraPos == NULL)231 continue;232 cameraPos->setPosition(cameraPos->getPosition()/factor);233 }234 }235 else if(this->isTerminating_ == true)236 this->Pickupable::destroy();237 238 302 } 239 303 … … 244 308 void ShrinkPickup::terminate(void) 245 309 { 246 this->isActive_ = false; 247 this->isTerminating_ = true; 248 setUsed(false); 310 this->setUsed(false); 249 311 } 250 312 … … 272 334 pickup->setShrinkFactor(this->getShrinkFactor()); 273 335 pickup->setDuration(this->getDuration()); 274 pickup->setShrink Speed(this->getShrinkSpeed());336 pickup->setShrinkDuration(this->getShrinkDuration()); 275 337 276 338 pickup->initializeIdentifier();
Note: See TracChangeset
for help on using the changeset viewer.