1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2007 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Fabian 'x3n' Landau |
---|
13 | co-programmer: |
---|
14 | */ |
---|
15 | |
---|
16 | #include "sound/resource_sound_buffer.h" |
---|
17 | #include "util/loading/load_param_xml.h" |
---|
18 | #include "util/loading/load_param.h" |
---|
19 | #include "util/loading/factory.h" |
---|
20 | #include "mover.h" |
---|
21 | |
---|
22 | #define CLOSED 0 |
---|
23 | #define OPEN 1 |
---|
24 | #define MOVE 2 |
---|
25 | #define WAIT 3 |
---|
26 | #define NEXT 4 |
---|
27 | #define DELAY 5 |
---|
28 | #define STAY 6 |
---|
29 | |
---|
30 | ObjectListDefinition(Mover); |
---|
31 | CREATE_FACTORY(Mover); |
---|
32 | |
---|
33 | |
---|
34 | Mover::Mover(const TiXmlElement* root) |
---|
35 | { |
---|
36 | PRINTF(0)("1_1 Mover %p created\n", this); |
---|
37 | this->registerObject(this, Mover::_objectList); |
---|
38 | this->toList(OM_ENVIRON); |
---|
39 | |
---|
40 | this->bLoop = false; |
---|
41 | this->bRepeat = false; |
---|
42 | this->bWaitAfterEachStation = false; |
---|
43 | this->bOnlyMoveWhileTriggered = false; |
---|
44 | this->bAttachTrigger = false; |
---|
45 | this->bReopen = false; |
---|
46 | this->bReclose = false; |
---|
47 | |
---|
48 | this->triggers = 0; |
---|
49 | this->stations = 0; |
---|
50 | |
---|
51 | this->repeats = 0; |
---|
52 | this->state = CLOSED; |
---|
53 | this->station = 0; |
---|
54 | this->repeatsToGo = 0; |
---|
55 | this->time = 0; |
---|
56 | |
---|
57 | this->originCoor = Vector(0, 0, 0); |
---|
58 | this->originDir = Quaternion(0, Vector(0, 0, 0)); |
---|
59 | |
---|
60 | this->soundSource_starting.setSourceNode(this); |
---|
61 | this->soundSource_moving.setSourceNode(this); |
---|
62 | this->soundSource_ending.setSourceNode(this); |
---|
63 | |
---|
64 | PRINTF(0)("1_2\n"); |
---|
65 | if (root != NULL) |
---|
66 | this->loadParams(root); |
---|
67 | |
---|
68 | this->updateNode(0.001); |
---|
69 | |
---|
70 | this->originCoor = this->getAbsCoor(); |
---|
71 | this->originDir = this->getAbsDir(); |
---|
72 | |
---|
73 | PRINTF(0)("1_3\n"); |
---|
74 | if (this->stations) |
---|
75 | this->station = this->stations->getNextStation(0); |
---|
76 | PRINTF(0)("1_4\n"); |
---|
77 | if (this->triggers) |
---|
78 | this->triggers->setBaseCoor(this->getAbsCoor()); |
---|
79 | PRINTF(0)("1_5\n"); |
---|
80 | } |
---|
81 | |
---|
82 | Mover::~Mover() |
---|
83 | { |
---|
84 | if (this->triggers) |
---|
85 | delete this->triggers; |
---|
86 | if (this->stations) |
---|
87 | delete this->stations; |
---|
88 | |
---|
89 | if (this->soundSource_starting.isPlaying()) |
---|
90 | this->soundSource_starting.stop(); |
---|
91 | if (this->soundSource_moving.isPlaying()) |
---|
92 | this->soundSource_moving.stop(); |
---|
93 | if (this->soundSource_ending.isPlaying()) |
---|
94 | this->soundSource_ending.stop(); |
---|
95 | } |
---|
96 | |
---|
97 | void Mover::loadParams(const TiXmlElement* root) |
---|
98 | { |
---|
99 | PRINTF(0)("2_1\n"); |
---|
100 | WorldEntity::loadParams(root); |
---|
101 | |
---|
102 | LoadParam(root, "bLoop", this, Mover, setLoop) |
---|
103 | .describe("After getting triggered, the mover loops forever.") |
---|
104 | .defaultValues(false); |
---|
105 | LoadParam(root, "bRepeat", this, Mover, setRepeat) |
---|
106 | .describe("After getting triggered, the mover moves n times.") |
---|
107 | .defaultValues(1); |
---|
108 | LoadParam(root, "bWaitAfterEachStation", this, Mover, setWaitAfterEachStation) |
---|
109 | .describe("After each station, the mover waits until he gets triggered.") |
---|
110 | .defaultValues(false); |
---|
111 | LoadParam(root, "bOnlyMoveWhileTriggered", this, Mover, setOnlyMoveWhileTriggered) |
---|
112 | .describe("The mover stops as soon as he gets untriggered.") |
---|
113 | .defaultValues(false); |
---|
114 | LoadParam(root, "bAttachTrigger", this, Mover, setAttachTrigger) |
---|
115 | .describe("The trigger follows the mover.") |
---|
116 | .defaultValues(false); |
---|
117 | LoadParam(root, "bReopen", this, Mover, setReopen) |
---|
118 | .describe("Stops closing and reopens the mover when he gets triggered again.") |
---|
119 | .defaultValues(false); |
---|
120 | LoadParam(root, "bReclose", this, Mover, setReclose) |
---|
121 | .describe("Stops opening and recloses the mover when he gets untriggered.") |
---|
122 | .defaultValues(false); |
---|
123 | PRINTF(0)("2_2\n"); |
---|
124 | LoadParamXML(root, "triggers", this, Mover, setTriggers) |
---|
125 | .describe("Adds a trigger that releases the mover."); |
---|
126 | PRINTF(0)("2_3\n"); |
---|
127 | LoadParamXML(root, "stations", this, Mover, setStations) |
---|
128 | .describe("Adds a station to the movers stations-list."); |
---|
129 | PRINTF(0)("2_4\n"); |
---|
130 | } |
---|
131 | |
---|
132 | void Mover::setTriggers(const TiXmlElement* root) |
---|
133 | { |
---|
134 | PRINTF(0)("3_1\n"); |
---|
135 | this->triggers = new MoverTriggerList(); |
---|
136 | const TiXmlElement* element = root->FirstChildElement(); |
---|
137 | |
---|
138 | PRINTF(0)("3_2\n"); |
---|
139 | while (element != NULL) |
---|
140 | { |
---|
141 | PRINTF(0)("3_3\n"); |
---|
142 | // PRINTF(0)("TiXmlElement Value: %s\n", element->Value()); |
---|
143 | BaseObject *newObj = Factory::fabricate(element); |
---|
144 | // PRINTF(0)("BaseObject ClassName: %s\n", newObj->getClassCName()); |
---|
145 | // MoverTrigger *newTrigger = ((MoverTrigger*)(&newObj)); |
---|
146 | MoverTrigger *newTrigger = (dynamic_cast< MoverTrigger *> ( newObj )); |
---|
147 | this->triggers->addTrigger(newTrigger); |
---|
148 | // this->triggers->addTrigger(MoverTrigger::createTrigger(element)); |
---|
149 | PRINTF(0)("3_4\n"); |
---|
150 | element = element->NextSiblingElement(); |
---|
151 | PRINTF(0)("3_5\n"); |
---|
152 | } |
---|
153 | PRINTF(0)("3_6\n"); |
---|
154 | } |
---|
155 | |
---|
156 | void Mover::setStations(const TiXmlElement* root) |
---|
157 | { |
---|
158 | PRINTF(0)("4_1\n"); |
---|
159 | this->stations = new MoverStationList(); |
---|
160 | const TiXmlElement* element = root->FirstChildElement(); |
---|
161 | |
---|
162 | PRINTF(0)("4_2\n"); |
---|
163 | while (element != NULL) |
---|
164 | { |
---|
165 | PRINTF(0)("4_3\n"); |
---|
166 | this->stations->addStation(new MoverStation(element)); |
---|
167 | element = element->NextSiblingElement(); |
---|
168 | } |
---|
169 | PRINTF(0)("4_4\n"); |
---|
170 | } |
---|
171 | |
---|
172 | void Mover::tick(float dt) |
---|
173 | { |
---|
174 | // PRINTF(0)("15_1 mover state of mover %p is %i, triggerlist is %p\n", this, this->state, this->triggers); |
---|
175 | if (this->state == DELAY || this->state == MOVE || this->state == STAY) |
---|
176 | this->time += dt; |
---|
177 | |
---|
178 | if (this->state == NEXT) |
---|
179 | this->changeState(this->next()); |
---|
180 | |
---|
181 | if (this->state == WAIT) |
---|
182 | this->changeState(this->wait()); |
---|
183 | |
---|
184 | if (this->state == CLOSED) |
---|
185 | this->changeState(this->closed()); |
---|
186 | |
---|
187 | if (this->state == OPEN) |
---|
188 | this->changeState(this->open()); |
---|
189 | |
---|
190 | if (this->state == DELAY) |
---|
191 | this->changeState(this->delay()); |
---|
192 | |
---|
193 | if (this->state == MOVE) |
---|
194 | this->changeState(this->move(dt)); |
---|
195 | |
---|
196 | if (this->state == STAY) |
---|
197 | this->changeState(this->stay()); |
---|
198 | } |
---|
199 | |
---|
200 | void Mover::changeState(int state) |
---|
201 | { |
---|
202 | if (this->state == state) |
---|
203 | return; |
---|
204 | |
---|
205 | this->state = state; |
---|
206 | |
---|
207 | if (this->state == DELAY || this->state == STAY) |
---|
208 | this->time = 0; |
---|
209 | } |
---|
210 | |
---|
211 | int Mover::closed() |
---|
212 | { |
---|
213 | if (this->triggers && this->triggers->isTriggered()) |
---|
214 | { |
---|
215 | if (this->bRepeat) |
---|
216 | this->repeatsToGo = this->repeats; |
---|
217 | |
---|
218 | return DELAY; |
---|
219 | } |
---|
220 | |
---|
221 | return CLOSED; |
---|
222 | } |
---|
223 | |
---|
224 | int Mover::open() |
---|
225 | { |
---|
226 | if (this->triggers && !this->triggers->isTriggered()) |
---|
227 | { |
---|
228 | return DELAY; |
---|
229 | } |
---|
230 | |
---|
231 | return OPEN; |
---|
232 | } |
---|
233 | |
---|
234 | int Mover::wait() |
---|
235 | { |
---|
236 | if (this->triggers && this->triggers->isTriggered()) |
---|
237 | { |
---|
238 | this->soundSource_moving.play(); |
---|
239 | return MOVE; |
---|
240 | } |
---|
241 | |
---|
242 | return WAIT; |
---|
243 | } |
---|
244 | |
---|
245 | int Mover::next() |
---|
246 | { |
---|
247 | bool isOpen = false; |
---|
248 | bool isClosed = false; |
---|
249 | if (this->stations) |
---|
250 | { |
---|
251 | isOpen = this->stations->isOpen(station); |
---|
252 | isClosed = this->stations->isClosed(station); |
---|
253 | this->station = this->stations->getNextStation(this->station); |
---|
254 | } |
---|
255 | |
---|
256 | if (isClosed && this->bRepeat) |
---|
257 | this->repeatsToGo--; |
---|
258 | |
---|
259 | if (isClosed && (!this->bLoop && this->repeatsToGo <= 0)) |
---|
260 | return CLOSED; |
---|
261 | |
---|
262 | if (isOpen && (!this->bLoop && this->repeatsToGo <= 0)) |
---|
263 | return OPEN; |
---|
264 | |
---|
265 | if (this->bWaitAfterEachStation) |
---|
266 | return WAIT; |
---|
267 | |
---|
268 | if ((this->bLoop || this->repeats > 0) || (!isOpen && !isClosed)) |
---|
269 | return DELAY; |
---|
270 | |
---|
271 | return NEXT; |
---|
272 | } |
---|
273 | |
---|
274 | int Mover::delay() |
---|
275 | { |
---|
276 | if (!this->stations || (this->time < this->stations->getDelay(this->station))) |
---|
277 | return DELAY; |
---|
278 | |
---|
279 | if (this->stations->getStartingSound(this->station).loaded()) |
---|
280 | this->soundSource_starting.play(this->stations->getStartingSound(this->station)); |
---|
281 | if (this->stations->getMovingSound(this->station).loaded()) |
---|
282 | this->soundSource_moving.play(this->stations->getMovingSound(this->station), 1.0, true); |
---|
283 | |
---|
284 | this->time = 0; |
---|
285 | |
---|
286 | return MOVE; |
---|
287 | } |
---|
288 | |
---|
289 | int Mover::move(float dt) |
---|
290 | { |
---|
291 | if (this->stations) |
---|
292 | { |
---|
293 | this->shiftCoor(this->stations->getVelocity(this->station) * dt); |
---|
294 | this->setAbsDir(this->originDir * (this->stations->getRotation(this->station) * this->time)); |
---|
295 | } |
---|
296 | |
---|
297 | if (this->bAttachTrigger && this->triggers) |
---|
298 | this->triggers->setBaseCoor(this->getAbsCoor()); |
---|
299 | |
---|
300 | if (!this->bLoop && this->repeatsToGo <= 0) |
---|
301 | { |
---|
302 | if (this->stations && this->triggers && this->stations->changeDirection(this->bReopen, this->bReclose, this->triggers->isTriggered())) |
---|
303 | { |
---|
304 | this->time = this->stations->getMovingTime(this->station) - this->time; |
---|
305 | this->originCoor = this->originCoor - this->stations->getRelTargetCoor(this->station); |
---|
306 | this->originDir = this->originDir / this->stations->getRelTargetDir(this->station); |
---|
307 | } |
---|
308 | } |
---|
309 | |
---|
310 | if (/*this->reachedStationsTarget(dt) || */(this->stations && (this->time >= 1.1 * this->stations->getMovingTime(this->station)))) |
---|
311 | // if (this->stations && (this->time >= this->stations->getMovingTime(this->station))) |
---|
312 | { |
---|
313 | this->setAbsCoor(this->originCoor + this->stations->getRelTargetCoor(this->station)); |
---|
314 | this->setAbsDir(this->originDir * this->stations->getRelTargetDir(this->station)); |
---|
315 | |
---|
316 | if (this->stations->getEndingSound(this->station).loaded()) |
---|
317 | this->soundSource_ending.play(this->stations->getEndingSound(this->station)); |
---|
318 | this->soundSource_moving.stop(); |
---|
319 | |
---|
320 | this->originCoor = this->originCoor + this->stations->getRelTargetCoor(this->station); |
---|
321 | this->originDir = this->originDir * this->stations->getRelTargetDir(this->station); |
---|
322 | |
---|
323 | return STAY; |
---|
324 | } |
---|
325 | |
---|
326 | if (this->triggers && this->bOnlyMoveWhileTriggered && (!this->triggers->isTriggered())) |
---|
327 | { |
---|
328 | this->soundSource_moving.stop(); |
---|
329 | return WAIT; |
---|
330 | } |
---|
331 | |
---|
332 | return MOVE; |
---|
333 | } |
---|
334 | |
---|
335 | int Mover::stay() |
---|
336 | { |
---|
337 | if (!this->stations || (this->time < this->stations->getStayOpenTime(this->station))) |
---|
338 | return STAY; |
---|
339 | |
---|
340 | return NEXT; |
---|
341 | } |
---|
342 | |
---|
343 | bool Mover::reachedStationsTarget(float dt) |
---|
344 | { |
---|
345 | if (this->stations) |
---|
346 | if ((this->getAbsCoor() - (this->originCoor + this->stations->getRelTargetCoor(this->station))).len() <= 1.2 * (this->stations->getVelocity(this->station) * dt).len()) |
---|
347 | if (fabs((this->getAbsDir() / (this->originDir * this->stations->getRelTargetDir(this->station))).w) <= 1.2 * fabs(this->stations->getRotation(this->station).w * dt)) |
---|
348 | return true; |
---|
349 | |
---|
350 | return false; |
---|
351 | } |
---|
352 | |
---|