From 189afb447e2c76e5dd5e4550e786c5175a828cbc Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Mon, 5 Aug 2024 21:49:23 -0400 Subject: add lua support --- main.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 22 deletions(-) (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp index c137cc1..0d3feeb 100644 --- a/main.cpp +++ b/main.cpp @@ -6,36 +6,66 @@ #include "window.hpp" #include +#include #include #include #include +#include #include 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(ent); - registry.emplace(ent, 0.f, WINDOW_HEIGHT - 200.f); - registry.emplace(ent, 0.f, 0.f); - registry.emplace(ent, "img/player.png"); - } + sol::optional entities = lua["entities"]; + if (entities) { + entities->for_each([®istry](sol::object _, sol::object val) { + const auto ent = registry.create(); + auto tbl = val.as(); - { - const auto ent = registry.create(); - registry.emplace(ent, 0.f, 0.f); - registry.emplace(ent, "img/level.png"); - registry.emplace(ent, "img/level.png"); + sol::optional solid = tbl["solid"]; + if (solid) { + registry.emplace(ent, solid->c_str()); + } + + sol::optional texture = tbl["texture"]; + if (texture) { + registry.emplace(ent, texture->c_str()); + } + + sol::optional point = tbl["point"]; + if (point) { + registry.emplace(ent, + static_cast((*point)["x"]), + static_cast((*point)["y"])); + } + + sol::optional velocity = tbl["velocity"]; + if (velocity) { + registry.emplace(ent, + static_cast((*velocity)["x"]), + static_cast((*velocity)["y"])); + } + + sol::optional player = tbl["player"]; + if (player) { + registry.emplace(ent); + } + }); + } else { + std::cout << "No entities defined..." << std::endl; } do { @@ -50,14 +80,9 @@ int main() registry.view().each( [®istry](auto& s, auto& p) { registry.view().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; } -- cgit v1.2.3