-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
81 lines (67 loc) · 2.33 KB
/
main.cpp
File metadata and controls
81 lines (67 loc) · 2.33 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
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
#include <iostream>
#include "sim/Simulation.h"
#include "sim/lib/components/Sprite.h"
#include "sim/lib/components/Transform.h"
#include "sim/lib/systems/Interactor.h"
#include "sim/lib/systems/Movement.h"
#include "sim/lib/systems/Renderer.h"
#include "sim/lib/systems/World.h"
using namespace sim;
using namespace sim::lib;
struct TestSystem {
void operator()(const event::Cycle, Context ctx) const {
ctx.view<Transform, Movable>().for_each([](const Entity e, auto& t, auto& m) {
std::cout << "Entity #" << e.id() << " at (" << t.x << ", " << t.y << ") with speed " << m.speed <<
std::endl;
});
}
};
int main() {
struct Grass {};
struct Sheep {};
struct Wolf {};
auto s = Simulation<
Movement, WorldBoundary,
TargetResolver<Sheep, Grass, Wolf>,
TouchableTargets<Sheep, Grass>,
Renderer
>();
Sprite grass(Color{60, 240, 80});
Sprite sheep(Color{220, 210, 193});
Sprite wolf{};
constexpr int grass_count = 2000;
constexpr int sheep_count = 100;
constexpr int wolves = 5;
constexpr uint SEED = 42;
std::mt19937 rng{SEED};
std::uniform_int_distribution rand_coord{0, 1000};
std::uniform_int_distribution rand_speed{1, 3};
// Grass
for (int i = 0; i < grass_count; ++i)
s.create()
.emplace<Grass>()
.emplace<Transform>(rand_coord(rng), rand_coord(rng))
.emplace<Sprite>(grass);
// Sheep
for (int i = 0; i < sheep_count; ++i)
s.create()
.emplace<Sheep>()
.emplace<Transform>(rand_coord(rng), rand_coord(rng)).emplace<Movable>(rand_speed(rng))
.emplace<Target>()
.emplace<FollowClosest<Grass> >()
.emplace<AvoidClosest<Wolf> >()
.emplace<DestroyByTouch<Grass> >()
.emplace<Sprite>(sheep);
// Wolves
for (int i = 0; i < wolves; ++i)
s.create()
.emplace<Wolf>()
.emplace<Transform>(rand_coord(rng), rand_coord(rng)).emplace<Movable>(rand_speed(rng))
.emplace<Target>()
.emplace<FollowClosest<Sheep> >()
.emplace<DestroyByTouch<Sheep> >()
.push_back(wolf);
s.run(10000);
std::cout << "Done" << std::endl;
return 0;
}