forked from phisko/kengine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaSystem.hpp
More file actions
224 lines (190 loc) · 7.72 KB
/
Copy pathLuaSystem.hpp
File metadata and controls
224 lines (190 loc) · 7.72 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#pragma once
#include "System.hpp"
#include "lua/plua.hpp"
#include "reflection/Reflectible.hpp"
#include "EntityManager.hpp"
#include "common/components/LuaComponent.hpp"
namespace kengine
{
class LuaSystem : public kengine::System<LuaSystem>
{
public:
LuaSystem(kengine::EntityManager &em) : _em(em)
{
try { addScriptDirectory("scripts"); }
catch (const std::runtime_error &e) {}
_lua.open_libraries();
_lua.set_function("getGameObjects", [&em] { return std::ref(em.getGameObjects()); });
_lua.set_function("createEntity",
[&em] (const std::string &type, const std::string &name, const sol::function &f)
{ return std::ref(em.createEntity(type, name, f)); }
);
_lua.set_function("removeEntity",
[&em] (const std::string &name)
{ em.removeEntity(name); }
);
_lua.set_function("getEntity",
[&em] (std::string_view name)
{ return std::ref(em.getEntity(name)); }
);
_lua.set_function("hasEntity",
[&em] (std::string_view name)
{ return em.hasEntity(name); }
);
_lua.set_function("getDeltaTime", [this] { return time.getDeltaTime(); });
_lua.set_function("getFixedDeltaTime", [this] { return time.getFixedDeltaTime(); });
_lua.set_function("getDeltaFrames", [this] { return time.getDeltaFrames(); });
_lua.set_function("stopRunning", [&em] { em.running = false; });
registerType<kengine::GameObject>();
}
template<typename T>
void registerType() noexcept
{
putils::lua::registerType<T>(_lua);
const auto sender = putils::concat("send", T::get_class_name());
_lua.set_function(sender, [this](const T &packet) { send(packet); });
if constexpr (kengine::is_component<T>::value)
registerComponent<T>();
}
template<typename ...Types>
void registerTypes() noexcept
{
pmeta::tuple_for_each(std::make_tuple(pmeta::type<Types>()...),
[this](auto &&t)
{
using Type = pmeta_wrapped(t);
registerType<Type>();
}
);
}
public:
sol::state &getState() { return _lua; }
const sol::state &getState() const { return _lua; }
public:
void addScriptDirectory(std::string_view dir)
{
try
{
putils::Directory d(dir);
_directories.push_back(dir.data());
}
catch (const std::runtime_error &e)
{
std::cerr << e.what() << std::endl;
}
}
// System methods
public:
void execute() final
{
executeDirectories();
executeScriptedObjects();
}
// Helpers
private:
template<typename T>
void registerComponent() noexcept
{
_lua[putils::concat("getGameObjectsWith", T::get_class_name())] =
[this] { return std::ref(_em.getGameObjects<T>()); };
_lua[kengine::GameObject::get_class_name()][putils::concat("get", T::get_class_name())] =
[](kengine::GameObject &self) { return std::ref(self.getComponent<T>()); };
_lua[kengine::GameObject::get_class_name()][putils::concat("has", T::get_class_name())] =
[](kengine::GameObject &self) { return self.hasComponent<T>(); };
_lua[kengine::GameObject::get_class_name()][putils::concat("attach", T::get_class_name())] =
[](kengine::GameObject &self) { return std::ref(self.attachComponent<T>()); };
_lua[kengine::GameObject::get_class_name()][putils::concat("detach", T::get_class_name())] =
[](kengine::GameObject &self) { self.detachComponent<T>(); };
}
template<typename F>
void addEntityFunction(const std::string &name, F &&func) noexcept
{
_lua[kengine::GameObject::get_class_name()][name] = func;
}
void executeDirectories() noexcept
{
for (const auto &dir : _directories)
{
try
{
putils::Directory d(dir);
d.for_each(
[this](const putils::Directory::File &f)
{
if (!f.isDirectory)
executeScript(f.fullPath);
}
);
}
catch (const std::runtime_error &e)
{
std::cerr << e.what() << std::endl;
}
}
}
void executeScriptedObjects() noexcept
{
struct Create
{
std::string type;
std::string name;
std::function<void(kengine::GameObject &)> postCreate;
};
std::vector<std::function<void()>> toExecute;
std::vector<std::string> toRemove;
_lua["createEntity"] =
[this, &toExecute] (const std::string &type, const std::string &name, const sol::function &f)
{
toExecute.push_back([this, type, name, f]{ _em.createEntity(type, name, f); });
};
_lua["removeEntity"] =
[this, &toExecute, &toRemove](const std::string &name)
{
toExecute.push_back([this, name]{ _em.removeEntity(name); });
toRemove.push_back(name);
};
_lua["hasEntity"] =
[this, &toExecute, &toRemove](const std::string &name)
{
return _em.hasEntity(name) &&
std::find(toRemove.begin(), toRemove.end(), name) == toRemove.end();
};
for (const auto go : _em.getGameObjects<kengine::LuaComponent>())
{
const auto &comp = go->getComponent<kengine::LuaComponent>();
for (const auto &s : comp.getScripts())
{
_lua["self"] = go;
executeScript(s);
}
}
_lua["self"] = sol::nil;
for (const auto &f : toExecute)
f();
_lua["createEntity"] =
[this] (const std::string &type, const std::string &name, const sol::function &f)
{ return std::ref(_em.createEntity(type, name, f)); };
_lua["removeEntity"] =
[this] (const std::string &name)
{ _em.removeEntity(name); };
_lua["hasEntity"] =
[this, &toExecute](const std::string &name)
{ return _em.hasEntity(name); };
}
void executeScript(std::string_view fileName) noexcept
{
try
{
_lua.script_file(fileName.data());
}
catch (const std::exception &e)
{
std::cerr << "[LuaSystem] Error in '" << fileName << "': " << e.what() << std::endl;
}
}
private:
kengine::EntityManager &_em;
std::vector<std::string> _directories;
sol::state _lua;
};
}