Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/objects/triggers/DistanceTrigger.cc @ 7647

Last change on this file since 7647 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
RevLine 
[1693]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
[7601]29/**
30    @file DistanceTrigger.cc
31    @brief Implementation of the DistanceTrigger class.
32    @ingroup NormalTrigger
33*/
34
[1693]35#include "DistanceTrigger.h"
36
37#include "core/CoreIncludes.h"
38#include "core/XMLPort.h"
[5735]39#include "worldentities/pawns/Pawn.h"
[6906]40#include "DistanceTriggerBeacon.h"
[2261]41
[2071]42namespace orxonox
43{
[1693]44  CreateFactory(DistanceTrigger);
45
[5727]46  DistanceTrigger::DistanceTrigger(BaseObject* creator) : Trigger(creator)
[1693]47  {
48    RegisterObject(DistanceTrigger);
49
[2029]50    this->distance_ = 100;
51    this->targetMask_.exclude(Class(BaseObject));
[6906]52    this->targetName_ = BLANKSTRING;
53    this->singleTargetMode_ = false;
[1693]54  }
55
56  DistanceTrigger::~DistanceTrigger()
57  {
58  }
59
[2029]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);
[6906]65    XMLPortParamLoadOnly(DistanceTrigger, "target", addTargets, xmlelement, mode).defaultValues("Pawn");
66    XMLPortParam(DistanceTrigger, "targetname", setTargetName, getTargetName, xmlelement, mode);
[2029]67  }
68
[1693]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;
[2029]82      for (it = this->targetSet_.begin(); it != this->targetSet_.end(); ++it)
[1693]83      {
84        COUT(4) << *it << std::endl;
85      }
86      COUT(4) << "End of targetSet of trigger " << this << std::endl;
87    }
88  }
89
[1969]90  void DistanceTrigger::addTargets(const std::string& targets)
[1693]91  {
[1961]92    Identifier* targetId = ClassByString(targets);
[3034]93
[2261]94    //! Checks whether the target is (or is derived from) a ControllableEntity.
[6906]95    Identifier* pawnId = Class(Pawn);
96    Identifier* distanceTriggerBeaconId = Class(DistanceTriggerBeacon);
97    if(targetId->isA(pawnId) || targetId->isA(distanceTriggerBeaconId))
[2261]98    {
99      this->setForPlayer(true);
100    }
[3034]101
[2069]102    if (!targetId)
[2071]103    {
[6417]104        COUT(1) << "Error: \"" << targets << "\" is not a valid class name to include in ClassTreeMask (in " << this->getName() << ", class " << this->getIdentifier()->getName() << ')' << std::endl;
[2069]105        return;
[2071]106    }
[2069]107
108    this->targetMask_.include(targetId);
109
[1693]110    // trigger shouldn't react on itself or other triggers
[2069]111    this->targetMask_.exclude(Class(Trigger), true);
[1693]112
[2069]113    // we only want WorldEntities
114    ClassTreeMask WEMask;
115    WEMask.include(Class(WorldEntity));
116    this->targetMask_ *= WEMask;
[3033]117
118    this->notifyMaskUpdate();
[1693]119  }
120
[1969]121  void DistanceTrigger::removeTargets(const std::string& targets)
[1693]122  {
[1961]123    Identifier* targetId = ClassByString(targets);
[2069]124    this->targetMask_.exclude(targetId);
[1693]125  }
126
127  bool DistanceTrigger::checkDistance()
128  {
[2069]129    // Iterate through all objects
130    for (ClassTreeMaskObjectIterator it = this->targetMask_.begin(); it != this->targetMask_.end(); ++it)
[1693]131    {
[3325]132      WorldEntity* entity = orxonox_cast<WorldEntity*>(*it);
[2069]133      if (!entity)
134        continue;
135
[7301]136      // If the DistanceTrigger is in single-target mode.
[6906]137      if(this->singleTargetMode_)
138      {
[7301]139        // If the object that is a target is no DistanceTriggerBeacon, then the DistanceTrigger can't be in single-target-mode.
[6906]140        if(!(*it)->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier()))
[7301]141        {
[6906]142          this->singleTargetMode_ = false;
[7301]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.
[6906]146        else if(entity->getName().compare(this->targetName_) != 0)
147          continue;
148      }
[7163]149
[2069]150      Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition();
151      if (distanceVec.length() < this->distance_)
[2261]152      {
[3034]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.
[2261]155        if(this->isForPlayer())
[3034]156        {
[6906]157
[7301]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.
[6906]159          if(this->singleTargetMode_)
160            entity = entity->getParent();
161
[3325]162          Pawn* player = orxonox_cast<Pawn*>(entity);
[3034]163          this->setTriggeringPlayer(player);
164        }
165
[2069]166        return true;
[2261]167      }
[1693]168    }
[7163]169
[1693]170    return false;
171  }
172
[3280]173  bool DistanceTrigger::isTriggered(TriggerMode::Value mode)
[1693]174  {
[2029]175    if (Trigger::isTriggered(mode))
[2261]176    {
[1693]177      return checkDistance();
[2261]178    }
[1693]179    else
180      return false;
181  }
182}
Note: See TracBrowser for help on using the repository browser.