Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/ggz2/src/modules/objects/triggers/DistanceTrigger.cc

Last change on this file was 5738, checked in by landauf, 15 years ago

merged libraries2 back to trunk

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