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 | * Daniel 'Huty' Haggenmueller |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file |
---|
31 | @brief Implementation of PickupSpawner. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "PickupSpawner.h" |
---|
35 | |
---|
36 | #include "BaseItem.h" |
---|
37 | |
---|
38 | #include "core/CoreIncludes.h" |
---|
39 | #include "core/GUIManager.h" // HACK; see below |
---|
40 | #include "core/Template.h" |
---|
41 | #include "core/XMLPort.h" |
---|
42 | #include "worldentities/pawns/Pawn.h" |
---|
43 | #include "PickupInventory.h" // HACK; Only for hack, remove later |
---|
44 | |
---|
45 | |
---|
46 | namespace orxonox |
---|
47 | { |
---|
48 | const float PickupSpawner::bounceSpeed_s = 6.0f; |
---|
49 | const float PickupSpawner::rotationSpeed_s = 1.0f; |
---|
50 | const float PickupSpawner::bounceDistance_s = 4.0f; |
---|
51 | |
---|
52 | CreateFactory(PickupSpawner); |
---|
53 | |
---|
54 | /** |
---|
55 | @brief Constructor. Registers the PickupSpawner. |
---|
56 | @param creator Pointer to the object which created this item. |
---|
57 | */ |
---|
58 | PickupSpawner::PickupSpawner(BaseObject* creator) : StaticEntity(creator) |
---|
59 | { |
---|
60 | RegisterObject(PickupSpawner); |
---|
61 | |
---|
62 | this->itemTemplate_ = 0; |
---|
63 | this->triggerDistance_ = 20; |
---|
64 | this->respawnTime_ = 0.0f; |
---|
65 | this->tickSum_ = 0.0f; |
---|
66 | } |
---|
67 | //! Deconstructor. |
---|
68 | PickupSpawner::~PickupSpawner() |
---|
69 | { |
---|
70 | } |
---|
71 | /** |
---|
72 | @brief Method for creating a PickupSpawner through XML. |
---|
73 | @param xmlelement XML element which contains the PickupSpawner. |
---|
74 | @param mode XMLPort mode. |
---|
75 | */ |
---|
76 | void PickupSpawner::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
77 | { |
---|
78 | SUPER(PickupSpawner, XMLPort, xmlelement, mode); |
---|
79 | |
---|
80 | XMLPortParam(PickupSpawner, "item", setItemTemplateName, getItemTemplateName, xmlelement, mode); |
---|
81 | XMLPortParam(PickupSpawner, "triggerDistance", setTriggerDistance, getTriggerDistance, xmlelement, mode); |
---|
82 | XMLPortParam(PickupSpawner, "respawnTime", setRespawnTime, getRespawnTime, xmlelement, mode); |
---|
83 | |
---|
84 | // HACKs |
---|
85 | // Load the GUI image as soon as the PickupSpawner gets loaded |
---|
86 | // = less delays while running |
---|
87 | BaseObject* newObject = this->itemTemplate_->getBaseclassIdentifier()->fabricate(this); |
---|
88 | BaseItem* asItem = orxonox_cast<BaseItem*>(newObject); |
---|
89 | if (asItem) |
---|
90 | { |
---|
91 | asItem->addTemplate(this->itemTemplate_); |
---|
92 | PickupInventory::getImageForItem(asItem); |
---|
93 | newObject->destroy(); |
---|
94 | } |
---|
95 | |
---|
96 | // & load the GUI itself too, along with some empty windows |
---|
97 | // = even less delays |
---|
98 | GUIManager::getInstance().showGUI("PickupInventory"); |
---|
99 | GUIManager::getInstance().executeCode("hideGUI(\"PickupInventory\")"); |
---|
100 | PickupInventory::getSingleton(); |
---|
101 | } |
---|
102 | /** |
---|
103 | @brief Invoked when the activity has changed. Sets visibility of attached objects. |
---|
104 | */ |
---|
105 | void PickupSpawner::changedActivity() |
---|
106 | { |
---|
107 | SUPER(PickupSpawner, changedActivity); |
---|
108 | |
---|
109 | for (std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin(); it != this->getAttachedObjects().end(); it++) |
---|
110 | (*it)->setVisible(this->isActive()); |
---|
111 | } |
---|
112 | /** |
---|
113 | @brief Set the template name of the item to spawn, also loads the template. |
---|
114 | @param name Name of the new template. |
---|
115 | */ |
---|
116 | void PickupSpawner::setItemTemplateName(const std::string& name) |
---|
117 | { |
---|
118 | this->itemTemplateName_ = name; |
---|
119 | this->itemTemplate_ = Template::getTemplate(name); |
---|
120 | } |
---|
121 | /** |
---|
122 | @brief Tick, checks if any Pawn is close enough to trigger. |
---|
123 | @param dt Time since last tick. |
---|
124 | */ |
---|
125 | void PickupSpawner::tick(float dt) |
---|
126 | { |
---|
127 | if (this->isActive()) |
---|
128 | { |
---|
129 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it) |
---|
130 | { |
---|
131 | Vector3 distance = it->getWorldPosition() - this->getWorldPosition(); |
---|
132 | if (distance.length() < this->triggerDistance_) |
---|
133 | this->trigger(*it); |
---|
134 | } |
---|
135 | this->yaw(Radian(rotationSpeed_s*dt)); |
---|
136 | this->tickSum_ += bounceSpeed_s*dt; |
---|
137 | this->translate(Vector3(0,bounceDistance_s*dt*sin(this->tickSum_),0)); |
---|
138 | if (this->tickSum_ > 2*Ogre::Math::PI) |
---|
139 | this->tickSum_ -= 2*Ogre::Math::PI; |
---|
140 | } |
---|
141 | } |
---|
142 | /** |
---|
143 | @brief |
---|
144 | Trigger the PickupSpawner. |
---|
145 | |
---|
146 | Adds the pickup to the Pawn that triggered, |
---|
147 | sets the timer to re-activate and deactives the PickupSpawner. |
---|
148 | |
---|
149 | @param pawn Pawn which triggered the PickupSpawner. |
---|
150 | */ |
---|
151 | void PickupSpawner::trigger(Pawn* pawn) |
---|
152 | { |
---|
153 | if (this->isActive() && this->itemTemplate_ && this->itemTemplate_->getBaseclassIdentifier()) |
---|
154 | { |
---|
155 | BaseObject* newObject = this->itemTemplate_->getBaseclassIdentifier()->fabricate(this); |
---|
156 | BaseItem* asItem = orxonox_cast<BaseItem*>(newObject); |
---|
157 | if (asItem) |
---|
158 | { |
---|
159 | asItem->setPickupIdentifier(this->itemTemplateName_); |
---|
160 | asItem->addTemplate(this->itemTemplate_); |
---|
161 | |
---|
162 | if (asItem->pickedUp(pawn)) |
---|
163 | { |
---|
164 | COUT(3) << this->itemTemplateName_ << " got picked up." << std::endl; |
---|
165 | |
---|
166 | if (this->respawnTime_ > 0.0f) |
---|
167 | { |
---|
168 | this->respawnTimer_.setTimer(this->respawnTime_, false, createExecutor(createFunctor(&PickupSpawner::respawnTimerCallback, this))); |
---|
169 | |
---|
170 | this->setActive(false); |
---|
171 | this->fireEvent(); |
---|
172 | } |
---|
173 | } |
---|
174 | else |
---|
175 | newObject->destroy(); |
---|
176 | } |
---|
177 | } |
---|
178 | } |
---|
179 | /** |
---|
180 | @brief Invoked by the timer, re-activates the PickupSpawner. |
---|
181 | */ |
---|
182 | void PickupSpawner::respawnTimerCallback() |
---|
183 | { |
---|
184 | COUT(3) << "PickupSpawner reactivated." << std::endl; |
---|
185 | |
---|
186 | this->setActive(true); |
---|
187 | } |
---|
188 | } |
---|