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