Debian Codegraph @

Oodle/src/CGotoEvent.cpp

raw
1 #include "CGotoEvent.h"
2
3 CGotoEvent::CGotoEvent(CParticle &o, CVector dest, double speed, bool c)
4 : CSequencedEvent(c), obj(o), dest(dest), start(ZERO_VECTOR), speed(speed)
5 {
6
7 }
8
9 CGotoEvent* CGotoEvent::getCopy()
10 {
11 return new CGotoEvent(*this);
12 }
13
14 CGotoData* CGotoEvent::getEventData()
15 {
16 int dtype = 0;
17 if (start.getY() < dest.getY())
18 {
19 dtype = dtype | static_cast<int> (CGotoData::BOTTOM);
20 }
21 else if (start.getY() > dest.getY())
22 {
23 dtype |= static_cast<int> (CGotoData::TOP);
24 }
25
26 if(start.getX() < dest.getX())
27 {
28 dtype |= static_cast<int> (CGotoData::RIGHT);
29 }
30 else if (start.getX() > dest.getX())
31 {
32 dtype |= static_cast<int> (CGotoData::LEFT);
33 }
34 return new CGotoData(dtype);
35 }
36
37 void CGotoEvent::initialize()
38 {
39 CSequencedEvent::initialize();
40
41 start = obj.getPos();
42 CVector vel = (dest - start);
43 vel.setMag(speed);
44 obj.setVelocity( vel );
45 }
46
47 void CGotoEvent::doUpdate()
48 {
49 if (!isRunning())
50 {
51 return;
52 }
53
54 bool valY = false, valX = false;
55
56 // first check if it is at or past dest.y (check greater than if it is going down, check less than if going up)
57 if (obj.getVel().getY() < 0)
58 {
59 if (obj.getPos().getY() <= dest.getY())
60 {
61 valY = true;
62 }
63 }
64 else
65 {
66 if (obj.getPos().getY() >= dest.getY())
67 {
68 valY = true;
69 }
70 }
71
72 // check if it is at or past dest.x (check greater than if it is going right, check less than if going left)
73 if (obj.getVel().getX() < 0)
74 {
75 if (obj.getPos().getX() <= dest.getX())
76 {
77 valX = true;
78 }
79 }
80 else
81 {
82 if (obj.getPos().getX() >= dest.getX())
83 {
84 valX = true;
85 }
86 }
87
88 if (valX && valY)
89 {
90 obj.setVelocity(ZERO_VECTOR);
91 obj.setPos(dest);
92 setDone();
93 }
94 }