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 | * Damian 'Mozork' Frick |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file |
---|
31 | @brief Implementation of the QuestEffectBeacon class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "QuestEffectBeacon.h" |
---|
35 | |
---|
36 | #include "core/CoreIncludes.h" |
---|
37 | #include "core/XMLPort.h" |
---|
38 | #include "core/EventIncludes.h" |
---|
39 | #include "worldentities/pawns/Pawn.h" |
---|
40 | #include "interfaces/PlayerTrigger.h" |
---|
41 | #include "QuestEffect.h" |
---|
42 | |
---|
43 | namespace orxonox |
---|
44 | { |
---|
45 | CreateFactory(QuestEffectBeacon); |
---|
46 | |
---|
47 | /** |
---|
48 | @brief |
---|
49 | Constructor. Registers the object and initializes defaults. |
---|
50 | */ |
---|
51 | QuestEffectBeacon::QuestEffectBeacon(BaseObject* creator) : StaticEntity(creator) |
---|
52 | { |
---|
53 | RegisterObject(QuestEffectBeacon); |
---|
54 | |
---|
55 | this->status_ = QuestEffectBeaconStatus::Active; |
---|
56 | this->times_ = INFINITE_TIME; |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | Destructor. |
---|
61 | */ |
---|
62 | QuestEffectBeacon::~QuestEffectBeacon() |
---|
63 | { |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | @brief |
---|
68 | Method for creating a QuestEffectBeacon object through XML. |
---|
69 | */ |
---|
70 | void QuestEffectBeacon::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
71 | { |
---|
72 | SUPER(QuestEffectBeacon, XMLPort, xmlelement, mode); |
---|
73 | |
---|
74 | XMLPortParam(QuestEffectBeacon, "times", setTimes, getTimes, xmlelement, mode); |
---|
75 | XMLPortObject(QuestEffectBeacon, QuestEffect, "effects", addEffect, getEffect, xmlelement, mode); |
---|
76 | |
---|
77 | XMLPortEventState(QuestEffectBeacon, PlayerTrigger, "execute", execute, xmlelement, mode); |
---|
78 | |
---|
79 | COUT(3) << "New QuestEffectBeacon created." << std::endl; |
---|
80 | } |
---|
81 | |
---|
82 | void QuestEffectBeacon::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
---|
83 | { |
---|
84 | SUPER(QuestEffectBeacon, XMLEventPort, xmlelement, mode); |
---|
85 | |
---|
86 | XMLPortEventState(QuestEffectBeacon, PlayerTrigger, "execute", execute, xmlelement, mode); |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | @brief |
---|
91 | Executes the QuestEffectBeacon. |
---|
92 | This means extracting the ControllableEntity from the PlayerTrigger, provided by the Event causing the execution, and the extracting the PlayerInfo from the received ControllableEntity and invoking the QuestEffectbeacon's QuestEffects on the received PlayerInfo. |
---|
93 | @param trigger |
---|
94 | Apointer to the PlayerTrigger that threw the Event. |
---|
95 | @return |
---|
96 | Returns true if successfully executed, false if not. |
---|
97 | */ |
---|
98 | bool QuestEffectBeacon::execute(bool b, PlayerTrigger* trigger) |
---|
99 | { |
---|
100 | if(!b) |
---|
101 | { |
---|
102 | return false; |
---|
103 | } |
---|
104 | if(!(this->isActive())) //!< If the QuestEffectBeacon is inactive it cannot be executed. |
---|
105 | { |
---|
106 | COUT(3) << "The QuestEffectBeacon is inactive." << std::endl; |
---|
107 | return false; |
---|
108 | } |
---|
109 | |
---|
110 | if(!trigger->isForPlayer()) //!< The PlayerTrigger is not exclusively for ControllableEntities which means we cannot extract one. |
---|
111 | { |
---|
112 | return false; |
---|
113 | } |
---|
114 | |
---|
115 | //! Extracting the ControllableEntity form the PlayerTrigger. |
---|
116 | Pawn* pawn = trigger->getTriggeringPlayer(); |
---|
117 | |
---|
118 | if(pawn == NULL) |
---|
119 | { |
---|
120 | COUT(2) << "The QuestEffectBeacon was triggered by an entity other than a Pawn." << std::endl; |
---|
121 | return false; |
---|
122 | } |
---|
123 | |
---|
124 | //! Extract the PlayerInfo from the ControllableEntity. |
---|
125 | PlayerInfo* player = pawn->getPlayer(); |
---|
126 | |
---|
127 | if(player == NULL) |
---|
128 | { |
---|
129 | COUT(3) << "The PlayerInfo* is NULL." << std::endl; |
---|
130 | return false; |
---|
131 | } |
---|
132 | |
---|
133 | COUT(3) << "QuestEffectBeacon executed on player: " << player << " ." << std::endl; |
---|
134 | |
---|
135 | bool check = QuestEffect::invokeEffects(player, this->effects_); //!< Invoke the QuestEffects on the PlayerInfo. |
---|
136 | if(check) |
---|
137 | { |
---|
138 | this->decrementTimes(); //!< Decrement the number of times the beacon can be used. |
---|
139 | return true; |
---|
140 | } |
---|
141 | |
---|
142 | return false; |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | @brief |
---|
147 | Set the status of the QuestEffectBeacon. |
---|
148 | @param activate |
---|
149 | If true the QuestEffectBeacon is activated, if false it is deactivated. |
---|
150 | @return |
---|
151 | Returns whether the activation/deactivation was successful. |
---|
152 | */ |
---|
153 | bool QuestEffectBeacon::setActive(bool activate) |
---|
154 | { |
---|
155 | if(this->getTimes() == 0 && activate) //!< A QuestEffectBeacon that can be executed only 0 times is always inactive. |
---|
156 | { |
---|
157 | return false; |
---|
158 | } |
---|
159 | |
---|
160 | if(activate) |
---|
161 | { |
---|
162 | this->status_ = QuestEffectBeaconStatus::Active; |
---|
163 | return true; |
---|
164 | } |
---|
165 | |
---|
166 | this->status_ = QuestEffectBeaconStatus::Inactive; |
---|
167 | return true; |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | @brief |
---|
172 | Decrement the number of times the QuestEffectBeacon can be executed. |
---|
173 | @return |
---|
174 | Returns true if successful. |
---|
175 | */ |
---|
176 | bool QuestEffectBeacon::decrementTimes(void) |
---|
177 | { |
---|
178 | if(!(this->isActive())) //!< The QuestEffectBeacon mus be active to decrement the number of times it can be executed. |
---|
179 | { |
---|
180 | return false; |
---|
181 | } |
---|
182 | if(this->getTimes() == INFINITE_TIME) //!< If times is infinity the QuestEffectBeacon can be executed an infinite number fo times. |
---|
183 | { |
---|
184 | return true; |
---|
185 | } |
---|
186 | |
---|
187 | this->times_ = this->times_ - 1; //!< Decrement number of times the QuestEffectBeacon can be executed. |
---|
188 | if(this->getTimes() == 0) //!< Set the QuestEffectBeacon to inactive when the number of times it can be executed is reduced to 0. |
---|
189 | { |
---|
190 | this->status_ = QuestEffectBeaconStatus::Inactive; |
---|
191 | } |
---|
192 | |
---|
193 | return true; |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | @brief |
---|
198 | Set the number of times the QuestEffectBeacon can be executed. |
---|
199 | The number must be eighter <= 0, or INFINITY which is '-1'. |
---|
200 | @param n |
---|
201 | The number of times the QuestEffectBeacon can be executed. |
---|
202 | The number must be eighter <= 0, or INFINITY which is '-1'. |
---|
203 | @return |
---|
204 | Returns true if successful. |
---|
205 | */ |
---|
206 | bool QuestEffectBeacon::setTimes(const int & n) |
---|
207 | { |
---|
208 | if(n < 0 && n != INFINITE_TIME) |
---|
209 | { |
---|
210 | return false; |
---|
211 | } |
---|
212 | |
---|
213 | this->times_ = n; |
---|
214 | return true; |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | @brief |
---|
219 | Adds a QuestEffect to the QuestEffectBeacon. |
---|
220 | @param effect |
---|
221 | A pointer to the QuestEffect to be added. |
---|
222 | @return |
---|
223 | Returns true if successful. |
---|
224 | */ |
---|
225 | bool QuestEffectBeacon::addEffect(QuestEffect* effect) |
---|
226 | { |
---|
227 | if(effect == NULL) //!< NULL-pointers are not well liked here... |
---|
228 | { |
---|
229 | COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl; |
---|
230 | return false; |
---|
231 | } |
---|
232 | |
---|
233 | this->effects_.push_back(effect); |
---|
234 | |
---|
235 | COUT(3) << "A QuestEffect was added to a QuestEffectBeacon." << std::endl; |
---|
236 | return true; |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | @brief |
---|
241 | Returns the QuestEffect at the given index. |
---|
242 | @param index |
---|
243 | The index. |
---|
244 | @return |
---|
245 | Returns a pointer to the QuestEffect at the given index. |
---|
246 | */ |
---|
247 | const QuestEffect* QuestEffectBeacon::getEffect(unsigned int index) const |
---|
248 | { |
---|
249 | int i = index; |
---|
250 | for (std::list<QuestEffect*>::const_iterator effect = this->effects_.begin(); effect != this->effects_.end(); ++effect) |
---|
251 | { |
---|
252 | if(i == 0) |
---|
253 | { |
---|
254 | return *effect; |
---|
255 | } |
---|
256 | i--; |
---|
257 | } |
---|
258 | return NULL; |
---|
259 | } |
---|
260 | |
---|
261 | } |
---|