[6474] | 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 | * ... |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
[6540] | 30 | @file PickupManager.cc |
---|
[6474] | 31 | @brief Implementation of the PickupManager class. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "PickupManager.h" |
---|
| 35 | |
---|
| 36 | #include "core/CoreIncludes.h" |
---|
[6711] | 37 | #include "core/LuaState.h" |
---|
| 38 | #include "core/GUIManager.h" |
---|
[6474] | 39 | #include "core/ScopedSingletonManager.h" |
---|
| 40 | #include "core/Identifier.h" |
---|
[6996] | 41 | #include "util/Convert.h" |
---|
[6474] | 42 | #include "interfaces/PickupCarrier.h" |
---|
[6711] | 43 | #include "infos/PlayerInfo.h" |
---|
[6474] | 44 | #include "worldentities/pawns/Pawn.h" |
---|
| 45 | #include "PickupRepresentation.h" |
---|
| 46 | |
---|
[6711] | 47 | #include "ToluaBindPickup.h" |
---|
| 48 | |
---|
[6474] | 49 | namespace orxonox |
---|
| 50 | { |
---|
[6711] | 51 | // Register tolua_open function when loading the library |
---|
| 52 | DeclareToluaInterface(Pickup); |
---|
| 53 | |
---|
[6725] | 54 | ManageScopedSingleton(PickupManager, ScopeID::Graphics, false); |
---|
[6474] | 55 | |
---|
[6711] | 56 | /*static*/ const std::string PickupManager::guiName_s = "PickupInventory"; |
---|
| 57 | |
---|
[6474] | 58 | /** |
---|
| 59 | @brief |
---|
| 60 | Constructor. Registers the PickupManager and creates the default PickupRepresentation. |
---|
| 61 | */ |
---|
[6540] | 62 | PickupManager::PickupManager() : defaultRepresentation_(NULL) |
---|
[6474] | 63 | { |
---|
| 64 | RegisterRootObject(PickupManager); |
---|
| 65 | |
---|
| 66 | this->defaultRepresentation_ = new PickupRepresentation(); |
---|
[6725] | 67 | |
---|
| 68 | COUT(3) << "PickupManager created." << std::endl; |
---|
[6474] | 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | @brief |
---|
| 73 | Destructor. |
---|
| 74 | Destroys the default PickupRepresentation. |
---|
| 75 | */ |
---|
| 76 | PickupManager::~PickupManager() |
---|
| 77 | { |
---|
| 78 | if(this->defaultRepresentation_ != NULL) |
---|
| 79 | this->defaultRepresentation_->destroy(); |
---|
[6725] | 80 | |
---|
| 81 | this->representations_.clear(); |
---|
| 82 | |
---|
| 83 | COUT(3) << "PickupManager destroyed." << std::endl; |
---|
[6474] | 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | @brief |
---|
| 88 | Registers a PickupRepresentation together with the PickupIdentifier of the Pickupable the PickupRepresentation represents. |
---|
| 89 | For every type of Pickupable (uniquely idnetified by a PickupIdentifier) there can be one (and just one) PickupRepresentation registered. |
---|
| 90 | @param identifier |
---|
| 91 | The PickupIdentifier identifying the Pickupable. |
---|
| 92 | @param representation |
---|
| 93 | A pointer to the PickupRepresentation. |
---|
| 94 | @return |
---|
| 95 | Returns true if successful and false if not. |
---|
| 96 | */ |
---|
[6475] | 97 | bool PickupManager::registerRepresentation(const PickupIdentifier* identifier, PickupRepresentation* representation) |
---|
[6725] | 98 | { |
---|
| 99 | if(identifier == NULL || representation == NULL || this->representations_.find(identifier) != this->representations_.end()) //!< If the Pickupable already has a Representation registered. |
---|
[6474] | 100 | return false; |
---|
| 101 | |
---|
| 102 | this->representations_[identifier] = representation; |
---|
| 103 | |
---|
| 104 | COUT(4) << "PickupRepresentation " << representation << " registered with the PickupManager." << std::endl; |
---|
| 105 | return true; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | @brief |
---|
[6725] | 110 | Unegisters a PickupRepresentation together with the PickupIdentifier of the Pickupable the PickupRepresentation represents. |
---|
| 111 | @param identifier |
---|
| 112 | The PickupIdentifier identifying the Pickupable. |
---|
| 113 | @param representation |
---|
| 114 | A pointer to the PickupRepresentation. |
---|
| 115 | @return |
---|
| 116 | Returns true if successful and false if not. |
---|
| 117 | */ |
---|
| 118 | bool PickupManager::unregisterRepresentation(const PickupIdentifier* identifier, PickupRepresentation* representation) |
---|
| 119 | { |
---|
| 120 | if(identifier == NULL || representation == NULL) |
---|
| 121 | return false; |
---|
| 122 | |
---|
| 123 | std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier); |
---|
| 124 | if(it == this->representations_.end()) //!< If the Pickupable is not registered in the first place. |
---|
| 125 | return false; |
---|
| 126 | |
---|
| 127 | this->representations_.erase(it); |
---|
| 128 | |
---|
| 129 | COUT(4) << "PickupRepresentation " << representation << " unregistered with the PickupManager." << std::endl; |
---|
| 130 | return true; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /** |
---|
| 134 | @brief |
---|
[6474] | 135 | Get the PickupRepresentation representing the Pickupable with the input PickupIdentifier. |
---|
| 136 | @param identifier |
---|
| 137 | The PickupIdentifier. |
---|
| 138 | @return |
---|
| 139 | Returns a pointer to the PickupRepresentation. |
---|
| 140 | */ |
---|
| 141 | PickupRepresentation* PickupManager::getRepresentation(const PickupIdentifier* identifier) |
---|
| 142 | { |
---|
[6475] | 143 | std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier); |
---|
[6474] | 144 | if(it == this->representations_.end()) |
---|
| 145 | { |
---|
| 146 | COUT(4) << "PickupManager::getRepresentation() returned default representation." << std::endl; |
---|
| 147 | return this->defaultRepresentation_; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | return it->second; |
---|
| 151 | } |
---|
[6965] | 152 | |
---|
| 153 | int PickupManager::getNumPickups(void) |
---|
[6711] | 154 | { |
---|
[6965] | 155 | this->pickupsList_.clear(); |
---|
| 156 | |
---|
[6752] | 157 | PlayerInfo* player = GUIManager::getInstance().getPlayer(PickupManager::guiName_s); |
---|
[6965] | 158 | PickupCarrier* carrier = NULL; |
---|
[6752] | 159 | if (player != NULL) |
---|
[6965] | 160 | carrier = dynamic_cast<PickupCarrier*>(player->getControllableEntity()); |
---|
[6752] | 161 | else |
---|
[6711] | 162 | return 0; |
---|
[6965] | 163 | |
---|
| 164 | std::vector<PickupCarrier*>* carriers = this->getAllCarriers(carrier); |
---|
| 165 | for(std::vector<PickupCarrier*>::iterator it = carriers->begin(); it != carriers->end(); it++) |
---|
| 166 | { |
---|
| 167 | std::set<Pickupable*> pickups = (*it)->getPickups(); |
---|
| 168 | for(std::set<Pickupable*>::iterator pickup = pickups.begin(); pickup != pickups.end(); pickup++) |
---|
| 169 | { |
---|
[6996] | 170 | this->pickupsList_.insert(std::pair<Pickupable*, WeakPtr<Pickupable> >(*pickup, WeakPtr<Pickupable>(*pickup))); |
---|
[6965] | 171 | } |
---|
| 172 | } |
---|
| 173 | delete carriers; |
---|
| 174 | |
---|
| 175 | this->pickupsIterator_ = this->pickupsList_.begin(); |
---|
| 176 | return this->pickupsList_.size(); |
---|
[6711] | 177 | } |
---|
[6965] | 178 | |
---|
| 179 | std::vector<PickupCarrier*>* PickupManager::getAllCarriers(PickupCarrier* carrier) |
---|
[6711] | 180 | { |
---|
[6965] | 181 | //TODO: More efficiently. |
---|
| 182 | std::vector<PickupCarrier*>* carriers = new std::vector<PickupCarrier*>(); |
---|
| 183 | carriers->insert(carriers->end(), carrier); |
---|
| 184 | std::vector<PickupCarrier*>* children = carrier->getCarrierChildren(); |
---|
| 185 | for(std::vector<PickupCarrier*>::iterator it = children->begin(); it != children->end(); it++) |
---|
| 186 | { |
---|
| 187 | std::vector<PickupCarrier*>* childrensChildren = this->getAllCarriers(*it); |
---|
| 188 | for(std::vector<PickupCarrier*>::iterator it2 = childrensChildren->begin(); it2 != childrensChildren->end(); it2++) |
---|
| 189 | { |
---|
| 190 | carriers->insert(carriers->end(), *it2); |
---|
| 191 | } |
---|
| 192 | delete childrensChildren; |
---|
| 193 | } |
---|
| 194 | delete children; |
---|
| 195 | return carriers; |
---|
[6711] | 196 | } |
---|
[6965] | 197 | |
---|
| 198 | void PickupManager::dropPickup(orxonox::Pickupable* pickup) |
---|
[6711] | 199 | { |
---|
[6996] | 200 | std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); |
---|
| 201 | if(pickup == NULL || it == this->pickupsList_.end() || it->second.get() == NULL) |
---|
| 202 | return; |
---|
| 203 | |
---|
[6965] | 204 | if(!pickup->isPickedUp()) |
---|
| 205 | return; |
---|
[6996] | 206 | |
---|
[6965] | 207 | PickupCarrier* carrier = pickup->getCarrier(); |
---|
| 208 | if(pickup != NULL && carrier != NULL) |
---|
[6996] | 209 | { |
---|
[6728] | 210 | carrier->drop(pickup); |
---|
[6996] | 211 | } |
---|
[6711] | 212 | } |
---|
[6965] | 213 | |
---|
| 214 | void PickupManager::usePickup(orxonox::Pickupable* pickup, bool use) |
---|
[6711] | 215 | { |
---|
[6996] | 216 | std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); |
---|
| 217 | if(pickup == NULL || it == this->pickupsList_.end() || it->second.get() == NULL) |
---|
| 218 | return; |
---|
| 219 | |
---|
[6965] | 220 | if(!pickup->isPickedUp()) |
---|
| 221 | return; |
---|
| 222 | |
---|
| 223 | PickupCarrier* carrier = pickup->getCarrier(); |
---|
| 224 | if(pickup != NULL && carrier != NULL) |
---|
[6728] | 225 | pickup->setUsed(use); |
---|
[6711] | 226 | } |
---|
| 227 | |
---|
[6474] | 228 | } |
---|