diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-05 20:20:21 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-05 20:20:21 -0500 |
commit | bd1a66887f00e186ab86a5195ebb3271ea732b38 (patch) | |
tree | d3fc0335d98f386877b991f538e2645ff6d74fd1 /src/script_system.cpp | |
parent | ccf0fab9f7b8057afc5884e70fff29109f707bf8 (diff) | |
download | tanetane-bd1a66887f00e186ab86a5195ebb3271ea732b38.tar.gz tanetane-bd1a66887f00e186ab86a5195ebb3271ea732b38.tar.bz2 tanetane-bd1a66887f00e186ab86a5195ebb3271ea732b38.zip |
Created script system
Diffstat (limited to 'src/script_system.cpp')
-rw-r--r-- | src/script_system.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/script_system.cpp b/src/script_system.cpp new file mode 100644 index 0000000..7f4729b --- /dev/null +++ b/src/script_system.cpp | |||
@@ -0,0 +1,65 @@ | |||
1 | #include "script_system.h" | ||
2 | #include <iostream> | ||
3 | #include "game.h" | ||
4 | #include "message_system.h" | ||
5 | |||
6 | ScriptSystem::ScriptSystem(Game& game) : game_(game) { | ||
7 | engine_.open_libraries( | ||
8 | sol::lib::base, | ||
9 | sol::lib::coroutine, | ||
10 | sol::lib::math); | ||
11 | |||
12 | engine_.new_usertype<MessageSystem>( | ||
13 | "message", | ||
14 | "displayMessage", &MessageSystem::displayMessage, | ||
15 | "hideCutsceneBars", &MessageSystem::hideCutsceneBars, | ||
16 | "isMessageActive", sol::property(&MessageSystem::isMessageActive)); | ||
17 | |||
18 | engine_.set_function( | ||
19 | "message", | ||
20 | [&] () -> MessageSystem& { | ||
21 | return game_.getSystem<MessageSystem>(); | ||
22 | }); | ||
23 | |||
24 | engine_.script_file("../res/scripts/common.lua"); | ||
25 | } | ||
26 | |||
27 | void ScriptSystem::tick(double dt) { | ||
28 | if (callable_ && *callable_) { | ||
29 | auto result = (*callable_)(dt); | ||
30 | if (!result.valid()) { | ||
31 | sol::error e = result; | ||
32 | throw std::runtime_error(e.what()); | ||
33 | } | ||
34 | |||
35 | if (!*callable_) { | ||
36 | callable_.reset(); | ||
37 | runner_.reset(); | ||
38 | } | ||
39 | } | ||
40 | } | ||
41 | |||
42 | void ScriptSystem::runScript(std::string name) { | ||
43 | if (!loadedScripts_.count(name)) { | ||
44 | engine_.script_file("../res/scripts/" + name + ".lua"); | ||
45 | loadedScripts_.insert(name); | ||
46 | } | ||
47 | |||
48 | runner_.reset(new sol::thread(sol::thread::create(engine_.lua_state()))); | ||
49 | callable_.reset(new sol::coroutine(runner_->state().get<sol::function>(name))); | ||
50 | |||
51 | if (!*callable_) { | ||
52 | throw std::runtime_error("Error running script: " + name); | ||
53 | } | ||
54 | |||
55 | auto result = (*callable_)(); | ||
56 | if (!result.valid()) { | ||
57 | sol::error e = result; | ||
58 | throw std::runtime_error(e.what()); | ||
59 | } | ||
60 | |||
61 | if (!*callable_) { | ||
62 | callable_.reset(); | ||
63 | runner_.reset(); | ||
64 | } | ||
65 | } | ||