Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11388 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

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