diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2017-10-21 19:56:48 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2017-10-21 19:56:48 -0400 |
commit | b64aa31b4dc0c5e050c4978bae7bd43a040a368b (patch) | |
tree | 4c16f9d1eb3bac11f74508c241e76cfddabc7a19 | |
parent | 7644b740e87053838f3c7a80e88ad192fcf1a5e2 (diff) |
lua scriptable world gen
-rw-r--r-- | include/systems/light.hpp | 3 | ||||
-rw-r--r-- | include/world.hpp | 1 | ||||
-rw-r--r-- | src/world.cpp | 39 | ||||
-rw-r--r-- | xml/!town.xml | 25 | ||||
-rw-r--r-- | xml/wavy.xml | 28 |
5 files changed, 94 insertions, 2 deletions
diff --git a/include/systems/light.hpp b/include/systems/light.hpp index 71c541a..ba91113 100644 --- a/include/systems/light.hpp +++ b/include/systems/light.hpp @@ -29,6 +29,9 @@ public: static int addLight(vec2 pos, float radius, Color color = Color(1, 1, 1)); static void updateLight(int index, vec2 pos, float radius = -1); static void removeLight(int index); + + static inline void clear(void) + { lights.clear(); } }; #endif // SYSTEM_LIGHT_HPP_ diff --git a/include/world.hpp b/include/world.hpp index 059efa3..991990e 100644 --- a/include/world.hpp +++ b/include/world.hpp @@ -159,6 +159,7 @@ public: static void goWorldRight(Position& p, Solid &d); static void goWorldPortal(Position& p); + static void generate(LuaScript& script); static void generate(int width = 0); static bool save(void); diff --git a/src/world.cpp b/src/world.cpp index ea2566a..f55b39d 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -27,6 +27,7 @@ using namespace tinyxml2; #include <ui.hpp> #include <vector3.hpp> #include <weather.hpp> +#include <systems/lua.hpp> WorldData2 WorldSystem::world; Mix_Music* WorldSystem::bgmObj; @@ -68,6 +69,34 @@ int WorldSystem::getLineIndex(float x) 0, static_cast<int>(world.data.size())); } +void WorldSystem::generate(LuaScript& script) +{ + int i = 0; + world.data.clear(); + do { + float h, g[2]; + script("ground", {LuaVariable("height", h)}); + if (h == -1.0f) + break; + script("grass", {LuaVariable("height", g[0])}); + script("grass", {LuaVariable("height", g[1])}); + world.data.push_back(WorldData {true, {g[0], g[1]}, h, + static_cast<unsigned char>(randGet() % 32 / 8)}); + } while (++i); + + // define x-coordinate of world's leftmost 'line' + world.startX = HLINES(i * -0.5); + + // gen. star coordinates + if (stars.empty()) { + stars.resize(game::SCREEN_WIDTH / 30); + for (auto& s : stars) { + s.x = world.startX + (randGet() % (int)HLINES(i)); + s.y = game::SCREEN_HEIGHT - (randGet() % (int)HLINES(game::SCREEN_HEIGHT / 1.3f)); + } + } +} + void WorldSystem::generate(int width) { float geninc = 0; @@ -191,6 +220,8 @@ void WorldSystem::loader(void) if (!currentXMLFile.empty()) save(); + LightSystem::clear(); + // load file data to string auto xmlPath = game::config::xmlFolder + toLoad; auto xmlRaw = readFile(xmlPath); @@ -277,7 +308,13 @@ void WorldSystem::loader(void) // world generation else if (tagName == "generation") { - generate(wxml->IntAttribute("width")); + auto text = wxml->GetText(); + if (text == nullptr) + generate(wxml->IntAttribute("width")); + else { + LuaScript script (text); + generate(script); + } } // indoor stuff diff --git a/xml/!town.xml b/xml/!town.xml index 29673b0..26a62a1 100644 --- a/xml/!town.xml +++ b/xml/!town.xml @@ -12,7 +12,30 @@ <layer path="bg/dirt.png"/> <layer path="bg/grass.png"/> </style> - <generation width="320"/> + <generation> + x = 0 + + ground = function() + if (x == 330) then + height = -1 + else + if (x < 10) then + height = 800 + else + if (x < 30) then + height = -100 * math.tan(0.08 * (x - 10) + 1.77) + 75 + else + height = 60 + end + end + end + x = x + 1 + end + + grass = function() + height = math.random(2, 7) + end + </generation> <weather>Sunny</weather> <link right="!town2.xml"/> <spawnx>-300</spawnx> diff --git a/xml/wavy.xml b/xml/wavy.xml new file mode 100644 index 0000000..3f0c187 --- /dev/null +++ b/xml/wavy.xml @@ -0,0 +1,28 @@ +<?xml version="1.0"?> + +<World> + <style bgm="assets/music/town.ogg" folder="assets/style/winter/"> + <layer path="bg/bg.png"/> + <layer path="bg/bgFarMountain.png"/> + <layer path="bg/dirt.png"/> + <layer path="bg/grass.png"/> + </style> + <generation> + x = 0 + ground = function() + if (x == 600) then + height = -1 + else + height = math.sin(x * 3.14 / 90) * 50 + 60 + end + x = x + 1 + end + + grass = function() + height = math.random(2, 4) + end + </generation> + <weather>Rainy</weather> + <time>10000</time> +</World> + |