zecsy/zecsy.hpp

290 lines
7.4 KiB
C++
Raw Normal View History

2025-02-14 21:35:40 +03:00
#pragma once
2025-02-16 20:39:41 +03:00
#include <algorithm>
2025-02-18 22:44:21 +03:00
#include <concepts>
2025-02-14 21:35:40 +03:00
#include <cstdint>
2025-02-16 20:39:41 +03:00
#include <cstdlib>
2025-02-14 22:18:00 +03:00
#include <format>
2025-02-16 20:39:41 +03:00
#include <functional>
2025-02-16 21:18:39 +03:00
#include <set>
2025-02-14 22:27:52 +03:00
#include <stdexcept>
2025-02-18 22:27:54 +03:00
#include <type_traits>
2025-02-14 23:42:01 +03:00
#include <unordered_map>
2025-02-17 22:35:43 +03:00
#include <vector>
2025-02-14 21:35:40 +03:00
2025-02-16 21:18:39 +03:00
namespace zecsy
2025-02-14 21:35:40 +03:00
{
using entity_id = uint64_t;
2025-02-18 22:27:54 +03:00
template<typename... T>
concept Component = []
{
2025-02-18 22:44:21 +03:00
static_assert((std::is_default_constructible_v<T> && ...),
"Should have a default constructor");
2025-02-18 22:27:54 +03:00
static_assert((std::is_trivially_copyable_v<T> && ...),
2025-02-18 22:44:21 +03:00
"Should be trivially copyable");
2025-02-18 22:27:54 +03:00
static_assert((std::is_trivially_destructible_v<T> && ...),
2025-02-18 22:44:21 +03:00
"Should be trivially destructible");
static_assert((std::is_standard_layout_v<T> && ...),
"Should have standard layout");
2025-02-18 22:27:54 +03:00
return true;
}();
2025-02-19 22:50:21 +03:00
class system_scheduler final
{
public:
void add_system(float freq, auto&& func);
void add_system(int freq, auto&& func);
void update(float dt);
private:
struct system_handler
{
double interval;
double accumulator = 0.0f;
std::function<void(float)> callback;
};
std::vector<system_handler> systems;
};
inline void system_scheduler::add_system(float freq, auto&& func)
{
systems.emplace_back(1.0f / freq, 0.0f,
std::forward<decltype(func)>(func));
}
inline void system_scheduler::add_system(int freq, auto&& func)
{
add_system(float(freq), func);
}
inline void system_scheduler::update(float dt)
{
dt = std::max(0.0f, dt);
for(auto& s: systems)
{
s.accumulator += dt;
while(s.accumulator >= s.interval)
{
s.callback(dt);
s.accumulator -= s.interval;
}
}
}
class world final
2025-02-14 23:42:01 +03:00
{
public:
2025-02-19 22:50:21 +03:00
entity_id make_entity();
void destroy_entity(entity_id e);
bool is_alive(entity_id e) const;
2025-02-18 22:27:54 +03:00
template<Component T>
2025-02-14 23:42:01 +03:00
bool has(entity_id e) const;
2025-02-18 22:27:54 +03:00
template<Component First, Component Second, Component... Rest>
2025-02-15 01:13:51 +03:00
bool has(entity_id e) const;
2025-02-18 22:27:54 +03:00
template<Component T>
2025-02-14 23:42:01 +03:00
T& get(entity_id e);
2025-02-18 22:27:54 +03:00
template<Component T>
2025-02-15 01:47:12 +03:00
void set(entity_id e);
2025-02-16 21:18:39 +03:00
2025-02-18 22:27:54 +03:00
template<Component T>
2025-02-15 01:47:12 +03:00
void set(entity_id e, const T& comp);
2025-02-16 21:18:39 +03:00
2025-02-18 22:27:54 +03:00
template<Component First, Component Second, Component... Rest>
2025-02-15 01:47:12 +03:00
void set(entity_id e);
2025-02-15 00:23:01 +03:00
2025-02-18 22:27:54 +03:00
template<Component First, Component Second, Component... Rest>
2025-02-16 21:18:39 +03:00
void set(entity_id e, const First& comp0, const Second& comp1,
const Rest&... rest_comps);
2025-02-18 22:27:54 +03:00
template<Component T>
2025-02-15 00:23:01 +03:00
void remove(entity_id e);
2025-02-16 21:18:39 +03:00
2025-02-18 22:27:54 +03:00
template<Component First, Component Second, Component... Rest>
2025-02-15 01:55:05 +03:00
void remove(entity_id e);
2025-02-16 21:18:39 +03:00
2025-02-19 22:50:21 +03:00
template<Component... T>
void query(std::invocable<T&...> auto&& system);
2025-02-14 23:42:01 +03:00
private:
2025-02-17 22:35:43 +03:00
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;
2025-02-14 23:42:01 +03:00
2025-02-17 22:35:43 +03:00
struct component_pool
2025-02-16 20:39:41 +03:00
{
2025-02-17 22:35:43 +03:00
std::vector<uint8_t> data;
std::vector<size_t> free_list; // Reusable indices
2025-02-17 22:35:43 +03:00
std::unordered_map<entity_id, size_t> entity_to_index;
std::unordered_map<size_t, entity_id> index_to_entity;
2025-02-16 20:39:41 +03:00
};
2025-02-17 22:35:43 +03:00
std::unordered_map<comp_id, component_pool> pools;
2025-02-14 23:42:01 +03:00
template<typename T>
2025-02-17 22:35:43 +03:00
static comp_id get_component_id()
{
static comp_id id = next_component_id++;
return id;
}
2025-02-16 21:18:39 +03:00
2025-02-17 22:35:43 +03:00
static comp_id next_component_id;
2025-02-14 21:35:40 +03:00
};
2025-02-19 22:50:21 +03:00
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] = {};
2025-02-19 22:50:21 +03:00
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);
2025-02-19 22:50:21 +03:00
}
inline bool world::is_alive(entity_id e) const
{
return alive_entities.contains(e);
}
2025-02-14 23:42:01 +03:00
2025-02-18 22:27:54 +03:00
template<Component T>
2025-02-19 22:50:21 +03:00
inline bool world::has(entity_id e) const
2025-02-14 23:42:01 +03:00
{
if(entity_to_comps.contains(e))
2025-02-14 23:42:01 +03:00
{
return entity_to_comps.at(e).contains(get_component_id<T>());
2025-02-14 23:42:01 +03:00
}
return false;
}
2025-02-18 22:27:54 +03:00
template<Component T>
2025-02-19 22:50:21 +03:00
inline T& world::get(entity_id e)
2025-02-14 23:42:01 +03:00
{
2025-02-17 22:35:43 +03:00
auto id = get_component_id<T>();
2025-02-14 23:42:01 +03:00
if(!has<T>(e))
{
2025-02-16 21:18:39 +03:00
throw std::runtime_error(
std::format("Entity #{} doesn't have {}", e, typeid(T).name()));
2025-02-14 23:42:01 +03:00
}
2025-02-17 22:35:43 +03:00
auto& pool = pools.at(id);
auto index = pool.entity_to_index.at(e);
return *reinterpret_cast<T*>(&pool.data[index * sizeof(T)]);
2025-02-14 23:42:01 +03:00
}
2025-02-18 22:27:54 +03:00
template<Component T>
2025-02-19 22:50:21 +03:00
inline void world::set(entity_id e, const T& comp)
2025-02-14 23:42:01 +03:00
{
2025-02-17 22:35:43 +03:00
auto id = get_component_id<T>();
auto& pool = pools[id];
2025-02-14 23:42:01 +03:00
2025-02-17 22:35:43 +03:00
size_t index;
if(!pool.free_list.empty())
2025-02-15 00:23:01 +03:00
{
index = pool.free_list.back();
pool.free_list.pop_back();
2025-02-17 22:35:43 +03:00
}
else
{
index = pool.data.size() / sizeof(T);
pool.data.resize(pool.data.size() + sizeof(T));
2025-02-15 00:23:01 +03:00
}
2025-02-17 22:35:43 +03:00
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);
2025-02-14 23:42:01 +03:00
}
2025-02-16 21:18:39 +03:00
2025-02-18 22:27:54 +03:00
template<Component T>
2025-02-19 22:50:21 +03:00
inline void world::set(entity_id e)
2025-02-15 22:33:02 +03:00
{
set(e, T{});
}
2025-02-18 22:27:54 +03:00
template<Component T>
2025-02-19 22:50:21 +03:00
inline void world::remove(entity_id e)
2025-02-15 00:23:01 +03:00
{
2025-02-17 22:35:43 +03:00
auto id = get_component_id<T>();
2025-02-15 00:23:01 +03:00
if(!has<T>(e))
{
return;
}
2025-02-17 22:35:43 +03:00
auto& pool = pools[id];
auto index = pool.entity_to_index[e];
pool.free_list.push_back(index);
2025-02-17 22:35:43 +03:00
pool.entity_to_index.erase(e);
pool.index_to_entity.erase(index);
entity_to_comps[e].erase(id);
2025-02-15 01:13:51 +03:00
}
2025-02-18 22:27:54 +03:00
template<Component First, Component Second, Component... Rest>
2025-02-19 22:50:21 +03:00
inline bool world::has(entity_id e) const
2025-02-15 01:13:51 +03:00
{
return has<First>(e) && has<Second>(e) && (has<Rest>(e) && ...);
}
2025-02-16 21:18:39 +03:00
2025-02-18 22:27:54 +03:00
template<Component First, Component Second, Component... Rest>
2025-02-19 22:50:21 +03:00
inline void world::set(entity_id e)
2025-02-15 01:47:12 +03:00
{
set(e, First{});
set(e, Second{});
(set(e, Rest{}), ...);
}
2025-02-18 22:27:54 +03:00
template<Component First, Component Second, Component... Rest>
2025-02-19 22:50:21 +03:00
inline void world::set(entity_id e, const First& comp0, const Second& comp1,
const Rest&... rest_comps)
2025-02-15 01:47:12 +03:00
{
set(e, comp0);
set(e, comp1);
(set(e, rest_comps), ...);
}
2025-02-15 01:55:05 +03:00
2025-02-18 22:27:54 +03:00
template<Component First, Component Second, Component... Rest>
2025-02-19 22:50:21 +03:00
inline void world::remove(entity_id e)
2025-02-15 01:55:05 +03:00
{
remove<First>(e);
remove<Second>(e);
(remove<Rest>(e), ...);
}
2025-02-15 20:13:38 +03:00
2025-02-18 22:27:54 +03:00
template<Component... T>
2025-02-18 22:44:21 +03:00
inline void world::query(std::invocable<T&...> auto&& system)
2025-02-17 22:35:43 +03:00
{
for(auto e: alive_entities)
{
if((has<T...>(e)))
{
system(get<T>(e)...);
}
}
}
2025-02-16 21:18:39 +03:00
}; // namespace zecsy