]> code.bitgloo.com Git - clyne/game.git/commitdiff
add lua support
authorClyne Sullivan <clyne@bitgloo.com>
Tue, 6 Aug 2024 01:49:23 +0000 (21:49 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Tue, 6 Aug 2024 01:49:23 +0000 (21:49 -0400)
.gitmodules
Makefile
main.cpp
scripts/world.lua [new file with mode: 0644]
sol2 [new submodule]

index 3f53ce8b754aada2f1b5c6d938181994637056c8..4f0db41a967109a079e8d6a134dd61413fdf345b 100644 (file)
@@ -1,3 +1,6 @@
 [submodule "entt"]
        path = entt
        url = https://github.com/skypjack/entt
+[submodule "sol2"]
+       path = sol2
+       url = https://github.com/ThePhD/sol2
index 88cd5d7c92f7332041534487db32160e9331985d..9def519dc124182b4b6c65bbbd769b5d070b7fcf 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 CXXFLAGS := -std=c++23 -O0 -g3 -ggdb \
-            -Iinclude -Ientt/single_include
-LDFLAGS := -lSDL2 -lSDL2_image
+            -Iinclude -Ientt/single_include -Isol2/include `pkg-config --cflags lua5.4`
+LDFLAGS := -lSDL2 -lSDL2_image `pkg-config --libs lua5.4`
 
 OBJS := $(subst .cpp,.o,$(wildcard *.cpp))
 
index c137cc15b2572d425453b1363b3a7b8249581f65..0d3feeb9682a85ab72e9e266ddaf1816f164f51a 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -6,36 +6,66 @@
 #include "window.hpp"
 
 #include <chrono>
+#include <fstream>
 #include <iostream>
 #include <thread>
 
 #include <entt/entt.hpp>
+#include <sol/sol.hpp>
 #include <SDL2/SDL.h>
 
 constexpr std::chrono::microseconds FRAME_TIME (1'000'000 / 60);
 
 static bool handleInputs(entt::registry& registry);
 
-int main()
+int main(int argc, const char *argv[])
 {
+    entt::registry registry;
+    sol::state lua;
+
     if (auto err = sdl2Initialize(); err)
         return err;
 
-    entt::registry registry;
+    for (int i = 1; i < argc; ++i)
+        lua.script_file(argv[i]);
 
-    {
-        const auto ent = registry.create();
-        registry.emplace<Player>(ent);
-        registry.emplace<Point>(ent, 0.f, WINDOW_HEIGHT - 200.f);
-        registry.emplace<Velocity>(ent, 0.f, 0.f);
-        registry.emplace<Texture>(ent, "img/player.png");
-    }
+    sol::optional<sol::table> entities = lua["entities"];
+    if (entities) {
+        entities->for_each([&registry](sol::object _, sol::object val) {
+            const auto ent = registry.create();
+            auto tbl = val.as<sol::table>();
 
-    {
-        const auto ent = registry.create();
-        registry.emplace<Point>(ent, 0.f, 0.f);
-        registry.emplace<Texture>(ent, "img/level.png");
-        registry.emplace<Solid>(ent, "img/level.png");
+            sol::optional<std::string> solid = tbl["solid"];
+            if (solid) {
+                registry.emplace<Solid>(ent, solid->c_str());
+            }
+
+            sol::optional<std::string> texture = tbl["texture"];
+            if (texture) {
+                registry.emplace<Texture>(ent, texture->c_str());
+            }
+
+            sol::optional<sol::table> point = tbl["point"];
+            if (point) {
+                registry.emplace<Point>(ent,
+                    static_cast<float>((*point)["x"]),
+                    static_cast<float>((*point)["y"]));
+            }
+
+            sol::optional<sol::table> velocity = tbl["velocity"];
+            if (velocity) {
+                registry.emplace<Velocity>(ent,
+                    static_cast<float>((*velocity)["x"]),
+                    static_cast<float>((*velocity)["y"]));
+            }
+
+            sol::optional<sol::table> player = tbl["player"];
+            if (player) {
+                registry.emplace<Player>(ent);
+            }
+        });
+    } else {
+        std::cout << "No entities defined..." << std::endl;
     }
 
     do {
@@ -50,14 +80,9 @@ int main()
         registry.view<Solid, Point>().each(
             [&registry](auto& s, auto& p) {
                 registry.view<Player, Point, Velocity, Texture>().each(
-                    [&s, &p](auto& _, auto& pp, auto& pv, auto& t) {
-                        const auto c = s.collision(pp + t.dim());
-
-                        if (c) {
-                            if (std::abs(c) > 1.f)
-                                pv.y = c * 0.5f;
-                            else
-                                pv.y = 0.f;
+                    [&s, &p](auto& _, auto& pp, auto& pv, auto& pt) {
+                        if (const auto c = s.collision(pp + pt.dim()); c) {
+                            pv.y = (std::abs(c) > 1.f) ? c * 0.25f : 0.f;
                         } else {
                             pv.y += 0.1f;
                         }
diff --git a/scripts/world.lua b/scripts/world.lua
new file mode 100644 (file)
index 0000000..5be4441
--- /dev/null
@@ -0,0 +1,17 @@
+WINDOW_WIDTH  = 640
+WINDOW_HEIGHT = 480
+
+entities = {
+    [0] = {
+        point = { x = 0, y = WINDOW_HEIGHT - 200 },
+        texture = "img/player.png",
+        velocity = { x = 0, y = 0 },
+        player = {},
+    },
+    [1] = {
+        point = { x = 0, y = 0 },
+        texture = "img/level.png",
+        solid = "img/level.png",
+    },
+}
+
diff --git a/sol2 b/sol2
new file mode 160000 (submodule)
index 0000000..2b0d2fe
--- /dev/null
+++ b/sol2
@@ -0,0 +1 @@
+Subproject commit 2b0d2fe8ba0074e16b499940c4f3126b9c7d3471