| 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 | #include "DistanceMultiTrigger.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include "core/CoreIncludes.h" | 
|---|
| 32 | #include "core/XMLPort.h" | 
|---|
| 33 |  | 
|---|
| 34 | namespace orxonox | 
|---|
| 35 | { | 
|---|
| 36 |  | 
|---|
| 37 | CreateFactory(DistanceMultiTrigger); | 
|---|
| 38 |  | 
|---|
| 39 | DistanceMultiTrigger::DistanceMultiTrigger(BaseObject* creator) : MultiTrigger(creator) | 
|---|
| 40 | { | 
|---|
| 41 | RegisterObject(DistanceMultiTrigger); | 
|---|
| 42 |  | 
|---|
| 43 | this->distance_ = 100.0f; | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | DistanceMultiTrigger::~DistanceMultiTrigger() | 
|---|
| 47 | { | 
|---|
| 48 |  | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | void DistanceMultiTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 52 | { | 
|---|
| 53 | SUPER(DistanceMultiTrigger, XMLPort, xmlelement, mode); | 
|---|
| 54 |  | 
|---|
| 55 | XMLPortParam(DistanceMultiTrigger, "distance", setDistance, getDistance, xmlelement, mode).defaultValues(100.0f); | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | std::queue<MultiTriggerState*>* DistanceMultiTrigger::letTrigger(void) | 
|---|
| 59 | { | 
|---|
| 60 | ClassTreeMask targetMask = this->getTargetMask(); | 
|---|
| 61 |  | 
|---|
| 62 | std::queue<MultiTriggerState*>* queue = NULL; | 
|---|
| 63 | // Check for new objects that are in range | 
|---|
| 64 | for(ClassTreeMaskObjectIterator it = targetMask.begin(); it != targetMask.end(); ++it) | 
|---|
| 65 | { | 
|---|
| 66 | WorldEntity* entity = orxonox_cast<WorldEntity*>(*it); | 
|---|
| 67 | if (entity == NULL || this->inRange(entity)) //If the object is no WorldEntity or is already in range. | 
|---|
| 68 | continue; | 
|---|
| 69 |  | 
|---|
| 70 | Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition(); | 
|---|
| 71 | if (distanceVec.length() < this->distance_) | 
|---|
| 72 | { | 
|---|
| 73 | if(!this->addToRange(entity)) | 
|---|
| 74 | continue; | 
|---|
| 75 |  | 
|---|
| 76 | if(queue == NULL) | 
|---|
| 77 | { | 
|---|
| 78 | queue = new std::queue<MultiTriggerState*>(); | 
|---|
| 79 | } | 
|---|
| 80 | MultiTriggerState* state = new MultiTriggerState; | 
|---|
| 81 | state->bTriggered = true; | 
|---|
| 82 | state->originator = entity; | 
|---|
| 83 | queue->push(state); | 
|---|
| 84 | } | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | for(std::set<WorldEntity*>::iterator it = this->range_.begin(); it != this->range_.end(); ) | 
|---|
| 88 | { | 
|---|
| 89 | Vector3 distanceVec = (*it)->getWorldPosition() - this->getWorldPosition(); | 
|---|
| 90 | if (distanceVec.length() >= this->distance_) | 
|---|
| 91 | { | 
|---|
| 92 | WorldEntity* temp = *(it++); | 
|---|
| 93 | if(!this->removeFromRange(temp)) | 
|---|
| 94 | continue; | 
|---|
| 95 |  | 
|---|
| 96 | if(queue == NULL) | 
|---|
| 97 | { | 
|---|
| 98 | queue = new std::queue<MultiTriggerState*>(); | 
|---|
| 99 | } | 
|---|
| 100 | MultiTriggerState* state = new MultiTriggerState; | 
|---|
| 101 | state->bTriggered = false; | 
|---|
| 102 | state->originator = temp; | 
|---|
| 103 | queue->push(state); | 
|---|
| 104 | } | 
|---|
| 105 | else | 
|---|
| 106 | ++it; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | return queue; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | } | 
|---|