Switched back to using ids instead of names, added uuids to entities and started work on serialization

This commit is contained in:
2019-02-05 22:23:38 +00:00
parent 92f8fdd510
commit a6e5deb502
8 changed files with 259 additions and 40 deletions

View File

@@ -0,0 +1,147 @@
#include <iostream>
#include "ecs.h"
#include "iohelper/memstream.h"
#include "iohelper/write.h"
#include "iohelper/read.h"
#include <fstream>
struct Position : ecs::Component {
Position(float x, float y) : _x(x), _y(y) {}
float _x;
float _y;
};
struct Velocity : ecs::Component{
Velocity(float vx, float vy) : _vx(vx), _vy(vy) {}
float _vx;
float _vy;
};
std::unordered_map<size_t, std::pair<std::function<std::vector<uint8_t>(ecs::Component*)>, std::function<ecs::Component*(std::vector<uint8_t>)>>> _functions;
// @todo Add deserializer stuff
template <typename T>
void register_component(std::function<std::vector<uint8_t>(ecs::Component*)> serializer, std::function<ecs::Component*(std::vector<uint8_t>)> deserializer) {
_functions.insert({ecs::ComponentID::id<T>, {serializer, deserializer}});
}
enum class Marker : uint8_t {
ENTITY,
COMPONENT,
END
};
int main(int argc, const char* argv) {
ecs::Manager manager;
register_component<Position>([] (ecs::Component* component) {
Position* pos = (Position*)component;
std::vector<uint8_t> data;
iohelper::omemstream stream(data);
iohelper::write<float>(stream, pos->_x);
iohelper::write<float>(stream, pos->_y);
return data;
}, [] (std::vector<uint8_t> data) {
iohelper::imemstream stream(data);
float x = iohelper::read<float>(stream);
float y = iohelper::read<float>(stream);
return new Position(x, y);
});
register_component<Velocity>([] (ecs::Component* component) {
Velocity* vel = (Velocity*)component;
std::vector<uint8_t> data;
iohelper::omemstream stream(data);
iohelper::write<float>(stream, vel->_vx);
iohelper::write<float>(stream, vel->_vy);
return data;
}, [] (std::vector<uint8_t> data) {
iohelper::imemstream stream(data);
float vx = iohelper::read<float>(stream);
float vy = iohelper::read<float>(stream);
return new Velocity(vx, vy);
});
if (argc == 1) {
// 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->_vx;
pos->_y += vel->_vy;
});
std::ofstream file("entities", std::ios::out | std::ios::trunc);
for (auto [uuid, entity] : manager.view<>()) {
iohelper::write<uint8_t>(file, (uint8_t)Marker::ENTITY);
// iohelper::write_length(file, uuid);
auto uuid_data = reinterpret_cast<const uint8_t*>(uuid.as_bytes().data());
iohelper::write_vector<uint8_t>(file, std::vector<uint8_t>(uuid_data, uuid_data + 16), false);
for (auto [id, component] : entity->get_components()) {
iohelper::write<uint8_t>(file, (uint8_t)Marker::COMPONENT);
iohelper::write_length(file, id);
auto data = _functions[id].first(component);
iohelper::write_vector<uint8_t>(file, data);
}
}
iohelper::write<uint8_t>(file, (uint8_t)Marker::END);
file.close();
} else {
// Load entities from disk as a test
std::ifstream file("entities", std::ios::in);
bool loop = true;
ecs::Entity* entity = nullptr;
while (loop) {
Marker marker = (Marker)iohelper::read<uint8_t>(file);
switch (marker) {
case Marker::ENTITY: {
// size_t uuid = iohelper::read_length(file);
auto uuid_vector = iohelper::read_vector<uint8_t>(file, 16);
entity = manager.create_entity(uuids::uuid(uuid_vector.begin(), uuid_vector.end()));
break;
}
case Marker::COMPONENT: {
size_t id = iohelper::read_length(file);
std::vector<uint8_t> data = iohelper::read_vector<uint8_t>(file);
entity->add_component(id, _functions[id].second(data));
break;
}
case Marker::END:
loop = false;
break;
}
}
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';
});
}