summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/input_system.cpp4
-rw-r--r--src/main.cpp2
-rw-r--r--src/message_system.h2
-rw-r--r--src/script_system.cpp65
-rw-r--r--src/script_system.h32
-rw-r--r--src/system.h3
6 files changed, 106 insertions, 2 deletions
diff --git a/src/input_system.cpp b/src/input_system.cpp index b70b377..6ddb59e 100644 --- a/src/input_system.cpp +++ b/src/input_system.cpp
@@ -2,6 +2,7 @@
2#include "game.h" 2#include "game.h"
3#include "character_system.h" 3#include "character_system.h"
4#include "message_system.h" 4#include "message_system.h"
5#include "script_system.h"
5 6
6struct Input { 7struct Input {
7 bool left = false; 8 bool left = false;
@@ -32,9 +33,10 @@ void InputSystem::tick(double dt) {
32 } else { 33 } else {
33 game_.getSystem<MessageSystem>().displayCutsceneBars(); 34 game_.getSystem<MessageSystem>().displayCutsceneBars();
34 }*/ 35 }*/
35 game_.getSystem<MessageSystem>().displayMessage("Some people always try to avoid fighting when there are enemies around. You know the type, right? They use the dash ability to zoom right by. I guess you could say they're followers of \"peace at any price\".", "Sparrow", SpeakerType::Woman); 36 //game_.getSystem<MessageSystem>().displayMessage("Some people always try to avoid fighting when there are enemies around. You know the type, right? They use the dash ability to zoom right by. I guess you could say they're followers of \"peace at any price\".", "Sparrow", SpeakerType::Woman);
36 //game_.getSystem<MessageSystem>().displayMessage("Lucas. You're awful at hide-and-seek, you know that? Try harder.", "Kumatora", SpeakerType::Woman); 37 //game_.getSystem<MessageSystem>().displayMessage("Lucas. You're awful at hide-and-seek, you know that? Try harder.", "Kumatora", SpeakerType::Woman);
37 //game_.getSystem<MessageSystem>().displayMessage("Hi Tooth! I hope you're having a good day.", "Lucas", SpeakerType::Boy); 38 //game_.getSystem<MessageSystem>().displayMessage("Hi Tooth! I hope you're having a good day.", "Lucas", SpeakerType::Boy);
39 game_.getSystem<ScriptSystem>().runScript("script0001");
38 } else if (e.key.keysym.sym == SDLK_b) { 40 } else if (e.key.keysym.sym == SDLK_b) {
39 // TODO: Remove this, it's just for testing. 41 // TODO: Remove this, it's just for testing.
40 game_.getSystem<MessageSystem>().advanceText(); 42 game_.getSystem<MessageSystem>().advanceText();
diff --git a/src/main.cpp b/src/main.cpp index 935fc78..8f23881 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -10,6 +10,7 @@
10#include "character_system.h" 10#include "character_system.h"
11#include "input_system.h" 11#include "input_system.h"
12#include "message_system.h" 12#include "message_system.h"
13#include "script_system.h"
13 14
14void loop(Renderer& renderer) { 15void loop(Renderer& renderer) {
15 Game game(renderer); 16 Game game(renderer);
@@ -19,6 +20,7 @@ void loop(Renderer& renderer) {
19 game.emplaceSystem<AnimationSystem>(); 20 game.emplaceSystem<AnimationSystem>();
20 game.emplaceSystem<CameraSystem>(); 21 game.emplaceSystem<CameraSystem>();
21 game.emplaceSystem<MessageSystem>(); 22 game.emplaceSystem<MessageSystem>();
23 game.emplaceSystem<ScriptSystem>();
22 24
23 auto map = std::make_unique<Map>("../res/map1.tmx", renderer); 25 auto map = std::make_unique<Map>("../res/map1.tmx", renderer);
24 game.setMap(std::move(map)); 26 game.setMap(std::move(map));
diff --git a/src/message_system.h b/src/message_system.h index 93ca087..85ecd65 100644 --- a/src/message_system.h +++ b/src/message_system.h
@@ -51,6 +51,8 @@ public:
51 51
52 const std::list<MessageLine>& getLines() const { return linesToShow_; } 52 const std::list<MessageLine>& getLines() const { return linesToShow_; }
53 53
54 bool isMessageActive() const { return !linesToShow_.empty(); }
55
54 const std::string& getSpeaker() const { return speakerName_; } 56 const std::string& getSpeaker() const { return speakerName_; }
55 57
56 bool isNextArrowShowing() const { return showNextArrow_; } 58 bool isNextArrowShowing() const { return showNextArrow_; }
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
6ScriptSystem::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
27void 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
42void 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}
diff --git a/src/script_system.h b/src/script_system.h new file mode 100644 index 0000000..2832576 --- /dev/null +++ b/src/script_system.h
@@ -0,0 +1,32 @@
1#ifndef SCRIPT_SYSTEM_H_FD8A95B3
2#define SCRIPT_SYSTEM_H_FD8A95B3
3
4#include <sol/sol.hpp>
5#include <memory>
6#include <set>
7#include <string>
8#include "system.h"
9
10class Game;
11
12class ScriptSystem : public System {
13public:
14
15 static constexpr SystemKey Key = SystemKey::Script;
16
17 explicit ScriptSystem(Game& game);
18
19 void tick(double dt) override;
20
21 void runScript(std::string name);
22
23private:
24
25 Game& game_;
26 sol::state engine_;
27 std::unique_ptr<sol::thread> runner_;
28 std::unique_ptr<sol::coroutine> callable_;
29 std::set<std::string> loadedScripts_;
30};
31
32#endif /* end of include guard: SCRIPT_SYSTEM_H_FD8A95B3 */
diff --git a/src/system.h b/src/system.h index 298c5f8..218dc60 100644 --- a/src/system.h +++ b/src/system.h
@@ -7,7 +7,8 @@ enum class SystemKey {
7 Character, 7 Character,
8 Animation, 8 Animation,
9 Camera, 9 Camera,
10 Message 10 Message,
11 Script
11}; 12};
12 13
13class System { 14class System {