forked from phisko/kengine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptSystem.hpp
More file actions
81 lines (68 loc) · 2.01 KB
/
Copy pathScriptSystem.hpp
File metadata and controls
81 lines (68 loc) · 2.01 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
#pragma once
#include "EntityManager.hpp"
#include "string.hpp"
#include "with.hpp"
#include "function.hpp"
#ifndef KENGINE_SCRIPT_SYSTEM_MAX_FUNCTION_SIZE
# define KENGINE_SCRIPT_SYSTEM_MAX_FUNCTION_SIZE 64
#endif
namespace kengine::ScriptSystem {
template<typename Func>
using function = putils::function<Func, KENGINE_SCRIPT_SYSTEM_MAX_FUNCTION_SIZE>;
template<typename Func, typename Func2>
void init(EntityManager & em, Func && registerFunction, Func2 && registerType) {
registerFunction("createEntity",
function<Entity(const function<void(Entity &)> &)>(
[&](const function<void(Entity &)> & f) {
return em.createEntity(FWD(f));
}
)
);
registerFunction("removeEntity",
function<void(Entity &)>(
[&](Entity & go) { em.removeEntity(go); }
)
);
registerFunction("removeEntityById",
function<void(Entity::ID id)>(
[&](Entity::ID id) { em.removeEntity(id); }
)
);
registerFunction("getEntity",
function<Entity(Entity::ID id)>(
[&](Entity::ID id) { return em.getEntity(id); }
)
);
registerFunction("stopRunning",
function<void()>(
[&] { em.running = false; }
)
);
registerType(putils::meta::type<Entity>{});
}
template<typename T, typename Func>
void registerComponent(Func && registerEntityMember) {
static_assert(putils::reflection::has_class_name<T>());
const auto className = putils::reflection::get_class_name<T>();
registerEntityMember(putils::string<128>("get%s", className),
function<T & (Entity &)>(
[](Entity & self) { return std::ref(self.get<T>()); }
)
);
registerEntityMember(putils::string<128>("has%s", className),
function<bool(Entity &)>(
[](Entity & self) { return self.has<T>(); }
)
);
registerEntityMember(putils::string<128>("attach%s", className),
function<T & (Entity &)>(
[](Entity & self) { return std::ref(self.attach<T>()); }
)
);
registerEntityMember(putils::string<128>("detach%s", className),
function<void(Entity &)>(
[](Entity & self) { self.detach<T>(); }
)
);
}
}