Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/objects/worldentities/triggers/DistanceTrigger.cc @ 2193

Last change on this file since 2193 was 2071, checked in by landauf, 16 years ago
  • fixed another bug in Trigger
  • added EventTrigger which listens on "trigger" events
  • Property svn:eol-style set to native
  • Property svn:mergeinfo set to (toggle deleted branches)
    /code/branches/script_trigger/src/orxonox/objects/DistanceTrigger.ccmergedeligible
    /code/branches/ceguilua/src/orxonox/objects/DistanceTrigger.cc1802-1808
    /code/branches/core3/src/orxonox/objects/DistanceTrigger.cc1572-1739
    /code/branches/gcc43/src/orxonox/objects/DistanceTrigger.cc1580
    /code/branches/gui/src/orxonox/objects/DistanceTrigger.cc1635-1723
    /code/branches/input/src/orxonox/objects/DistanceTrigger.cc1629-1636
File size: 3.9 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
[1906]29#include "OrxonoxStableHeaders.h"
[1693]30#include "DistanceTrigger.h"
31
[2071]32#include <OgreNode.h>
33
[1693]34#include "core/CoreIncludes.h"
35#include "core/XMLPort.h"
36
[2071]37namespace orxonox
38{
[1693]39  CreateFactory(DistanceTrigger);
40
[2019]41  DistanceTrigger::DistanceTrigger(BaseObject* creator) : Trigger(creator)
[1693]42  {
43    RegisterObject(DistanceTrigger);
44
[2029]45    this->distance_ = 100;
46    this->targetMask_.exclude(Class(BaseObject));
[1693]47  }
48
49  DistanceTrigger::~DistanceTrigger()
50  {
51  }
52
[2029]53  void DistanceTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)
54  {
55    SUPER(DistanceTrigger, XMLPort, xmlelement, mode);
56
57    XMLPortParam(DistanceTrigger, "distance", setDistance, getDistance, xmlelement, mode).defaultValues(100.0f);
[2069]58    XMLPortParamLoadOnly(DistanceTrigger, "target", addTargets, xmlelement, mode).defaultValues("ControllableEntity");
[2029]59  }
60
[1693]61  void DistanceTrigger::addTarget(Ogre::Node* targetNode)
62  {
63    this->targetSet_.insert(targetNode);
64  }
65
66  void DistanceTrigger::removeTarget(Ogre::Node* targetNode)
67  {
68    int returnval = this->targetSet_.erase(targetNode);
69    if (returnval == 0)
70    {
71      COUT(2) << "Warning: Node " << targetNode << " did not exist in targetSet of trigger " << this << " !" << std::endl;
72      COUT(4) << "Content of targetSet of trigger " << this << " :" << std::endl;
73      std::set<Ogre::Node*>::iterator it;
[2029]74      for (it = this->targetSet_.begin(); it != this->targetSet_.end(); ++it)
[1693]75      {
76        COUT(4) << *it << std::endl;
77      }
78      COUT(4) << "End of targetSet of trigger " << this << std::endl;
79    }
80  }
81
[1969]82  void DistanceTrigger::addTargets(const std::string& targets)
[1693]83  {
[1961]84    Identifier* targetId = ClassByString(targets);
[2069]85    if (!targetId)
[2071]86    {
87        COUT(1) << "Error: \"" << targets << "\" is not a valid class name to include in ClassTreeMask (in " << this->getName() << ", class " << this->getIdentifier()->getName() << ")" << std::endl;
[2069]88        return;
[2071]89    }
[2069]90
91    this->targetMask_.include(targetId);
92
[1693]93    // trigger shouldn't react on itself or other triggers
[2069]94    this->targetMask_.exclude(Class(Trigger), true);
[1693]95
[2069]96    // we only want WorldEntities
97    ClassTreeMask WEMask;
98    WEMask.include(Class(WorldEntity));
99    this->targetMask_ *= WEMask;
[1693]100  }
101
[1969]102  void DistanceTrigger::removeTargets(const std::string& targets)
[1693]103  {
[1961]104    Identifier* targetId = ClassByString(targets);
[2069]105    this->targetMask_.exclude(targetId);
[1693]106  }
107
108  bool DistanceTrigger::checkDistance()
109  {
[2069]110    // Iterate through all objects
111    for (ClassTreeMaskObjectIterator it = this->targetMask_.begin(); it != this->targetMask_.end(); ++it)
[1693]112    {
[2069]113      WorldEntity* entity = dynamic_cast<WorldEntity*>(*it);
114      if (!entity)
115        continue;
116
117      Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition();
118      if (distanceVec.length() < this->distance_)
119        return true;
[1693]120    }
121    return false;
122
123  }
124
125  bool DistanceTrigger::isTriggered(TriggerMode mode)
126  {
[2029]127    if (Trigger::isTriggered(mode))
[1693]128      return checkDistance();
129    else
130      return false;
131  }
132}
Note: See TracBrowser for help on using the repository browser.