| [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 |  | 
|---|
|  | 40 | namespace 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 | { | 
|---|
|  | 64 | } | 
|---|
| [6857] | 65 |  | 
|---|
|  | 66 | /** | 
|---|
|  | 67 | @brief | 
|---|
|  | 68 | Method for creating a DistanceMultiTrigger object through XML. | 
|---|
|  | 69 | */ | 
|---|
| [6800] | 70 | void DistanceMultiTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
|  | 71 | { | 
|---|
|  | 72 | SUPER(DistanceMultiTrigger, XMLPort, xmlelement, mode); | 
|---|
|  | 73 |  | 
|---|
| [6857] | 74 | XMLPortParam(DistanceMultiTrigger, "distance", setDistance, getDistance, xmlelement, mode); | 
|---|
| [6906] | 75 | XMLPortParam(DistanceMultiTrigger, "targetname", setTargetName, getTargetName, xmlelement, mode); | 
|---|
| [6800] | 76 | } | 
|---|
| [6857] | 77 |  | 
|---|
|  | 78 | /** | 
|---|
|  | 79 | @brief | 
|---|
|  | 80 | This method is called by the MultiTrigger to get information about new trigger events that need to be looked at. | 
|---|
|  | 81 |  | 
|---|
|  | 82 | 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. | 
|---|
|  | 83 | */ | 
|---|
| [6800] | 84 | std::queue<MultiTriggerState*>* DistanceMultiTrigger::letTrigger(void) | 
|---|
|  | 85 | { | 
|---|
| [6857] | 86 |  | 
|---|
| [6800] | 87 | std::queue<MultiTriggerState*>* queue = NULL; | 
|---|
| [6857] | 88 |  | 
|---|
|  | 89 | // Check for objects that were in range but no longer are. Iterate through all objects, that are in range. | 
|---|
| [6860] | 90 | for(std::map<WorldEntity*, WeakPtr<WorldEntity>* >::iterator it = this->range_.begin(); it != this->range_.end(); ) | 
|---|
| [6857] | 91 | { | 
|---|
| [6860] | 92 | WorldEntity* entity = it->second->get(); | 
|---|
|  | 93 | WorldEntity* key = it->first; | 
|---|
|  | 94 | if(entity == NULL) | 
|---|
|  | 95 | { | 
|---|
| [6864] | 96 | ++it; | 
|---|
| [6860] | 97 | this->removeFromRange(key); | 
|---|
|  | 98 | continue; | 
|---|
|  | 99 | } | 
|---|
| [7163] | 100 |  | 
|---|
| [6860] | 101 | Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition(); | 
|---|
| [6857] | 102 | // If the object is no longer in range. | 
|---|
|  | 103 | if (distanceVec.length() > this->distance_) | 
|---|
|  | 104 | { | 
|---|
| [6864] | 105 | if(!this->removeFromRange(key)) | 
|---|
|  | 106 | { | 
|---|
|  | 107 | ++it; | 
|---|
| [6857] | 108 | continue; | 
|---|
| [6864] | 109 | } | 
|---|
| [6857] | 110 |  | 
|---|
|  | 111 | // If no queue has been created, yet. | 
|---|
|  | 112 | if(queue == NULL) | 
|---|
|  | 113 | queue = new std::queue<MultiTriggerState*>(); | 
|---|
|  | 114 |  | 
|---|
|  | 115 | // Create a state and append it to the queue. | 
|---|
|  | 116 | MultiTriggerState* state = new MultiTriggerState; | 
|---|
|  | 117 | state->bTriggered = false; | 
|---|
| [6860] | 118 | state->originator = entity; | 
|---|
| [6857] | 119 | queue->push(state); | 
|---|
|  | 120 | } | 
|---|
|  | 121 | else | 
|---|
|  | 122 | ++it; | 
|---|
|  | 123 | } | 
|---|
|  | 124 |  | 
|---|
| [6864] | 125 | ClassTreeMask& targetMask = this->getTargetMask(); | 
|---|
|  | 126 |  | 
|---|
| [6800] | 127 | // Check for new objects that are in range | 
|---|
|  | 128 | for(ClassTreeMaskObjectIterator it = targetMask.begin(); it != targetMask.end(); ++it) | 
|---|
|  | 129 | { | 
|---|
| [6860] | 130 | WorldEntity* entity = static_cast<WorldEntity*>(*it); | 
|---|
|  | 131 | if (entity == NULL) //If the object is no WorldEntity or is already in range. | 
|---|
| [6800] | 132 | continue; | 
|---|
|  | 133 |  | 
|---|
| [6906] | 134 | // If the DistanceMultiTrigger is in single-target-mode. | 
|---|
|  | 135 | if(this->singleTargetMode_) | 
|---|
|  | 136 | { | 
|---|
|  | 137 | // If the object that is a target is no DistanceTriggerBeacon, then the DistanceMultiTrigger can't be in single-target-mode. | 
|---|
|  | 138 | if(!(*it)->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier())) | 
|---|
|  | 139 | this->singleTargetMode_ = false; | 
|---|
|  | 140 | // If the target name and the name of the DistancTriggreBeacon don't match. | 
|---|
|  | 141 | else if(entity->getName().compare(this->targetName_) != 0) | 
|---|
|  | 142 | continue; | 
|---|
|  | 143 | } | 
|---|
|  | 144 |  | 
|---|
| [6800] | 145 | Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition(); | 
|---|
| [6857] | 146 | // If the object is in range. | 
|---|
|  | 147 | if (distanceVec.length() <= this->distance_) | 
|---|
| [6800] | 148 | { | 
|---|
| [6857] | 149 | // Add the object to the objects that are in range. | 
|---|
| [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; | 
|---|
|  | 163 | state->bTriggered = true; | 
|---|
|  | 164 | state->originator = entity; | 
|---|
|  | 165 | queue->push(state); | 
|---|
|  | 166 | } | 
|---|
|  | 167 | } | 
|---|
| [7163] | 168 |  | 
|---|
| [6800] | 169 | return queue; | 
|---|
|  | 170 | } | 
|---|
| [7163] | 171 |  | 
|---|
| [6800] | 172 | } | 
|---|