Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/objects/triggers/DistanceMultiTrigger.cc @ 7601

Last change on this file since 7601 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
RevLine 
[6800]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
[6857]29/**
30    @file DistanceMultiTrigger.cc
31    @brief Implementation of the DistanceMultiTrigger class.
[7601]32    @ingroup MultiTrigger
[6857]33*/
34
[6800]35#include "DistanceMultiTrigger.h"
36
37#include "core/CoreIncludes.h"
38#include "core/XMLPort.h"
[7601]39
[6906]40#include "DistanceTriggerBeacon.h"
[6800]41
42namespace orxonox
43{
[7163]44
[6800]45    CreateFactory(DistanceMultiTrigger);
[6857]46
47    /**
48    @brief
[7163]49        Default Constructor. Registers the object and initializes default values.
[6857]50    */
[6800]51    DistanceMultiTrigger::DistanceMultiTrigger(BaseObject* creator) : MultiTrigger(creator)
52    {
53        RegisterObject(DistanceMultiTrigger);
[7163]54
[6800]55        this->distance_ = 100.0f;
[6906]56        this->targetName_ = BLANKSTRING;
57        this->singleTargetMode_ = false;
[6800]58    }
[6857]59
60    /**
61    @brief
62        Destructor.
63    */
[6800]64    DistanceMultiTrigger::~DistanceMultiTrigger()
65    {
[7301]66
[6800]67    }
[6857]68
69    /**
70    @brief
71        Method for creating a DistanceMultiTrigger object through XML.
72    */
[6800]73    void DistanceMultiTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)
74    {
75        SUPER(DistanceMultiTrigger, XMLPort, xmlelement, mode);
76
[6857]77        XMLPortParam(DistanceMultiTrigger, "distance", setDistance, getDistance, xmlelement, mode);
[6906]78        XMLPortParam(DistanceMultiTrigger, "targetname", setTargetName, getTargetName, xmlelement, mode);
[6800]79    }
[6857]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.
[7301]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.
[6857]88    */
[6800]89    std::queue<MultiTriggerState*>* DistanceMultiTrigger::letTrigger(void)
90    {
[6857]91
[6800]92        std::queue<MultiTriggerState*>* queue = NULL;
[6857]93
94        // Check for objects that were in range but no longer are. Iterate through all objects, that are in range.
[6860]95        for(std::map<WorldEntity*, WeakPtr<WorldEntity>* >::iterator it = this->range_.begin(); it != this->range_.end(); )
[6857]96        {
[6860]97            WorldEntity* entity = it->second->get();
98            WorldEntity* key = it->first;
[7301]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.
[6860]101            if(entity == NULL)
102            {
103                this->removeFromRange(key);
104                continue;
105            }
[7163]106
[6860]107            Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition();
[6857]108            // If the object is no longer in range.
109            if (distanceVec.length() > this->distance_)
110            {
[7301]111                // If for some reason the entity could not be removed.
[6864]112                if(!this->removeFromRange(key))
[6857]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;
[7301]121                state->bTriggered = false; // Because the entity went out of range.
[6860]122                state->originator = entity;
[6857]123                queue->push(state);
124            }
125        }
126
[6800]127        // Check for new objects that are in range
[7301]128        for(ClassTreeMaskObjectIterator it = this->getTargetMask().begin(); it != this->getTargetMask().end(); ++it)
[6800]129        {
[6860]130            WorldEntity* entity = static_cast<WorldEntity*>(*it);
[6800]131
[7301]132            // If the DistanceMultiTrigger is in single-target mode.
[6906]133            if(this->singleTargetMode_)
134            {
[7601]135                // If the object that is a target is no DistanceTriggerBeacon, then the DistanceMultiTrigger can't be in single-target mode.
[7301]136                if(!entity->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier()))
137                {
[6906]138                    this->singleTargetMode_ = false;
[7301]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.
[6906]142                else if(entity->getName().compare(this->targetName_) != 0)
143                    continue;
144            }
145
[6800]146            Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition();
[6857]147            // If the object is in range.
148            if (distanceVec.length() <= this->distance_)
[6800]149            {
[6857]150                // Add the object to the objects that are in range.
[7301]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.
[6800]152                if(!this->addToRange(entity))
153                    continue;
[6857]154
[6906]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();
[7163]158
[6857]159                // If no queue has been created, yet.
[6800]160                if(queue == NULL)
161                    queue = new std::queue<MultiTriggerState*>();
[6857]162
163                // Create a state and append it to the queue.
[6800]164                MultiTriggerState* state = new MultiTriggerState;
[7301]165                state->bTriggered = true; // Because the entity came into range.
[6800]166                state->originator = entity;
167                queue->push(state);
168            }
169        }
[7163]170
[6800]171        return queue;
172    }
[7163]173
[7301]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
[6800]230}
Note: See TracBrowser for help on using the repository browser.