-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.cpp
107 lines (86 loc) · 2.27 KB
/
main.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
///
/// [EntityFu](https://github.com/NatWeiss/EntityFu)
/// A simple, fast entity component system written in C++.
/// Under the MIT license.
///
#include "EntityFu.h"
#include <iostream>
#ifdef _WIN32
#include <windows.h>
#define _sleep(ms) Sleep(ms)
#else
#include <unistd.h>
#define _sleep(ms) usleep(ms * 1000)
#endif
using namespace std;
/// An example component.
struct HealthComponent : Entity::Component
{
int hp, maxHP;
HealthComponent(int _hp, int _maxHP)
{
hp = _hp;
maxHP = _maxHP;
}
HealthComponent() : HealthComponent(0, 0) {}
virtual bool empty() const {return maxHP == 0;}
static Cid cid;
};
/// Assign unique class IDs from 0 to `numClassIds` - 1
static Cid _id = 0;
Cid HealthComponent::cid = _id++;
Cid Entity::Component::numCids = _id; // also critical to assign `numClassIds`
/// An example `Ent`.
struct System::Ent
{
Eid id;
HealthComponent& health;
/// Add more components your systems will use frequently
Ent(Eid _id) :
health(Entity::get<HealthComponent>(_id)),
id(_id)
{}
};
/// An example system.
struct HealthSystem : System
{
static void tick(double fixedDelta)
{
// create a copy of the vector<Eid> for all health components so we
// don't cause an assertion failure: "vector iterator not incrementable"
// for more speed you can instead loop over a reference as long as you schedule
// Entity::destroyNow for after the loop
auto all = Entity::getAll<HealthComponent>();
// for this example, just decrement all health components each tick
for (auto eid : all)
{
Ent e(eid);
// this is overly pragmatic, but you get the drift of how to check if a component is valid
if (e.health.empty())
continue;
// decrement
e.health.hp--;
if (e.health.hp < 0)
e.health.hp = 0;
cout << "Entity " << (int)e.id << " has " << e.health.hp << "/" << e.health.maxHP << " hit points." << endl;
// destroy entity if zero health
if (e.health.hp <= 0)
Entity::destroyNow(eid);
}
}
};
int main(int argc, const char * argv[])
{
// create some entities
Entity::create(new HealthComponent(100, 100));
Entity::create(new HealthComponent(7, 7));
// run the system
while (Entity::count())
{
HealthSystem::tick(0.1);
_sleep(100);
}
Entity::dealloc();
cout << "Goodbye, World!\n";
return 0;
}