Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6858 was 6857, checked in by dafrick, 15 years ago

Documented and simplified DistanceMultiTrigger.

File size: 4.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 DistanceMultiTrigger.cc
31    @brief Implementation of the DistanceMultiTrigger class.
32*/
33
34#include "DistanceMultiTrigger.h"
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38
39namespace orxonox
40{
41   
42    CreateFactory(DistanceMultiTrigger);
43
44    /**
45    @brief
46        Default Constructor. Registers the object and initializes default values.
47    */
48    DistanceMultiTrigger::DistanceMultiTrigger(BaseObject* creator) : MultiTrigger(creator)
49    {
50        RegisterObject(DistanceMultiTrigger);
51       
52        this->distance_ = 100.0f;
53    }
54
55    /**
56    @brief
57        Destructor.
58    */
59    DistanceMultiTrigger::~DistanceMultiTrigger()
60    {
61       
62    }
63
64    /**
65    @brief
66        Method for creating a DistanceMultiTrigger object through XML.
67    */
68    void DistanceMultiTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)
69    {
70        SUPER(DistanceMultiTrigger, XMLPort, xmlelement, mode);
71
72        XMLPortParam(DistanceMultiTrigger, "distance", setDistance, getDistance, xmlelement, mode);
73    }
74
75    /**
76    @brief
77        This method is called by the MultiTrigger to get information about new trigger events that need to be looked at.
78
79        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.
80    */
81    std::queue<MultiTriggerState*>* DistanceMultiTrigger::letTrigger(void)
82    {
83        ClassTreeMask& targetMask = this->getTargetMask();
84
85        std::queue<MultiTriggerState*>* queue = NULL;
86
87        // Check for objects that were in range but no longer are. Iterate through all objects, that are in range.
88        for(std::set<WorldEntity*>::iterator it = this->range_.begin(); it != this->range_.end(); )
89        {
90            Vector3 distanceVec = (*it)->getWorldPosition() - this->getWorldPosition();
91            // If the object is no longer in range.
92            if (distanceVec.length() > this->distance_)
93            {
94                WorldEntity* temp = *(it++);
95                if(!this->removeFromRange(temp))
96                    continue;
97
98                // If no queue has been created, yet.
99                if(queue == NULL)
100                    queue = new std::queue<MultiTriggerState*>();
101
102                // Create a state and append it to the queue.
103                MultiTriggerState* state = new MultiTriggerState;
104                state->bTriggered = false;
105                state->originator = temp;
106                queue->push(state);
107            }
108            else
109                ++it;
110        }
111
112        // Check for new objects that are in range
113        for(ClassTreeMaskObjectIterator it = targetMask.begin(); it != targetMask.end(); ++it)
114        {
115            WorldEntity* entity = orxonox_cast<WorldEntity*>(*it);
116            if (entity == NULL || this->inRange(entity)) //If the object is no WorldEntity or is already in range.
117                continue;
118
119            Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition();
120            // If the object is in range.
121            if (distanceVec.length() <= this->distance_)
122            {
123                // Add the object to the objects that are in range.
124                if(!this->addToRange(entity))
125                    continue;
126
127                // If no queue has been created, yet.
128                if(queue == NULL)
129                    queue = new std::queue<MultiTriggerState*>();
130
131                // Create a state and append it to the queue.
132                MultiTriggerState* state = new MultiTriggerState;
133                state->bTriggered = true;
134                state->originator = entity;
135                queue->push(state);
136            }
137        }
138       
139        return queue;
140    }
141   
142}
Note: See TracBrowser for help on using the repository browser.