]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
lua scriptable world gen
authorClyne Sullivan <tullivan99@gmail.com>
Sat, 21 Oct 2017 23:56:48 +0000 (19:56 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Sat, 21 Oct 2017 23:56:48 +0000 (19:56 -0400)
include/systems/light.hpp
include/world.hpp
src/world.cpp
xml/!town.xml
xml/wavy.xml [new file with mode: 0644]

index 71c541af2982b33911baa4c23dd604bf0e15b43f..ba91113ec4697c4b7c4af4eb3331ddc2dd5d5cb5 100644 (file)
@@ -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_
index 059efa3d87ed1acf4ecb877c18e92f8d5cb31239..991990e9af047c0a6d3e63bb6e64a3cccdd9e93a 100644 (file)
@@ -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);
index ea2566afebc9c08190e95096e20ae409e661d9f2..f55b39de2f6b77c0b05208ad1e62f35142c5f47d 100644 (file)
@@ -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
index 29673b01314984617d85f95f99e94ba5c4f885a9..26a62a10c95e4d3a8d0263da491c9561915fbb3d 100644 (file)
                <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 &lt; 10) then
+                                       height = 800
+                               else
+                                       if (x &lt; 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 (file)
index 0000000..3f0c187
--- /dev/null
@@ -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>
+