Debian Codegraph @

Oodle/src/CParticle.cpp

raw
1 #include "oodle.h"
2
3 CParticle::CParticle(CVector v, CVector pos)
4 : velocity(v), pos(pos), box(CRect(0,0,0,0)), timer(), dead(false)
5 {
6 }
7
8 CParticle::CParticle(CVector v, CVector pos, const CRect &r)
9 : velocity(v), pos(pos), box(r), timer(), dead(false)
10 {
11 }
12
13 CParticle::CParticle(const CParticle &p)
14 : velocity(p.velocity), pos(p.pos), moving(p.moving), timer(p.timer), box(p.box), dead(p.dead)
15 {
16 // copy over all the events (need to make copies, not just copy pointers)
17 for (CSequencedEvent::ConstIter i = p.events.begin(); i != p.events.end(); ++i)
18 {
19 CSequencedEvent* e = (*i)->getCopy();
20 events.push_back(e);
21 }
22
23 }
24
25 CParticle::~CParticle()
26 {
27 for (CSequencedEvent::Iter i = events.begin(); i != events.end(); ++i)
28 {
29 if( (*i)->minusRef() == 0)
30 delete *i;
31 }
32 }
33
34 void CParticle::update()
35 {
36 CEventData* d = NULL;
37 bool concurrent = false;
38
39 /*if (dynamic_cast<CBlock*> (this) != NULL && (dynamic_cast<CBlock*> (this))->getBoardY() < 11 && getVel() == ZERO_VECTOR)
40 cout << "zero {" << this << "} [" << velocity << "]" << endl;*/
41
42 pos += velocity * (timer.getElapsedReset()/1000.); // dP = v * dT
43
44 for (CSequencedEvent::Iter i = events.begin(); i != events.end(); ++i)
45 {
46 if ((*i)->isDone())
47 {
48 d = (*i)->getEventData();
49
50 CSequencedEvent::Iter tmp = i;
51 if (tmp != events.end())
52 tmp++;
53
54 if( (*i)->minusRef() == 0)
55 delete (*i);
56
57 events.erase(i);
58 i = tmp;
59 // i++;
60 if (i != events.end()) // have to go ahead and evaluate the i element here because if we let the loop run again, it would skip an event
61 {
62 (*i)->passEventData(d); // event data is a way for sequenced events to pass information
63
64 (*i)->update();
65
66 if ( !(*i)->isConcurrent() )
67 {
68 concurrent = true;
69 break;
70 }
71 d = (*i)->getEventData();
72 }
73 else
74 {
75 delete d;
76 d = NULL;
77 }
78 }
79 else
80 {
81 //(*i)->passEventData(d);
82 (*i)->update();
83 //d = (*i)->getEventData();
84
85 if ( !(*i)->isConcurrent() && !(*i)->isDone() ) // if the event is not concurrent, break because it must run to completion
86 {
87 concurrent = true;
88 break;
89 }
90 else if ((*i)->isDone())
91 {
92 d = (*i)->getEventData();
93
94 CSequencedEvent::Iter tmp = i;
95
96 if (tmp != events.end())
97 tmp++;
98
99 if( (*i)->minusRef() == 0)
100 delete (*i);
101
102 events.erase(i);
103 i = tmp;
104 // i++;
105 if (i != events.end()) // have to go ahead and evaluate the i element here because if we let the loop run again, it would skip an event
106 {
107 (*i)->passEventData(d); // event data is a way for sequenced events to pass information
108 (*i)->update();
109
110 if ( !(*i)->isConcurrent() )
111 {
112 concurrent = true;
113 break;
114 }
115 }
116 else
117 {
118 delete d;
119 d = NULL;
120 }
121 }
122 }
123 }
124
125 if (!concurrent)
126 {
127 for (CSequencedEvent::Iter i = behaviors.begin(); i != behaviors.end(); ++i)
128 {
129 if ((*i)->isDone())
130 {
131 d = (*i)->getEventData();
132
133 CSequencedEvent::Iter tmp = i;
134 if (tmp != events.end())
135 tmp++;
136
137 if( (*i)->minusRef() == 0)
138 delete (*i);
139
140 events.erase(i);
141 i = tmp;
142
143 if (i != events.end()) // have to go ahead and evaluate the i element here because if we let the loop run again, it would skip an event
144 {
145 (*i)->passEventData(d); // event data is a way for sequenced events to pass information
146
147 (*i)->update();
148
149 if ( !(*i)->isConcurrent() )
150 {
151 concurrent = true;
152 break;
153 }
154 d = (*i)->getEventData();
155 }
156 else
157 {
158 d = NULL;
159 }
160 }
161 else
162 {
163 //(*i)->passEventData(d);
164 (*i)->update();
165 //d = (*i)->getEventData();
166
167 if ( !(*i)->isConcurrent() && !(*i)->isDone() ) // if the event is not concurrent, break because it must run to completion
168 {
169 concurrent = true;
170 break;
171 }
172 else if ((*i)->isDone())
173 {
174 d = (*i)->getEventData();
175
176 CSequencedEvent::Iter tmp = i;
177
178 if (tmp != events.end())
179 tmp++;
180
181 if( (*i)->minusRef() == 0)
182 delete (*i);
183
184 events.erase(i);
185 i = tmp;
186 // i++;
187 if (i != events.end()) // have to go ahead and evaluate the i element here because if we let the loop run again, it would skip an event
188 {
189 (*i)->passEventData(d); // event data is a way for sequenced events to pass information
190 (*i)->update();
191
192 if ( !(*i)->isConcurrent() )
193 {
194 concurrent = true;
195 break;
196 }
197 }
198 else
199 {
200 delete d;
201 d = NULL;
202 }
203 }
204 }
205 }
206 }
207
208
209
210 stayInBoundary();
211
212 }
213
214 CParticle::BoundaryInfo CParticle::inBoundaryHoriz()
215 {
216 return inBoundaryHoriz(pos.getX());
217 }
218
219 CParticle::BoundaryInfo CParticle::inBoundaryHoriz(double x)
220 {
221 if (box != CRect(0,0,0,0))
222 {
223 if (! box.checkLeft(x))
224 {
225 return OUTSIDE_LOW;
226 }
227 else if (! box.checkRight(x))
228 {
229 return OUTSIDE_HIGH;
230 }
231 else
232 {
233 return INSIDE;
234 }
235 }
236 else
237 {
238 return INSIDE;
239 }
240 }
241
242 void CParticle::clear()
243 {
244 dead = true;
245 }
246
247 CParticle::BoundaryInfo CParticle::inBoundaryVert()
248 {
249 return inBoundaryVert(pos.getY());
250 }
251
252 CParticle::BoundaryInfo CParticle::inBoundaryVert(double y)
253 {
254 if (box != CRect(0,0,0,0))
255 {
256 if (! box.checkBottom(y))
257 {
258 return OUTSIDE_HIGH;
259 }
260 else if (! box.checkTop(y))
261 {
262 return OUTSIDE_LOW;
263 }
264 else
265 {
266 return INSIDE;
267 }
268 }
269 else
270 {
271 return INSIDE;
272 }
273 }
274
275 void CParticle::stayInBoundary()
276 {
277 // Check the horizontal position
278 switch (inBoundaryHoriz())
279 {
280 case OUTSIDE_LOW:
281 // Only bounce once
282 if (velocity.getX() < 0)
283 {
284 cout << "bounce x for low" << endl;
285 velocity.setX(-velocity.getX());
286 pos.setX( box.getX());
287 }
288 break;
289 case OUTSIDE_HIGH:
290 // Only bounce once
291 if (velocity.getX() > 0)
292 {
293 cout << "bounce x for high" << endl;
294 velocity.setX(-velocity.getX());
295 pos.setX( box.getX() + box.getWidth());
296 }
297 break;
298 default:
299 break;
300 }
301
302 switch (inBoundaryVert())
303 {
304 case OUTSIDE_LOW:
305 // Only bounce once
306 if (velocity.getY() < 0)
307 {
308 velocity.setY(-velocity.getY());
309 pos.setY( box.getY());
310 }
311 break;
312 case OUTSIDE_HIGH:
313 // Only bounce once
314 if (velocity.getY() > 0)
315 {
316 velocity.setY(-velocity.getY());
317 pos.setY( box.getY() + box.getHeight());
318 }
319 break;
320 default:
321 break;
322 }
323 }
324
325 void CParticle::setPos(CVector pos)
326 {
327 if (inBoundaryHoriz(pos.getX()) == INSIDE)
328 {
329 CParticle::pos.setX(pos.getX());
330 }
331
332 if (inBoundaryVert(pos.getY()) == INSIDE)
333 {
334 CParticle::pos.setY(pos.getY());
335 }
336 }
337
338 void CParticle::setPos(double x, double y)
339 {
340 if (inBoundaryHoriz(pos.getX()) == INSIDE)
341 {
342 CParticle::pos.setX(x);
343 }
344
345 if (inBoundaryVert(pos.getY()) == INSIDE)
346 {
347 CParticle::pos.setY(y);
348 }
349 }
350
351 void CParticle::setPosRel(double x, double y)
352 {
353 if (inBoundaryHoriz(pos.getX()) == INSIDE)
354 {
355 CParticle::pos.addX(x);
356 }
357
358 if (inBoundaryVert(pos.getY()) == INSIDE)
359 {
360 CParticle::pos.addY(y);
361 }
362 }
363
364 void CParticle::appendEvent(CSequencedEvent* e)
365 {
366 events.push_back(e);
367 }
368
369 void CParticle::appendBehavior(CSequencedEvent* e)
370 {
371 behaviors.push_back(e);
372 }
373
374 void CParticle::prependEvent(CSequencedEvent* e)
375 {
376 //events.push_front(e);
377 }