Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem2/src/orxonox/objects/worldentities/triggers/DistanceTrigger.cc @ 2209

Last change on this file since 2209 was 2209, checked in by dafrick, 15 years ago

Resolved some issues with triggers.

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