[2917] | 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 | * Daniel 'Huty' Haggenmueller |
---|
| 24 | * Co-authors: |
---|
[6405] | 25 | * Damian 'Mozork' Frick |
---|
[2917] | 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
[6540] | 30 | @file PickupSpawner.cc |
---|
| 31 | @brief Implementation of the PickupSpawner class. |
---|
[2917] | 32 | */ |
---|
| 33 | |
---|
| 34 | #include "PickupSpawner.h" |
---|
[3196] | 35 | |
---|
[2917] | 36 | #include "core/CoreIncludes.h" |
---|
[3196] | 37 | #include "core/Template.h" |
---|
[2917] | 38 | #include "core/XMLPort.h" |
---|
[5735] | 39 | #include "worldentities/pawns/Pawn.h" |
---|
[6466] | 40 | #include "PickupManager.h" |
---|
| 41 | #include "PickupRepresentation.h" |
---|
[2917] | 42 | |
---|
| 43 | namespace orxonox |
---|
| 44 | { |
---|
[5953] | 45 | |
---|
[2917] | 46 | CreateFactory(PickupSpawner); |
---|
| 47 | |
---|
| 48 | /** |
---|
[5947] | 49 | @brief |
---|
[6421] | 50 | Constructor. Creates a blank PickupSpawner. |
---|
[5947] | 51 | @param creator |
---|
| 52 | Pointer to the object which created this item. |
---|
[2917] | 53 | */ |
---|
[6540] | 54 | PickupSpawner::PickupSpawner(BaseObject* creator) : StaticEntity(creator), pickup_(NULL) |
---|
[2917] | 55 | { |
---|
[6540] | 56 | RegisterObject(PickupSpawner); |
---|
| 57 | |
---|
[6514] | 58 | this->initialize(); |
---|
[5953] | 59 | } |
---|
| 60 | |
---|
[6421] | 61 | /** |
---|
| 62 | @brief |
---|
| 63 | Constructor, Creates a fully functional PickupSpawner. |
---|
| 64 | @param creator |
---|
| 65 | The creator of this PickupSpawner. |
---|
| 66 | @param pickup |
---|
| 67 | The Pickupable to be spawned by this PickupSpawner. |
---|
| 68 | @param triggerDistance |
---|
| 69 | The distance at which the PickupSpawner will trigger. |
---|
| 70 | @param respawnTime |
---|
| 71 | The minimum time between two spawns. |
---|
| 72 | @param maySpawnedItems |
---|
| 73 | The maximum number of items spawned by this PickupSpawner. |
---|
| 74 | */ |
---|
[6540] | 75 | PickupSpawner::PickupSpawner(BaseObject* creator, Pickupable* pickup, float triggerDistance, float respawnTime, int maxSpawnedItems) : StaticEntity(creator), pickup_(NULL) |
---|
[5953] | 76 | { |
---|
[6466] | 77 | RegisterObject(PickupSpawner); |
---|
| 78 | |
---|
[5953] | 79 | this->initialize(); |
---|
| 80 | |
---|
[6405] | 81 | this->pickup_ = pickup; |
---|
[5953] | 82 | |
---|
| 83 | this->triggerDistance_ = triggerDistance; |
---|
| 84 | this->respawnTime_ = respawnTime; |
---|
| 85 | this->setMaxSpawnedItems(maxSpawnedItems); |
---|
[6466] | 86 | |
---|
[6475] | 87 | if(this->pickup_ == NULL) |
---|
| 88 | { |
---|
| 89 | COUT(2) << "A PickupSpawner was created without a valid Pickupable. This won't work." << std::endl; |
---|
| 90 | this->setActive(false); |
---|
| 91 | } |
---|
| 92 | else |
---|
| 93 | { |
---|
| 94 | PickupRepresentation* representation = PickupManager::getInstance().getRepresentation(this->pickup_->getPickupIdentifier()); |
---|
| 95 | this->attach(representation->getSpawnerRepresentation(this)); |
---|
| 96 | } |
---|
[5953] | 97 | } |
---|
| 98 | |
---|
[6421] | 99 | /** |
---|
| 100 | @brief |
---|
| 101 | Registers the object and sets some default values. |
---|
| 102 | */ |
---|
[5953] | 103 | void PickupSpawner::initialize(void) |
---|
| 104 | { |
---|
[6563] | 105 | this->triggerDistance_ = 10; |
---|
[6405] | 106 | this->respawnTime_ = 0; |
---|
[5953] | 107 | this->maxSpawnedItems_ = INF; |
---|
| 108 | this->spawnsRemaining_ = INF; |
---|
[2917] | 109 | } |
---|
[5947] | 110 | |
---|
| 111 | /** |
---|
| 112 | @brief |
---|
| 113 | Destructor. |
---|
| 114 | */ |
---|
[2917] | 115 | PickupSpawner::~PickupSpawner() |
---|
| 116 | { |
---|
[6421] | 117 | if(this->pickup_ != NULL) |
---|
[6478] | 118 | this->pickup_->destroy(); |
---|
[2917] | 119 | } |
---|
[5947] | 120 | |
---|
[2917] | 121 | /** |
---|
[5947] | 122 | @brief |
---|
| 123 | Method for creating a PickupSpawner through XML. |
---|
| 124 | @param xmlelement |
---|
| 125 | XML element which contains the PickupSpawner. |
---|
| 126 | @param mode |
---|
| 127 | XMLPort mode. |
---|
[2917] | 128 | */ |
---|
| 129 | void PickupSpawner::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 130 | { |
---|
| 131 | SUPER(PickupSpawner, XMLPort, xmlelement, mode); |
---|
| 132 | |
---|
[6421] | 133 | XMLPortObject(PickupSpawner, Pickupable, "pickup", setPickupable, getPickupable, xmlelement, mode); |
---|
[6405] | 134 | |
---|
[2917] | 135 | XMLPortParam(PickupSpawner, "triggerDistance", setTriggerDistance, getTriggerDistance, xmlelement, mode); |
---|
| 136 | XMLPortParam(PickupSpawner, "respawnTime", setRespawnTime, getRespawnTime, xmlelement, mode); |
---|
[5953] | 137 | XMLPortParam(PickupSpawner, "maxSpawnedItems", setMaxSpawnedItems, getMaxSpawnedItems, xmlelement, mode); |
---|
[6475] | 138 | |
---|
| 139 | if(this->pickup_ == NULL) |
---|
[6405] | 140 | { |
---|
[6475] | 141 | COUT(2) << "A PickupSpawner was created without a valid Pickupable. This won't work." << std::endl; |
---|
| 142 | this->setActive(false); |
---|
[6405] | 143 | } |
---|
[6475] | 144 | else |
---|
[6405] | 145 | { |
---|
[6475] | 146 | PickupRepresentation* representation = PickupManager::getInstance().getRepresentation(this->pickup_->getPickupIdentifier()); |
---|
[6484] | 147 | representation->setVisible(this->isActive()); |
---|
[6475] | 148 | this->attach(representation->getSpawnerRepresentation(this)); |
---|
[6484] | 149 | this->setActive(true); |
---|
[6405] | 150 | } |
---|
[2917] | 151 | } |
---|
[6405] | 152 | |
---|
[6421] | 153 | /** |
---|
| 154 | @brief |
---|
[6475] | 155 | Invoked when the activity has changed. Sets visibility of attached objects. |
---|
[6421] | 156 | */ |
---|
[6475] | 157 | void PickupSpawner::changedActivity() |
---|
[6405] | 158 | { |
---|
[6475] | 159 | SUPER(PickupSpawner, changedActivity); |
---|
[5947] | 160 | |
---|
[6486] | 161 | this->setVisible(this->isActive()); |
---|
[5953] | 162 | } |
---|
[6475] | 163 | |
---|
[2917] | 164 | /** |
---|
[5947] | 165 | @brief |
---|
| 166 | Tick, checks if any Pawn is close enough to trigger. |
---|
| 167 | @param dt |
---|
| 168 | Time since last tick. |
---|
[2917] | 169 | */ |
---|
[6540] | 170 | //TODO: Replace with collisions. |
---|
[2917] | 171 | void PickupSpawner::tick(float dt) |
---|
| 172 | { |
---|
[6540] | 173 | SUPER(PickupSpawner, tick, dt); |
---|
| 174 | |
---|
[6405] | 175 | //! If the PickupSpawner is active. |
---|
[2917] | 176 | if (this->isActive()) |
---|
| 177 | { |
---|
[7086] | 178 | SmartPtr<PickupSpawner> temp = this; // create a smart pointer to keep the PickupSpawner alive until we iterated through all Pawns (in case a Pawn takes the last pickup) |
---|
| 179 | |
---|
[6405] | 180 | //! Iterate trough all Pawns. |
---|
[5929] | 181 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it) |
---|
[2917] | 182 | { |
---|
| 183 | Vector3 distance = it->getWorldPosition() - this->getWorldPosition(); |
---|
[6711] | 184 | PickupCarrier* carrier = dynamic_cast<PickupCarrier*>(*it); |
---|
[6405] | 185 | //! If a Pawn, that fits the target-range of the item spawned by this Pickup, is in trigger-distance. |
---|
[6711] | 186 | if (distance.length() < this->triggerDistance_ && carrier != NULL && carrier->isTarget(this->pickup_)) |
---|
[6405] | 187 | { |
---|
[2917] | 188 | this->trigger(*it); |
---|
[6405] | 189 | } |
---|
[2917] | 190 | } |
---|
| 191 | } |
---|
| 192 | } |
---|
[6475] | 193 | |
---|
[2917] | 194 | /** |
---|
[5947] | 195 | @brief |
---|
[6475] | 196 | Sets the maximum number of spawned items. |
---|
| 197 | @param items |
---|
| 198 | The maximum number of spawned items to be set. |
---|
[2917] | 199 | */ |
---|
[6475] | 200 | void PickupSpawner::setMaxSpawnedItems(int items) |
---|
[2917] | 201 | { |
---|
[6475] | 202 | this->maxSpawnedItems_ = items; |
---|
| 203 | this->spawnsRemaining_ = items; |
---|
[6405] | 204 | } |
---|
| 205 | |
---|
[6421] | 206 | /** |
---|
| 207 | @brief |
---|
| 208 | Decrements the number of remaining spawns. |
---|
| 209 | Sets the PickupSpawner to inactive for the duration of the respawnTime. |
---|
| 210 | Destroys the PickupSpawner if the number of remaining spawns has reached zero. |
---|
| 211 | */ |
---|
[6405] | 212 | void PickupSpawner::decrementSpawnsRemaining(void) |
---|
| 213 | { |
---|
| 214 | if(this->spawnsRemaining_ != INF) |
---|
| 215 | { |
---|
| 216 | this->spawnsRemaining_--; |
---|
| 217 | } |
---|
| 218 | if(this->spawnsRemaining_ != 0 && this->respawnTime_ > 0) |
---|
| 219 | { |
---|
[6512] | 220 | this->startRespawnTimer(); |
---|
[5953] | 221 | |
---|
[6405] | 222 | this->setActive(false); |
---|
| 223 | this->fireEvent(); |
---|
| 224 | } |
---|
| 225 | else |
---|
[5953] | 226 | { |
---|
[7038] | 227 | COUT(4) << "PickupSpawner (&" << this << ") empty, selfdestruct initialized." << std::endl; |
---|
[5953] | 228 | this->setActive(false); |
---|
[6475] | 229 | this->destroy(); |
---|
[5953] | 230 | } |
---|
[2917] | 231 | } |
---|
[6475] | 232 | |
---|
| 233 | /** |
---|
| 234 | @brief |
---|
[6512] | 235 | Starts the respawn timer. |
---|
| 236 | */ |
---|
| 237 | void PickupSpawner::startRespawnTimer(void) |
---|
| 238 | { |
---|
| 239 | this->respawnTimer_.setTimer(this->respawnTime_, false, createExecutor(createFunctor(&PickupSpawner::respawnTimerCallback, this))); |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | /** |
---|
| 243 | @brief |
---|
[6475] | 244 | Sets a Pickupable for the PickupSpawner to spawn. |
---|
| 245 | @param pickup |
---|
| 246 | The Pickupable to be set. |
---|
| 247 | */ |
---|
| 248 | void PickupSpawner::setPickupable(Pickupable* pickup) |
---|
| 249 | { |
---|
| 250 | if(this->pickup_ != NULL) |
---|
| 251 | { |
---|
[7038] | 252 | COUT(1) << "In PickupSpawner (&" << this << "): setPickupable called, with this->pickup_ already set." << std::endl; |
---|
[6475] | 253 | return; |
---|
| 254 | } |
---|
| 255 | if(pickup == NULL) |
---|
| 256 | { |
---|
[7038] | 257 | COUT(1) << "In PickupSpawner (&" << this << "): Argument of setPickupable is NULL." << std::endl; |
---|
[6475] | 258 | return; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | this->pickup_ = pickup; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | /** |
---|
| 265 | @brief |
---|
| 266 | Get the Pickupable that is spawned by this PickupSpawner. |
---|
| 267 | @return |
---|
| 268 | Returns the Pickupable that is spawned by this PickupSpawner. |
---|
| 269 | */ |
---|
| 270 | const Pickupable* PickupSpawner::getPickupable(void) |
---|
| 271 | { |
---|
| 272 | return this->pickup_; |
---|
| 273 | } |
---|
[5947] | 274 | |
---|
[2917] | 275 | /** |
---|
[5947] | 276 | @brief |
---|
[6475] | 277 | Trigger the PickupSpawner. |
---|
| 278 | Adds the pickup to the Pawn that triggered, sets the timer to re-activate and deactives the PickupSpawner. |
---|
| 279 | @param pawn |
---|
| 280 | Pawn which triggered the PickupSpawner. |
---|
| 281 | */ |
---|
| 282 | void PickupSpawner::trigger(Pawn* pawn) |
---|
| 283 | { |
---|
[6484] | 284 | if (this->isActive()) //!< Checks whether PickupSpawner is active. |
---|
[6475] | 285 | { |
---|
[7038] | 286 | COUT(4) << "PickupSpawner (&" << this << ") triggered and active." << std::endl; |
---|
[6484] | 287 | |
---|
[6475] | 288 | PickupCarrier* carrier = dynamic_cast<PickupCarrier*>(pawn); |
---|
| 289 | if(carrier == NULL) |
---|
| 290 | { |
---|
| 291 | COUT(1) << "This is bad. Pawn isn't PickupCarrier." << std::endl; |
---|
| 292 | return; |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | if(!carrier->isTarget(this->pickup_)) |
---|
| 296 | { |
---|
[7038] | 297 | COUT(4) << "PickupSpawner (&" << this << ") triggered but Pawn wasn't a target of the Pickupable." << std::endl; |
---|
[6475] | 298 | return; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | PickupCarrier* target = carrier->getTarget(this->pickup_); |
---|
| 302 | Pickupable* pickup = this->getPickup(); |
---|
| 303 | |
---|
[6492] | 304 | if(target != NULL && pickup != NULL) |
---|
[6475] | 305 | { |
---|
[6492] | 306 | if(target->pickup(pickup)) |
---|
[6475] | 307 | { |
---|
| 308 | this->decrementSpawnsRemaining(); |
---|
| 309 | } |
---|
| 310 | else |
---|
| 311 | { |
---|
| 312 | pickup->destroy(); |
---|
| 313 | } |
---|
| 314 | } |
---|
| 315 | else |
---|
| 316 | { |
---|
| 317 | if(target == NULL) |
---|
[7038] | 318 | COUT(1) << "PickupSpawner (&" << this << "): Pickupable has no target." << std::endl; |
---|
[6475] | 319 | |
---|
| 320 | if(pickup == NULL) |
---|
| 321 | { |
---|
[7038] | 322 | COUT(1) << "PickupSpawner (&" << this << "): getPickup produced an error, no Pickupable created." << std::endl; |
---|
[6475] | 323 | } |
---|
| 324 | else |
---|
| 325 | { |
---|
| 326 | pickup->destroy(); |
---|
| 327 | } |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | /** |
---|
| 333 | @brief |
---|
[6405] | 334 | Creates a new Pickupable. |
---|
[5953] | 335 | @return |
---|
[6405] | 336 | The Pickupable created. |
---|
[5953] | 337 | */ |
---|
[6405] | 338 | Pickupable* PickupSpawner::getPickup(void) |
---|
[5953] | 339 | { |
---|
[6405] | 340 | if(this->spawnsRemaining_ == 0) |
---|
| 341 | { |
---|
| 342 | COUT(1) << "Massive Error: PickupSpawner still alive until having spawned last item." << std::endl; |
---|
| 343 | return NULL; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | Pickupable* pickup = this->pickup_->clone(); |
---|
| 347 | return pickup; |
---|
[5953] | 348 | } |
---|
| 349 | |
---|
| 350 | /** |
---|
| 351 | @brief |
---|
[5947] | 352 | Invoked by the timer, re-activates the PickupSpawner. |
---|
[2917] | 353 | */ |
---|
| 354 | void PickupSpawner::respawnTimerCallback() |
---|
| 355 | { |
---|
[7038] | 356 | COUT(4) << "PickupSpawner (&" << this << ") reactivated." << std::endl; |
---|
[2917] | 357 | |
---|
| 358 | this->setActive(true); |
---|
| 359 | } |
---|
| 360 | } |
---|