Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/worldentities/triggers/DistanceTrigger.cc @ 3137

Last change on this file since 3137 was 3110, checked in by rgrieder, 16 years ago

Removed old msvc specific support for precompiled header files.

  • Property svn:eol-style set to native
File size: 4.5 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#include "DistanceTrigger.h"
30
31#include <OgreNode.h>
32
33#include "core/CoreIncludes.h"
34#include "core/XMLPort.h"
35
36#include "orxonox/objects/worldentities/pawns/Pawn.h"
37
38namespace orxonox
39{
40  CreateFactory(DistanceTrigger);
41
42  DistanceTrigger::DistanceTrigger(BaseObject* creator) : PlayerTrigger(creator)
43  {
44    RegisterObject(DistanceTrigger);
45
46    this->distance_ = 100;
47    this->targetMask_.exclude(Class(BaseObject));
48    this->setForPlayer(false); //!< Normally hasn't just ControllableEntities as targets.
49  }
50
51  DistanceTrigger::~DistanceTrigger()
52  {
53  }
54
55  void DistanceTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)
56  {
57    SUPER(DistanceTrigger, XMLPort, xmlelement, mode);
58
59    XMLPortParam(DistanceTrigger, "distance", setDistance, getDistance, xmlelement, mode).defaultValues(100.0f);
60    XMLPortParamLoadOnly(DistanceTrigger, "target", addTargets, xmlelement, mode).defaultValues("ControllableEntity");
61  }
62
63  void DistanceTrigger::addTarget(Ogre::Node* targetNode)
64  {
65    this->targetSet_.insert(targetNode);
66  }
67
68  void DistanceTrigger::removeTarget(Ogre::Node* targetNode)
69  {
70    int returnval = this->targetSet_.erase(targetNode);
71    if (returnval == 0)
72    {
73      COUT(2) << "Warning: Node " << targetNode << " did not exist in targetSet of trigger " << this << " !" << std::endl;
74      COUT(4) << "Content of targetSet of trigger " << this << " :" << std::endl;
75      std::set<Ogre::Node*>::iterator it;
76      for (it = this->targetSet_.begin(); it != this->targetSet_.end(); ++it)
77      {
78        COUT(4) << *it << std::endl;
79      }
80      COUT(4) << "End of targetSet of trigger " << this << std::endl;
81    }
82  }
83
84  void DistanceTrigger::addTargets(const std::string& targets)
85  {
86    Identifier* targetId = ClassByString(targets);
87
88    //! Checks whether the target is (or is derived from) a ControllableEntity.
89    Identifier* controllableEntityId = Class(ControllableEntity);
90    if(targetId->isA(controllableEntityId))
91    {
92      this->setForPlayer(true);
93    }
94
95    if (!targetId)
96    {
97        COUT(1) << "Error: \"" << targets << "\" is not a valid class name to include in ClassTreeMask (in " << this->getName() << ", class " << this->getIdentifier()->getName() << ")" << std::endl;
98        return;
99    }
100
101    this->targetMask_.include(targetId);
102
103    // trigger shouldn't react on itself or other triggers
104    this->targetMask_.exclude(Class(Trigger), true);
105
106    // we only want WorldEntities
107    ClassTreeMask WEMask;
108    WEMask.include(Class(WorldEntity));
109    this->targetMask_ *= WEMask;
110
111    this->notifyMaskUpdate();
112  }
113
114  void DistanceTrigger::removeTargets(const std::string& targets)
115  {
116    Identifier* targetId = ClassByString(targets);
117    this->targetMask_.exclude(targetId);
118  }
119
120  bool DistanceTrigger::checkDistance()
121  {
122    // Iterate through all objects
123    for (ClassTreeMaskObjectIterator it = this->targetMask_.begin(); it != this->targetMask_.end(); ++it)
124    {
125      WorldEntity* entity = dynamic_cast<WorldEntity*>(*it);
126      if (!entity)
127        continue;
128
129      Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition();
130      if (distanceVec.length() < this->distance_)
131      {
132
133        // If the target is a player (resp. is a, or is derived from a, ControllableEntity) the triggeringPlayer is set to the target entity.
134        if(this->isForPlayer())
135        {
136          Pawn* player = dynamic_cast<Pawn*>(entity);
137          this->setTriggeringPlayer(player);
138        }
139
140        return true;
141      }
142    }
143    return false;
144  }
145
146  bool DistanceTrigger::isTriggered(TriggerMode mode)
147  {
148    if (Trigger::isTriggered(mode))
149    {
150      return checkDistance();
151    }
152    else
153      return false;
154  }
155}
Note: See TracBrowser for help on using the repository browser.