zecsy/zecsy.hpp

241 lines
6.3 KiB
C++

#pragma once
#include <concepts>
#include <cstdint>
#include <cstdlib>
#include <format>
#include <set>
#include <stdexcept>
#include <type_traits>
#include <unordered_map>
#include <vector>
namespace zecsy
{
using entity_id = uint64_t;
template<typename... T>
concept Component = []
{
static_assert((std::is_default_constructible_v<T> && ...),
"Should have a default constructor");
static_assert((std::is_trivially_copyable_v<T> && ...),
"Should be trivially copyable");
static_assert((std::is_trivially_destructible_v<T> && ...),
"Should be trivially destructible");
static_assert((std::is_standard_layout_v<T> && ...),
"Should have standard layout");
return true;
}();
class world final
{
public:
entity_id make_entity();
void destroy_entity(entity_id e);
bool is_alive(entity_id e) const;
template<Component T>
bool has(entity_id e) const;
template<Component First, Component Second, Component... Rest>
bool has(entity_id e) const;
template<Component T>
T& get(entity_id e);
template<Component T>
void set(entity_id e);
template<Component T>
void set(entity_id e, const T& comp);
template<Component First, Component Second, Component... Rest>
void set(entity_id e);
template<Component First, Component Second, Component... Rest>
void set(entity_id e, const First& comp0, const Second& comp1,
const Rest&... rest_comps);
template<Component T>
void remove(entity_id e);
template<Component First, Component Second, Component... Rest>
void remove(entity_id e);
template<Component... T>
void query(std::invocable<T&...> auto&& system);
private:
using comp_id = size_t;
std::set<entity_id> alive_entities;
std::unordered_map<entity_id, std::set<comp_id>> entity_to_comps;
entity_id entity_counter = 0;
struct component_pool
{
std::vector<uint8_t> data;
std::vector<size_t> free_list; // Reusable indices
std::unordered_map<entity_id, size_t> entity_to_index;
std::unordered_map<size_t, entity_id> index_to_entity;
};
std::unordered_map<comp_id, component_pool> pools;
template<typename T>
static comp_id get_component_id()
{
static comp_id id = next_component_id++;
return id;
}
static comp_id next_component_id;
};
inline world::comp_id world::next_component_id = 0;
inline entity_id world::make_entity()
{
auto id = ++entity_counter;
alive_entities.emplace(id);
// entity_to_comps[id] = {};
return id;
}
inline void world::destroy_entity(entity_id e)
{
alive_entities.erase(e);
for(comp_id id: entity_to_comps[e])
{
auto& pool = pools[id];
auto index = pool.entity_to_index[e];
pool.entity_to_index.erase(e);
pool.index_to_entity.erase(index);
pool.free_list.emplace_back(index);
}
entity_to_comps.erase(e);
}
inline bool world::is_alive(entity_id e) const
{
return alive_entities.contains(e);
}
template<Component T>
inline bool world::has(entity_id e) const
{
if(entity_to_comps.contains(e))
{
return entity_to_comps.at(e).contains(get_component_id<T>());
}
return false;
}
template<Component T>
inline T& world::get(entity_id e)
{
auto id = get_component_id<T>();
if(!has<T>(e))
{
throw std::runtime_error(
std::format("Entity #{} doesn't have {}", e, typeid(T).name()));
}
auto& pool = pools.at(id);
auto index = pool.entity_to_index.at(e);
return *reinterpret_cast<T*>(&pool.data[index * sizeof(T)]);
}
template<Component T>
inline void world::set(entity_id e, const T& comp)
{
auto id = get_component_id<T>();
auto& pool = pools[id];
size_t index;
if(!pool.free_list.empty())
{
index = pool.free_list.back();
pool.free_list.pop_back();
}
else
{
index = pool.data.size() / sizeof(T);
pool.data.resize(pool.data.size() + sizeof(T));
}
new(&pool.data[index * sizeof(T)]) T(comp);
pool.entity_to_index[e] = index;
pool.index_to_entity[index] = e;
entity_to_comps[e].emplace(id);
}
template<Component T>
inline void world::set(entity_id e)
{
set(e, T{});
}
template<Component T>
inline void world::remove(entity_id e)
{
auto id = get_component_id<T>();
if(!has<T>(e))
{
return;
}
auto& pool = pools[id];
auto index = pool.entity_to_index[e];
pool.free_list.push_back(index);
pool.entity_to_index.erase(e);
pool.index_to_entity.erase(index);
entity_to_comps[e].erase(id);
}
template<Component First, Component Second, Component... Rest>
inline bool world::has(entity_id e) const
{
return has<First>(e) && has<Second>(e) && (has<Rest>(e) && ...);
}
template<Component First, Component Second, Component... Rest>
inline void world::set(entity_id e)
{
set(e, First{});
set(e, Second{});
(set(e, Rest{}), ...);
}
template<Component First, Component Second, Component... Rest>
inline void world::set(entity_id e, const First& comp0, const Second& comp1,
const Rest&... rest_comps)
{
set(e, comp0);
set(e, comp1);
(set(e, rest_comps), ...);
}
template<Component First, Component Second, Component... Rest>
inline void world::remove(entity_id e)
{
remove<First>(e);
remove<Second>(e);
(remove<Rest>(e), ...);
}
template<Component... T>
inline void world::query(std::invocable<T&...> auto&& system)
{
for(auto e: alive_entities)
{
if((has<T...>(e)))
{
system(get<T>(e)...);
}
}
}
}; // namespace zecsy