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