Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pickup/PickupManager.h @ 11642

Last change on this file since 11642 was 11353, checked in by patricwi, 8 years ago

merged HUD branch to trunk

  • Property svn:eol-style set to native
File size: 12.3 KB
RevLine 
[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 *      Damian 'Mozork' Frick
24 *   Co-authors:
25 *      ...
26 *
27*/
28
29/**
[6540]30    @file PickupManager.h
[6474]31    @brief Definition of the PickupManager class.
[7456]32    @ingroup Pickup
[6474]33*/
34
35#ifndef _PickupManager_H__
36#define _PickupManager_H__
37
38#include "PickupPrereqs.h"
39
40#include <map>
[9667]41#include "core/object/WeakPtr.h"
[7504]42
[6474]43#include "PickupRepresentation.h"
[11353]44#include "interfaces/Pickupable.h"
[7504]45#include "util/Singleton.h"
46#include "interfaces/PickupListener.h"
[11353]47#include "overlays/hud/HUDPickupSystem.h"
[6474]48
[6711]49namespace orxonox // tolua_export
50{ // tolua_export
[6474]51
[7504]52    // tolua_begin
[7533]53    /**
54    @brief
55        Data structure to store collected data for one specific @ref orxonox::Pickupable "Pickupable".
56        This is used to not have to synchronise @ref orxonox::Pickupable "Pickupable" just to have the needed information for the PickupInventory available on all clients. Instead the information is sent over the network and stored in a PickupInventoryContainer.
57
58    @ingroup Pickup
59    */
[7504]60    struct PickupInventoryContainer
61    {
[7533]62        uint32_t pickup; //!< An indentifier for the @ref orxonox::Pickupable "Pickupable" that is associated with the information stored here.
63        bool inUse; //!< Whether the @ref orxonox::Pickupable "Pickupable" is currently in use.
64        bool pickedUp; //!< Whether the @ref orxonox::Pickupable "Pickupable" is currently picked up.
65        bool usable; //!< Whether the @ref orxonox::Pickupable "Pickupable" is usable.
66        bool unusable; //!< Whether the @ref orxonox::Pickupable "Pickupable" is droppable.
67        uint32_t representationObjectId; //!< The objectId of the @ref orxonox::PickupRepresentation "PickupRepresentation" that represents the @ref orxonox::Pickupable "Pickupable".
[9348]68        std::string representationName; //!< The name of the associated PickupRepresentation
[7504]69    };
70    // tolua_end
71
[6474]72    /**
73    @brief
[7533]74        The PickupManager class manages @ref orxonox::Pickupable "Pickupables".
75
[9348]76        It has in essence two tasks to fulfill. Firstly it must link @ref orxonox::Pickupable "Pickupables" (through their representation-name attribute) to the respective @ref orxonox::PickupRepresentation "PickupRepresentations". Secondly it manages the PickupInventory. (The PickupInventory is the GUI that displays @ref orxonox::Pickupable "Pickupables" for the covenience of the user.)
[7533]77
78        @section PickupManagerTechnicalDetails Technical details
79        Unfortunately <em>under the hood</em> it isn't just as easy. At least the PickupInventory part isn't. To grasp why this is we need to have a brief look at how the pickups module works over the network:
80
81        The pickups module essentially just exists on the @ref orxonox::Server "Server", since it's all game logic. That means, a @ref orxonox::PickupSpawner "PickupSpawner" is always triggered on the server, the @ref orxonox::Pickupable "Pickupable" is then picked up (and maybe used already) on the server. The effects the pickup has are synchronised over the network, if they have to be, but this is the responsibility of the entities that are affected, not the pickups module, and normally this is already implemented. The only two things that need to be communicated over the network are the graphical component of the @ref orxonox::PickupSpawner "PickupSpawner", since it needs to be displayed on the @ref orxonox::Client "Clients" as well, and anything that is needed for the PickupInventory, since it exists for each @ref orxonox::Host "Host" seperatly.
82
83        Fortunately synchronising the @ref orxonox::PickupSpawner "PickupSpawner" is already being taken care of by the synchronisation of the @ref orxonox::StaticEntity "StaticEntity" (or parents thereof).
84
85        This leaves us with the PickupInventory component (and this is really the source of all the complexity).
86
87        Firstly there are a number of lists (where by list I really mean any kind of ordered data structure) kept.
[9348]88        - The @ref orxonox::PickupManager::representations_ "representations_" list links @ref orxonox::PickupRepresentation "PickupRepresentations" with its name and can be used to get the @ref orxonox::PickupRepresentation "PickupRepresentation" for a given @ref orxonox::Pickupable "Pickupable". It is only populated on the server (or in standalone mode). Each @ref orxonox::PickupRepresentation "PickupRepresentation" that is generated through XML registers itself with the PickupManager and is thereby added to the list.
[7547]89        - The @ref orxonox::PickupManager::pickupInventoryContainers_ "pickupInventoryContainers_" list links a number identifying a @ref orxonox::Pickupable "Pickupable" to a data structure (the @ref orxonox::PickupInventoryContainer "PickupInventoryContainer"), which contains all necessary information about that @ref orxonox::Pickupable "Pickupable". This list is maintained on all hosts, a new container is inserted when a @ref orxonox::Pickupable "Pickupable" is picked up, but only if it has been picked up by the repsective host. This list is then used by the PickupInventory to access the required information and to get the correct @ref orxonox::PickupRepresentation "PickupRepresentation".
[7533]90        - The @ref orxonox::PickupManager::pickups_ "pickups_" list links a number identifiying a @ref orxonox::Pickupable "Pickupable" to a (weak pointer to a) @ref orxonox::Pickupable "Pickupable". It is only maintained by the server (or in standalone mode), to be able to use, unuse and drop @ref orxonox::Pickupable "Pickupables" through the PickupInventory. Since @ref orxonox::Pickupable "Pickupables" only exist on the server a client that wants to change a pickups status has so send a request over the network (with the number identifying the pickup) to the server and then the server facilitates the change, using this list to map from the identifyer to the actual @ref orxonox::Pickupable "Pickupable".
91        - The @ref orxonox::PickupManager::indexes_ "indexes_" list links a @ref orxonox::Pickupable "Pickupable" to the number identifying it. This is only maintained on the server (or in standalone mode), and is used for the inverse mapping of the previous list, which means the server uses it identify pickups on clients when it communicates changes in pickups to clients.
92
93        There is communication in both directions. From server to client, when the server informs the client that the state of some @ref orxonox::Pickupable "Pickupable" has changed, during which all necessary information to create or update the PickupInventoryContainer for that pickup on the client is sent as well. Or from client to server, when the client wants the server to change the state of some @ref orxonox::Pickupable "Pickupable".
94
[6474]95    @author
96        Damian 'Mozork' Frick
[7533]97
98    @ingroup Pickup
[6474]99    */
[6711]100    class _PickupExport PickupManager // tolua_export
[7504]101        : public Singleton<PickupManager>, public PickupListener
[6711]102    { // tolua_export
[6474]103        friend class Singleton<PickupManager>;
[7163]104
[6474]105        public:
[7533]106            PickupManager(); //!< Constructor.
107            virtual ~PickupManager(); //!< Destructor.
[7163]108
[7533]109            /**
110            @brief Get the instance of the PickupManager singleton.
111            @return Returns the instance of the PickupManager singleton.
112            */
[6711]113            static PickupManager& getInstance() { return Singleton<PickupManager>::getInstance(); } // tolua_export
[7163]114
[9348]115            bool registerRepresentation(const std::string& name, PickupRepresentation* representation);
116            bool unregisterRepresentation(const std::string& name);
[7533]117
[9348]118            PickupRepresentation* getRepresentation(const std::string& name); // tolua_export
[7533]119
[11071]120            virtual void pickupChangedUsed(Pickupable* pickup, bool used) override; //!< Is called by the PickupListener to notify the PickupManager, that the input Pickupable has transited to the input used state.
[7533]121            static void pickupChangedUsedNetwork(uint32_t pickup, bool inUse, bool usable, bool unusable); //!< Helper method to react to the change in the used status of a Pickupable.
[11071]122            virtual void pickupChangedPickedUp(Pickupable* pickup, bool pickedUp) override; //!< Is called by the PickupListener to notify the PickupManager, that the input Pickupable has transited to the input pickedUp state.
[9348]123            static void pickupChangedPickedUpNetwork(uint32_t pickup, bool usable, uint32_t representationObjectId, const std::string& representationName, bool pickedUp); //!< Helper method to react to the change in the pickedUp status of a Pickupable.
[7533]124
125        // Methods to be used by the PickupInventory.
126        public:
[6711]127            // tolua_begin
[7533]128            int getNumPickups(void); //!< Get the number of pickups currently picked up by the player.
[7206]129            /**
[7533]130            @brief Get the next PickupInventoryContainer in the list.
131                   Use this, after having called getNumPickups() to access all the PickupInventoryContainers individually and in succession.
132            @return Returns the next PickupInventoryContainer in the list.
[7206]133            */
[7547]134            const orxonox::PickupInventoryContainer* popPickup(void)
[7533]135                { return (this->pickupsIterator_++)->second; }
[7163]136
[7533]137            void dropPickup(uint32_t pickup); //!< Drop the input Pickupable.
[11353]138            static void useUnusePickup(uint32_t index); //tolua_export
[7533]139            void usePickup(uint32_t pickup, bool use); //!< Use (or unuse) the input Pickupable.
140             /**
141            @brief Check whether the input Pickupable is valid, meaning that it is in the PickupManager's list and still exists.
142            @param pickup The Pickupable.
143            @return Returns true if the input Pickupable is still valid, false if not.
144            */
[8706]145            bool isValidPickup(uint32_t pickup) const
[7533]146                { return this->pickups_.find(pickup) != this->pickups_.end(); }
[6711]147            // tolua_end
[7163]148
[7533]149            static void dropPickupNetworked(uint32_t pickup); //!< Helper method to drop the input pickup on the server.
150            static void usePickupNetworked(uint32_t pickup, bool use); //!< Helper method to use (or unuse) the input Pickupable on the server.
[11353]151            void setPickupSystem(HUDPickupSystem* system);
[7504]152
[6474]153        private:
[11353]154            HUDPickupSystem* pickupSystem;
155            std::vector<Pickupable*> picks;
156
[6474]157            static PickupManager* singletonPtr_s;
[7206]158            static const std::string guiName_s; //!< The name of the PickupInventory
[7533]159            bool guiLoaded_; //!< Whether the PickupInventory GUI has been loaded, yet.
160            uint32_t pickupHighestIndex_; //!< The highest pickup index currently in use. (not taking wrap-around into account)
[7163]161
[6474]162            PickupRepresentation* defaultRepresentation_; //!< The default PickupRepresentation.
[7163]163
[9348]164            std::map<std::string, PickupRepresentation*> representations_; //!< Map linking PickupRepresentations and their names.
[7163]165
[7540]166            std::map<uint32_t, PickupInventoryContainer*> pickupInventoryContainers_; //!< Map linking a number identifying a Pickupable to a PickupInventoryContainer, which contains all necessary information about that Pickupable.
[7533]167            std::map<uint32_t, PickupInventoryContainer*>::iterator pickupsIterator_; //!< An iterator pointing to the current Pickupable in pickupsList_.
[7163]168
[11071]169            std::map<uint32_t, WeakPtr<Pickupable>> pickups_; //!< Map linking a number identifying a Pickupable to a weak pointer of a Pickupable.
[7540]170            std::map<Pickupable*, uint32_t> indexes_;//!< Map linking Pickupable to the number identifying it.
[7504]171
[7533]172            void updateGUI(void); //!< Updates the PickupInventory GUI.
173            uint32_t getPickupIndex(void); //!< Get a new index for a Pickupable.
[11353]174           
[6711]175    }; // tolua_export
[7163]176
[6711]177} // tolua_export
[6474]178
179#endif // _PickupManager_H__
Note: See TracBrowser for help on using the repository browser.