changeset 225 | 22ecb9cb9245 |
parent 223 | 2fcaf54ed37b |
child 228 | dbc1bb7a98b5 |
224:e6faefba2ec1 | 225:22ecb9cb9245 |
---|---|
2 #include "PhysicsObject.hh" |
2 #include "PhysicsObject.hh" |
3 #include "Engine.hh" |
3 #include "Engine.hh" |
4 |
4 |
5 #include <cmath> |
5 #include <cmath> |
6 |
6 |
7 PhysicsObject::PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity) : |
7 PhysicsObject::PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity, bool enabled) : |
8 world(world), position(position), velocity(velocity), mass(mass), inAir(true), aim(0), facingRight(true), alive(true) |
8 world(world), position(position), velocity(velocity), mass(mass), inAir(true), aim(0), facingRight(true), alive(false), shouldDelete(false) |
9 { |
9 { |
10 world.addPhysicsObject(this); |
10 if (enabled) |
11 enable(); |
|
11 } |
12 } |
12 |
13 |
13 PhysicsObject::~PhysicsObject (void) { |
14 PhysicsObject::~PhysicsObject (void) { |
14 // Engine::log(DEBUG, "PhysicsObject.destructor") << this /* << ": objects.size=" << ((int) world.objects.size()) */; |
15 // Engine::log(DEBUG, "PhysicsObject.destructor") << this /* << ": objects.size=" << ((int) world.objects.size()) */; |
15 } |
16 } |
16 |
17 |
17 /** |
18 /** |
18 * Player walks on floor. |
19 * Player walks on floor. |
19 */ |
20 */ |
294 this->velocity = velocity; |
295 this->velocity = velocity; |
295 this->inAir = inAir; |
296 this->inAir = inAir; |
296 this->facingRight = facingRight; |
297 this->facingRight = facingRight; |
297 this->aim = aim; |
298 this->aim = aim; |
298 } |
299 } |
299 |
300 |
300 Vector PhysicsObject::getPosition () { |
301 Vector PhysicsObject::getPosition () { |
301 return this->position; |
302 return this->position; |
302 } |
303 } |
303 |
304 |
304 Vector PhysicsObject::getVelocity () { |
305 Vector PhysicsObject::getVelocity () { |
323 |
324 |
324 void PhysicsObject::tick (TimeMS tick_length) { |
325 void PhysicsObject::tick (TimeMS tick_length) { |
325 this->updatePosition(tick_length); |
326 this->updatePosition(tick_length); |
326 } |
327 } |
327 |
328 |
329 void PhysicsObject::enable (void) { |
|
330 alive = true; |
|
331 world.addPhysicsObject(this); |
|
332 } |
|
333 |
|
334 void PhysicsObject::disable (void) { |
|
335 alive = false; |
|
336 } |
|
337 |
|
328 void PhysicsObject::destroy (void) { |
338 void PhysicsObject::destroy (void) { |
329 alive = false; |
339 alive = false; |
340 shouldDelete = true; |
|
330 } |
341 } |
331 |
342 |
332 bool PhysicsObject::isDestroyed (void) { |
343 bool PhysicsObject::isDestroyed (void) { |
333 return !alive; |
344 return !alive; |
334 } |
345 } |
335 |
346 |
336 bool PhysicsObject::removeIfDestroyed (void) { |
347 bool PhysicsObject::removeIfDestroyed (void) { |
337 if (!alive) { |
348 if (!alive) { |
338 delete this; |
349 if (shouldDelete) |
350 delete this; |
|
351 |
|
339 return true; |
352 return true; |
353 |
|
340 } else { |
354 } else { |
341 return false; |
355 return false; |
342 } |
356 } |
343 } |
357 } |
344 |
358 |