|
|
|
#include "components/point.hpp"
|
|
|
|
#include "components/player.hpp"
|
|
|
|
#include "components/solid.hpp"
|
|
|
|
#include "components/texture.hpp"
|
|
|
|
#include "components/velocity.hpp"
|
|
|
|
#include "window.hpp"
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <ranges>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include <entt/entt.hpp>
|
|
|
|
#include <sol/sol.hpp>
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
|
|
|
|
constexpr std::chrono::duration<double> FRAME_TIME (1.0 / 60.0);
|
|
|
|
|
|
|
|
static bool handleInputs(entt::registry& registry);
|
|
|
|
|
|
|
|
int main(int argc, const char *argv[])
|
|
|
|
{
|
|
|
|
entt::registry registry;
|
|
|
|
sol::state lua;
|
|
|
|
|
|
|
|
if (auto err = sdl2Initialize(); err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
for (auto file : std::views::counted(argv + 1, argc - 1))
|
|
|
|
lua.script_file(file);
|
|
|
|
|
|
|
|
if (auto entities = lua.get<sol::optional<sol::table>>("entities"); entities) {
|
|
|
|
entities->for_each([®istry](sol::object _, sol::object val) {
|
|
|
|
const auto ent = registry.create();
|
|
|
|
auto tbl = val.as<sol::table>();
|
|
|
|
|
|
|
|
if (auto solid = tbl.get<sol::optional<std::string>>("solid"); solid)
|
|
|
|
registry.emplace<Solid>(ent, *solid);
|
|
|
|
|
|
|
|
if (auto tex = tbl.get<sol::optional<std::string>>("texture"); tex)
|
|
|
|
registry.emplace<Texture>(ent, *tex);
|
|
|
|
|
|
|
|
if (auto point = tbl.get<sol::optional<sol::table>>("point"); point)
|
|
|
|
registry.emplace<Point>(ent, point->get<float, float>("x", "y"));
|
|
|
|
|
|
|
|
if (auto vel = tbl.get<sol::optional<sol::table>>("velocity"); vel)
|
|
|
|
registry.emplace<Velocity>(ent, vel->get<float, float>("x", "y"));
|
|
|
|
|
|
|
|
if (auto player = tbl.get<sol::optional<sol::table>>("player"); player)
|
|
|
|
registry.emplace<Player>(ent);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
std::cout << "No entities defined..." << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
const auto now = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
registry.view<Texture, Point>().each([](auto& tex, auto& p) { tex(renderer, p); });
|
|
|
|
SDL_RenderPresent(renderer);
|
|
|
|
|
|
|
|
registry.view<Velocity, Point>().each([](auto& v, auto& p) { p += v; });
|
|
|
|
|
|
|
|
registry.view<Solid, Point>().each(
|
|
|
|
[®istry](auto& s, auto& p) {
|
|
|
|
registry.view<Player, Point, Velocity, Texture>().each(
|
|
|
|
[&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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
std::this_thread::sleep_until(now + FRAME_TIME);
|
|
|
|
} while (handleInputs(registry));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool handleInputs(entt::registry& registry)
|
|
|
|
{
|
|
|
|
bool quit = false;
|
|
|
|
|
|
|
|
for (SDL_Event e; SDL_PollEvent(&e);) {
|
|
|
|
if (e.type == SDL_QUIT) {
|
|
|
|
quit = true;
|
|
|
|
} else if (e.type == SDL_KEYDOWN && !e.key.repeat) {
|
|
|
|
auto view = registry.view<Player, Velocity>();
|
|
|
|
|
|
|
|
switch (e.key.keysym.sym) {
|
|
|
|
case SDLK_d:
|
|
|
|
view.each([](Player& p, Velocity& v) { v.x += 1.5f; });
|
|
|
|
break;
|
|
|
|
case SDLK_a:
|
|
|
|
view.each([](Player& p, Velocity& v) { v.x -= 1.5f; });
|
|
|
|
break;
|
|
|
|
case SDLK_SPACE:
|
|
|
|
view.each([](Player& p, Velocity& v) { if (v.y <= 0.f) v.y -= 3.f; });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (e.type == SDL_KEYUP && !e.key.repeat) {
|
|
|
|
auto view = registry.view<Player, Velocity>();
|
|
|
|
|
|
|
|
switch (e.key.keysym.sym) {
|
|
|
|
case SDLK_d:
|
|
|
|
view.each([](Player& p, Velocity& v) { v.x -= 1.5f; });
|
|
|
|
break;
|
|
|
|
case SDLK_a:
|
|
|
|
view.each([](Player& p, Velocity& v) { v.x += 1.5f; });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return !quit;
|
|
|
|
}
|
|
|
|
|