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_(NULL) |
---|
17 | { |
---|
18 | RegisterObject(PortalEndPoint); |
---|
19 | this->trigger_ = new DistanceMultiTrigger(this); |
---|
20 | this->trigger_->setName("portal"); |
---|
21 | this->attach(trigger_); |
---|
22 | } |
---|
23 | |
---|
24 | PortalEndPoint::~PortalEndPoint() |
---|
25 | { |
---|
26 | delete this->trigger_; |
---|
27 | } |
---|
28 | |
---|
29 | void PortalEndPoint::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
30 | { |
---|
31 | SUPER(PortalEndPoint, XMLPort, xmlelement, mode); |
---|
32 | |
---|
33 | XMLPortParam(PortalEndPoint, "id", setID, getID, xmlelement, mode); |
---|
34 | XMLPortParam(PortalEndPoint, "design", setTemplate, getTemplate, xmlelement, mode); |
---|
35 | XMLPortParamExtern(PortalEndPoint, DistanceMultiTrigger, this->trigger_, "distance", setDistance, getDistance, xmlelement, mode); |
---|
36 | XMLPortParamLoadOnly(PortalEndPoint, "target", setTarget, xmlelement, mode).defaultValues("Pawn"); |
---|
37 | |
---|
38 | // Add the DistanceMultiTrigger as event source. |
---|
39 | this->addEventSource(this->trigger_, EVENTFUNCTIONNAME); |
---|
40 | |
---|
41 | if(mode == XMLPort::LoadObject) |
---|
42 | { |
---|
43 | PortalEndPoint::idMap_s[this->id_] = this; |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | void PortalEndPoint::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
---|
48 | { |
---|
49 | SUPER(PortalEndPoint, XMLEventPort, xmlelement, mode); |
---|
50 | |
---|
51 | XMLPortEventSink(PortalEndPoint, BaseObject, EVENTFUNCTIONNAME, execute, xmlelement, mode); |
---|
52 | } |
---|
53 | |
---|
54 | bool PortalEndPoint::execute(bool bTriggered, BaseObject* trigger) |
---|
55 | { |
---|
56 | if(!this->isActive()) |
---|
57 | return true; |
---|
58 | |
---|
59 | MultiTriggerContainer * cont = orxonox_cast<MultiTriggerContainer *>(trigger); |
---|
60 | if(cont == 0) |
---|
61 | return true; |
---|
62 | |
---|
63 | DistanceMultiTrigger * originatingTrigger = orxonox_cast<DistanceMultiTrigger *>(cont->getOriginator()); |
---|
64 | if(originatingTrigger == 0) |
---|
65 | { |
---|
66 | COUT(1) << "originator no DistanceMultiTrigger\n" << std::endl; |
---|
67 | return true; |
---|
68 | } |
---|
69 | |
---|
70 | MobileEntity * entity = orxonox_cast<MobileEntity *>(cont->getData()); |
---|
71 | if(entity == 0) |
---|
72 | return true; |
---|
73 | |
---|
74 | if(bTriggered) |
---|
75 | { |
---|
76 | if(this->recentlyJumpedOut_.find(entity) == this->recentlyJumpedOut_.end()) // only enter the portal if not just jumped out of it |
---|
77 | { |
---|
78 | PortalLink::use(entity, this); |
---|
79 | } |
---|
80 | } |
---|
81 | else |
---|
82 | { |
---|
83 | this->recentlyJumpedOut_.erase(entity); |
---|
84 | } |
---|
85 | |
---|
86 | return true; |
---|
87 | } |
---|
88 | |
---|
89 | void PortalEndPoint::jumpOut(MobileEntity* entity) |
---|
90 | { |
---|
91 | this->recentlyJumpedOut_.insert(entity); |
---|
92 | |
---|
93 | entity->setPosition(this->getWorldPosition()); |
---|
94 | entity->rotate(this->getWorldOrientation()); |
---|
95 | entity->setVelocity(this->getWorldOrientation() * entity->getVelocity()); |
---|
96 | entity->setVelocity(entity->getVelocity() * 1.5); |
---|
97 | } |
---|
98 | |
---|
99 | } |
---|