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_ = 50; |
---|
19 | } |
---|
20 | |
---|
21 | PickupSpawner::~PickupSpawner() |
---|
22 | { |
---|
23 | } |
---|
24 | |
---|
25 | void PickupSpawner::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
26 | { |
---|
27 | SUPER(PickupSpawner, XMLPort, xmlelement, mode); |
---|
28 | |
---|
29 | XMLPortParam(PickupSpawner, "item", setItemTemplate, getItemTemplate, xmlelement, mode); |
---|
30 | XMLPortParam(PickupSpawner, "distance", setDistance, getDistance, xmlelement, mode).defaultValues(50.0f); |
---|
31 | } |
---|
32 | |
---|
33 | void PickupSpawner::tick(float dt) |
---|
34 | { |
---|
35 | if (this->isActive()) |
---|
36 | { |
---|
37 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it) |
---|
38 | { |
---|
39 | Vector3 distanceVec = it->getWorldPosition() - this->getWorldPosition(); |
---|
40 | if (distanceVec.length() < this->distance_) |
---|
41 | this->triggering(*it); |
---|
42 | } |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | void PickupSpawner::setItemTemplate(const std::string& itemtemplate) |
---|
47 | { |
---|
48 | this->itemtemplate_ = itemtemplate; |
---|
49 | this->template_ = Template::getTemplate(itemtemplate); |
---|
50 | } |
---|
51 | |
---|
52 | void PickupSpawner::triggering(Pawn* player) |
---|
53 | { |
---|
54 | if (this->isActive() && this->template_ && this->template_->getBaseclassIdentifier()) |
---|
55 | { |
---|
56 | COUT(0) << "activated" << std::endl; |
---|
57 | //if(player->isA(itemtemplate_->getPlayerBaseClass())) |
---|
58 | { |
---|
59 | BaseObject* newobject = this->template_->getBaseclassIdentifier()->fabricate(this); |
---|
60 | Item* newitem = dynamic_cast<Item*>(newobject); |
---|
61 | if (newitem) |
---|
62 | { |
---|
63 | newitem->addTemplate(this->itemtemplate_); |
---|
64 | if (newitem->pickedUp(player)== true) |
---|
65 | this->setActive(false); |
---|
66 | else |
---|
67 | delete newobject; |
---|
68 | } |
---|
69 | } |
---|
70 | //else |
---|
71 | // delete newobject; |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|