-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cc
168 lines (135 loc) · 4.01 KB
/
test.cc
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <string>
#include <vector>
#include <array>
#include <random>
#include <cmath>
#include <cassert>
#include "util/util.hh"
#include "util/benchmark.hh"
#include "entity_space.hh"
#include "system.hh"
using namespace util::io;
struct Vec2// : public BasicComponent
{
using Number = float;
using Array = std::array<Number, 2>;
union { Array v; struct { Number x, y; }; };
Vec2(Array a) : v(a) {}
Vec2(Number _x, Number _y) : v {{_x, _y}} {}
Vec2() : v {{0, 0}} {}
using InputType = Array;
};
#define EXTEND_Vec2(DerivedName) \
struct DerivedName : public Vec2 \
{ \
static std::string name() { return #DerivedName; } \
std::string to_string() const \
{ \
std::stringstream out; \
print_to(out, #DerivedName "(", x, ", ", y, ")"); \
return out.str(); \
} \
\
using Vec2::Vec2; \
}
EXTEND_Vec2(Pos);
EXTEND_Vec2(Vel);
EXTEND_Vec2(Accel);
struct Energy// : public BasicComponent
{
float e;
std::string to_string() const
{
return util::text("E(", e, ")");
}
};
struct Mass
{
float m;
std::string to_string() const
{
return util::text("M(", m, ")");
}
};
struct Motion : public Logic<Pos, Vel>
{
void operate(Pos& p, Vel& v)
{
// Logic<Pos, Vel>::operate(p, v);
// println("Motion operating on (", p, ", ", v, ")");
p.x += v.x;
p.y += v.y;
}
};
struct Motion2 : public Logic<Vel, Accel>
{
void operate(Vel& v, Accel& a)
{
// println("Motion operating on (", p, ", ", v, ")");
v.x += a.x;
v.y += a.y;
}
};
struct KineticEnergy : public Logic<Vel, Mass, Energy>
{
void operate(Vel& v, Mass& m, Energy& e)
{
e.e += sqrt(v.x * v.x + v.y * v.y) * m.m;
}
};
int main(int argc, char* argv[]) {
int n_updates = argc > 1 ? std::stoi(argv[1]) : 10,
n_entities = argc > 2 ? std::stoi(argv[2]) : 1;
using Space = EntitySpace<Pos, Vel, Accel, Mass, Energy>;
using System = System<Space, Motion2, Motion, KineticEnergy>;
std::default_random_engine rng;
std::uniform_int_distribution<int> di(-100, 100);
std::uniform_real_distribution<float> df(0, 1);
std::uniform_real_distribution<float> dfn(-1, 1);
System sys;
std::vector<EntityID> ents;
for (int i = 0; i < n_entities; ++i) {
// ents.push_back(sys.template create_entity<Pos>());
// ents.push_back(sys.create_entity(Pos (1,1)));
// ents.push_back(sys.template create_entity<Vel>());
// va.push_back(sys.create_entity(Vel()));
// pv.push_back(sys.template create_entity<Pos, Vel>());
// ents.push_back(sys.create_entity(
// Pos(),
// Vel(1,-1)));
// Vel(1,-1)));
ents.push_back(sys.create_entity(
Pos(),
Vel(),
Accel(dfn(rng), dfn(rng)),
Energy {df(rng)},
Mass {df(rng)}
));
}
// for (auto ent : v) println(sys.space[ent]);
auto show = [&] {
for (auto& ent : ents)
println(ent, ": ", sys.space[ent]);
};
// println(" initial:");
// show();
auto time = util::benchmark([&] {
for (int i = 0; i < n_updates; ++i)
sys.update();
});
println(format("Performed %d updates on %d entities in %s seconds",
n_updates, n_entities, double(time)/1000));
// println(text(" after ", n_updates, " updates:"));
// show();
// game.enqueue(
// CSpace::Spawn<RandomInput>(1),
// CSpace::Spawn<Pos>(n_ent, )
// );
// TextUI ui;
// game.attach_UI(ui);
// // 100 "clock ticks"
// // want this to spawn its own thread.
// game.go(100);
// ui.show();
return 0;
}