Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11175 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 5.5 KB
RevLine 
[7601]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
[9667]48    RegisterClass(TriggerBase);
[7601]49
50    /**
51    @brief
52        Constructor. Registers the object and initializes some values.
53    */
[9667]54    TriggerBase::TriggerBase(Context* context) : StaticEntity(context)
[7601]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
[11071]69        this->parent_ = nullptr;
[7601]70
71        this->bMultiTrigger_ = false;
72
[8706]73        this->setSyncMode(ObjectDirection::None);
[7601]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        Set the mode of the trigger.
107    @param modeName
108        The name of the mode as a string.
109    */
110    void TriggerBase::setMode(const std::string& modeName)
111    {
112        if (modeName == TriggerBase::and_s)
113            this->setMode(TriggerMode::EventTriggerAND);
114        else if (modeName == TriggerBase::or_s)
115            this->setMode(TriggerMode::EventTriggerOR);
116        else if (modeName == TriggerBase::xor_s)
117            this->setMode(TriggerMode::EventTriggerXOR);
118        else
[8858]119            orxout(internal_warning, context::triggers) << "Invalid mode '" << modeName << "' in TriggerBase " << this->getName() << " &(" << this << "). Leaving mode at '" << this->getModeString() << "'." << endl;
[7601]120    }
121
122    /**
123    @brief
124        Get the mode of the MultiTrigger.
125    @return
126        Returns the mode as a string.
127    */
128    const std::string& TriggerBase::getModeString(void) const
129    {
130        if (this->mode_ == TriggerMode::EventTriggerAND)
131            return TriggerBase::and_s;
132        else if (this->mode_ == TriggerMode::EventTriggerOR)
133            return TriggerBase::or_s;
134        else if (this->mode_ == TriggerMode::EventTriggerXOR)
135            return TriggerBase::xor_s;
136        else // This can never happen, but the compiler needs it to feel secure.
137            return TriggerBase::and_s;
138    }
139
140    /**
141    @brief
142        Adds a trigger as a child to the trigger.
143        Beware: Loops are not prevented and potentially very bad, so just don't create any loops.
144    @param trigger
145        The trigger to be added.
146    */
147    void TriggerBase::addTrigger(TriggerBase* trigger)
148    {
149        assert(trigger);
150        if (this != trigger)
151            this->children_.insert(trigger);
152        trigger->addParentTrigger(this);
153    }
154
155    /**
156    @brief
157        Get the child of this trigger at the given index.
158    @param index
159        The index.
160    @return
[11071]161        Returns a pointer ot the trigger. nullptr if no such trigger exists.
[7601]162    */
163    const TriggerBase* TriggerBase::getTrigger(unsigned int index) const
164    {
165        // If the index is greater than the number of children.
166        if (this->children_.size() <= index)
[11071]167            return nullptr;
[7601]168
169        std::set<TriggerBase*>::const_iterator it;
170        it = this->children_.begin();
171
172        for (unsigned int i = 0; i != index; ++i)
173            ++it;
174
175        return (*it);
176    }
177
178}
Note: See TracBrowser for help on using the repository browser.