author | ekku |
Sat, 06 Dec 2008 17:51:19 +0000 | |
changeset 222 | 293ddf4c067d |
parent 221 | fbc5db6fce45 |
child 223 | 2fcaf54ed37b |
permissions | -rw-r--r-- |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
1 |
|
197 | 2 |
#include "PhysicsObject.hh" |
3 |
#include "Engine.hh" |
|
4 |
||
5 |
#include <cmath> |
|
6 |
||
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
7 |
PhysicsObject::PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity) : |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
8 |
world(world), position(position), velocity(velocity), mass(mass), inAir(true), aim(0), facingRight(true), alive(true) |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
9 |
{ |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
10 |
world.addPhysicsObject(this); |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
11 |
} |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
12 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
13 |
PhysicsObject::~PhysicsObject (void) { |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
14 |
Engine::log(DEBUG, "PhysicsObject.destructor") << this /* << ": objects.size=" << ((int) world.objects.size()) */; |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
15 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
16 |
// world.objects.remove(this); |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
17 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
18 |
Engine::log(DEBUG, "PhysicsObject.destructor") << this /* << ": objects.size=" << ((int) world.objects.size()) */; |
197 | 19 |
} |
20 |
||
21 |
/** |
|
22 |
* Player walks on floor. |
|
23 |
*/ |
|
24 |
Vector PhysicsObject::walk_one_step (float partial, bool right) { |
|
25 |
// which way we are walking |
|
26 |
float deltaX = right ? partial : -partial; |
|
27 |
Vector reached = this->position; |
|
28 |
if(reached.roundToInt() == (reached+Vector(deltaX, 0)).roundToInt()) { |
|
29 |
return reached+Vector(deltaX, 0); |
|
30 |
} |
|
31 |
// Is there upward ramp |
|
32 |
if(!possibleLocation(position+Vector(deltaX, 0))) { |
|
33 |
// Yes. Then we check n pixels up |
|
34 |
for(int i = 1; i < 3; i++) { |
|
35 |
if(possibleLocation(position+Vector(deltaX, -i))) { |
|
36 |
// and when there is finally EMPTY, we can walk |
|
37 |
reached = position+Vector(deltaX, -i); |
|
38 |
break; |
|
39 |
} |
|
40 |
} |
|
41 |
} else { |
|
42 |
// Or downward ramp or flat |
|
43 |
for(int i = 0; 1; i++) { |
|
44 |
||
45 |
// And when there is finally ground we can step on |
|
46 |
// it. If there is no gound we still step there, |
|
47 |
// but will fall one pixel down |
|
48 |
if(possibleLocation(position+Vector(deltaX, i))) { |
|
49 |
reached = position+Vector(deltaX, i); |
|
50 |
} else { |
|
51 |
break; |
|
52 |
} |
|
53 |
||
54 |
// If the fall is big enough, set the worm in the air |
|
55 |
if (i >= 2) { |
|
56 |
// Vector back = walk(dt, !right); |
|
57 |
this->inAir = true; |
|
58 |
// this->velocity.x = right ? velocity : -velocity; |
|
59 |
// Avoid stepping two pixels down when it starts to free fall |
|
60 |
reached.y -= 2; |
|
61 |
// this->velocity = (reached-back)*1000/dt; |
|
62 |
break; |
|
63 |
} |
|
64 |
} |
|
65 |
} |
|
66 |
// And we return where we got |
|
67 |
return reached; |
|
68 |
} |
|
69 |
void PhysicsObject::walk (TimeMS dt, bool right) { |
|
70 |
float velocity = PLAYER_WALK_SPEED; |
|
71 |
float walkAmount = (velocity*dt)/1000; |
|
72 |
Vector reached = this->position; |
|
221 | 73 |
while (walkAmount > 0 && !this->inAir) { |
197 | 74 |
this->position = walk_one_step((1 < walkAmount ? 1 : walkAmount), right); |
75 |
walkAmount--; |
|
76 |
} |
|
77 |
// TODO: Should the remaining walkAmount be handled somehow? |
|
78 |
} |
|
79 |
||
80 |
/** |
|
81 |
* Makes the player jump in the air. |
|
82 |
* @param direction -1: jump left, 0: jump up, 1: jump right |
|
83 |
*/ |
|
84 |
void PhysicsObject::jump (int direction) { |
|
85 |
// Jump only if player is "on the ground" |
|
86 |
if (!this->inAir) { |
|
87 |
velocity.y = -100; |
|
88 |
switch (direction) { |
|
89 |
case 1: |
|
90 |
velocity.x += 20; |
|
91 |
break; |
|
92 |
case -1: |
|
93 |
velocity.x -= 20; |
|
94 |
break; |
|
95 |
case 0: |
|
96 |
break; |
|
97 |
default: |
|
98 |
throw std::logic_error("Invalid jump direction"); |
|
99 |
} |
|
100 |
inAir = true; |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
bool PhysicsObject::possibleLocation (Vector loc) { |
|
105 |
for(unsigned int i = 0; i < this->shape.size(); i++) { |
|
106 |
if(world.collides(loc+shape[i])) |
|
107 |
return false; |
|
108 |
} |
|
109 |
return true; |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* Updates object speed and position. This function organises force |
|
114 |
* integration and collision detection. |
|
115 |
*/ |
|
205 | 116 |
void PhysicsObject::updatePosition (TimeMS dt) { |
197 | 117 |
// Add gravity to the force queue |
118 |
forceq.push(world.gravity); |
|
119 |
||
120 |
// Go trough every force in the queue |
|
121 |
Force total; |
|
122 |
while (!forceq.empty()) { |
|
123 |
total += forceq.front(); |
|
124 |
forceq.pop(); |
|
125 |
} |
|
126 |
||
127 |
// If the player has stopped and there's some ground under some of the 3 some of the 3t |
|
128 |
// set inAir false |
|
129 |
if (this->velocity == Vector(0,0)) { |
|
130 |
this->inAir = !world.collides(this->position+shape[1]+Vector(0, 1)) |
|
131 |
&& !world.collides(this->position+shape[2]+Vector(0, 1)) |
|
132 |
&& !world.collides(this->position+shape[3]+Vector(0, 1)); |
|
133 |
// If, however, there's a force caused by a bomb, e.g., set it in air. |
|
134 |
// Still, we have to be able to separate forces caused by walking attempts |
|
135 |
// and bombs etc (+0.1 because float comparison can be dangerous) |
|
136 |
if (total.y < 0 || abs(total.x) > PLAYER_MOVE_FORCE + 0.1) |
|
137 |
this->inAir = true; |
|
138 |
} |
|
139 |
||
140 |
if(!possibleLocation(position)) { |
|
141 |
//if we are trapped in ground form dirtball or something |
|
142 |
//we might want to just return and set velocity to some value |
|
143 |
//return; |
|
144 |
} |
|
145 |
||
146 |
// If the worm is not in the air make it walk, |
|
147 |
// otherwise integrate the new position and velocity |
|
148 |
if (!this->inAir) { |
|
149 |
//std::cout << "Tryin to walk" << std::endl; |
|
150 |
// It walks only if there's some vertical force |
|
151 |
if (total.x != 0) { |
|
152 |
std::cout << "Succeeding to walk" << std::endl; |
|
205 | 153 |
walk(dt, total.x > 0); |
197 | 154 |
this->velocity = Vector(0,0); |
155 |
} |
|
156 |
} |
|
157 |
||
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
158 |
if (!possibleLocation(position)) |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
159 |
Engine::log(DEBUG, "PhysicsObject.updatePosition") << "impossible location: " << position; |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
160 |
|
197 | 161 |
Vector newPosition; |
162 |
Vector velAfterTick; |
|
163 |
// Calculate new position and velocity to the given references |
|
205 | 164 |
integrate(total, dt, newPosition, velAfterTick); |
197 | 165 |
this->velocity = velAfterTick; |
166 |
||
167 |
||
168 |
// Collision detection |
|
169 |
bool collided = false; |
|
170 |
||
171 |
const Vector diffVec = newPosition-position; |
|
172 |
const Vector unitVector = diffVec / diffVec.length(); |
|
173 |
Vector reached = position; |
|
174 |
||
175 |
while ((position-reached).sqrLength() < diffVec.sqrLength()) { |
|
176 |
reached += unitVector; |
|
177 |
// Check if any of the shapes points collide |
|
178 |
for (uint64_t i = 0; i < shape.size(); i++) { |
|
179 |
if (world.collides(reached+shape[i])) { // Collision |
|
180 |
if (inAir) { |
|
181 |
// Engine::log(DEBUG, "Here"); |
|
182 |
this->bounce(world.getNormal(reached+shape[i], |
|
183 |
reached-unitVector+shape[i])); |
|
184 |
//this->velocity *= COLLISION_ELASTICITY; |
|
185 |
} |
|
186 |
reached = reached - unitVector; // Return to last point |
|
187 |
collided = true; |
|
188 |
if (this->velocity.sqrLength() < PLAYER_MIN_SPEED * PLAYER_MIN_SPEED) { |
|
189 |
this->velocity = Vector(0,0); |
|
190 |
} |
|
191 |
break; |
|
192 |
} |
|
193 |
} |
|
194 |
if (collided) |
|
195 |
break; |
|
196 |
// reached += unitVector; |
|
197 |
} |
|
198 |
||
199 |
||
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
200 |
if (!possibleLocation(reached)) |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
201 |
Engine::log(DEBUG, "PhysicsObject.updatePosition") << "impossible location: " << position << ", diffVec=" << diffVec; |
197 | 202 |
|
203 |
// In case of some float error check the final coordinate |
|
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
204 |
if (!collided) { |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
205 |
if (!possibleLocation(newPosition)) { |
197 | 206 |
newPosition = reached; |
207 |
} else { |
|
208 |
// This means everything was ok, so no need to do anything |
|
209 |
} |
|
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
210 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
211 |
this->position = newPosition; |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
212 |
|
197 | 213 |
} else { |
214 |
newPosition = reached; |
|
211
d5d52fb191e4
some minor fixes, shots now explode in collisionpoint
nireco
parents:
210
diff
changeset
|
215 |
this->position = newPosition; |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
216 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
217 |
// the following may delete this object, so it must be the last thing called |
197 | 218 |
onCollision(); |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
219 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
220 |
return; |
197 | 221 |
} |
222 |
} |
|
223 |
||
224 |
/** |
|
225 |
* Bounces from straight wall in any direction. |
|
226 |
* Direction given as normal of that wall |
|
227 |
*/ |
|
228 |
void PhysicsObject::bounce (Vector normal) { |
|
229 |
// normal.sqrLength can't be 0 when got from getNormal() |
|
230 |
if (normal.sqrLength() != 0) { |
|
231 |
Vector nvel = velocity; |
|
232 |
// We project the velocity on normal and remove twice that much from velocity |
|
233 |
nvel = nvel - ((2)*((nvel*normal)/(normal*normal))*normal); |
|
234 |
velocity = nvel; |
|
235 |
// We lose some of our speed on collision |
|
236 |
this->velocity *= this->collision_elasticity; |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* Integrates given force over time and stores new position to |
|
242 |
* posAfterTick and new velocity to velAfterTick. |
|
243 |
* @param force Force vector. |
|
244 |
* @param dt The time the force is applied (<=PHYSICS_TICK_MS) |
|
245 |
*/ |
|
246 |
void PhysicsObject::integrate(Force force, TimeMS dt, Vector &posAfterTick, Vector &velAfterTick) { |
|
247 |
posAfterTick = position; |
|
248 |
velAfterTick = velocity; |
|
249 |
Derivative tmpd; |
|
250 |
Derivative k1 = evaluate(force, 0, tmpd, posAfterTick, velAfterTick); |
|
251 |
Derivative k2 = evaluate(force, 0.5f*dt, k1, posAfterTick, velAfterTick); |
|
252 |
Derivative k3 = evaluate(force, 0.5f*dt, k2, posAfterTick, velAfterTick); |
|
253 |
Derivative k4 = evaluate(force, dt, k3, posAfterTick, velAfterTick); |
|
254 |
||
255 |
||
256 |
const Vector dxdt = (k1.dx + (k2.dx + k3.dx) * 2.0f + k4.dx) * 1.0f/6.0f; |
|
257 |
const Vector dvdt = (k1.dv + (k2.dv + k3.dv) * 2.0f + k4.dv) * 1.0f/6.0f; |
|
258 |
||
259 |
// Engine::log(DEBUG, "PhysicsObject.integrate") << "Changes: "<< dxdt << " " << dvdt << " Time: " <<dt; |
|
260 |
posAfterTick = posAfterTick + (dxdt * dt)/1000; |
|
261 |
velAfterTick = velAfterTick + (dvdt * dt)/1000; |
|
262 |
//Engine::log(DEBUG, "PhysicsObject.integrate") << "velAfterTick: " << velAfterTick; |
|
263 |
} |
|
264 |
||
265 |
Derivative PhysicsObject::evaluate(Force force, TimeMS dt, Derivative &d, const Vector &posAfterTick, const Vector &velAfterTick) { |
|
266 |
Vector curPos = posAfterTick + (d.dx*dt)/1000; |
|
267 |
Vector curVel = velAfterTick + (d.dv*dt)/1000; |
|
268 |
||
269 |
Derivative out; |
|
270 |
out.dx = curVel; |
|
271 |
out.dv = acceleration(force); |
|
272 |
//Engine::log(DEBUG, "PhysicsObject.evaluate") << "Out.dx: " << out.dx; |
|
273 |
return out; |
|
274 |
} |
|
275 |
||
276 |
Vector PhysicsObject::acceleration(const Force &force) { |
|
277 |
return (force/mass); |
|
278 |
} |
|
279 |
||
280 |
void PhysicsObject::applyForce (Force force) { |
|
281 |
// Add applied force to the queue |
|
282 |
forceq.push(force); |
|
283 |
} |
|
284 |
||
285 |
void PhysicsObject::changeAim(float da) { |
|
286 |
this->aim += da; |
|
287 |
||
288 |
if (this->aim > PLAYER_AIM_MAX) this->aim = PLAYER_AIM_MAX; |
|
289 |
if (this->aim < PLAYER_AIM_MIN) this->aim = PLAYER_AIM_MIN; |
|
290 |
//Engine::log(DEBUG, "PhysicsObject.changeAim") << "Player aim: " << this->aim; |
|
291 |
} |
|
292 |
||
293 |
void PhysicsObject::setFacing(bool facingRight) { |
|
294 |
//Engine::log(DEBUG, "PhysicsObject.setFacing") << "Facing: " << right; |
|
295 |
this->facingRight = facingRight; |
|
296 |
} |
|
297 |
||
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
197
diff
changeset
|
298 |
void PhysicsObject::updatePhysics (Vector position, Vector velocity, bool inAir, bool facingRight, float aim) { |
197 | 299 |
this->position = position; |
300 |
this->velocity = velocity; |
|
301 |
this->inAir = inAir; |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
197
diff
changeset
|
302 |
this->facingRight = facingRight; |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
197
diff
changeset
|
303 |
this->aim = aim; |
197 | 304 |
} |
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
197
diff
changeset
|
305 |
|
197 | 306 |
Vector PhysicsObject::getPosition () { |
307 |
return this->position; |
|
308 |
} |
|
309 |
||
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
310 |
Vector PhysicsObject::getVelocity () { |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
311 |
return this->velocity; |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
312 |
} |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
313 |
|
197 | 314 |
bool PhysicsObject::getFacing() { |
315 |
return this->facingRight; |
|
316 |
} |
|
317 |
||
318 |
float PhysicsObject::getAim() { |
|
319 |
return this->aim; |
|
320 |
} |
|
321 |
||
322 |
std::vector<Vector>& PhysicsObject::getShape () { |
|
323 |
return this->shape; |
|
324 |
} |
|
325 |
||
326 |
void PhysicsObject::setShape (std::vector<Vector> shape) { |
|
327 |
this->shape = shape; |
|
328 |
} |
|
329 |
||
205 | 330 |
void PhysicsObject::tick (TimeMS tick_length) { |
331 |
this->updatePosition(tick_length); |
|
197 | 332 |
} |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
333 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
334 |
void PhysicsObject::destroy (void) { |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
335 |
alive = false; |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
336 |
} |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
337 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
338 |
bool PhysicsObject::isDestroyed (void) { |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
339 |
return !alive; |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
340 |
} |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
341 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
342 |
bool PhysicsObject::removeIfDestroyed (void) { |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
343 |
if (!alive) { |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
344 |
delete this; |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
345 |
return true; |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
346 |
} else { |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
347 |
return false; |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
348 |
} |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
349 |
} |
197 | 350 |
|
351 |
void PhysicsObject::draw(CL_GraphicContext *gc) { |
|
352 |
CL_Quad player( |
|
353 |
(position+shape[0]).x, (position+shape[0]).y, |
|
354 |
(position+shape[1]).x, (position+shape[1]).y, |
|
355 |
(position+shape[2]).x, (position+shape[2]).y, |
|
356 |
(position+shape[3]).x, (position+shape[3]).y |
|
357 |
); |
|
358 |
||
359 |
gc->fill_quad(player, CL_Color::green); |
|
360 |
||
361 |
const uint16_t chlen = 10; |
|
362 |
uint16_t x = player.center().x; |
|
363 |
uint16_t y = player.center().y; |
|
364 |
if (facingRight) { |
|
365 |
gc->draw_line(x, y, |
|
366 |
x + std::cos(aim)*chlen, |
|
367 |
y - std::sin(aim)*chlen, |
|
368 |
CL_Color::black); |
|
369 |
} else { |
|
370 |
gc->draw_line(x, y, |
|
371 |
x - std::cos(aim)*chlen, |
|
372 |
y - std::sin(aim)*chlen, |
|
373 |
CL_Color::black); |
|
374 |
} |
|
375 |
} |
|
376 |