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 | * Benedict Simlinger |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file InvisiblePickup.cc |
---|
31 | @brief Implementation of the InvisiblePickup class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "InvisiblePickup.h" |
---|
35 | |
---|
36 | #include <sstream> |
---|
37 | //#include <OgreEntity.h> |
---|
38 | //#include <OgreAnimationState.h> |
---|
39 | #include "core/CoreIncludes.h" |
---|
40 | #include "core/XMLPort.h" |
---|
41 | |
---|
42 | #include "worldentities/pawns/Pawn.h" |
---|
43 | |
---|
44 | namespace orxonox |
---|
45 | { |
---|
46 | |
---|
47 | CreateFactory(InvisiblePickup); |
---|
48 | |
---|
49 | /** |
---|
50 | @brief |
---|
51 | Constructor. Registers the object and initializes the member variables. |
---|
52 | */ |
---|
53 | InvisiblePickup::InvisiblePickup(BaseObject* creator) : Pickup(creator) |
---|
54 | { |
---|
55 | RegisterObject(InvisiblePickup); |
---|
56 | this->initialize(); |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | @brief |
---|
61 | Destructor. |
---|
62 | */ |
---|
63 | InvisiblePickup::~InvisiblePickup() |
---|
64 | { |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | @brief |
---|
69 | Initializes the member variables. |
---|
70 | */ |
---|
71 | void InvisiblePickup::initialize(void) |
---|
72 | { |
---|
73 | this->duration_ = 0.0f; |
---|
74 | // Defines who is allowed to pick up the pickup. |
---|
75 | this->addTarget(ClassIdentifier<Pawn>::getIdentifier()); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | @brief |
---|
80 | Method for creating a HealthPickup object through XML. |
---|
81 | */ |
---|
82 | void InvisiblePickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) |
---|
83 | { |
---|
84 | SUPER(InvisiblePickup, XMLPort, xmlelement, mode); |
---|
85 | XMLPortParam(InvisiblePickup, "duration", setDuration, getDuration, xmlelement, mode); |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | @brief |
---|
90 | Is called when the pickup has transited from used to unused or the other way around. |
---|
91 | */ |
---|
92 | void InvisiblePickup::changedUsed(void) |
---|
93 | { |
---|
94 | SUPER(InvisiblePickup, changedUsed); |
---|
95 | |
---|
96 | // If the pickup has transited to used. |
---|
97 | if (this->isUsed()) |
---|
98 | { |
---|
99 | // If its durationType is continuous, we set a Timer to be reminded, when the time has run out. |
---|
100 | if(this->isContinuous()) |
---|
101 | { |
---|
102 | if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f) |
---|
103 | { |
---|
104 | this->durationTimer_.unpauseTimer(); |
---|
105 | } |
---|
106 | else |
---|
107 | { |
---|
108 | this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&InvisiblePickup::pickupTimerCallback, this))); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | this->setInvisible(true); |
---|
113 | |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | this->setInvisible(false); |
---|
118 | |
---|
119 | // We destroy the pickup if either, the pickup has activationType immediate and durationType once or it has durationType continuous and the duration was exceeded. |
---|
120 | if((!this->isContinuous() && this->isImmediate()) || (this->isContinuous() && !this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration())) |
---|
121 | { |
---|
122 | this->Pickupable::destroy(); |
---|
123 | } |
---|
124 | // We pause the Timer if the pickup is continuous and the duration is not yet exceeded, |
---|
125 | else if(this->isContinuous() && this->durationTimer_.isActive()) |
---|
126 | { |
---|
127 | this->durationTimer_.pauseTimer(); |
---|
128 | } |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | @brief |
---|
134 | Creates a duplicate of the input OrxonoxClass. |
---|
135 | @param item |
---|
136 | A pointer to the Orxonox class. |
---|
137 | */ |
---|
138 | void InvisiblePickup::clone(OrxonoxClass*& item) |
---|
139 | { |
---|
140 | if(item == NULL) |
---|
141 | item = new InvisiblePickup(this); |
---|
142 | |
---|
143 | SUPER(InvisiblePickup, clone, item); |
---|
144 | |
---|
145 | InvisiblePickup* pickup = orxonox_cast<InvisiblePickup*>(item); |
---|
146 | pickup->setDuration(this->getDuration()); |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | @brief |
---|
151 | Sets the invisibility. |
---|
152 | @param invisibility |
---|
153 | The invisibility. |
---|
154 | */ |
---|
155 | bool InvisiblePickup::setInvisible(bool invisibility) |
---|
156 | { |
---|
157 | Pawn* pawn = this->carrierToPawnHelper(); |
---|
158 | if(pawn == NULL) |
---|
159 | return false; |
---|
160 | |
---|
161 | pawn->setVisible(!invisibility); |
---|
162 | //TODO: Invisibility should imply radar invisibility as well, thus this should be solved in Pawn. |
---|
163 | pawn->setRadarVisibility(!invisibility); |
---|
164 | |
---|
165 | // Test to change Material at runtime! |
---|
166 | |
---|
167 | // Ogre::MaterialPtr mat = this->mesh_.getEntity()->getSubEntity(0)->getMaterial(); |
---|
168 | // mat->setDiffuse(0.4, 0.3, 0.1, 0.1); |
---|
169 | // mat->setAmbient(0.3, 0.7, 0.8); |
---|
170 | // mat->setSpecular(0.5, 0.5, 0.5, 0.1); |
---|
171 | // Ogre::SceneBlendType sbt = Ogre::SBT_ADD; |
---|
172 | // |
---|
173 | // mat->setSceneBlending(sbt); |
---|
174 | |
---|
175 | return true; |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | @brief |
---|
180 | Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails. |
---|
181 | @return |
---|
182 | A pointer to the Pawn, or NULL if the conversion failed. |
---|
183 | */ |
---|
184 | Pawn* InvisiblePickup::carrierToPawnHelper(void) |
---|
185 | { |
---|
186 | PickupCarrier* carrier = this->getCarrier(); |
---|
187 | Pawn* pawn = orxonox_cast<Pawn*>(carrier); |
---|
188 | |
---|
189 | if(pawn == NULL) |
---|
190 | { |
---|
191 | orxout(internal_error, context::pickups) << "Invalid PickupCarrier in InvisiblePickup." << endl; |
---|
192 | } |
---|
193 | return pawn; |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | @brief |
---|
198 | Sets the time the InvisibilityPickup will last. |
---|
199 | @param duration |
---|
200 | The duration in seconds. |
---|
201 | */ |
---|
202 | void InvisiblePickup::setDuration(float duration) |
---|
203 | { |
---|
204 | if(duration >= 0.0f) |
---|
205 | { |
---|
206 | this->duration_ = duration; |
---|
207 | } |
---|
208 | else |
---|
209 | { |
---|
210 | orxout(internal_error, context::pickups) << "Invalid duration in InvisiblePickup." << endl; |
---|
211 | this->duration_ = 0.0f; |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | @brief |
---|
217 | Helper method. Is called by the Timer as soon as it expires. |
---|
218 | */ |
---|
219 | void InvisiblePickup::pickupTimerCallback(void) |
---|
220 | { |
---|
221 | this->setUsed(false); |
---|
222 | } |
---|
223 | |
---|
224 | } |
---|