Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7561 was 7301, checked in by dafrick, 14 years ago

Improving documentation for MultiTriggers, also some small bugfixes, simplifications and added features.

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