[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" |
---|
[7493] | 37 | #include "core/GameMode.h" |
---|
[3196] | 38 | #include "core/Template.h" |
---|
[2917] | 39 | #include "core/XMLPort.h" |
---|
[7547] | 40 | |
---|
[5735] | 41 | #include "worldentities/pawns/Pawn.h" |
---|
[7547] | 42 | |
---|
[6466] | 43 | #include "PickupManager.h" |
---|
| 44 | #include "PickupRepresentation.h" |
---|
[2917] | 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
[5953] | 48 | |
---|
[9667] | 49 | RegisterClass(PickupSpawner); |
---|
[2917] | 50 | |
---|
| 51 | /** |
---|
[5947] | 52 | @brief |
---|
[6421] | 53 | Constructor. Creates a blank PickupSpawner. |
---|
[5947] | 54 | @param creator |
---|
| 55 | Pointer to the object which created this item. |
---|
[2917] | 56 | */ |
---|
[9667] | 57 | PickupSpawner::PickupSpawner(Context* context) : StaticEntity(context), pickup_(NULL), representation_(NULL), pickupTemplate_(NULL) |
---|
[2917] | 58 | { |
---|
[6540] | 59 | RegisterObject(PickupSpawner); |
---|
[7163] | 60 | |
---|
[6514] | 61 | this->initialize(); |
---|
[5953] | 62 | } |
---|
| 63 | |
---|
[6421] | 64 | /** |
---|
| 65 | @brief |
---|
| 66 | Registers the object and sets some default values. |
---|
| 67 | */ |
---|
[5953] | 68 | void PickupSpawner::initialize(void) |
---|
| 69 | { |
---|
[6563] | 70 | this->triggerDistance_ = 10; |
---|
[7549] | 71 | this->respawnTime_ = 5.0f; |
---|
[5953] | 72 | this->maxSpawnedItems_ = INF; |
---|
| 73 | this->spawnsRemaining_ = INF; |
---|
[7163] | 74 | this->selfDestruct_ = false; |
---|
[9348] | 75 | |
---|
| 76 | this->setPickupable(NULL); |
---|
[2917] | 77 | } |
---|
[5947] | 78 | |
---|
| 79 | /** |
---|
| 80 | @brief |
---|
| 81 | Destructor. |
---|
| 82 | */ |
---|
[2917] | 83 | PickupSpawner::~PickupSpawner() |
---|
| 84 | { |
---|
[7801] | 85 | if(this->isInitialized() && this->selfDestruct_ && this->pickup_ != NULL) |
---|
[6478] | 86 | this->pickup_->destroy(); |
---|
[2917] | 87 | } |
---|
[5947] | 88 | |
---|
[2917] | 89 | /** |
---|
[5947] | 90 | @brief |
---|
[9348] | 91 | Factory method, Creates a fully functional PickupSpawner. |
---|
| 92 | @param creator |
---|
| 93 | The creator of this PickupSpawner. |
---|
| 94 | @param pickup |
---|
| 95 | The Pickupable to be spawned by this PickupSpawner. |
---|
| 96 | @param carrier |
---|
| 97 | The PickupCarrier that carried the input pickup before it was dropped. |
---|
| 98 | @param triggerDistance |
---|
| 99 | The distance at which the PickupSpawner will trigger. |
---|
| 100 | */ |
---|
[9667] | 101 | /*static*/ PickupSpawner* PickupSpawner::createDroppedPickup(Context* context, Pickupable* pickup, PickupCarrier* carrier, float triggerDistance) |
---|
[9348] | 102 | { |
---|
[9667] | 103 | PickupSpawner* spawner = new PickupSpawner(context); |
---|
[9348] | 104 | |
---|
| 105 | spawner->setPickupable(pickup); |
---|
| 106 | spawner->setTriggerDistance(triggerDistance); |
---|
| 107 | spawner->setMaxSpawnedItems(1); |
---|
| 108 | |
---|
| 109 | spawner->setPosition(carrier->getCarrierPosition()); |
---|
| 110 | spawner->block(carrier); |
---|
| 111 | |
---|
| 112 | return spawner; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /** |
---|
| 116 | @brief |
---|
[5947] | 117 | Method for creating a PickupSpawner through XML. |
---|
| 118 | @param xmlelement |
---|
| 119 | XML element which contains the PickupSpawner. |
---|
| 120 | @param mode |
---|
| 121 | XMLPort mode. |
---|
[2917] | 122 | */ |
---|
| 123 | void PickupSpawner::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 124 | { |
---|
| 125 | SUPER(PickupSpawner, XMLPort, xmlelement, mode); |
---|
| 126 | |
---|
[9348] | 127 | XMLPortParam(PickupSpawner, "pickup", setPickupTemplateName, getPickupTemplateName, xmlelement, mode); |
---|
[2917] | 128 | XMLPortParam(PickupSpawner, "triggerDistance", setTriggerDistance, getTriggerDistance, xmlelement, mode); |
---|
| 129 | XMLPortParam(PickupSpawner, "respawnTime", setRespawnTime, getRespawnTime, xmlelement, mode); |
---|
[5953] | 130 | XMLPortParam(PickupSpawner, "maxSpawnedItems", setMaxSpawnedItems, getMaxSpawnedItems, xmlelement, mode); |
---|
[2917] | 131 | } |
---|
[7163] | 132 | |
---|
[6421] | 133 | /** |
---|
| 134 | @brief |
---|
[5947] | 135 | Tick, checks if any Pawn is close enough to trigger. |
---|
| 136 | @param dt |
---|
| 137 | Time since last tick. |
---|
[2917] | 138 | */ |
---|
[7493] | 139 | //TODO: Replace with collisions? |
---|
[2917] | 140 | void PickupSpawner::tick(float dt) |
---|
| 141 | { |
---|
[6540] | 142 | SUPER(PickupSpawner, tick, dt); |
---|
[7163] | 143 | |
---|
[7493] | 144 | // If the PickupSpawner is active. |
---|
| 145 | if(GameMode::isMaster() && this->isActive()) |
---|
[2917] | 146 | { |
---|
[10624] | 147 | // TODO: why is this a WeakPtr when the comment says StrongPtr? |
---|
| 148 | WeakPtr<PickupSpawner> spawner = this; // Create a strong pointer to keep the PickupSpawner alive until we iterated through all Pawns (in case a Pawn takes the last pickup) |
---|
[7163] | 149 | |
---|
[7549] | 150 | // Remove PickupCarriers from the blocked list if they have exceeded their time. |
---|
| 151 | for(std::map<PickupCarrier*, std::time_t>::iterator it = this->blocked_.begin(); it != this->blocked_.end(); ) |
---|
| 152 | { |
---|
| 153 | std::map<PickupCarrier*, std::time_t>::iterator temp = it; |
---|
| 154 | it++; |
---|
| 155 | if(temp->second < std::time(0)) |
---|
| 156 | this->blocked_.erase(temp); |
---|
| 157 | } |
---|
| 158 | |
---|
[7493] | 159 | // Iterate trough all Pawns. |
---|
[7549] | 160 | for(ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it) |
---|
[2917] | 161 | { |
---|
[8891] | 162 | if(spawner == NULL) // Stop if the PickupSpawner has been deleted (e.g. because it has run out of pickups to distribute). |
---|
| 163 | break; |
---|
| 164 | |
---|
[2917] | 165 | Vector3 distance = it->getWorldPosition() - this->getWorldPosition(); |
---|
[9348] | 166 | PickupCarrier* carrier = static_cast<PickupCarrier*>(*it); |
---|
[8891] | 167 | // If a PickupCarrier, that fits the target-range of the Pickupable spawned by this PickupSpawner, is in trigger-distance and the carrier is not blocked. |
---|
[7549] | 168 | if(distance.length() < this->triggerDistance_ && carrier != NULL && this->blocked_.find(carrier) == this->blocked_.end()) |
---|
[6405] | 169 | { |
---|
[7547] | 170 | if(carrier->isTarget(this->pickup_)) |
---|
| 171 | this->trigger(*it); |
---|
[6405] | 172 | } |
---|
[2917] | 173 | } |
---|
| 174 | } |
---|
| 175 | } |
---|
[7163] | 176 | |
---|
[2917] | 177 | /** |
---|
[5947] | 178 | @brief |
---|
[9348] | 179 | Trigger the PickupSpawner. |
---|
| 180 | Adds the pickup to the Pawn that triggered, sets the timer to re-activate and deactives the PickupSpawner. |
---|
| 181 | @param carrier |
---|
| 182 | Carrier which triggered the PickupSpawner. |
---|
| 183 | */ |
---|
| 184 | void PickupSpawner::trigger(PickupCarrier* carrier) |
---|
| 185 | { |
---|
| 186 | orxout(verbose, context::pickups) << "PickupSpawner (&" << this << ") triggered and active." << endl; |
---|
| 187 | |
---|
| 188 | PickupCarrier* target = carrier->getTarget(this->pickup_); |
---|
| 189 | |
---|
| 190 | this->block(carrier); |
---|
| 191 | |
---|
| 192 | assert(target); |
---|
| 193 | bool pickedUp = this->pickup_->pickup(target); |
---|
| 194 | assert(pickedUp); |
---|
| 195 | pickedUp = false; // To avoid compiler warning. |
---|
| 196 | |
---|
| 197 | this->setPickupable(NULL); |
---|
| 198 | this->decrementSpawnsRemaining(); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | void PickupSpawner::setPickupTemplateName(const std::string& name) |
---|
| 202 | { |
---|
| 203 | Template* temp = Template::getTemplate(name); |
---|
| 204 | if (temp) |
---|
| 205 | this->setPickupTemplate(temp); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | void PickupSpawner::setPickupTemplate(Template* temp) |
---|
| 209 | { |
---|
| 210 | this->pickupTemplate_ = temp; |
---|
| 211 | this->pickupTemplateName_ = temp->getName(); |
---|
| 212 | |
---|
| 213 | this->setPickupable(this->createPickup()); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | /** |
---|
| 217 | @brief |
---|
[6475] | 218 | Sets the maximum number of spawned items. |
---|
| 219 | @param items |
---|
| 220 | The maximum number of spawned items to be set. |
---|
[2917] | 221 | */ |
---|
[6475] | 222 | void PickupSpawner::setMaxSpawnedItems(int items) |
---|
[2917] | 223 | { |
---|
[6475] | 224 | this->maxSpawnedItems_ = items; |
---|
| 225 | this->spawnsRemaining_ = items; |
---|
[6405] | 226 | } |
---|
[7163] | 227 | |
---|
[6421] | 228 | /** |
---|
| 229 | @brief |
---|
| 230 | Decrements the number of remaining spawns. |
---|
| 231 | Sets the PickupSpawner to inactive for the duration of the respawnTime. |
---|
| 232 | Destroys the PickupSpawner if the number of remaining spawns has reached zero. |
---|
| 233 | */ |
---|
[6405] | 234 | void PickupSpawner::decrementSpawnsRemaining(void) |
---|
| 235 | { |
---|
| 236 | if(this->spawnsRemaining_ != INF) |
---|
| 237 | this->spawnsRemaining_--; |
---|
[7493] | 238 | |
---|
[9348] | 239 | this->setActive(false); |
---|
| 240 | |
---|
[6405] | 241 | if(this->spawnsRemaining_ != 0 && this->respawnTime_ > 0) |
---|
| 242 | { |
---|
[6512] | 243 | this->startRespawnTimer(); |
---|
[6405] | 244 | this->fireEvent(); |
---|
| 245 | } |
---|
| 246 | else |
---|
[5953] | 247 | { |
---|
[8858] | 248 | orxout(verbose, context::pickups) << "PickupSpawner (&" << this << ") empty, selfdestruct initialized." << endl; |
---|
[6475] | 249 | this->destroy(); |
---|
[5953] | 250 | } |
---|
[2917] | 251 | } |
---|
[7163] | 252 | |
---|
[6475] | 253 | /** |
---|
| 254 | @brief |
---|
[6512] | 255 | Starts the respawn timer. |
---|
| 256 | */ |
---|
| 257 | void PickupSpawner::startRespawnTimer(void) |
---|
| 258 | { |
---|
| 259 | this->respawnTimer_.setTimer(this->respawnTime_, false, createExecutor(createFunctor(&PickupSpawner::respawnTimerCallback, this))); |
---|
| 260 | } |
---|
[7163] | 261 | |
---|
[6512] | 262 | /** |
---|
| 263 | @brief |
---|
[9348] | 264 | Invoked by the timer, re-activates the PickupSpawner. |
---|
[6475] | 265 | */ |
---|
[9348] | 266 | void PickupSpawner::respawnTimerCallback() |
---|
[6475] | 267 | { |
---|
[9348] | 268 | orxout(verbose, context::pickups) << "PickupSpawner (&" << this << ") reactivated." << endl; |
---|
[7163] | 269 | |
---|
[9348] | 270 | this->setPickupable(this->createPickup()); |
---|
[6475] | 271 | } |
---|
[7163] | 272 | |
---|
[6475] | 273 | /** |
---|
| 274 | @brief |
---|
[9348] | 275 | Creates a new Pickupable. |
---|
[6475] | 276 | @return |
---|
[9348] | 277 | The Pickupable created. |
---|
[6475] | 278 | */ |
---|
[9348] | 279 | Pickupable* PickupSpawner::createPickup(void) |
---|
[6475] | 280 | { |
---|
[9348] | 281 | if(this->spawnsRemaining_ == 0) |
---|
| 282 | { |
---|
| 283 | orxout(internal_error, context::pickups) << "Massive Error: PickupSpawner still alive until having spawned last item." << endl; |
---|
| 284 | return NULL; |
---|
| 285 | } |
---|
[5947] | 286 | |
---|
[9348] | 287 | if (this->pickupTemplate_ != NULL) |
---|
[6475] | 288 | { |
---|
[9348] | 289 | Identifier* identifier = this->pickupTemplate_->getBaseclassIdentifier(); |
---|
| 290 | if (identifier != NULL) |
---|
[6475] | 291 | { |
---|
[9667] | 292 | Pickupable* pickup = orxonox_cast<Pickupable*>(identifier->fabricate(this->getContext())); |
---|
[9348] | 293 | orxonox_cast<BaseObject*>(pickup)->addTemplate(this->pickupTemplate_); |
---|
| 294 | return pickup; |
---|
[6475] | 295 | } |
---|
[9348] | 296 | else |
---|
| 297 | orxout(internal_error, context::pickups) << "No base class defined in pickup-template " << this->pickupTemplateName_ << endl; |
---|
| 298 | } |
---|
[7163] | 299 | |
---|
[9348] | 300 | return NULL; |
---|
[6475] | 301 | } |
---|
| 302 | |
---|
| 303 | /** |
---|
| 304 | @brief |
---|
[9348] | 305 | Sets a Pickupable for the PickupSpawner to spawn. |
---|
| 306 | @param pickup |
---|
| 307 | The Pickupable to be set. |
---|
[7163] | 308 | */ |
---|
[9348] | 309 | void PickupSpawner::setPickupable(Pickupable* pickup) |
---|
[5953] | 310 | { |
---|
[9348] | 311 | if (this->representation_ != NULL) |
---|
[6405] | 312 | { |
---|
[9348] | 313 | this->representation_->destroy(); |
---|
| 314 | this->representation_ = NULL; |
---|
[6405] | 315 | } |
---|
[7163] | 316 | |
---|
[9348] | 317 | if (pickup != NULL) |
---|
| 318 | { |
---|
| 319 | if (this->pickup_ != NULL) |
---|
| 320 | this->pickup_->destroy(); |
---|
[5953] | 321 | |
---|
[9348] | 322 | PickupRepresentation* representation = PickupManager::getInstance().getRepresentation(pickup->getRepresentationName()); |
---|
| 323 | this->representation_ = representation->createSpawnerRepresentation(this); |
---|
| 324 | this->attach(this->representation_); |
---|
| 325 | this->setActive(true); |
---|
| 326 | } |
---|
| 327 | else |
---|
| 328 | { |
---|
| 329 | this->setActive(false); |
---|
| 330 | } |
---|
[2917] | 331 | |
---|
[9348] | 332 | this->pickup_ = pickup; |
---|
[2917] | 333 | } |
---|
| 334 | } |
---|