[2202] | 1 | #include "PickupSpawner.h" |
---|
[2227] | 2 | #include "Item.h" |
---|
| 3 | #include "objects/worldentities/pawns/Pawn.h" |
---|
| 4 | #include "objects/worldentities/triggers/DistanceTrigger.h" |
---|
[2202] | 5 | #include "core/CoreIncludes.h" |
---|
[2227] | 6 | #include "core/XMLPort.h" |
---|
[2202] | 7 | #include "core/Template.h" |
---|
| 8 | |
---|
| 9 | namespace orxonox |
---|
| 10 | { |
---|
| 11 | CreateFactory(PickupSpawner); |
---|
| 12 | |
---|
| 13 | PickupSpawner::PickupSpawner(BaseObject* creator) : PositionableEntity(creator) |
---|
| 14 | { |
---|
| 15 | RegisterObject(PickupSpawner); |
---|
| 16 | |
---|
| 17 | this->template_ = 0; |
---|
[2389] | 18 | this->distance_ = 20; |
---|
| 19 | this->respawntimer_= 0; |
---|
[2202] | 20 | } |
---|
| 21 | |
---|
[2227] | 22 | PickupSpawner::~PickupSpawner() |
---|
[2202] | 23 | { |
---|
| 24 | } |
---|
| 25 | |
---|
[2227] | 26 | void PickupSpawner::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 27 | { |
---|
| 28 | SUPER(PickupSpawner, XMLPort, xmlelement, mode); |
---|
| 29 | |
---|
| 30 | XMLPortParam(PickupSpawner, "item", setItemTemplate, getItemTemplate, xmlelement, mode); |
---|
[2389] | 31 | XMLPortParam(PickupSpawner, "distance", setDistance, getDistance, xmlelement, mode).defaultValues(20.0f); |
---|
| 32 | XMLPortParam(PickupSpawner, "respawntimer", setRespawnTimer, getRespawnTimer, xmlelement, mode); |
---|
| 33 | |
---|
[2227] | 34 | } |
---|
| 35 | |
---|
| 36 | void PickupSpawner::tick(float dt) |
---|
| 37 | { |
---|
| 38 | if (this->isActive()) |
---|
| 39 | { |
---|
| 40 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it) |
---|
[2202] | 41 | { |
---|
[2227] | 42 | Vector3 distanceVec = it->getWorldPosition() - this->getWorldPosition(); |
---|
| 43 | if (distanceVec.length() < this->distance_) |
---|
| 44 | this->triggering(*it); |
---|
[2202] | 45 | } |
---|
[2227] | 46 | } |
---|
| 47 | } |
---|
[2202] | 48 | |
---|
[2227] | 49 | void PickupSpawner::setItemTemplate(const std::string& itemtemplate) |
---|
| 50 | { |
---|
| 51 | this->itemtemplate_ = itemtemplate; |
---|
| 52 | this->template_ = Template::getTemplate(itemtemplate); |
---|
| 53 | } |
---|
[2202] | 54 | |
---|
[2227] | 55 | void PickupSpawner::triggering(Pawn* player) |
---|
[2202] | 56 | { |
---|
[2227] | 57 | if (this->isActive() && this->template_ && this->template_->getBaseclassIdentifier()) |
---|
[2202] | 58 | { |
---|
[2389] | 59 | COUT(0) << "ITEM PICKED UP" << std::endl; |
---|
[2227] | 60 | //if(player->isA(itemtemplate_->getPlayerBaseClass())) |
---|
[2202] | 61 | { |
---|
[2227] | 62 | BaseObject* newobject = this->template_->getBaseclassIdentifier()->fabricate(this); |
---|
[2202] | 63 | Item* newitem = dynamic_cast<Item*>(newobject); |
---|
| 64 | if (newitem) |
---|
[2227] | 65 | { |
---|
| 66 | newitem->addTemplate(this->itemtemplate_); |
---|
| 67 | if (newitem->pickedUp(player)== true) |
---|
[2389] | 68 | { |
---|
| 69 | if(respawntimer_!=0) |
---|
| 70 | this->triggerRespawnTimer(); |
---|
[2227] | 71 | this->setActive(false); |
---|
[2389] | 72 | this->fireEvent(); |
---|
| 73 | } |
---|
[2227] | 74 | else |
---|
| 75 | delete newobject; |
---|
[2202] | 76 | } |
---|
[2227] | 77 | } |
---|
| 78 | //else |
---|
| 79 | // delete newobject; |
---|
[2202] | 80 | } |
---|
| 81 | } |
---|
[2389] | 82 | |
---|
| 83 | void PickupSpawner::triggerRespawnTimer() |
---|
| 84 | { |
---|
| 85 | |
---|
| 86 | if(respawntimer_!=0) |
---|
| 87 | { |
---|
| 88 | ExecutorMember<BaseObject>* executor = createExecutor(createFunctor(&BaseObject::setActive)); |
---|
| 89 | executor->setDefaultValues(true); |
---|
| 90 | RespawnTimer_.setTimer(this->respawntimer_, false, (BaseObject*)this, executor); |
---|
| 91 | COUT(0) << "TIMER SET" << std::endl; |
---|
| 92 | } |
---|
[2227] | 93 | } |
---|
[2389] | 94 | void PickupSpawner::changedActivity() |
---|
| 95 | { |
---|
| 96 | /* |
---|
| 97 | COUT(0) << "Visble?" << std::endl; |
---|
| 98 | if(isActive()) |
---|
| 99 | { |
---|
| 100 | setVisible(true); |
---|
| 101 | COUT(0) << "Visble!" << std::endl; |
---|
| 102 | } |
---|
| 103 | if(isActive()==false) |
---|
| 104 | { |
---|
| 105 | setVisible(false); |
---|
| 106 | COUT(0) << "INvisble!" << std::endl; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | */ |
---|
| 110 | SUPER(PickupSpawner, changedActivity); |
---|
| 111 | |
---|
| 112 | for (std::set<WorldEntity*>::iterator it = this->getAttachedObjects().begin(); it != this->getAttachedObjects().end(); ++it) |
---|
| 113 | (*it)->setVisible(this->isActive()); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | |
---|