190 lines
4.8 KiB
C++
190 lines
4.8 KiB
C++
#include "components.h"
|
|
#include "ecs-lua.h"
|
|
#include "ecs-serial.h"
|
|
|
|
#include <bits/stdint-intn.h>
|
|
#include <bits/stdint-uintn.h>
|
|
#include <cstddef>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
|
|
#include <ostream>
|
|
#include <stdexcept>
|
|
#include <sys/types.h>
|
|
#include "ecs.h"
|
|
#include "ecs_components.h"
|
|
|
|
class Orb : public ecs::Entity {
|
|
public:
|
|
Orb(uuids::uuid uuid, float x, float y) : Entity(uuid) {
|
|
auto position = add_component<Position>(x, y);
|
|
auto velocity = add_component<Velocity>(0, 0);
|
|
}
|
|
};
|
|
|
|
inline void handle_error(sol::optional<std::string> maybe_msg) {
|
|
std::cerr << "Lua error, aborting!\n";
|
|
if (maybe_msg) {
|
|
const std::string& msg = maybe_msg.value();
|
|
std::cerr << "Error: " << msg << '\n';
|
|
}
|
|
}
|
|
|
|
int main(int argc, const char** argv) {
|
|
bool save;
|
|
if (argc == 2) {
|
|
if (!std::string("save").compare(argv[1])) {
|
|
save = true;
|
|
} else if (!std::string("load").compare(argv[1])) {
|
|
save = false;
|
|
} else {
|
|
std::cerr << "Usage: " << argv[0] << " [save/load]\n";
|
|
return -1;
|
|
}
|
|
} else {
|
|
std::cerr << "Usage: " << argv[0] << " [save/load]\n";
|
|
return -1;
|
|
}
|
|
|
|
ecs::ComponentID::add<Position>("Position");
|
|
ecs::ComponentID::add<Velocity>("Velocity");
|
|
ecs::ComponentID::add<Meta>("Meta");
|
|
|
|
sol::state lua(sol::c_call<decltype(&handle_error), &handle_error>);
|
|
lua.open_libraries(sol::lib::base, sol::lib::package);
|
|
|
|
ecs::lua::init(lua);
|
|
|
|
generated::init(lua);
|
|
generated::init();
|
|
|
|
if (save) {
|
|
ecs::Manager manager;
|
|
|
|
// Testing interop with lua
|
|
{
|
|
|
|
lua.set_function("get_manager", [&manager] () -> ecs::Manager& {
|
|
return manager;
|
|
});
|
|
|
|
lua.safe_script_file("test.lua");
|
|
|
|
sol::protected_function init = lua["init"];
|
|
std::cout << "Running init()\n";
|
|
init();
|
|
|
|
sol::protected_function run = lua["run"];
|
|
std::cout << "Running run()\n";
|
|
run();
|
|
|
|
std::cout << "Update position\n";
|
|
manager.view<Position, Velocity>().for_each([](ecs::Entity*, Position* pos, Velocity* vel) {
|
|
pos->x += vel->x;
|
|
pos->y += vel->y;
|
|
});
|
|
|
|
std::cout << "Show position!\n";
|
|
manager.view<Position>().for_each([](ecs::Entity*, Position* pos) {
|
|
std::cout << "X: " << pos->x << '\n';
|
|
std::cout << "Y: " << pos->y << '\n';
|
|
});
|
|
|
|
std::cout << "Adding Orb!\n";
|
|
manager.create_entity<Orb>(1.2f, 3.4f);
|
|
for (auto [ent, pos, vel] : manager.view<Position, Velocity>()) {
|
|
std::cout << "X: " << pos->x << " + " << vel->x << '\n';
|
|
std::cout << "Y: " << pos->y << " + " << vel->y << '\n';
|
|
}
|
|
}
|
|
|
|
// Test serialization
|
|
{
|
|
std::cout << "STORE\n";
|
|
|
|
// Create entities and store them
|
|
for (int i = 0; i < 10; ++i) {
|
|
// We can create entities
|
|
ecs::Entity* entity = manager.create_entity();
|
|
// Then we can add components to them
|
|
entity->add_component<Position>(0.0f, 0.0f);
|
|
if (i % 2 == 0) {
|
|
entity->add_component<Velocity>(0.1f, 0.2f);
|
|
}
|
|
}
|
|
|
|
manager.view<Position, Velocity>().for_each([] (ecs::Entity*, Position* pos, Velocity* vel) {
|
|
pos->x += vel->x;
|
|
pos->y += vel->y;
|
|
});
|
|
|
|
std::ofstream file("entities", std::ios::out | std::ios::trunc);
|
|
ecs::serial::serialize_ids(file);
|
|
|
|
io::write<size_t>(file, manager.view<>().size());
|
|
for (auto [entity] : manager.view<>()) {
|
|
ecs::serial::serialize(file, entity);
|
|
}
|
|
file.close();
|
|
|
|
manager.view<Position>().for_each([] (ecs::Entity* entity, Position* pos) {
|
|
std::cout << "uuid: " << entity->uuid << '\n';
|
|
std::cout << "x: " << pos->x << '\n';
|
|
std::cout << "y: " << pos->y << '\n';
|
|
});
|
|
}
|
|
} else {
|
|
ecs::Manager manager;
|
|
{
|
|
std::cout << "LOAD\n";
|
|
|
|
lua.safe_script_file("test2.lua");
|
|
sol::protected_function init = lua["init"];
|
|
std::cout << "Running init()\n";
|
|
init();
|
|
|
|
// Load entities from disk as a test
|
|
std::ifstream file("entities", std::ios::in);
|
|
|
|
ecs::serial::deserialize_ids(file);
|
|
|
|
size_t entity_count = io::read<size_t>(file);
|
|
size_t pos = file.tellg();
|
|
for (size_t i = 0; i < entity_count; ++i) {
|
|
ecs::serial::deserialize(file, manager);
|
|
}
|
|
|
|
// Load the first entity again (simulating an update)
|
|
file.seekg(pos, std::ios::beg);
|
|
ecs::serial::deserialize(file, manager);
|
|
|
|
if (false) {
|
|
auto ent = manager.get_entity(uuids::uuid::from_string("6d58fdb5-6d8c-4e6f-89d4-f7d7b184f463").value());
|
|
if (ent->has_components<Position>()) {
|
|
auto pos = ent->get_component<Position>();
|
|
pos->x = 1.2f;
|
|
pos->y = 3.4f;
|
|
}
|
|
}
|
|
|
|
manager.view<Position>().for_each([] (ecs::Entity* entity, Position* pos) {
|
|
std::cout << "uuid: " << entity->uuid << '\n';
|
|
std::cout << "x: " << pos->x << '\n';
|
|
std::cout << "y: " << pos->y << '\n';
|
|
});
|
|
|
|
lua.set_function("get_manager", [&manager] () -> ecs::Manager& {
|
|
return manager;
|
|
});
|
|
|
|
sol::protected_function run = lua["run"];
|
|
std::cout << "Running run()\n";
|
|
run();
|
|
}
|
|
}
|
|
|
|
for (auto [name, id] : ecs::ComponentID::get_map()) {
|
|
std::cout << name << ' ' << id << '\n';
|
|
}
|
|
}
|