Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/objects/triggers/TriggerBase.cc @ 7621

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

Adding all classes in modules/objects to Objects module (in doxygen).
Created TriggerBase which is the base class of Trigger and MultiTrigger and now provides the shared functionality and data.
Updated some of the documentation in MultiTrigger and Script.

File size: 5.6 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.cc
31    @brief Implementation of the TriggerBase class.
32    @ingroup Triggers
33*/
34
35#include "TriggerBase.h"
36
37#include "core/CoreIncludes.h"
38#include "core/XMLPort.h"
39
40namespace orxonox {
41
42    // Initialization of some static (magic) variables.
43    /*static*/ const int TriggerBase::INF_s = -1;
44    /*static*/ const std::string TriggerBase::and_s = "and";
45    /*static*/ const std::string TriggerBase::or_s = "or";
46    /*static*/ const std::string TriggerBase::xor_s = "xor";
47
48    CreateFactory(TriggerBase);
49
50    /**
51    @brief
52        Constructor. Registers the object and initializes some values.
53    */
54    TriggerBase::TriggerBase(BaseObject* creator) : StaticEntity(creator)
55    {
56        RegisterObject(TriggerBase);
57
58        this->bFirstTick_ = true;
59
60        this->delay_ = 0.0f;
61        this->bSwitch_ = false;
62        this->bStayActive_ = false;
63
64        this->remainingActivations_ = INF_s;
65
66        this->bInvertMode_ = false;
67        this->mode_ = TriggerMode::EventTriggerAND;
68
69        this->parent_ = NULL;
70
71        this->bMultiTrigger_ = false;
72
73        this->setSyncMode(0x0);
74    }
75
76    /**
77    @brief
78        Destructor.
79    */
80    TriggerBase::~TriggerBase()
81    {
82       
83    }
84
85    /**
86    @brief
87        Method for creating a TriggerBase object through XML.
88        For a detailed description of the parameters please see the class description in the header file.
89    */
90    void TriggerBase::XMLPort(Element& xmlelement, XMLPort::Mode mode)
91    {
92        SUPER(TriggerBase, XMLPort, xmlelement, mode);
93
94        XMLPortParam(TriggerBase, "delay", setDelay, getDelay, xmlelement, mode);
95        XMLPortParam(TriggerBase, "switch", setSwitch, getSwitch, xmlelement, mode);
96        XMLPortParam(TriggerBase, "stayactive", setStayActive, getStayActive, xmlelement, mode);
97        XMLPortParam(TriggerBase, "activations", setActivations, getActivations, xmlelement, mode);
98        XMLPortParam(TriggerBase, "invert", setInvert, getInvert, xmlelement, mode);
99        XMLPortParamTemplate(TriggerBase, "mode", setMode, getModeString, xmlelement, mode, const std::string&);
100
101        XMLPortObject(TriggerBase, TriggerBase, "", addTrigger, getTrigger, xmlelement, mode);
102    }
103
104    /**
105    @brief
106        A method that is executed each tick.
107    @param dt
108        The duration of the last tick.
109    */
110    void TriggerBase::tick(float dt)
111    {
112        SUPER(TriggerBase, tick, dt);
113    }
114
115    /**
116    @brief
117        Set the mode of the trigger.
118    @param modeName
119        The name of the mode as a string.
120    */
121    void TriggerBase::setMode(const std::string& modeName)
122    {
123        if (modeName == TriggerBase::and_s)
124            this->setMode(TriggerMode::EventTriggerAND);
125        else if (modeName == TriggerBase::or_s)
126            this->setMode(TriggerMode::EventTriggerOR);
127        else if (modeName == TriggerBase::xor_s)
128            this->setMode(TriggerMode::EventTriggerXOR);
129        else
130            COUT(2) << "Invalid mode '" << modeName << "' in TriggerBase " << this->getName() << " &(" << this << "). Leaving mode at '" << this->getModeString() << "'." << std::endl;
131    }
132
133    /**
134    @brief
135        Get the mode of the MultiTrigger.
136    @return
137        Returns the mode as a string.
138    */
139    const std::string& TriggerBase::getModeString(void) const
140    {
141        if (this->mode_ == TriggerMode::EventTriggerAND)
142            return TriggerBase::and_s;
143        else if (this->mode_ == TriggerMode::EventTriggerOR)
144            return TriggerBase::or_s;
145        else if (this->mode_ == TriggerMode::EventTriggerXOR)
146            return TriggerBase::xor_s;
147        else // This can never happen, but the compiler needs it to feel secure.
148            return TriggerBase::and_s;
149    }
150
151    /**
152    @brief
153        Adds a trigger as a child to the trigger.
154        Beware: Loops are not prevented and potentially very bad, so just don't create any loops.
155    @param trigger
156        The trigger to be added.
157    */
158    void TriggerBase::addTrigger(TriggerBase* trigger)
159    {
160        assert(trigger);
161        if (this != trigger)
162            this->children_.insert(trigger);
163        trigger->addParentTrigger(this);
164    }
165
166    /**
167    @brief
168        Get the child of this trigger at the given index.
169    @param index
170        The index.
171    @return
172        Returns a pointer ot the trigger. NULL if no such trigger exists.
173    */
174    const TriggerBase* TriggerBase::getTrigger(unsigned int index) const
175    {
176        // If the index is greater than the number of children.
177        if (this->children_.size() <= index)
178            return NULL;
179
180        std::set<TriggerBase*>::const_iterator it;
181        it = this->children_.begin();
182
183        for (unsigned int i = 0; i != index; ++i)
184            ++it;
185
186        return (*it);
187    }
188
189}
Note: See TracBrowser for help on using the repository browser.