-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathexample.cpp
More file actions
53 lines (46 loc) · 1.23 KB
/
example.cpp
File metadata and controls
53 lines (46 loc) · 1.23 KB
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
#include "Mix/World.hpp"
#include <iostream>
using namespace Mix;
struct PositionComponent
{
PositionComponent(int x = 0, int y = 0) : x(x), y(y) {}
int x, y;
};
struct VelocityComponent
{
VelocityComponent(int dx = 0, int dy = 0) : dx(dx), dy(dy) {}
int dx, dy;
};
class MoveSystem : public System
{
public:
MoveSystem()
{
requireComponent<PositionComponent>();
requireComponent<VelocityComponent>();
}
void update()
{
for (auto e : getEntities()) {
auto &position = e.getComponent<PositionComponent>();
const auto velocity = e.getComponent<VelocityComponent>();
position.x += velocity.x;
position.y += velocity.y;
}
}
};
int main()
{
World world;
auto e = world.createEntity();
e.addComponent<PositionComponent>(100, 100);
e.addComponent<VelocityComponent>(10, 10);
world.getSystemManager().addSystem<MoveSystem>();
for (int i = 0; i < 10; i++) {
world.update();
world.getSystemManager().getSystem<MoveSystem>().update();
auto &position = e.getComponent<PositionComponent>();
std::cout << "x: " << position.x << ", y: " << position.y << std::endl;
}
return 0;
}