Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/cpp11_v2/src/modules/objects/triggers/TriggerBase.h @ 10998

Last change on this file since 10998 was 10998, checked in by landauf, 8 years ago

using strongly typed enum class in pickups and triggers.

  • Property svn:eol-style set to native
File size: 8.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 *      Damian 'Mozork' Frick
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file TriggerBase.h
31    @brief Definition of the TriggerBase class.
32    @ingroup Triggers
33*/
34
35#ifndef _TriggerBase_H__
36#define _TriggerBase_H__
37
38#include "objects/ObjectsPrereqs.h"
39
40#include <set>
41#include <string>
42
43#include "tools/interfaces/Tickable.h"
44#include "worldentities/StaticEntity.h"
45
46namespace orxonox {
47
48    /**
49    @brief
50        The different modes the trigger can be in.
51
52    @ingroup Triggers
53    */
54    enum class TriggerMode
55    {
56        EventTriggerAND, //!< The <em>and</em> mode. The trigger can only trigger if all the children are active.
57        EventTriggerOR, //!< The <em>or</em> mode. The trigger can only trigger if at least one child is active.
58        EventTriggerXOR, //!< The <em>xor</em> mode. The trigger can only trigger if exactly one child is active.
59    };
60
61    /**
62    @brief
63        The TriggerBase class is a base class for the two types of triggers, the (normal) @ref orxonox::Trigger "Triggers" and the @ref orxonox::MultiTrigger "MultiTriggers", it encompasses the shared data and functionality between these two types of triggers, but is in itself not a trigger that has any meaningful behavior and thus should not be instantiated.
64
65    @author
66        Damian 'Mozork' Frick
67
68    @ingroup Triggers
69    */
70    class _ObjectsExport TriggerBase : public StaticEntity, public Tickable
71    {
72        public:
73            TriggerBase(Context* context);
74            virtual ~TriggerBase();
75
76            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);  //!< Method for creating a TriggerBase object through XML.
77            virtual void tick(float dt); //!< A method that is executed each tick.
78
79            /**
80            @brief Check whether the trigger is active. (i.e. triggered to the outside)
81            @return Returns if the trigger is active.
82            */
83            virtual bool isActive(void) const
84                { return false; }
85
86            /**
87            @brief Set the delay of the trigger.
88            @param delay The delay to be set.
89            */
90            inline void setDelay(float delay)
91                { if(delay > 0.0f) this->delay_= delay; this->delayChanged(); }
92            /**
93            @brief Get the delay of the trigger.
94            @return The delay.
95            */
96            inline float getDelay(void) const
97                { return this->delay_; }
98            virtual void delayChanged(void) {}
99
100            /**
101            @brief Set switch-mode of the trigger.
102            @param bSwitch If true the trigger is set to switched.
103            */
104            inline void setSwitch(bool bSwitch)
105                { this->bSwitch_ = bSwitch; }
106            /**
107            @brief Get the switch-mode of the trigger.
108            @return Returns true if the trigger is in switch-mode.
109            */
110            inline bool getSwitch(void) const
111                { return this->bSwitch_; }
112
113            /**
114            @brief Set the stay-active-mode of the trigger.
115            @param bStayActive If true the trigger is set to stay active.
116            */
117            inline void setStayActive(bool bStayActive)
118                { this->bStayActive_ = bStayActive; }
119            /**
120            @brief Get the stay-active-mode of the trigger.
121            @return Returns true if the trigger stays active.
122            */
123            inline bool getStayActive(void) const
124                { return this->bStayActive_; }
125
126            /**
127            @brief Get the number of remaining activations of the trigger.
128            @return The number of activations. -1 denotes infinity.
129            */
130            inline int getActivations(void) const
131                { return this->remainingActivations_; }
132            /**
133            @brief Check whether the trigger has still at least one remaining activation.
134            @return Returns true if the trigger has remaining activations (i.e. the number of remaining activations is not zero).
135            */
136            inline bool hasRemainingActivations(void) const
137                { return this->remainingActivations_ > 0 || this->remainingActivations_ == INF_s; }
138
139            /**
140            @brief Set the invert-mode of triggerhe trigger.
141            @param bInvert If true the t is set to invert.
142            */
143            inline void setInvert(bool bInvert)
144                { this->bInvertMode_ = bInvert; }
145            /**
146            @brief Get the invert-mode of the trigger.
147            @return Returns true if the trigger is set to invert.
148            */
149            inline bool getInvert(void) const
150                { return this->bInvertMode_; }
151
152            void setMode(const std::string& modeName); //!< Set the mode of the trigger.
153            /**
154            @brief Set the mode of the trigger.
155            @param mode The mode of the trigger.
156            */
157            inline void setMode(TriggerMode mode) //!< Get the mode of the trigger.
158                { this->mode_ = mode; }
159            const std::string& getModeString(void) const;
160            /**
161            @brief Get the mode of the trigger.
162            @return Returns and Enum for the mode of the trigger.
163            */
164            inline TriggerMode getMode(void) const
165                { return mode_; }
166
167            void addTrigger(TriggerBase* trigger);
168            const TriggerBase* getTrigger(unsigned int index) const;
169
170            /**
171            @brief Check whether this trigger is a @ref orxonox::MultiTrigger "MultiTrigger".
172                This is done for performance reasons.
173            */
174            inline bool isMultiTrigger(void)
175                { return this->bMultiTrigger_; }
176
177        protected:
178            static const int INF_s; //!< Magic number for infinity.
179            //! Magic strings for the mode.
180            static const std::string and_s;
181            static const std::string or_s;
182            static const std::string xor_s;
183
184            /**
185            @brief Set the number of activations the trigger can go through.
186            @param activations The number of activations. -1 denotes infinitely many activations.
187            */
188            inline void setActivations(int activations)
189                { if(activations >= 0 || activations == INF_s) this->remainingActivations_ = activations; }
190               
191            inline bool hasRemainingActivations(void)
192                { return this->remainingActivations_ == INF_s || this->remainingActivations_  > 0; }
193
194            /**
195            @brief Adds the parent of a MultiTrigger.
196            @param parent A pointer to the parent MultiTrigger.
197            */
198            inline void addParentTrigger(TriggerBase* parent)
199                { this->parent_ = parent; }
200
201            bool bFirstTick_; //!< Bool to check whether this is the first tick.
202
203            float delay_; //!< The delay that is imposed on all new trigger events.
204            bool bSwitch_; //!< Bool for the switch-mode, if true the trigger behaves like a switch.
205            bool bStayActive_; //!< Bool for the stay-active-mode, if true the trigger stays active after its last activation.;
206
207            int remainingActivations_; //!< The remaining activations of this trigger.
208
209            bool bInvertMode_; //!< Bool for the invert-mode, if true the trigger is inverted.
210            TriggerMode mode_; //!< The mode of the trigger.
211
212            TriggerBase* parent_; //!< The parent of this trigger.
213            std::set<TriggerBase*> children_; //!< The children of this trigger.
214
215            bool bMultiTrigger_; //!< Specifies whether this trigger is a MultiTrigger.
216
217    };
218}
219
220#endif /* _Trigger_H__ */
Note: See TracBrowser for help on using the repository browser.