Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup2/src/modules/objects/triggers/DistanceTrigger.cc @ 6554

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

Merged presentation2 branch into pickup2 branch.

  • Property svn:eol-style set to native
File size: 4.5 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
29#include "DistanceTrigger.h"
30
31#include "core/CoreIncludes.h"
32#include "core/XMLPort.h"
[5735]33#include "worldentities/pawns/Pawn.h"
[2261]34
[2071]35namespace orxonox
36{
[1693]37  CreateFactory(DistanceTrigger);
38
[5727]39  DistanceTrigger::DistanceTrigger(BaseObject* creator) : Trigger(creator)
[1693]40  {
41    RegisterObject(DistanceTrigger);
42
[2029]43    this->distance_ = 100;
44    this->targetMask_.exclude(Class(BaseObject));
[5937]45    this->setForPlayer(false); //!< Normally hasn't just players as targets.
[1693]46  }
47
48  DistanceTrigger::~DistanceTrigger()
49  {
50  }
51
[2029]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);
[2069]57    XMLPortParamLoadOnly(DistanceTrigger, "target", addTargets, xmlelement, mode).defaultValues("ControllableEntity");
[2029]58  }
59
[1693]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;
[2029]73      for (it = this->targetSet_.begin(); it != this->targetSet_.end(); ++it)
[1693]74      {
75        COUT(4) << *it << std::endl;
76      }
77      COUT(4) << "End of targetSet of trigger " << this << std::endl;
78    }
79  }
80
[1969]81  void DistanceTrigger::addTargets(const std::string& targets)
[1693]82  {
[1961]83    Identifier* targetId = ClassByString(targets);
[3034]84
[2261]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    }
[3034]91
[2069]92    if (!targetId)
[2071]93    {
[6412]94        COUT(1) << "Error: \"" << targets << "\" is not a valid class name to include in ClassTreeMask (in " << this->getName() << ", class " << this->getIdentifier()->getName() << ')' << std::endl;
[2069]95        return;
[2071]96    }
[2069]97
98    this->targetMask_.include(targetId);
99
[1693]100    // trigger shouldn't react on itself or other triggers
[2069]101    this->targetMask_.exclude(Class(Trigger), true);
[1693]102
[2069]103    // we only want WorldEntities
104    ClassTreeMask WEMask;
105    WEMask.include(Class(WorldEntity));
106    this->targetMask_ *= WEMask;
[3033]107
108    this->notifyMaskUpdate();
[1693]109  }
110
[1969]111  void DistanceTrigger::removeTargets(const std::string& targets)
[1693]112  {
[1961]113    Identifier* targetId = ClassByString(targets);
[2069]114    this->targetMask_.exclude(targetId);
[1693]115  }
116
117  bool DistanceTrigger::checkDistance()
118  {
[2069]119    // Iterate through all objects
120    for (ClassTreeMaskObjectIterator it = this->targetMask_.begin(); it != this->targetMask_.end(); ++it)
[1693]121    {
[3325]122      WorldEntity* entity = orxonox_cast<WorldEntity*>(*it);
[2069]123      if (!entity)
124        continue;
125
126      Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition();
127      if (distanceVec.length() < this->distance_)
[2261]128      {
[3034]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.
[2261]131        if(this->isForPlayer())
[3034]132        {
[3325]133          Pawn* player = orxonox_cast<Pawn*>(entity);
[3034]134          this->setTriggeringPlayer(player);
135        }
136
[2069]137        return true;
[2261]138      }
[1693]139    }
140    return false;
141  }
142
[3280]143  bool DistanceTrigger::isTriggered(TriggerMode::Value mode)
[1693]144  {
[2029]145    if (Trigger::isTriggered(mode))
[2261]146    {
[1693]147      return checkDistance();
[2261]148    }
[1693]149    else
150      return false;
151  }
152}
Note: See TracBrowser for help on using the repository browser.