-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathexample.cpp
More file actions
55 lines (48 loc) · 1.15 KB
/
example.cpp
File metadata and controls
55 lines (48 loc) · 1.15 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
54
55
#include "entity/World.hpp"
#include <iostream>
using namespace entity;
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.dx;
position.y += velocity.dy;
}
}
};
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;
}