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 | |
---|
8 | namespace 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", setTarget, 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 | if(!this->isActive()) |
---|
56 | return true; |
---|
57 | |
---|
58 | MultiTriggerContainer * cont = orxonox_cast<MultiTriggerContainer *>(trigger); |
---|
59 | if(cont == 0) |
---|
60 | return true; |
---|
61 | |
---|
62 | DistanceMultiTrigger * originatingTrigger = orxonox_cast<DistanceMultiTrigger *>(cont->getOriginator()); |
---|
63 | if(originatingTrigger == 0) |
---|
64 | { |
---|
65 | COUT(1) << "originator no DistanceMultiTrigger\n" << std::endl; |
---|
66 | return true; |
---|
67 | } |
---|
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 just 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 | |
---|
92 | entity->setPosition(this->getWorldPosition()); |
---|
93 | entity->rotate(this->getWorldOrientation()); |
---|
94 | entity->setVelocity(this->getWorldOrientation() * entity->getVelocity()); |
---|
95 | entity->setVelocity(entity->getVelocity() * 1.5); |
---|
96 | } |
---|
97 | |
---|
98 | } |
---|