forked from phisko/kengine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPySystem.hpp
More file actions
63 lines (53 loc) · 2.1 KB
/
Copy pathPySystem.hpp
File metadata and controls
63 lines (53 loc) · 2.1 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
#pragma once
#pragma once
#include "ScriptSystem.hpp"
#include "data/LuaStateComponent.hpp"
#include "data/PythonStateComponent.hpp"
#include "python.hpp"
#include "EntityCreator.hpp"
namespace kengine {
EntityCreatorFunctor<64> PySystem(EntityManager & em);
namespace python {
template<typename Ret, typename ...Args>
void registerEntityMember(py::class_<Entity> & entity, const char * name, const ScriptSystem::function<Ret(Args...)> & func) {
if constexpr (std::is_reference_v<Ret>)
entity.def(name, func, py::return_value_policy::reference);
else
entity.def(name, func);
}
template<typename T>
void registerTypeWithState(PythonStateComponent::Data & state) {
if constexpr (std::is_same<T, Entity>()) {
state.entity = new py::class_<Entity>(state.module, putils::reflection::get_class_name<Entity>(), py::dynamic_attr());
putils::reflection::for_each_attribute<Entity>([&](auto name, auto member) {
state.entity->def_readwrite(name, member);
});
putils::reflection::for_each_method<Entity>([&](auto name, auto member) {
state.entity->def(name, member);
});
}
else
putils::python::registerType<T>(state.module);
ScriptSystem::registerComponent<T>([&](auto && ... args) {
registerEntityMember(*state.entity, FWD(args)...);
});
}
template<typename T>
void registerType(EntityManager & em) {
for (const auto & [e, comp] : em.getEntities<PythonStateComponent>())
registerTypeWithState<T>(*comp.data);
}
template<typename Ret, typename ...Args>
void registerFunctionWithState(PythonStateComponent::Data & state, const char * name, const ScriptSystem::function<Ret(Args...)> & func) {
if constexpr (std::is_reference_v<Ret>)
state.module.def(name, func, py::return_value_policy::reference);
else
state.module.def(name, func);
}
template<typename Ret, typename ...Args>
void registerFunction(EntityManager & em, const char * name, const ScriptSystem::function<Ret(Args...)> & func) {
for (const auto & [e, comp] : em.getEntities<PythonStateComponent>())
registerFunctionWithState(*comp.data, name, func);
}
}
}