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 |
---|
56 | Constructor. Registers the PickupSpawner. |
---|
57 | @param creator |
---|
58 | Pointer to the object which created this item. |
---|
59 | */ |
---|
60 | PickupSpawner::PickupSpawner(BaseObject* creator) : StaticEntity(creator) |
---|
61 | { |
---|
62 | RegisterObject(PickupSpawner); |
---|
63 | |
---|
64 | this->itemTemplate_ = 0; |
---|
65 | this->triggerDistance_ = 20; |
---|
66 | this->respawnTime_ = 0.0f; |
---|
67 | this->tickSum_ = 0.0f; |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | @brief |
---|
72 | Destructor. |
---|
73 | */ |
---|
74 | PickupSpawner::~PickupSpawner() |
---|
75 | { |
---|
76 | |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | @brief |
---|
81 | Method for creating a PickupSpawner through XML. |
---|
82 | @param xmlelement |
---|
83 | XML element which contains the PickupSpawner. |
---|
84 | @param mode |
---|
85 | XMLPort mode. |
---|
86 | */ |
---|
87 | void PickupSpawner::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
88 | { |
---|
89 | SUPER(PickupSpawner, XMLPort, xmlelement, mode); |
---|
90 | |
---|
91 | XMLPortParam(PickupSpawner, "item", setItemTemplateName, getItemTemplateName, xmlelement, mode); |
---|
92 | XMLPortParam(PickupSpawner, "triggerDistance", setTriggerDistance, getTriggerDistance, xmlelement, mode); |
---|
93 | XMLPortParam(PickupSpawner, "respawnTime", setRespawnTime, getRespawnTime, xmlelement, mode); |
---|
94 | |
---|
95 | //TODO: Kill hack. |
---|
96 | // HACKs |
---|
97 | // Load the GUI image as soon as the PickupSpawner gets loaded |
---|
98 | // = less delays while running |
---|
99 | BaseObject* newObject = this->itemTemplate_->getBaseclassIdentifier()->fabricate(this); |
---|
100 | BaseItem* asItem = orxonox_cast<BaseItem*>(newObject); |
---|
101 | if (asItem) |
---|
102 | { |
---|
103 | asItem->addTemplate(this->itemTemplate_); |
---|
104 | PickupInventory::getImageForItem(asItem); |
---|
105 | newObject->destroy(); |
---|
106 | } |
---|
107 | |
---|
108 | // & load the GUI itself too, along with some empty windows |
---|
109 | // = even less delays |
---|
110 | GUIManager::getInstance().showGUI("PickupInventory"); |
---|
111 | GUIManager::getInstance().executeCode("hideGUI(\"PickupInventory\")"); |
---|
112 | PickupInventory::getSingleton(); |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | @brief |
---|
117 | Invoked when the activity has changed. Sets visibility of attached objects. |
---|
118 | */ |
---|
119 | void PickupSpawner::changedActivity() |
---|
120 | { |
---|
121 | SUPER(PickupSpawner, changedActivity); |
---|
122 | |
---|
123 | for (std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin(); it != this->getAttachedObjects().end(); it++) |
---|
124 | (*it)->setVisible(this->isActive()); |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | @brief |
---|
129 | Set the template name of the item to spawn, also loads the template. |
---|
130 | @param name |
---|
131 | Name of the new template. |
---|
132 | */ |
---|
133 | void PickupSpawner::setItemTemplateName(const std::string& name) |
---|
134 | { |
---|
135 | this->itemTemplateName_ = name; |
---|
136 | this->itemTemplate_ = Template::getTemplate(name); |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | @brief |
---|
141 | Tick, checks if any Pawn is close enough to trigger. |
---|
142 | @param dt |
---|
143 | Time since last tick. |
---|
144 | */ |
---|
145 | //TODO: Replace this with a real DistanceTrigger. |
---|
146 | void PickupSpawner::tick(float dt) |
---|
147 | { |
---|
148 | if (this->isActive()) |
---|
149 | { |
---|
150 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it) |
---|
151 | { |
---|
152 | Vector3 distance = it->getWorldPosition() - this->getWorldPosition(); |
---|
153 | if (distance.length() < this->triggerDistance_) |
---|
154 | this->trigger(*it); |
---|
155 | } |
---|
156 | this->yaw(Radian(rotationSpeed_s*dt)); |
---|
157 | this->tickSum_ += bounceSpeed_s*dt; |
---|
158 | this->translate(Vector3(0,bounceDistance_s*dt*sin(this->tickSum_),0)); |
---|
159 | if (this->tickSum_ > 2*Ogre::Math::PI) |
---|
160 | this->tickSum_ -= 2*Ogre::Math::PI; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | @brief |
---|
166 | Trigger the PickupSpawner. |
---|
167 | |
---|
168 | Adds the pickup to the Pawn that triggered, |
---|
169 | sets the timer to re-activate and deactives the PickupSpawner. |
---|
170 | |
---|
171 | @param pawn |
---|
172 | Pawn which triggered the PickupSpawner. |
---|
173 | */ |
---|
174 | void PickupSpawner::trigger(Pawn* pawn) |
---|
175 | { |
---|
176 | if (this->isActive() && this->itemTemplate_ && this->itemTemplate_->getBaseclassIdentifier()) |
---|
177 | { |
---|
178 | BaseObject* newObject = this->itemTemplate_->getBaseclassIdentifier()->fabricate(this); |
---|
179 | BaseItem* asItem = orxonox_cast<BaseItem*>(newObject); |
---|
180 | if (asItem) |
---|
181 | { |
---|
182 | asItem->setPickupIdentifier(this->itemTemplateName_); |
---|
183 | asItem->addTemplate(this->itemTemplate_); |
---|
184 | |
---|
185 | if (asItem->pickedUp(pawn)) |
---|
186 | { |
---|
187 | COUT(3) << this->itemTemplateName_ << " got picked up." << std::endl; |
---|
188 | |
---|
189 | if (this->respawnTime_ > 0.0f) |
---|
190 | { |
---|
191 | this->respawnTimer_.setTimer(this->respawnTime_, false, createExecutor(createFunctor(&PickupSpawner::respawnTimerCallback, this))); |
---|
192 | |
---|
193 | this->setActive(false); |
---|
194 | this->fireEvent(); |
---|
195 | } |
---|
196 | } |
---|
197 | else |
---|
198 | newObject->destroy(); |
---|
199 | } |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | @brief |
---|
205 | Invoked by the timer, re-activates the PickupSpawner. |
---|
206 | */ |
---|
207 | void PickupSpawner::respawnTimerCallback() |
---|
208 | { |
---|
209 | COUT(3) << "PickupSpawner reactivated." << std::endl; |
---|
210 | |
---|
211 | this->setActive(true); |
---|
212 | } |
---|
213 | } |
---|