You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.1 KiB
C++

#include "components/point.hpp"
#include "components/player.hpp"
#include "components/solid.hpp"
#include "components/texture.hpp"
#include "components/velocity.hpp"
#include "window.hpp"
3 months ago
#include <chrono>
3 months ago
#include <fstream>
#include <iostream>
3 months ago
#include <thread>
#include <entt/entt.hpp>
3 months ago
#include <sol/sol.hpp>
3 months ago
#include <SDL2/SDL.h>
constexpr std::chrono::microseconds FRAME_TIME (1'000'000 / 60);
3 months ago
static bool handleInputs(entt::registry& registry);
3 months ago
3 months ago
int main(int argc, const char *argv[])
3 months ago
{
entt::registry registry;
3 months ago
sol::state lua;
3 months ago
3 months ago
if (auto err = sdl2Initialize(); err)
return err;
3 months ago
for (int i = 1; i < argc; ++i)
lua.script_file(argv[i]);
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>();
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;
}
3 months ago
do {
const auto now = std::chrono::high_resolution_clock::now();
3 months ago
SDL_RenderClear(renderer);
registry.view<Texture, Point>().each([](auto& tex, auto& p) { tex(renderer, p); });
3 months ago
SDL_RenderPresent(renderer);
registry.view<Velocity, Point>().each([](auto& v, auto& p) { p += v; });
3 months ago
registry.view<Solid, Point>().each(
[&registry](auto& s, auto& p) {
registry.view<Player, Point, Velocity, Texture>().each(
3 months ago
[&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));
3 months ago
}
bool handleInputs(entt::registry& registry)
3 months ago
{
bool quit = false;
3 months ago
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;
}
3 months ago
}
}
return !quit;
3 months ago
}