-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexample.cpp
115 lines (103 loc) · 3.29 KB
/
example.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
106
107
108
109
110
111
112
113
114
115
/// --------------------------------------------------------------------------
/// Copyright (C) 2015 Robin Grönberg
///
/// This program is free software: you can redistribute it and/or modify
/// it under the terms of the GNU General Public License as published by
/// the Free Software Foundation, either version 3 of the License, or
/// (at your option) any later version.
///
/// This program is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <iostream>
#include "ecs.h"
#include "ecs.h"
using namespace ecs;
//Components as properties, we skip defining a constructor, since we
//don't do anything unusual.
struct Health : Property<int>{};
struct Mana : Property<int>{};
struct Name : Property<std::string>{};
struct Spellcaster : public EntityAlias<Name, Health, Mana>{
//We don't need to define a constructor since all of
//our components only have 1 member.
bool isOom(){
return get<Mana>() == 0;// <- override == operator
}
bool isAlive(){
return get<Health>() > 0;
}
void castSpell(Spellcaster& target){
if(!isOom()){
--get<Mana>();
--target.get<Health>();
}
}
};
class RemoveCorpsesSystem : public System{
public:
void update(float time) override {
//Method 1
//Any health entity
entities().with([](Health& health, Entity entity){
if(health <= 0){
entity.destroy();
}
});
//Method 2
//Any spellcaster that is dead
entities().fetch_every([](Spellcaster& spellcaster){
if(!spellcaster.isAlive()){
spellcaster.destroy();
}
});
}
};
class CastSpellSystem : public System{
public:
void update(float time) override {
entities().fetch_every([&] (Spellcaster& spellcaster1){
entities().fetch_every([&] (Spellcaster& spellcaster2){
if(spellcaster1 != spellcaster2){
spellcaster1.castSpell(spellcaster2);
}
});
});
}
};
class GiveManaSystem : public System{
public:
void update(float time) override {
entities().fetch_every([] (Spellcaster& spellcaster){
if(spellcaster.isOom()) spellcaster.set<Mana>(1337);
});
}
};
class Game{
public:
Game() : systems(entities) {}
void run(){
systems.add<CastSpellSystem>();
systems.add<GiveManaSystem>();
systems.add<RemoveCorpsesSystem>();
entities.create<Spellcaster>("Alice", 8, 12);
entities.create<Spellcaster>("Bob", 12, 8);
while(entities.count() > 1) systems.update(1);
entities.with([] (Name& name, Health& health, Mana& mana){
std::cout << name << " won!" << std::endl;
std::cout << "Health: " << health << std::endl;
std::cout << "Mana: " << mana << std::endl;
});
}
private:
EntityManager entities;
SystemManager systems;
};
int main(){
Game game;
game.run();
}