Debian Codegraph @

TetrisAttack/src/CFieldSprite.cpp

raw
1 #include "tetris.h"
2
3 /*void CFieldSprite::moveRelBoardCords(int dX, int dY, double vel)
4 {
5 setDest( screenCords( boardX + dX, boardY + dY ) );
6 velocity.setY(vel);
7 boardX += dX;
8 boardY += dY;
9 }
10 */
11
12 CGotoEvent* CFieldSprite::moveRelBoardCords(int dX, int dY, double speed)
13 {
14 int x = boardX + dX;
15 int y = boardY + dY;
16
17 if (x < fieldWidth && x >= 0 && y < fieldHeight && y >= 0)
18 {
19 CVector sc = screenCords(boardX + dX, boardY + dY);
20
21 //setDest( sc );
22 CGotoEvent *e = new CGotoEvent(*this, sc, speed, false);
23 appendEvent(e);
24
25 boardX += dX;
26 boardY += dY;
27 return e;
28 }
29 return NULL;
30 }
31
32 void CFieldSprite::moveBoardCords(int x, int y)
33 {
34 if (x < fieldWidth && x >= 0 && y < fieldHeight && y >= 0)
35 {
36 CVector sc = screenCords(x, y);
37
38 setPos( sc );
39 boardX = x;
40 boardY = y;
41 }
42 }
43
44 void CFieldSprite::update()
45 {
46 CSprite::update();
47 if (dest != ZERO_VECTOR)
48 {
49 if (atDest()) // we are at or beyond dest (note: this doesn't work so well if vel doesn't aim at dest)
50 {
51 pos = dest; // "stick" to the dest. if other behavior is desired, override this function, but make sure to call this function and then do your custom stuff
52 dest = ZERO_VECTOR;
53 velocity = ZERO_VECTOR;
54 dest = ZERO_VECTOR;
55 }
56 }
57 }
58
59 CFieldSprite* CFieldSprite::getClone(int x, int y)
60 {
61 CFieldSprite* clone = new CFieldSprite(*this, x, y);
62 return clone;
63 }
64
65 CFieldSprite::CFieldSprite(CSurface *s, int x, int y,
66 int width, int height, int fieldWidth,
67 int fieldHeight, CVector offset)
68 : CSprite(ZERO_VECTOR, ZERO_VECTOR, s, "static"),
69 boardX(x), boardY(y), offset(offset), dest(ZERO_VECTOR),
70 widthBoard(width), heightBoard(height),
71 fieldWidth(fieldWidth), fieldHeight(fieldHeight),
72 dtype(NA)
73 {
74 setRect(CRect(offset.getX(), offset.getY(), fieldWidth * getWidth()/widthBoard, fieldHeight * getHeight()/heightBoard));
75 moveBoardCords(x, y);
76 }
77
78 CFieldSprite::CFieldSprite(const string& fileName, int x, int y,
79 int width, int height,
80 int fieldWidth, int fieldHeight,
81 CVector offset, bool alpha)
82 : CSprite(ZERO_VECTOR, ZERO_VECTOR, fileName, "static", CRect(0,0,0,0), alpha),
83 boardX(x), boardY(y), offset(offset), dest(ZERO_VECTOR),
84 widthBoard(width), heightBoard(height),
85 fieldWidth(fieldWidth), fieldHeight(fieldHeight),
86 dtype(NA)
87 {
88 setRect(CRect(offset.getX(), offset.getY(), fieldWidth * getWidth()/widthBoard, fieldHeight * getHeight()/heightBoard));
89 moveBoardCords(x, y);
90 }
91
92 CVector CFieldSprite::screenCords(int x, int y)
93 {
94 return (CVector(x * getWidth()/widthBoard, y * getHeight()/heightBoard) + offset);
95 }
96
97 void CFieldSprite::setDest(CVector d)
98 {
99 dest = d;
100 dtype = 0;
101 if (pos.getY() < dest.getY())
102 {
103 dtype = dtype | static_cast<int> (BOTTOM);
104 }
105 else if (pos.getY() > dest.getY())
106 {
107 dtype |= static_cast<int> (TOP);
108 }
109
110 if(pos.getX() < dest.getX())
111 {
112 dtype |= static_cast<int> (RIGHT);
113 }
114 else if (pos.getX() > dest.getX())
115 {
116 dtype |= static_cast<int> (LEFT);
117 }
118 }
119
120 bool CFieldSprite::atDest()
121 {
122 bool valY = false, valX = false;
123 if (velocity.getY() < 0)
124 {
125 if (pos.getY() <= dest.getY())
126 {
127 valY = true;
128 }
129 }
130 else
131 {
132 if (pos.getY() >= dest.getY())
133 {
134 valY = true;
135 }
136 }
137
138 if (velocity.getX() < 0)
139 {
140 if (pos.getX() <= dest.getX())
141 {
142 valX = true;
143 }
144 }
145 else
146 {
147 if (pos.getX() >= dest.getX())
148 {
149 valX = true;
150 }
151 }
152 return (valX && valY);
153 }
154
155 CFieldSprite::CFieldSprite(const CFieldSprite& b, int x, int y)
156 : CSprite(b, true, ZERO_VECTOR, ZERO_VECTOR),
157 boardX(x), boardY(y), offset(b.offset), dest(ZERO_VECTOR),
158 widthBoard(b.widthBoard), heightBoard(b.heightBoard),
159 fieldWidth(b.fieldWidth), fieldHeight(b.fieldHeight),
160 dtype(NA)
161
162 {
163 moveBoardCords(x, y); // have to do this _after_ CSprite(...) because the width and height need to be setup
164 }
165
166 CFieldSprite::CFieldSprite(const CFieldSprite& b, int x, int y, CVector offset)
167 : CSprite(b, true, ZERO_VECTOR, ZERO_VECTOR),
168 boardX(x), boardY(y), offset(offset), dest(ZERO_VECTOR),
169 widthBoard(b.widthBoard), heightBoard(b.heightBoard),
170 fieldWidth(b.fieldWidth), fieldHeight(b.fieldHeight),
171 dtype(NA)
172 {
173 moveBoardCords(x, y); // have to do this _after_ CSprite(...) because the width and height need to be setup
174 }
175
176 CFieldSprite::CFieldSprite(const CFieldSprite& b, int x, int y, int fieldWidth, int fieldHeight, CVector offset)
177 : CSprite(b, true, ZERO_VECTOR, ZERO_VECTOR),
178 boardX(x), boardY(y), offset(offset), dest(ZERO_VECTOR),
179 widthBoard(b.widthBoard), heightBoard(b.heightBoard),
180 fieldWidth(fieldWidth), fieldHeight(fieldHeight), dtype(NA)
181 {
182 moveBoardCords(x, y); // have to do this _after_ CSprite(...) because the width and height need to be setup
183 }