Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/lastmanstanding3/src/modules/objects/triggers/DistanceTrigger.cc @ 8569

Last change on this file since 8569 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: 5.8 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 *      Benjamin Knecht
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file DistanceTrigger.cc
31    @brief Implementation of the DistanceTrigger class.
32    @ingroup NormalTrigger
33*/
34
35#include "DistanceTrigger.h"
36
37#include "core/CoreIncludes.h"
38#include "core/XMLPort.h"
39#include "worldentities/pawns/Pawn.h"
40#include "DistanceTriggerBeacon.h"
41
42namespace orxonox
43{
44  CreateFactory(DistanceTrigger);
45
46  DistanceTrigger::DistanceTrigger(BaseObject* creator) : Trigger(creator)
47  {
48    RegisterObject(DistanceTrigger);
49
50    this->distance_ = 100;
51    this->targetMask_.exclude(Class(BaseObject));
52    this->targetName_ = BLANKSTRING;
53    this->singleTargetMode_ = false;
54  }
55
56  DistanceTrigger::~DistanceTrigger()
57  {
58  }
59
60  void DistanceTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)
61  {
62    SUPER(DistanceTrigger, XMLPort, xmlelement, mode);
63
64    XMLPortParam(DistanceTrigger, "distance", setDistance, getDistance, xmlelement, mode).defaultValues(100.0f);
65    XMLPortParamLoadOnly(DistanceTrigger, "target", addTargets, xmlelement, mode).defaultValues("Pawn");
66    XMLPortParam(DistanceTrigger, "targetname", setTargetName, getTargetName, xmlelement, mode);
67  }
68
69  void DistanceTrigger::addTarget(Ogre::Node* targetNode)
70  {
71    this->targetSet_.insert(targetNode);
72  }
73
74  void DistanceTrigger::removeTarget(Ogre::Node* targetNode)
75  {
76    int returnval = this->targetSet_.erase(targetNode);
77    if (returnval == 0)
78    {
79      COUT(2) << "Warning: Node " << targetNode << " did not exist in targetSet of trigger " << this << " !" << std::endl;
80      COUT(4) << "Content of targetSet of trigger " << this << " :" << std::endl;
81      std::set<Ogre::Node*>::iterator it;
82      for (it = this->targetSet_.begin(); it != this->targetSet_.end(); ++it)
83      {
84        COUT(4) << *it << std::endl;
85      }
86      COUT(4) << "End of targetSet of trigger " << this << std::endl;
87    }
88  }
89
90  void DistanceTrigger::addTargets(const std::string& targets)
91  {
92    Identifier* targetId = ClassByString(targets);
93
94    //! Checks whether the target is (or is derived from) a ControllableEntity.
95    Identifier* pawnId = Class(Pawn);
96    Identifier* distanceTriggerBeaconId = Class(DistanceTriggerBeacon);
97    if(targetId->isA(pawnId) || targetId->isA(distanceTriggerBeaconId))
98    {
99      this->setForPlayer(true);
100    }
101
102    if (!targetId)
103    {
104        COUT(1) << "Error: \"" << targets << "\" is not a valid class name to include in ClassTreeMask (in " << this->getName() << ", class " << this->getIdentifier()->getName() << ')' << std::endl;
105        return;
106    }
107
108    this->targetMask_.include(targetId);
109
110    // trigger shouldn't react on itself or other triggers
111    this->targetMask_.exclude(Class(Trigger), true);
112
113    // we only want WorldEntities
114    ClassTreeMask WEMask;
115    WEMask.include(Class(WorldEntity));
116    this->targetMask_ *= WEMask;
117
118    this->notifyMaskUpdate();
119  }
120
121  void DistanceTrigger::removeTargets(const std::string& targets)
122  {
123    Identifier* targetId = ClassByString(targets);
124    this->targetMask_.exclude(targetId);
125  }
126
127  bool DistanceTrigger::checkDistance()
128  {
129    // Iterate through all objects
130    for (ClassTreeMaskObjectIterator it = this->targetMask_.begin(); it != this->targetMask_.end(); ++it)
131    {
132      WorldEntity* entity = orxonox_cast<WorldEntity*>(*it);
133      if (!entity)
134        continue;
135
136      // If the DistanceTrigger is in single-target mode.
137      if(this->singleTargetMode_)
138      {
139        // If the object that is a target is no DistanceTriggerBeacon, then the DistanceTrigger can't be in single-target-mode.
140        if(!(*it)->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier()))
141        {
142          this->singleTargetMode_ = false;
143          COUT(2) << "DistanceTrigger " << 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;
144        }
145        // If the target name and the name of the DistancTriggerBeacon don't match.
146        else if(entity->getName().compare(this->targetName_) != 0)
147          continue;
148      }
149
150      Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition();
151      if (distanceVec.length() < this->distance_)
152      {
153
154        // If the target is a player (resp. is a, or is derived from a, ControllableEntity) the triggeringPlayer is set to the target entity.
155        if(this->isForPlayer())
156        {
157
158          // Change the entity to the parent of the DistanceTriggerBeacon (if in single-target-mode), which is the entity to which the beacon is attached.
159          if(this->singleTargetMode_)
160            entity = entity->getParent();
161
162          Pawn* player = orxonox_cast<Pawn*>(entity);
163          this->setTriggeringPlayer(player);
164        }
165
166        return true;
167      }
168    }
169
170    return false;
171  }
172
173  bool DistanceTrigger::isTriggered(TriggerMode::Value mode)
174  {
175    if (Trigger::isTriggered(mode))
176    {
177      return checkDistance();
178    }
179    else
180      return false;
181  }
182}
Note: See TracBrowser for help on using the repository browser.