Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/portals/PortalEndPoint.cc @ 9039

Last change on this file since 9039 was 8767, checked in by dafrick, 13 years ago

Some cleanup…

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