197
|
1 |
|
|
2 |
#include "GameState.hh"
|
|
3 |
#include "PhysicsWorld.hh"
|
|
4 |
#include "Engine.hh"
|
|
5 |
|
|
6 |
PhysicsWorld::PhysicsWorld (Vector gravity, Vector dimensions)
|
|
7 |
: Terrain(1337), tick_timer(PHYSICS_TICK_MS), tick_counter(0), dimensions(dimensions),
|
|
8 |
gravity(gravity) {
|
|
9 |
slots.connect(tick_timer.sig_timer(), this, &PhysicsWorld::tick);
|
|
10 |
tick_timer.enable();
|
|
11 |
}
|
|
12 |
|
|
13 |
void PhysicsWorld::addPlayerObject (PhysicsObject *object) {
|
|
14 |
players.push_back(object);
|
|
15 |
}
|
|
16 |
|
|
17 |
void PhysicsWorld::addProjectile (Shot *projectile) {
|
|
18 |
projectiles.push_back(projectile);
|
|
19 |
}
|
|
20 |
|
|
21 |
/**
|
|
22 |
* Function pointer used to clear the projectile list
|
|
23 |
* from those that have already been destroyd.
|
|
24 |
*/
|
|
25 |
bool isDestroyedProjectile (Shot* po) { return (*po).isDestroyed(); }
|
|
26 |
|
|
27 |
void PhysicsWorld::tick () {
|
|
28 |
// Engine::log(DEBUG, "physics.apply_force") << "*tick*";
|
|
29 |
|
|
30 |
for (std::list<PhysicsObject*>::iterator i = players.begin(); i != players.end(); i++) {
|
|
31 |
(*i)->tick();
|
|
32 |
}
|
|
33 |
|
|
34 |
for (std::list<Shot*>::iterator i = projectiles.begin(); i != projectiles.end(); i++) {
|
|
35 |
(*i)->tick();
|
|
36 |
}
|
|
37 |
|
|
38 |
// Delete destroyed projectiles
|
|
39 |
std::list<Shot*>::iterator new_end = remove_if(projectiles.begin(), projectiles.end(), isDestroyedProjectile);
|
|
40 |
projectiles.erase(new_end, projectiles.end());
|
|
41 |
|
|
42 |
tick_counter++;
|
|
43 |
}
|
|
44 |
|
|
45 |
uint32_t PhysicsWorld::getTick (void) {
|
|
46 |
return tick_counter;
|
|
47 |
}
|
|
48 |
|
|
49 |
void PhysicsWorld::draw(CL_GraphicContext *gc) {
|
|
50 |
Terrain::draw(gc);
|
|
51 |
|
|
52 |
// Draw players
|
|
53 |
for (std::list<PhysicsObject*>::iterator it = players.begin(); it != players.end(); it++) {
|
|
54 |
(*it)->draw(gc);
|
|
55 |
}
|
|
56 |
// Draw projectiles
|
|
57 |
for (std::list<Shot*>::iterator it = projectiles.begin(); it != projectiles.end(); it++) {
|
|
58 |
(*it)->draw(gc);
|
|
59 |
}
|
|
60 |
}
|
|
61 |
|