summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--res/maps/map1.tmx2
-rw-r--r--res/maps/map2.tmx2
-rw-r--r--res/scripts/common.lua2
-rw-r--r--res/scripts/global.lua (renamed from res/scripts/default.lua)4
-rw-r--r--res/scripts/map1.lua (renamed from res/scripts/script0001.lua)16
-rw-r--r--res/scripts/map1_off_right.lua3
-rw-r--r--res/scripts/map2.lua (renamed from res/scripts/map2_off_left.lua)4
-rw-r--r--res/scripts/test_trigger.lua7
-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
15 files changed, 44 insertions, 35 deletions
diff --git a/res/maps/map1.tmx b/res/maps/map1.tmx index 8afb909..e1a9049 100644 --- a/res/maps/map1.tmx +++ b/res/maps/map1.tmx
@@ -781,7 +781,7 @@
781 </object> 781 </object>
782 <object id="8" name="moveToRight" type="trigger" x="761" y="359.667" width="18" height="60.3333"> 782 <object id="8" name="moveToRight" type="trigger" x="761" y="359.667" width="18" height="60.3333">
783 <properties> 783 <properties>
784 <property name="script" value="map1_off_right"/> 784 <property name="script" value="off_right"/>
785 </properties> 785 </properties>
786 </object> 786 </object>
787 <object id="9" name="fromRight" type="warp" x="748.667" y="382.667"> 787 <object id="9" name="fromRight" type="warp" x="748.667" y="382.667">
diff --git a/res/maps/map2.tmx b/res/maps/map2.tmx index c1d2f2d..43fa850 100644 --- a/res/maps/map2.tmx +++ b/res/maps/map2.tmx
@@ -642,7 +642,7 @@
642 </object> 642 </object>
643 <object id="2" name="moveToLeft" type="trigger" x="-20.3333" y="304" width="22.6667" height="91.6667"> 643 <object id="2" name="moveToLeft" type="trigger" x="-20.3333" y="304" width="22.6667" height="91.6667">
644 <properties> 644 <properties>
645 <property name="script" value="map2_off_left"/> 645 <property name="script" value="off_left"/>
646 </properties> 646 </properties>
647 </object> 647 </object>
648 </objectgroup> 648 </objectgroup>
diff --git a/res/scripts/common.lua b/res/scripts/common.lua index 2023e63..3c412c6 100644 --- a/res/scripts/common.lua +++ b/res/scripts/common.lua
@@ -112,7 +112,7 @@ function ChangeMap(map, warp)
112 112
113 playerSprite.controllable = false 113 playerSprite.controllable = false
114 FadeToBlack(150) 114 FadeToBlack(150)
115 loadMap("../res/maps/" .. map .. ".tmx", warp, direction) 115 loadMap(map, warp, direction)
116 116
117 local newPlayerId = getPlayerSprite() 117 local newPlayerId = getPlayerSprite()
118 local newPlayerSprite = getSprite(newPlayerId) 118 local newPlayerSprite = getSprite(newPlayerId)
diff --git a/res/scripts/default.lua b/res/scripts/global.lua index b8f7768..48084c8 100644 --- a/res/scripts/default.lua +++ b/res/scripts/global.lua
@@ -1,4 +1,6 @@
1function default() 1global = {}
2
3function global.no_problem_here()
2 StartCutscene() 4 StartCutscene()
3 DisplayMessage("* No problem here.", "", SpeakerType.NONE) 5 DisplayMessage("* No problem here.", "", SpeakerType.NONE)
4 HideCutsceneBars() 6 HideCutsceneBars()
diff --git a/res/scripts/script0001.lua b/res/scripts/map1.lua index bf6a0c2..7d6014a 100644 --- a/res/scripts/script0001.lua +++ b/res/scripts/map1.lua
@@ -1,4 +1,6 @@
1function script0001() 1map1 = {}
2
3function map1.script0001()
2 StartCutscene() 4 StartCutscene()
3 SetAnimation("boney", "barking") 5 SetAnimation("boney", "barking")
4 local barkingNoise = LoopSound("barking_at_hallucination.wav") 6 local barkingNoise = LoopSound("barking_at_hallucination.wav")
@@ -22,3 +24,15 @@ function script0001()
22 SetAnimation("flint", "still") 24 SetAnimation("flint", "still")
23 HideCutsceneBars() 25 HideCutsceneBars()
24end 26end
27
28function map1.off_right()
29 ChangeMap("map2", "fromLeft")
30end
31
32function map1.test_trigger()
33 StartCutscene()
34 PlaySound("boney_growl.wav")
35 DisplayMessage("* Hi! Welcome to the funky zone.", "", SpeakerType.NONE)
36 WaitForEndOfMessage()
37 HideCutsceneBars()
38end
diff --git a/res/scripts/map1_off_right.lua b/res/scripts/map1_off_right.lua deleted file mode 100644 index 7dd0113..0000000 --- a/res/scripts/map1_off_right.lua +++ /dev/null
@@ -1,3 +0,0 @@
1function map1_off_right()
2 ChangeMap("map2", "fromLeft")
3end
diff --git a/res/scripts/map2_off_left.lua b/res/scripts/map2.lua index d4d9ce4..1f14b31 100644 --- a/res/scripts/map2_off_left.lua +++ b/res/scripts/map2.lua
@@ -1,3 +1,5 @@
1function map2_off_left() 1map2 = {}
2
3function map2.off_left()
2 ChangeMap("map1", "fromRight") 4 ChangeMap("map1", "fromRight")
3end 5end
diff --git a/res/scripts/test_trigger.lua b/res/scripts/test_trigger.lua deleted file mode 100644 index 4d9d06c..0000000 --- a/res/scripts/test_trigger.lua +++ /dev/null
@@ -1,7 +0,0 @@
1function test_trigger()
2 StartCutscene()
3 PlaySound("boney_growl.wav")
4 DisplayMessage("* Hi! Welcome to the funky zone.", "", SpeakerType.NONE)
5 WaitForEndOfMessage()
6 HideCutsceneBars()
7end
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