1 | #include "PickupSpawner.h" |
---|
2 | #include "Item.h" |
---|
3 | #include "objects/worldentities/pawns/Pawn.h" |
---|
4 | #include "objects/worldentities/triggers/DistanceTrigger.h" |
---|
5 | #include "core/CoreIncludes.h" |
---|
6 | #include "core/XMLPort.h" |
---|
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; |
---|
18 | this->distance_ = 20; |
---|
19 | this->respawntimer_= 0; |
---|
20 | } |
---|
21 | |
---|
22 | PickupSpawner::~PickupSpawner() |
---|
23 | { |
---|
24 | } |
---|
25 | |
---|
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); |
---|
31 | XMLPortParam(PickupSpawner, "distance", setDistance, getDistance, xmlelement, mode).defaultValues(20.0f); |
---|
32 | XMLPortParam(PickupSpawner, "respawntimer", setRespawnTimer, getRespawnTimer, xmlelement, mode); |
---|
33 | |
---|
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) |
---|
41 | { |
---|
42 | Vector3 distanceVec = it->getWorldPosition() - this->getWorldPosition(); |
---|
43 | if (distanceVec.length() < this->distance_) |
---|
44 | this->triggering(*it); |
---|
45 | } |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | void PickupSpawner::setItemTemplate(const std::string& itemtemplate) |
---|
50 | { |
---|
51 | this->itemtemplate_ = itemtemplate; |
---|
52 | this->template_ = Template::getTemplate(itemtemplate); |
---|
53 | } |
---|
54 | |
---|
55 | void PickupSpawner::triggering(Pawn* player) |
---|
56 | { |
---|
57 | if (this->isActive() && this->template_ && this->template_->getBaseclassIdentifier()) |
---|
58 | { |
---|
59 | COUT(0) << "ITEM PICKED UP" << std::endl; |
---|
60 | //if(player->isA(itemtemplate_->getPlayerBaseClass())) |
---|
61 | { |
---|
62 | BaseObject* newobject = this->template_->getBaseclassIdentifier()->fabricate(this); |
---|
63 | Item* newitem = dynamic_cast<Item*>(newobject); |
---|
64 | if (newitem) |
---|
65 | { |
---|
66 | newitem->addTemplate(this->itemtemplate_); |
---|
67 | if (newitem->pickedUp(player)== true) |
---|
68 | { |
---|
69 | if(respawntimer_!=0) |
---|
70 | this->triggerRespawnTimer(); |
---|
71 | this->setActive(false); |
---|
72 | this->fireEvent(); |
---|
73 | } |
---|
74 | else |
---|
75 | delete newobject; |
---|
76 | } |
---|
77 | } |
---|
78 | //else |
---|
79 | // delete newobject; |
---|
80 | } |
---|
81 | } |
---|
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 | } |
---|
93 | } |
---|
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 | |
---|