[8177] | 1 | #include "PortalEndPoint.h" |
---|
| 2 | #include "core/XMLPort.h" |
---|
[8243] | 3 | #include "objects/triggers/MultiTriggerContainer.h" |
---|
| 4 | #include "portals/PortalLink.h" |
---|
| 5 | #include "worldentities/MobileEntity.h" |
---|
[8177] | 6 | |
---|
[8243] | 7 | |
---|
[8177] | 8 | namespace orxonox |
---|
| 9 | { |
---|
| 10 | CreateFactory(PortalEndPoint); |
---|
[8290] | 11 | |
---|
| 12 | /*static*/ const std::string PortalEndPoint::EVENTFUNCTIONNAME = "execute"; |
---|
[8177] | 13 | |
---|
| 14 | std::map<unsigned int, PortalEndPoint *> PortalEndPoint::idMap_s; |
---|
| 15 | |
---|
[8511] | 16 | PortalEndPoint::PortalEndPoint(BaseObject* creator) : StaticEntity(creator), id_(0), trigger_(new DistanceMultiTrigger(this)), reenterDelay_(0) |
---|
[8177] | 17 | { |
---|
| 18 | RegisterObject(PortalEndPoint); |
---|
[8243] | 19 | this->trigger_->setName("portal"); |
---|
| 20 | this->attach(trigger_); |
---|
[8177] | 21 | } |
---|
| 22 | |
---|
| 23 | PortalEndPoint::~PortalEndPoint() |
---|
| 24 | { |
---|
[8243] | 25 | |
---|
[8177] | 26 | } |
---|
| 27 | |
---|
| 28 | void PortalEndPoint::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 29 | { |
---|
| 30 | SUPER(PortalEndPoint, XMLPort, xmlelement, mode); |
---|
[8243] | 31 | |
---|
[8177] | 32 | XMLPortParam(PortalEndPoint, "id", setID, getID, xmlelement, mode); |
---|
[8243] | 33 | XMLPortParam(PortalEndPoint, "design", setTemplate, getTemplate, xmlelement, mode); |
---|
[8511] | 34 | XMLPortParam(PortalEndPoint, "reenterDelay", setReenterDelay, getReenterDelay, xmlelement, mode); |
---|
| 35 | XMLPortParamExtern(PortalEndPoint, DistanceMultiTrigger, this->trigger_, "distance", setDistance, getDistance, xmlelement, mode); |
---|
[8457] | 36 | XMLPortParamLoadOnly(PortalEndPoint, "target", setTarget, xmlelement, mode).defaultValues("Pawn"); |
---|
[8177] | 37 | |
---|
[8290] | 38 | // Add the DistanceMultiTrigger as event source. |
---|
| 39 | this->addEventSource(this->trigger_, EVENTFUNCTIONNAME); |
---|
| 40 | |
---|
[8177] | 41 | if(mode == XMLPort::LoadObject) |
---|
| 42 | { |
---|
[8198] | 43 | PortalEndPoint::idMap_s[this->id_] = this; |
---|
[8177] | 44 | } |
---|
| 45 | } |
---|
[8198] | 46 | |
---|
[8243] | 47 | void PortalEndPoint::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[8198] | 48 | { |
---|
[8243] | 49 | SUPER(PortalEndPoint, XMLEventPort, xmlelement, mode); |
---|
| 50 | |
---|
[8290] | 51 | XMLPortEventSink(PortalEndPoint, BaseObject, EVENTFUNCTIONNAME, execute, xmlelement, mode); |
---|
[8198] | 52 | } |
---|
| 53 | |
---|
[8243] | 54 | bool PortalEndPoint::execute(bool bTriggered, BaseObject* trigger) |
---|
[8198] | 55 | { |
---|
[8454] | 56 | if(!this->isActive()) |
---|
| 57 | return true; |
---|
| 58 | |
---|
[8243] | 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) |
---|
[8198] | 65 | { |
---|
[8511] | 66 | // COUT(1) << "originator no DistanceMultiTrigger\n" << std::endl; |
---|
[8243] | 67 | return true; |
---|
[8198] | 68 | } |
---|
[8243] | 69 | |
---|
| 70 | MobileEntity * entity = orxonox_cast<MobileEntity *>(cont->getData()); |
---|
| 71 | if(entity == 0) |
---|
| 72 | return true; |
---|
| 73 | |
---|
| 74 | if(bTriggered) |
---|
| 75 | { |
---|
[8511] | 76 | if(this->letsEnter(entity)) // only enter the portal if not just (this very moment) jumped out of it, or if the reenterDelay expired |
---|
[8243] | 77 | { |
---|
| 78 | PortalLink::use(entity, this); |
---|
| 79 | } |
---|
| 80 | } |
---|
[8198] | 81 | else |
---|
[8243] | 82 | { |
---|
| 83 | this->recentlyJumpedOut_.erase(entity); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | return true; |
---|
[8198] | 87 | } |
---|
| 88 | |
---|
[8511] | 89 | bool PortalEndPoint::letsEnter(MobileEntity* entity) |
---|
| 90 | { |
---|
| 91 | // not allowed to enter if reenterDelay hasn't expired yet |
---|
| 92 | std::map<MobileEntity *, time_t>::const_iterator time = this->jumpOutTimes_.find(entity); |
---|
| 93 | if(time != this->jumpOutTimes_.end() && std::difftime(std::time(0),time->second) < this->reenterDelay_) |
---|
| 94 | return false; |
---|
| 95 | |
---|
| 96 | // not allowed to enter if jumped out of this portal and not left its activation radius yet |
---|
| 97 | std::set<MobileEntity *>::const_iterator recent = this->recentlyJumpedOut_.find(entity); |
---|
| 98 | if(recent != this->recentlyJumpedOut_.end()) |
---|
| 99 | return false; |
---|
| 100 | |
---|
| 101 | return true; |
---|
| 102 | } |
---|
| 103 | |
---|
[8243] | 104 | void PortalEndPoint::jumpOut(MobileEntity* entity) |
---|
| 105 | { |
---|
[8511] | 106 | this->jumpOutTimes_[entity] = std::time(0); |
---|
[8243] | 107 | this->recentlyJumpedOut_.insert(entity); |
---|
[8454] | 108 | |
---|
[8243] | 109 | entity->setPosition(this->getWorldPosition()); |
---|
| 110 | entity->rotate(this->getWorldOrientation()); |
---|
| 111 | entity->setVelocity(this->getWorldOrientation() * entity->getVelocity()); |
---|
[8454] | 112 | entity->setVelocity(entity->getVelocity() * 1.5); |
---|
[8243] | 113 | } |
---|
| 114 | |
---|
[8177] | 115 | } |
---|