[6405] | 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 | * Damian 'Mozork' Frick |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | @file |
---|
| 31 | @brief Implementation of PickupCollection. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "PickupCollection.h" |
---|
| 35 | |
---|
| 36 | #include "core/CoreIncludes.h" |
---|
| 37 | #include "core/Template.h" |
---|
| 38 | #include "core/XMLPort.h" |
---|
| 39 | #include "interfaces/PickupCarrier.h" |
---|
[6475] | 40 | #include "DroppedPickup.h" |
---|
[6405] | 41 | |
---|
[6475] | 42 | #include "PickupCollectionIdentifier.h" |
---|
| 43 | |
---|
[6405] | 44 | namespace orxonox |
---|
| 45 | { |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | @brief |
---|
| 49 | Default Constructor. |
---|
| 50 | */ |
---|
| 51 | PickupCollection::PickupCollection(BaseObject* creator) : BaseObject(creator) |
---|
| 52 | { |
---|
| 53 | RegisterObject(PickupCollection); |
---|
[6475] | 54 | |
---|
| 55 | this->pickupCollectionIdentifier_ = new PickupCollectionIdentifier(); |
---|
[6405] | 56 | } |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | @brief |
---|
| 60 | Destructor. |
---|
| 61 | */ |
---|
| 62 | PickupCollection::~PickupCollection() |
---|
| 63 | { |
---|
| 64 | //! Destroy all Pickupables constructing this PickupCollection. |
---|
[6421] | 65 | for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++) |
---|
[6405] | 66 | { |
---|
[6475] | 67 | (*it)->destroy(); |
---|
[6405] | 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | @brief |
---|
| 73 | Creates an instance of this Class through XML. |
---|
| 74 | */ |
---|
| 75 | void PickupCollection::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 76 | { |
---|
| 77 | SUPER(PickupCollection, XMLPort, xmlelement, mode); |
---|
| 78 | |
---|
[6466] | 79 | //TODO: Does this work? Problem could be, that Pickupable itself cannot be instantiated through XML, doubt that, though. |
---|
[6405] | 80 | XMLPortObject(PickupCollection, PickupCollection, "pickupables", addPickupable, getPickupable, xmlelement, mode); |
---|
[6466] | 81 | |
---|
| 82 | this->initializeIdentifier(); |
---|
[6405] | 83 | } |
---|
| 84 | |
---|
[6466] | 85 | void PickupCollection::initializeIdentifier(void) |
---|
| 86 | { |
---|
[6475] | 87 | this->pickupCollectionIdentifier_->addClass(this->getIdentifier()); |
---|
[6466] | 88 | |
---|
| 89 | for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++) |
---|
| 90 | { |
---|
[6475] | 91 | this->pickupCollectionIdentifier_->addPickup((*it)->getPickupIdentifier()); |
---|
[6466] | 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
[6405] | 95 | /** |
---|
| 96 | @brief |
---|
[6475] | 97 | Facilitates the creation of a PickupSpawner upon dropping of the Pickupable. |
---|
| 98 | This method must be implemented by any class directly inheriting from Pickupable. It is most easily done by just creating a new DroppedPickup, e.g.: |
---|
| 99 | DroppedPickup(BaseObject* creator, Pickupable* pickup, const Vector3& position); |
---|
| 100 | @param position |
---|
| 101 | The position at which the PickupSpawner should be placed. |
---|
| 102 | @return |
---|
| 103 | Returns true if a spawner was created, false if not. |
---|
| 104 | */ |
---|
| 105 | bool PickupCollection::createSpawner(const Vector3& position) |
---|
| 106 | { |
---|
| 107 | DroppedPickup::DroppedPickup(this, this, position); |
---|
| 108 | return true; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | @brief |
---|
[6405] | 113 | Add the input Pickupable to list of Pickupables combined by this PickupCollection. |
---|
| 114 | @param pickup |
---|
| 115 | The Pickupable to be added. |
---|
| 116 | @return |
---|
| 117 | Returns true if successful, |
---|
| 118 | */ |
---|
| 119 | bool PickupCollection::addPickupable(Pickupable* pickup) |
---|
| 120 | { |
---|
| 121 | if(pickup == NULL) |
---|
| 122 | return false; |
---|
| 123 | |
---|
[6421] | 124 | this->pickups_.push_back(pickup); |
---|
[6405] | 125 | return true; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /** |
---|
| 129 | @brief |
---|
| 130 | Get the Pickupable at the given index. |
---|
| 131 | @param index |
---|
| 132 | The index the Pickupable is fetched from. |
---|
| 133 | @return |
---|
| 134 | Returns a pointer to the Pickupable at the index given by index. |
---|
| 135 | */ |
---|
[6421] | 136 | const Pickupable* PickupCollection::getPickupable(unsigned int index) |
---|
[6405] | 137 | { |
---|
| 138 | return this->pickups_[index]; //TODO. Does this work? |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | //TODO: Steal description from Pickupable. |
---|
| 142 | void PickupCollection::changedUsed(void) |
---|
| 143 | { |
---|
| 144 | SUPER(PickupCollection, changedUsed); |
---|
| 145 | |
---|
| 146 | //! Change used for all Pickupables this PickupCollection consists of. |
---|
[6421] | 147 | for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++) |
---|
[6405] | 148 | { |
---|
[6466] | 149 | (*it)->setUsed(this->isUsed()); |
---|
[6405] | 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
[6466] | 153 | void PickupCollection::changedCarrier() |
---|
[6405] | 154 | { |
---|
[6466] | 155 | SUPER(PickupCollection, changedCarrier); |
---|
[6405] | 156 | |
---|
[6466] | 157 | //! Change the carrier for all Pickupables this PickupCollection consists of. |
---|
[6421] | 158 | for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++) |
---|
[6405] | 159 | { |
---|
[6466] | 160 | (*it)->setCarrier(this->getCarrier()); |
---|
[6405] | 161 | } |
---|
| 162 | } |
---|
| 163 | |
---|
[6466] | 164 | //TODO: Steal description from Pickupable. |
---|
| 165 | void PickupCollection::clone(OrxonoxClass* item) |
---|
[6405] | 166 | { |
---|
[6466] | 167 | if(item == NULL) |
---|
| 168 | item = new PickupCollection(this); |
---|
[6405] | 169 | |
---|
[6466] | 170 | SUPER(PickupCollection, clone, item); |
---|
[6405] | 171 | |
---|
[6466] | 172 | PickupCollection* pickup = dynamic_cast<PickupCollection*>(item); |
---|
[6421] | 173 | for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++) |
---|
[6405] | 174 | { |
---|
| 175 | Pickupable* newPickup = (*it)->clone(); |
---|
[6466] | 176 | pickup->addPickupable(newPickup); |
---|
[6405] | 177 | } |
---|
[6466] | 178 | |
---|
| 179 | pickup->initializeIdentifier(); |
---|
[6405] | 180 | } |
---|
| 181 | |
---|
[6475] | 182 | const PickupIdentifier* PickupCollection::getPickupIdentifier(void) |
---|
| 183 | { |
---|
| 184 | return this->pickupCollectionIdentifier_; |
---|
| 185 | } |
---|
| 186 | |
---|
[6405] | 187 | } |
---|