Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/modules/portals/PortalEndPoint.cc @ 8694

Last change on this file since 8694 was 8645, checked in by landauf, 14 years ago

set svn:eol-style to native, removed svn:executable property. no code changes.

  • Property svn:eol-style set to native
File size: 5.3 KB
RevLine 
[8605]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 *      Andreas Büchel
24 *   Co-authors:
25 *      ...
26 *
27 */
28
[8177]29#include "PortalEndPoint.h"
30#include "core/XMLPort.h"
[8243]31#include "objects/triggers/MultiTriggerContainer.h"
32#include "portals/PortalLink.h"
33#include "worldentities/MobileEntity.h"
[8605]34#include <ctime>
[8177]35
36namespace orxonox
37{
38    CreateFactory(PortalEndPoint);
[8290]39   
40    /*static*/ const std::string PortalEndPoint::EVENTFUNCTIONNAME = "execute";
[8177]41
42    std::map<unsigned int, PortalEndPoint *> PortalEndPoint::idMap_s;
43
[8628]44    PortalEndPoint::PortalEndPoint(BaseObject* creator) : StaticEntity(creator), RadarViewable(creator, static_cast<WorldEntity*>(this)), id_(0), trigger_(NULL), reenterDelay_(0)
[8177]45    {
46        RegisterObject(PortalEndPoint);
[8605]47       
[8471]48        this->trigger_ = new DistanceMultiTrigger(this);
[8243]49        this->trigger_->setName("portal");
50        this->attach(trigger_);
[8628]51
52        this->setRadarObjectColour(ColourValue::White);
53        this->setRadarObjectShape(RadarViewable::Dot);
54        this->setRadarVisibility(true);
[8177]55    }
56   
57    PortalEndPoint::~PortalEndPoint()
58    {
[8605]59        if(this->isInitialized() && this->trigger_ != NULL)
60            delete this->trigger_;
[8177]61    }
62
63    void PortalEndPoint::XMLPort(Element& xmlelement, XMLPort::Mode mode)
64    {
65        SUPER(PortalEndPoint, XMLPort, xmlelement, mode);
[8243]66       
[8177]67        XMLPortParam(PortalEndPoint, "id", setID, getID, xmlelement, mode);
[8243]68        XMLPortParam(PortalEndPoint, "design", setTemplate, getTemplate, xmlelement, mode);
[8605]69        XMLPortParam(PortalEndPoint, "reenterDelay", setReenterDelay, getReenterDelay, xmlelement, mode);
[8243]70        XMLPortParamExtern(PortalEndPoint, DistanceMultiTrigger, this->trigger_, "distance", setDistance, getDistance, xmlelement, mode);
[8457]71        XMLPortParamLoadOnly(PortalEndPoint, "target", setTarget, xmlelement, mode).defaultValues("Pawn");
[8177]72       
[8290]73        // Add the DistanceMultiTrigger as event source.
74        this->addEventSource(this->trigger_, EVENTFUNCTIONNAME);
75       
[8177]76        if(mode == XMLPort::LoadObject)
77        {
[8198]78            PortalEndPoint::idMap_s[this->id_] = this;
[8177]79        }
80    }
[8198]81
[8243]82    void PortalEndPoint::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
[8198]83    {
[8243]84        SUPER(PortalEndPoint, XMLEventPort, xmlelement, mode);
85       
[8290]86        XMLPortEventSink(PortalEndPoint, BaseObject, EVENTFUNCTIONNAME, execute, xmlelement, mode);
[8198]87    }
88
[8243]89    bool PortalEndPoint::execute(bool bTriggered, BaseObject* trigger)
[8198]90    {
[8454]91        if(!this->isActive())
92            return true;
93       
[8243]94        MultiTriggerContainer * cont = orxonox_cast<MultiTriggerContainer *>(trigger);
95        if(cont == 0)
96            return true;
97       
98        DistanceMultiTrigger * originatingTrigger = orxonox_cast<DistanceMultiTrigger *>(cont->getOriginator());
99        if(originatingTrigger == 0)
[8198]100        {
[8243]101            return true;
[8198]102        }
[8243]103       
104        MobileEntity * entity = orxonox_cast<MobileEntity *>(cont->getData());
105        if(entity == 0)
106            return true;
107       
108        if(bTriggered)
109        {
[8605]110            if(this->letsEnter(entity))  // only enter the portal if not just (this very moment) jumped out of it, or if the reenterDelay expired
[8243]111            {
112                PortalLink::use(entity, this);
113            }
114        }
[8198]115        else
[8243]116        {
117            this->recentlyJumpedOut_.erase(entity);
118        }
119       
120        return true;
[8198]121    }
122
[8628]123    void PortalEndPoint::changedActivity(void)
124    {
125        SUPER(PortalEndPoint, changedActivity);
126       
127        this->setRadarVisibility(this->isActive());
128    }
129
[8605]130    bool PortalEndPoint::letsEnter(MobileEntity* entity)
131    {
132        // not allowed to enter if reenterDelay hasn't expired yet
133        std::map<MobileEntity *, time_t>::const_iterator time = this->jumpOutTimes_.find(entity);
134        if(time != this->jumpOutTimes_.end() && std::difftime(std::time(0),time->second) < this->reenterDelay_)
135            return false;
136
137        // not allowed to enter if jumped out of this portal and not left its activation radius yet
138        std::set<MobileEntity *>::const_iterator recent = this->recentlyJumpedOut_.find(entity);
139        if(recent != this->recentlyJumpedOut_.end())
140            return false;
141       
142        return true;
143    }
144
[8243]145    void PortalEndPoint::jumpOut(MobileEntity* entity)
146    {
[8605]147        this->jumpOutTimes_[entity] = std::time(0);
[8243]148        this->recentlyJumpedOut_.insert(entity);
[8605]149
150        // adjust
[8243]151        entity->setPosition(this->getWorldPosition());
152        entity->rotate(this->getWorldOrientation());
153        entity->setVelocity(this->getWorldOrientation() * entity->getVelocity());
154    }
155
[8177]156}
Note: See TracBrowser for help on using the repository browser.