Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/kicklib/src/modules/objects/triggers/DistanceMultiTrigger.cc @ 8738

Last change on this file since 8738 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.

  • Property svn:eol-style set to native
File size: 8.5 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 DistanceMultiTrigger.cc
31    @brief Implementation of the DistanceMultiTrigger class.
32    @ingroup MultiTrigger
33*/
34
35#include "DistanceMultiTrigger.h"
36
37#include "core/CoreIncludes.h"
38#include "core/XMLPort.h"
39
40#include "DistanceTriggerBeacon.h"
41
42namespace orxonox
43{
44
45    CreateFactory(DistanceMultiTrigger);
46
47    /**
48    @brief
49        Default Constructor. Registers the object and initializes default values.
50    */
51    DistanceMultiTrigger::DistanceMultiTrigger(BaseObject* creator) : MultiTrigger(creator)
52    {
53        RegisterObject(DistanceMultiTrigger);
54
55        this->distance_ = 100.0f;
56        this->targetName_ = BLANKSTRING;
57        this->singleTargetMode_ = false;
58    }
59
60    /**
61    @brief
62        Destructor.
63    */
64    DistanceMultiTrigger::~DistanceMultiTrigger()
65    {
66
67    }
68
69    /**
70    @brief
71        Method for creating a DistanceMultiTrigger object through XML.
72    */
73    void DistanceMultiTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)
74    {
75        SUPER(DistanceMultiTrigger, XMLPort, xmlelement, mode);
76
77        XMLPortParam(DistanceMultiTrigger, "distance", setDistance, getDistance, xmlelement, mode);
78        XMLPortParam(DistanceMultiTrigger, "targetname", setTargetName, getTargetName, xmlelement, mode);
79    }
80
81    /**
82    @brief
83        This method is called by the MultiTrigger to get information about new trigger events that need to be looked at.
84
85        In this implementation we iterate through all possible objects and check whether the fact that they are in range or not has changed and fire and hand a state ofer to the MultiTrigger if so.
86    @return
87        Returns a pointer to a queue of MultiTriggerState pointers, containing all the necessary information to decide whether these states should indeed become new states of the MultiTrigger.
88    */
89    std::queue<MultiTriggerState*>* DistanceMultiTrigger::letTrigger(void)
90    {
91
92        std::queue<MultiTriggerState*>* queue = NULL;
93
94        // Check for objects that were in range but no longer are. Iterate through all objects, that are in range.
95        for(std::map<WorldEntity*, WeakPtr<WorldEntity>* >::iterator it = this->range_.begin(); it != this->range_.end(); )
96        {
97            WorldEntity* entity = it->second->get();
98            WorldEntity* key = it->first;
99            it++; // Incrementing the iterator in advance, since we don't need the current anymore and we potentially are going to delete the current element thus invalidating the iterator.
100            // If the entity no longer exists.
101            if(entity == NULL)
102            {
103                this->removeFromRange(key);
104                continue;
105            }
106
107            Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition();
108            // If the object is no longer in range.
109            if (distanceVec.length() > this->distance_)
110            {
111                // If for some reason the entity could not be removed.
112                if(!this->removeFromRange(key))
113                    continue;
114
115                // If no queue has been created, yet.
116                if(queue == NULL)
117                    queue = new std::queue<MultiTriggerState*>();
118
119                // Create a state and append it to the queue.
120                MultiTriggerState* state = new MultiTriggerState;
121                state->bTriggered = false; // Because the entity went out of range.
122                state->originator = entity;
123                queue->push(state);
124            }
125        }
126
127        // Check for new objects that are in range
128        for(ClassTreeMaskObjectIterator it = this->getTargetMask().begin(); it != this->getTargetMask().end(); ++it)
129        {
130            WorldEntity* entity = static_cast<WorldEntity*>(*it);
131
132            // If the DistanceMultiTrigger is in single-target mode.
133            if(this->singleTargetMode_)
134            {
135                // If the object that is a target is no DistanceTriggerBeacon, then the DistanceMultiTrigger can't be in single-target mode.
136                if(!entity->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier()))
137                {
138                    this->singleTargetMode_ = false;
139                    COUT(2) << "DistanceMultiTrigger " << this->getName() << " (&" << this <<  ")" << "is in single-target mode but the target is '" << entity->getIdentifier()->getName() << "' instead of DistanceTriggerBeacon. Setting single-target mode to false." << std::endl;
140                }
141                // If the target name and the name of the DistancTriggerBeacon don't match.
142                else if(entity->getName().compare(this->targetName_) != 0)
143                    continue;
144            }
145
146            Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition();
147            // If the object is in range.
148            if (distanceVec.length() <= this->distance_)
149            {
150                // Add the object to the objects that are in range.
151                // Objects that already are in range are not added twice, because in a map (this->range_) each key can only exist once and thus addToRange() will reject all attempts of duplicate entries.
152                if(!this->addToRange(entity))
153                    continue;
154
155                // Change the entity to the parent of the DistanceTriggerBeacon (if in single-target-mode), which is the entity to which the beacon is attached.
156                if(this->singleTargetMode_)
157                    entity = entity->getParent();
158
159                // If no queue has been created, yet.
160                if(queue == NULL)
161                    queue = new std::queue<MultiTriggerState*>();
162
163                // Create a state and append it to the queue.
164                MultiTriggerState* state = new MultiTriggerState;
165                state->bTriggered = true; // Because the entity came into range.
166                state->originator = entity;
167                queue->push(state);
168            }
169        }
170
171        return queue;
172    }
173
174    /**
175    @brief
176        Set the target name of DistanceTriggerBeacons that triggers this DistanceMultiTrigger.
177    @param targetname
178        The name of the DistanceTriggerBeacon as a string.
179    */
180    void DistanceMultiTrigger::setTargetName(const std::string& targetname)
181    {
182        // If the targetname is no blank string single-target mode is enabled.
183        if(targetname.compare(BLANKSTRING) != 0)
184            this->singleTargetMode_ = true;
185        else
186            this->singleTargetMode_ = false;
187
188        this->targetName_ = targetname;
189    }
190
191    /**
192    @brief
193        Add a given entity to the entities, that currently are in range of the DistanceMultiTrigger.
194    @param entity
195        A pointer to the entity.
196    @return
197        Returns true if successful, false if not.
198    */
199    bool DistanceMultiTrigger::addToRange(WorldEntity* entity)
200    {
201        WeakPtr<WorldEntity>* weakptr = new WeakPtr<WorldEntity>(entity);
202        std::pair<std::map<WorldEntity*, WeakPtr<WorldEntity>* >::iterator, bool> pair = this->range_.insert(std::pair<WorldEntity*, WeakPtr<WorldEntity>* >(entity, weakptr));
203
204        if(!pair.second)
205        {
206            delete weakptr;
207            return false;
208        }
209
210        return true;
211    }
212
213    /**
214    @brief
215        Remove a given entity from the set of entities, that currently are in range of the DistanceMultiTrigger.
216    @param entity
217        A pointer ot the entity.
218    @return
219        Returns true if successful.
220    */
221    bool DistanceMultiTrigger::removeFromRange(WorldEntity* entity)
222    {
223        WeakPtr<WorldEntity>* weakptr = this->range_.find(entity)->second;
224        bool erased = this->range_.erase(entity) > 0;
225        if(erased)
226            delete weakptr;
227        return erased;
228    }
229
230}
Note: See TracBrowser for help on using the repository browser.