Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Moving the things done with the eventTemplate into the PortalEndPoint class.

  • Property svn:executable set to *
File size: 3.3 KB
Line 
1#include "PortalEndPoint.h"
2#include "core/XMLPort.h"
3#include "objects/triggers/MultiTriggerContainer.h"
4#include "portals/PortalLink.h"
5#include "worldentities/MobileEntity.h"
6
7
8namespace orxonox
9{
10    CreateFactory(PortalEndPoint);
11   
12    /*static*/ const std::string PortalEndPoint::EVENTFUNCTIONNAME = "execute";
13
14    std::map<unsigned int, PortalEndPoint *> PortalEndPoint::idMap_s;
15
16    PortalEndPoint::PortalEndPoint(BaseObject* creator) : StaticEntity(creator), id_(0), trigger_(new DistanceMultiTrigger(this))
17    {
18        RegisterObject(PortalEndPoint);
19        this->trigger_->setName("portal");
20        this->attach(trigger_);
21    }
22   
23    PortalEndPoint::~PortalEndPoint()
24    {
25   
26    }
27
28    void PortalEndPoint::XMLPort(Element& xmlelement, XMLPort::Mode mode)
29    {
30        SUPER(PortalEndPoint, XMLPort, xmlelement, mode);
31       
32        XMLPortParam(PortalEndPoint, "id", setID, getID, xmlelement, mode);
33        XMLPortParam(PortalEndPoint, "design", setTemplate, getTemplate, xmlelement, mode);
34        XMLPortParamExtern(PortalEndPoint, DistanceMultiTrigger, this->trigger_, "distance", setDistance, getDistance, xmlelement, mode);
35        XMLPortParamLoadOnly(PortalEndPoint, "target", setTargets, xmlelement, mode).defaultValues("Pawn");
36       
37        // Add the DistanceMultiTrigger as event source.
38        this->addEventSource(this->trigger_, EVENTFUNCTIONNAME);
39       
40        if(mode == XMLPort::LoadObject)
41        {
42            PortalEndPoint::idMap_s[this->id_] = this;
43        }
44    }
45
46    void PortalEndPoint::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
47    {
48        SUPER(PortalEndPoint, XMLEventPort, xmlelement, mode);
49       
50        XMLPortEventSink(PortalEndPoint, BaseObject, EVENTFUNCTIONNAME, execute, xmlelement, mode);
51    }
52
53    bool PortalEndPoint::execute(bool bTriggered, BaseObject* trigger)
54    {
55        MultiTriggerContainer * cont = orxonox_cast<MultiTriggerContainer *>(trigger);
56        if(cont == 0)
57            return true;
58       
59        DistanceMultiTrigger * originatingTrigger = orxonox_cast<DistanceMultiTrigger *>(cont->getOriginator());
60        if(originatingTrigger == 0)
61        {
62            COUT(1) << "originator no DistanceMultiTrigger\n" << std::endl;
63            return true;
64        }
65       
66        if(this->getAttachedObjects().find(orxonox_cast<WorldEntity *>(originatingTrigger)) == this->getAttachedObjects().end())  // only necessary if events have the same name
67            return true;
68       
69        MobileEntity * entity = orxonox_cast<MobileEntity *>(cont->getData());
70        if(entity == 0)
71            return true;
72       
73        if(bTriggered)
74        {
75            if(this->recentlyJumpedOut_.find(entity) == this->recentlyJumpedOut_.end())  // only enter the portal if not recently jumped out of it
76            {
77                PortalLink::use(entity, this);
78            }
79        }
80        else
81        {
82            this->recentlyJumpedOut_.erase(entity);
83        }
84       
85        return true;
86    }
87
88    void PortalEndPoint::jumpOut(MobileEntity* entity)
89    {
90        this->recentlyJumpedOut_.insert(entity);
91        entity->setPosition(this->getWorldPosition());
92        entity->rotate(this->getWorldOrientation());
93        entity->setVelocity(this->getWorldOrientation() * entity->getVelocity());
94    }
95
96}
Note: See TracBrowser for help on using the repository browser.