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