Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/BaseObject.h @ 7163

Last change on this file since 7163 was 7163, checked in by dafrick, 14 years ago

Merged presentation3 branch into trunk.

  • Property svn:eol-style set to native
File size: 11.4 KB
Line 
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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file
31    @brief Definition of the BaseObject class.
32
33    The BaseObject is the parent of all classes representing an instance in the game.
34*/
35
36#ifndef _BaseObject_H__
37#define _BaseObject_H__
38
39#include "CorePrereqs.h"
40
41#include <map>
42#include <list>
43
44#include "util/mbool.h"
45#include "OrxonoxClass.h"
46#include "Super.h"
47#include "SmartPtr.h"
48
49namespace orxonox
50{
51    class Scene;
52    class Gametype;
53    class Level;
54
55    //! The BaseObject is the parent of all classes representing an instance in the game.
56    class _CoreExport BaseObject : virtual public OrxonoxClass
57    {
58        template <class T> friend class XMLPortClassParamContainer;
59
60        public:
61            BaseObject(BaseObject* creator);
62            virtual ~BaseObject();
63            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
64            virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
65
66            /** @brief Returns if the object was initialized (passed the object registration). @return True was the object is initialized */
67            inline bool isInitialized() const { return this->bInitialized_; }
68
69            /** @brief Sets the name of the object. @param name The name */
70            inline void setName(const std::string& name) { this->oldName_ = this->name_; this->name_ = name; this->changedName(); }
71            /** @brief Returns the name of the object. */
72            inline const std::string& getName() const { return this->name_; }
73            /** @brief Returns the old name of the object. */
74            inline const std::string& getOldName() const { return this->oldName_; }
75            /** @brief This function gets called if the name of the object changes. */
76            virtual void changedName() {}
77
78            /** @brief Sets the state of the objects activity. @param bActive True = active */
79            inline void setActive(bool bActive)
80            {
81                if (this->bActive_ != bActive)
82                {
83                    this->bActive_ = bActive;
84                    this->changedActivity();
85                }
86            }
87            /** @brief Returns the state of the objects activity. @return The state of the activity */
88            inline const mbool& isActive() const { return this->bActive_; }
89            /** @brief This function gets called if the activity of the object changes. */
90            virtual void changedActivity() {}
91
92            /** @brief Sets the state of the objects visibility. @param bVisible True = visible */
93            inline void setVisible(bool bVisible)
94            {
95                if (this->bVisible_ != bVisible)
96                {
97                    this->bVisible_ = bVisible;
98                    this->changedVisibility();
99                }
100            }
101            /** @brief Returns the state of the objects visibility. @return The state of the visibility */
102            inline const mbool& isVisible() const { return this->bVisible_; }
103            /** @brief This function gets called if the visibility of the object changes. */
104            virtual void changedVisibility() {}
105
106            void setMainState(bool state);
107
108            /** @brief Sets the name of the main state (used for event reactions). */
109            void setMainStateName(const std::string& name)
110            {
111                if (this->mainStateName_ != name)
112                {
113                    this->mainStateName_ = name;
114                    this->changedMainStateName();
115                }
116            }
117            /** @brief Returns the name of the main state. */
118            inline const std::string& getMainStateName() const { return this->mainStateName_; }
119            /** @brief This function gets called if the main state name of the object changes. */
120            virtual void changedMainStateName();
121
122            /** @brief Sets a pointer to the xml file that loaded this object. @param file The pointer to the XMLFile */
123            inline void setFile(const XMLFile* file) { this->file_ = file; }
124            /** @brief Returns a pointer to the XMLFile that loaded this object. @return The XMLFile */
125            inline const XMLFile* getFile() const { return this->file_; }
126            const std::string& getFilename() const;
127
128            void addTemplate(const std::string& name);
129            void addTemplate(Template* temp);
130            /** @brief Returns the set of all aplied templates. */
131            inline const std::set<Template*>& getTemplates() const
132                { return this->templates_; }
133
134            virtual inline void setNamespace(Namespace* ns) { this->namespace_ = ns; }
135            inline Namespace* getNamespace() const { return this->namespace_; }
136
137            inline void setCreator(BaseObject* creator) { this->creator_ = creator; }
138            inline BaseObject* getCreator() const { return this->creator_; }
139
140            inline void setScene(const SmartPtr<Scene>& scene, uint32_t sceneID) { this->scene_ = scene; this->sceneID_=sceneID; }
141            inline const SmartPtr<Scene>& getScene() const { return this->scene_; }
142            inline virtual uint32_t getSceneID() const { return this->sceneID_; }
143
144            inline void setGametype(const SmartPtr<Gametype>& gametype)
145            {
146                if (gametype != this->gametype_)
147                {
148                    this->oldGametype_ = this->gametype_;
149                    this->gametype_ = gametype;
150                    this->changedGametype();
151                }
152            }
153            inline const SmartPtr<Gametype>& getGametype() const { return this->gametype_; }
154            inline Gametype* getOldGametype() const { return this->oldGametype_; }
155            virtual void changedGametype() {}
156
157            inline void setLevel(const SmartPtr<Level>& level)
158            {
159                if (level != this->level_)
160                {
161                    this->level_ = level;
162                    this->changedLevel();
163                }
164            }
165            inline const SmartPtr<Level>& getLevel() const { return this->level_; }
166            virtual void changedLevel() {}
167
168            void addEventSource(BaseObject* source, const std::string& state);
169            void removeEventSource(BaseObject* source);
170            BaseObject* getEventSource(unsigned int index, const std::string& state) const;
171
172            void addEventListener(BaseObject* listener);
173            BaseObject* getEventListener(unsigned int index) const;
174
175            void fireEvent(const std::string& name = "");
176            void fireEvent(bool activate, const std::string& name = "");
177            void fireEvent(bool activate, BaseObject* originator, const std::string& name = "");
178            void fireEvent(Event& event);
179
180            virtual void processEvent(Event& event);
181
182            /** @brief Sets the indentation of the debug output in the Loader. @param indentation The indentation */
183            inline void setLoaderIndentation(const std::string& indentation) { this->loaderIndentation_ = indentation; }
184            /** @brief Returns the indentation of the debug output in the Loader. @return The indentation */
185            inline const std::string& getLoaderIndentation() const { return this->loaderIndentation_; }
186
187            static void loadAllEventStates(Element& xmlelement, XMLPort::Mode mode, BaseObject* object, Identifier* identifier);
188
189        protected:
190            void addEventState(const std::string& name, EventState* container);
191            EventState* getEventState(const std::string& name) const;
192
193            std::string             name_;                     //!< The name of the object
194            std::string             oldName_;                  //!< The old name of the object
195            mbool                   bActive_;                  //!< True = the object is active
196            mbool                   bVisible_;                 //!< True = the object is visible
197            std::string             mainStateName_;
198            Functor*                mainStateFunctor_;
199            std::set<std::string>   networkTemplateNames_;
200
201        private:
202            /** @brief Adds an object which listens to the events of this object. */
203            void registerEventListener(BaseObject* object);
204            /** @brief Removes an event listener from this object. */
205            inline void unregisterEventListener(BaseObject* object)
206                { this->eventListeners_.erase(object); }
207
208            void setXMLName(const std::string& name);
209            const std::string& getSingleTemplate(void) const;
210            Template* getTemplate(unsigned int index) const;
211            void registerEventStates();
212
213            bool                   bInitialized_;              //!< True if the object was initialized (passed the object registration)
214            const XMLFile*         file_;                      //!< The XMLFile that loaded this object
215            Element*               lastLoadedXMLElement_;      //!< Non 0 if the TinyXML attributes have already been copied to our own lowercase map
216            std::map<std::string, std::string> xmlAttributes_; //!< Lowercase XML attributes
217            std::string            loaderIndentation_;         //!< Indentation of the debug output in the Loader
218            Namespace*             namespace_;
219            BaseObject*            creator_;
220            SmartPtr<Scene>        scene_;
221            uint32_t               sceneID_;
222            SmartPtr<Gametype>     gametype_;
223            Gametype*              oldGametype_;
224            SmartPtr<Level>        level_;
225            std::set<Template*>    templates_;
226
227            std::map<BaseObject*, std::string>  eventSources_;           //!< List of objects which send events to this object, mapped to the state which they affect
228            std::set<BaseObject*>               eventListeners_;         //!< List of objects which listen to the events of this object
229            std::set<BaseObject*>               eventListenersXML_;      //!< List of objects which listen to the events of this object through the "eventlisteners" subsection in XML
230            std::map<std::string, EventState*>  eventStates_;            //!< Maps the name of the event states to their helper objects
231            bool                                bRegisteredEventStates_; //!< Becomes true after the object registered its event states (with XMLEventPort)
232    };
233
234    SUPER_FUNCTION(0, BaseObject, XMLPort, false);
235    SUPER_FUNCTION(2, BaseObject, changedActivity, false);
236    SUPER_FUNCTION(3, BaseObject, changedVisibility, false);
237    SUPER_FUNCTION(4, BaseObject, XMLEventPort, false);
238    SUPER_FUNCTION(8, BaseObject, changedName, false);
239    SUPER_FUNCTION(9, BaseObject, changedGametype, false);
240}
241
242#endif /* _BaseObject_H__ */
Note: See TracBrowser for help on using the repository browser.