-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys_physics.cpp
56 lines (41 loc) · 1.3 KB
/
sys_physics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "sys_physics.h"
#include "point.h"
#include "vector.h"
#include "entity_map.h"
#include "comp_keys.h"
#include <boost/variant.hpp>
using namespace cse;
namespace {
// This takes two variants and if
// one is a position and the other is a vector,
// updates the position with the vector as a velocity.
class UpdatePosition
: public boost::static_visitor<void> {
public:
template<typename T, typename U>
void operator()( const T& t, const U& u ) const {
assert( !"This function is only valid for position vs vector!" );
}
void operator()( Point2D& p, const Vector2D& v ) const
{
p.x += v.x;
p.y += v.y;
}
};
} // namespace {
void cse::PhysicsSystem( EntityMap& em ) {
auto updatePos = UpdatePosition();
for( auto & e : em ) {
auto & comps = e.second;
// If the entity has components for position and vector, then
// update the position component.
auto pos = comps.find(Position);
if ( pos == comps.end() )
continue;
auto vel = comps.find(Velocity);
if ( vel == comps.end() )
continue;
// The actual updating is performed
boost::apply_visitor( updatePos, pos->second, vel->second );
}
}