Engine
Raylib based game framework
Loading...
Searching...
No Matches
LuaManager.h
1#pragma once
2
3#include "Lua/MyLua.h"
4
5#include "entt/entt.h"
6
7#include <optional>
8#include <string>
9#include <unordered_map>
10
12{
13 sol::environment environment;
14 bool enabled = true;
15};
16
18{
19public:
20
21 LuaManager();
22
23 void Update(const float deltaT);
24
25 template <typename Event>
26 void RegisterRecieveEvent(entt::dispatcher& dispatcher)
27 {
28 if (!Lua::TypeExists<Event>(lua))
29 {
30 Event event;
31 event.LuaRegister(lua);
32 }
33
34 dispatcher.sink<Event>().template connect<&LuaManager::OnEvent<Event>>(this);
35 }
36
37 template <typename Event>
38 void RegisterSendEvent(entt::dispatcher& dispatcher)
39 {
40 if (!Lua::TypeExists<Event>(lua))
41 {
42 Event event;
43 event.LuaRegister(lua);
44 };
45
46 std::string functionName = "Trigger" + DemangleWithoutNamespace<Event>() + "Event";
47
48 if (!Lua::FunctionExists(lua, functionName.c_str()))
49 {
50 Lua::RegisterFunction(lua, functionName.c_str(), [this, &dispatcher](Event event)
51 {
52 dispatcher.trigger<Event>(std::move(event));
53 });
54 }
55 }
56
57 bool LoadScript(const char* path);
58 void RemoveScript(const char* path);
59
60 bool IsScriptLoaded(const char* path);
61
62 void EnableScript(const char* path);
63 void DisableScript(const char* path);
64
65 std::optional<sol::environment> GetScriptEnvironment(const char* path);
66
67 void ReloadScripts();
68
69 sol::state lua;
70
71private:
72
73 template <typename Event>
74 void OnEvent(const Event& event)
75 {
76 for (auto& [path, script] : m_scripts)
77 {
78 if (script.enabled)
79 {
80 std::string functionName = "On" + DemangleWithoutNamespace<Event>() + "Event";
81 Lua::CallFunction<false>(script.environment, functionName.c_str(), event);
82 }
83 }
84 }
85
86 std::unordered_map<std::string, LuaScript> m_scripts;
87};
Definition LuaManager.h:18
Definition LuaManager.h:12