summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/character_system.cpp4
-rw-r--r--src/input_system.cpp6
-rw-r--r--src/main.cpp2
-rw-r--r--src/map.cpp7
-rw-r--r--src/map.h6
-rw-r--r--src/script_system.cpp12
-rw-r--r--src/script_system.h2
7 files changed, 20 insertions, 19 deletions
diff --git a/src/character_system.cpp b/src/character_system.cpp index aad8c1b..a0572b0 100644 --- a/src/character_system.cpp +++ b/src/character_system.cpp
@@ -92,7 +92,7 @@ void CharacterSystem::tick(double dt) {
92 Sprite& collider = game_.getSprite(collision.horiz.colliderSprite); 92 Sprite& collider = game_.getSprite(collision.horiz.colliderSprite);
93 93
94 if (collider.walkthroughScript != "") { 94 if (collider.walkthroughScript != "") {
95 game_.getSystem<ScriptSystem>().runScript(collider.walkthroughScript); 95 game_.getSystem<ScriptSystem>().runScript(game_.getMap().getName(), collider.walkthroughScript);
96 } 96 }
97 } 97 }
98 98
@@ -102,7 +102,7 @@ void CharacterSystem::tick(double dt) {
102 Sprite& collider = game_.getSprite(collision.vert.colliderSprite); 102 Sprite& collider = game_.getSprite(collision.vert.colliderSprite);
103 103
104 if (collider.walkthroughScript != "") { 104 if (collider.walkthroughScript != "") {
105 game_.getSystem<ScriptSystem>().runScript(collider.walkthroughScript); 105 game_.getSystem<ScriptSystem>().runScript(game_.getMap().getName(), collider.walkthroughScript);
106 } 106 }
107 } 107 }
108 108
diff --git a/src/input_system.cpp b/src/input_system.cpp index 6ee442a..2201907 100644 --- a/src/input_system.cpp +++ b/src/input_system.cpp
@@ -54,7 +54,7 @@ void InputSystem::tick(double dt) {
54 54
55 Sprite& collider = game_.getSprite(collision.horiz.colliderSprite); 55 Sprite& collider = game_.getSprite(collision.horiz.colliderSprite);
56 if (collider.interactionScript != "") { 56 if (collider.interactionScript != "") {
57 game_.getSystem<ScriptSystem>().runScript(collider.interactionScript); 57 game_.getSystem<ScriptSystem>().runScript(game_.getMap().getName(), collider.interactionScript);
58 activated = true; 58 activated = true;
59 } 59 }
60 } else if (collision.vert.colliderSprite != -1) { 60 } else if (collision.vert.colliderSprite != -1) {
@@ -62,7 +62,7 @@ void InputSystem::tick(double dt) {
62 62
63 Sprite& collider = game_.getSprite(collision.vert.colliderSprite); 63 Sprite& collider = game_.getSprite(collision.vert.colliderSprite);
64 if (collider.interactionScript != "") { 64 if (collider.interactionScript != "") {
65 game_.getSystem<ScriptSystem>().runScript(collider.interactionScript); 65 game_.getSystem<ScriptSystem>().runScript(game_.getMap().getName(), collider.interactionScript);
66 activated = true; 66 activated = true;
67 } 67 }
68 } 68 }
@@ -70,7 +70,7 @@ void InputSystem::tick(double dt) {
70 } 70 }
71 71
72 if (inFrontOfSomething && !activated) { 72 if (inFrontOfSomething && !activated) {
73 game_.getSystem<ScriptSystem>().runScript("default"); 73 game_.getSystem<ScriptSystem>().runScript("global", "no_problem_here");
74 } 74 }
75 } 75 }
76 } else if (e.key.keysym.sym == SDLK_LEFT) { 76 } else if (e.key.keysym.sym == SDLK_LEFT) {
diff --git a/src/main.cpp b/src/main.cpp index 387f7e3..c9bfaf1 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -22,7 +22,7 @@ void loop(Renderer& renderer) {
22 game.emplaceSystem<CameraSystem>(); 22 game.emplaceSystem<CameraSystem>();
23 game.emplaceSystem<MessageSystem>(); 23 game.emplaceSystem<MessageSystem>();
24 24
25 game.loadMap("../res/maps/map1.tmx", "spawn", Direction::down); 25 game.loadMap("map1", "spawn", Direction::down);
26 game.getSprite(game.getSpriteByAlias("lucas")).controllable = true; 26 game.getSprite(game.getSpriteByAlias("lucas")).controllable = true;
27 27
28 renderer.render(game); 28 renderer.render(game);
diff --git a/src/map.cpp b/src/map.cpp index 99711c6..8d4aa2d 100644 --- a/src/map.cpp +++ b/src/map.cpp
@@ -5,10 +5,11 @@
5#include <tmxlite/TileLayer.hpp> 5#include <tmxlite/TileLayer.hpp>
6#include <tmxlite/Tileset.hpp> 6#include <tmxlite/Tileset.hpp>
7 7
8Map::Map(std::string_view filename) : filename_(filename) { 8Map::Map(std::string_view name) : name_(name) {
9 std::string filename = "../res/maps/" + std::string(name) + ".tmx";
9 tmx::Map mapfile; 10 tmx::Map mapfile;
10 if (!mapfile.load(filename.data())) { 11 if (!mapfile.load(filename.c_str())) {
11 throw std::invalid_argument("Could not find map file: " + std::string(filename)); 12 throw std::invalid_argument("Could not find map file: " + filename);
12 } 13 }
13 14
14 const tmx::Vector2u& mapSize = mapfile.getTileCount(); 15 const tmx::Vector2u& mapSize = mapfile.getTileCount();
diff --git a/src/map.h b/src/map.h index adf9b80..2d448e4 100644 --- a/src/map.h +++ b/src/map.h
@@ -35,9 +35,9 @@ struct Trigger {
35class Map { 35class Map {
36public: 36public:
37 37
38 explicit Map(std::string_view filename); 38 explicit Map(std::string_view name);
39 39
40 const std::string& getName() const { return filename_; } 40 const std::string& getName() const { return name_; }
41 41
42 const vec2i& getMapSize() const { return mapSize_; } 42 const vec2i& getMapSize() const { return mapSize_; }
43 43
@@ -61,7 +61,7 @@ public:
61 61
62private: 62private:
63 63
64 std::string filename_; 64 std::string name_;
65 vec2i mapSize_; 65 vec2i mapSize_;
66 vec2i tileSize_; 66 vec2i tileSize_;
67 std::vector<std::vector<Tile>> layers_; 67 std::vector<std::vector<Tile>> layers_;
diff --git a/src/script_system.cpp b/src/script_system.cpp index 21d4090..5b9b987 100644 --- a/src/script_system.cpp +++ b/src/script_system.cpp
@@ -120,17 +120,17 @@ void ScriptSystem::tick(double dt) {
120 } 120 }
121} 121}
122 122
123void ScriptSystem::runScript(std::string name) { 123void ScriptSystem::runScript(std::string mapName, std::string scriptName) {
124 if (!loadedScripts_.count(name)) { 124 if (!loadedScripts_.count(mapName)) {
125 engine_.script_file("../res/scripts/" + name + ".lua"); 125 engine_.script_file("../res/scripts/" + mapName + ".lua");
126 loadedScripts_.insert(name); 126 loadedScripts_.insert(mapName);
127 } 127 }
128 128
129 runner_.reset(new sol::thread(sol::thread::create(engine_.lua_state()))); 129 runner_.reset(new sol::thread(sol::thread::create(engine_.lua_state())));
130 callable_.reset(new sol::coroutine(runner_->state().get<sol::function>(name))); 130 callable_.reset(new sol::coroutine(runner_->state().traverse_get<sol::function>(mapName, scriptName)));
131 131
132 if (!*callable_) { 132 if (!*callable_) {
133 throw std::runtime_error("Error running script: " + name); 133 throw std::runtime_error("Error running script: " + mapName + "." + scriptName);
134 } 134 }
135 135
136 auto result = (*callable_)(); 136 auto result = (*callable_)();
diff --git a/src/script_system.h b/src/script_system.h index 2832576..9c901b7 100644 --- a/src/script_system.h +++ b/src/script_system.h
@@ -18,7 +18,7 @@ public:
18 18
19 void tick(double dt) override; 19 void tick(double dt) override;
20 20
21 void runScript(std::string name); 21 void runScript(std::string mapName, std::string scriptName);
22 22
23private: 23private:
24 24