[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); |
---|
[7010] | 65 | |
---|
| 66 | GUIManager::getInstance().loadGUI(PickupManager::guiName_s); |
---|
[6474] | 67 | this->defaultRepresentation_ = new PickupRepresentation(); |
---|
[6725] | 68 | |
---|
| 69 | COUT(3) << "PickupManager created." << std::endl; |
---|
[6474] | 70 | } |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | @brief |
---|
| 74 | Destructor. |
---|
| 75 | Destroys the default PickupRepresentation. |
---|
| 76 | */ |
---|
| 77 | PickupManager::~PickupManager() |
---|
| 78 | { |
---|
| 79 | if(this->defaultRepresentation_ != NULL) |
---|
| 80 | this->defaultRepresentation_->destroy(); |
---|
[6725] | 81 | |
---|
| 82 | this->representations_.clear(); |
---|
| 83 | |
---|
| 84 | COUT(3) << "PickupManager destroyed." << std::endl; |
---|
[6474] | 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | @brief |
---|
| 89 | Registers a PickupRepresentation together with the PickupIdentifier of the Pickupable the PickupRepresentation represents. |
---|
| 90 | For every type of Pickupable (uniquely idnetified by a PickupIdentifier) there can be one (and just one) PickupRepresentation registered. |
---|
| 91 | @param identifier |
---|
| 92 | The PickupIdentifier identifying the Pickupable. |
---|
| 93 | @param representation |
---|
| 94 | A pointer to the PickupRepresentation. |
---|
| 95 | @return |
---|
| 96 | Returns true if successful and false if not. |
---|
| 97 | */ |
---|
[6475] | 98 | bool PickupManager::registerRepresentation(const PickupIdentifier* identifier, PickupRepresentation* representation) |
---|
[6725] | 99 | { |
---|
| 100 | if(identifier == NULL || representation == NULL || this->representations_.find(identifier) != this->representations_.end()) //!< If the Pickupable already has a Representation registered. |
---|
[6474] | 101 | return false; |
---|
| 102 | |
---|
| 103 | this->representations_[identifier] = representation; |
---|
| 104 | |
---|
| 105 | COUT(4) << "PickupRepresentation " << representation << " registered with the PickupManager." << std::endl; |
---|
| 106 | return true; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | @brief |
---|
[6725] | 111 | Unegisters a PickupRepresentation together with the PickupIdentifier of the Pickupable the PickupRepresentation represents. |
---|
| 112 | @param identifier |
---|
| 113 | The PickupIdentifier identifying the Pickupable. |
---|
| 114 | @param representation |
---|
| 115 | A pointer to the PickupRepresentation. |
---|
| 116 | @return |
---|
| 117 | Returns true if successful and false if not. |
---|
| 118 | */ |
---|
| 119 | bool PickupManager::unregisterRepresentation(const PickupIdentifier* identifier, PickupRepresentation* representation) |
---|
| 120 | { |
---|
| 121 | if(identifier == NULL || representation == NULL) |
---|
| 122 | return false; |
---|
| 123 | |
---|
| 124 | std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier); |
---|
| 125 | if(it == this->representations_.end()) //!< If the Pickupable is not registered in the first place. |
---|
| 126 | return false; |
---|
| 127 | |
---|
| 128 | this->representations_.erase(it); |
---|
| 129 | |
---|
| 130 | COUT(4) << "PickupRepresentation " << representation << " unregistered with the PickupManager." << std::endl; |
---|
| 131 | return true; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | /** |
---|
| 135 | @brief |
---|
[6474] | 136 | Get the PickupRepresentation representing the Pickupable with the input PickupIdentifier. |
---|
| 137 | @param identifier |
---|
| 138 | The PickupIdentifier. |
---|
| 139 | @return |
---|
| 140 | Returns a pointer to the PickupRepresentation. |
---|
| 141 | */ |
---|
| 142 | PickupRepresentation* PickupManager::getRepresentation(const PickupIdentifier* identifier) |
---|
| 143 | { |
---|
[6475] | 144 | std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier); |
---|
[6474] | 145 | if(it == this->representations_.end()) |
---|
| 146 | { |
---|
| 147 | COUT(4) << "PickupManager::getRepresentation() returned default representation." << std::endl; |
---|
| 148 | return this->defaultRepresentation_; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | return it->second; |
---|
| 152 | } |
---|
[6965] | 153 | |
---|
| 154 | int PickupManager::getNumPickups(void) |
---|
[6711] | 155 | { |
---|
[6965] | 156 | this->pickupsList_.clear(); |
---|
| 157 | |
---|
[6752] | 158 | PlayerInfo* player = GUIManager::getInstance().getPlayer(PickupManager::guiName_s); |
---|
[6965] | 159 | PickupCarrier* carrier = NULL; |
---|
[6752] | 160 | if (player != NULL) |
---|
[6965] | 161 | carrier = dynamic_cast<PickupCarrier*>(player->getControllableEntity()); |
---|
[6752] | 162 | else |
---|
[6711] | 163 | return 0; |
---|
[6965] | 164 | |
---|
| 165 | std::vector<PickupCarrier*>* carriers = this->getAllCarriers(carrier); |
---|
| 166 | for(std::vector<PickupCarrier*>::iterator it = carriers->begin(); it != carriers->end(); it++) |
---|
| 167 | { |
---|
| 168 | std::set<Pickupable*> pickups = (*it)->getPickups(); |
---|
| 169 | for(std::set<Pickupable*>::iterator pickup = pickups.begin(); pickup != pickups.end(); pickup++) |
---|
| 170 | { |
---|
[6996] | 171 | this->pickupsList_.insert(std::pair<Pickupable*, WeakPtr<Pickupable> >(*pickup, WeakPtr<Pickupable>(*pickup))); |
---|
[6965] | 172 | } |
---|
| 173 | } |
---|
| 174 | delete carriers; |
---|
| 175 | |
---|
| 176 | this->pickupsIterator_ = this->pickupsList_.begin(); |
---|
| 177 | return this->pickupsList_.size(); |
---|
[6711] | 178 | } |
---|
[6965] | 179 | |
---|
| 180 | std::vector<PickupCarrier*>* PickupManager::getAllCarriers(PickupCarrier* carrier) |
---|
[6711] | 181 | { |
---|
[6965] | 182 | //TODO: More efficiently. |
---|
| 183 | std::vector<PickupCarrier*>* carriers = new std::vector<PickupCarrier*>(); |
---|
| 184 | carriers->insert(carriers->end(), carrier); |
---|
| 185 | std::vector<PickupCarrier*>* children = carrier->getCarrierChildren(); |
---|
| 186 | for(std::vector<PickupCarrier*>::iterator it = children->begin(); it != children->end(); it++) |
---|
| 187 | { |
---|
| 188 | std::vector<PickupCarrier*>* childrensChildren = this->getAllCarriers(*it); |
---|
| 189 | for(std::vector<PickupCarrier*>::iterator it2 = childrensChildren->begin(); it2 != childrensChildren->end(); it2++) |
---|
| 190 | { |
---|
| 191 | carriers->insert(carriers->end(), *it2); |
---|
| 192 | } |
---|
| 193 | delete childrensChildren; |
---|
| 194 | } |
---|
| 195 | delete children; |
---|
| 196 | return carriers; |
---|
[6711] | 197 | } |
---|
[6965] | 198 | |
---|
| 199 | void PickupManager::dropPickup(orxonox::Pickupable* pickup) |
---|
[6711] | 200 | { |
---|
[6996] | 201 | std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); |
---|
| 202 | if(pickup == NULL || it == this->pickupsList_.end() || it->second.get() == NULL) |
---|
| 203 | return; |
---|
| 204 | |
---|
[6965] | 205 | if(!pickup->isPickedUp()) |
---|
| 206 | return; |
---|
[6996] | 207 | |
---|
[6965] | 208 | PickupCarrier* carrier = pickup->getCarrier(); |
---|
| 209 | if(pickup != NULL && carrier != NULL) |
---|
[6996] | 210 | { |
---|
[6728] | 211 | carrier->drop(pickup); |
---|
[6996] | 212 | } |
---|
[6711] | 213 | } |
---|
[6965] | 214 | |
---|
| 215 | void PickupManager::usePickup(orxonox::Pickupable* pickup, bool use) |
---|
[6711] | 216 | { |
---|
[6996] | 217 | std::map<Pickupable*, WeakPtr<Pickupable> >::iterator it = this->pickupsList_.find(pickup); |
---|
| 218 | if(pickup == NULL || it == this->pickupsList_.end() || it->second.get() == NULL) |
---|
| 219 | return; |
---|
| 220 | |
---|
[6965] | 221 | if(!pickup->isPickedUp()) |
---|
| 222 | return; |
---|
| 223 | |
---|
| 224 | PickupCarrier* carrier = pickup->getCarrier(); |
---|
| 225 | if(pickup != NULL && carrier != NULL) |
---|
[6728] | 226 | pickup->setUsed(use); |
---|
[6711] | 227 | } |
---|
| 228 | |
---|
[6474] | 229 | } |
---|