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-15 18:24:18 +03:00
|
|
|
|
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-15 18:24:18 +03:00
|
|
|
/*
|
2025-02-17 22:35:43 +03:00
|
|
|
* Using std::set for entities_set to maintain sorted order, which can
|
|
|
|
* improve cache locality during queries and iterations.
|
2025-02-15 18:24:18 +03:00
|
|
|
*/
|
2025-02-16 21:18:39 +03:00
|
|
|
using entities_set = std::set<entity_id>;
|
2025-02-14 21:35:40 +03:00
|
|
|
|
2025-02-15 22:33:02 +03:00
|
|
|
class component_storage final
|
2025-02-14 23:42:01 +03:00
|
|
|
{
|
|
|
|
public:
|
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-14 23:42:01 +03:00
|
|
|
private:
|
2025-02-17 22:35:43 +03:00
|
|
|
using comp_id = size_t;
|
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;
|
2025-02-17 22:50:53 +03:00
|
|
|
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-17 22:35:43 +03:00
|
|
|
inline component_storage::comp_id component_storage::next_component_id = 0;
|
2025-02-14 23:42:01 +03:00
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
template<Component T>
|
2025-02-14 23:42:01 +03:00
|
|
|
inline bool component_storage::has(entity_id e) const
|
|
|
|
{
|
2025-02-17 22:35:43 +03:00
|
|
|
auto id = get_component_id<T>();
|
|
|
|
if(pools.contains(id))
|
2025-02-14 23:42:01 +03:00
|
|
|
{
|
2025-02-17 22:35:43 +03:00
|
|
|
return pools.at(id).entity_to_index.contains(e);
|
2025-02-14 23:42:01 +03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
template<Component T>
|
2025-02-14 23:42:01 +03:00
|
|
|
inline T& component_storage::get(entity_id e)
|
|
|
|
{
|
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-14 23:56:01 +03:00
|
|
|
inline void component_storage::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
|
|
|
{
|
2025-02-17 22:50:53 +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;
|
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-15 22:33:02 +03:00
|
|
|
inline void component_storage::set(entity_id e)
|
|
|
|
{
|
|
|
|
set(e, T{});
|
|
|
|
}
|
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
template<Component T>
|
2025-02-15 00:23:01 +03:00
|
|
|
inline void component_storage::remove(entity_id e)
|
|
|
|
{
|
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];
|
2025-02-18 18:45:49 +03:00
|
|
|
|
2025-02-17 22:50:53 +03:00
|
|
|
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);
|
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-15 01:13:51 +03:00
|
|
|
inline bool component_storage::has(entity_id e) const
|
|
|
|
{
|
|
|
|
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-15 01:47:12 +03:00
|
|
|
inline void component_storage::set(entity_id e)
|
|
|
|
{
|
|
|
|
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-16 21:18:39 +03:00
|
|
|
inline void component_storage::set(entity_id e, const First& comp0,
|
2025-02-17 22:50:53 +03:00
|
|
|
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-15 01:55:05 +03:00
|
|
|
inline void component_storage::remove(entity_id e)
|
|
|
|
{
|
|
|
|
remove<First>(e);
|
|
|
|
remove<Second>(e);
|
|
|
|
(remove<Rest>(e), ...);
|
|
|
|
}
|
2025-02-15 20:13:38 +03:00
|
|
|
|
2025-02-17 22:35:43 +03:00
|
|
|
class system_scheduler final
|
2025-02-15 20:13:38 +03:00
|
|
|
{
|
2025-02-17 22:35:43 +03:00
|
|
|
public:
|
|
|
|
void add_system(float freq, auto&& func);
|
|
|
|
|
|
|
|
void add_system(int freq, auto&& func);
|
|
|
|
|
|
|
|
void update(float dt);
|
2025-02-18 22:44:21 +03:00
|
|
|
|
2025-02-17 22:35:43 +03:00
|
|
|
private:
|
|
|
|
struct system_handler
|
2025-02-15 20:13:38 +03:00
|
|
|
{
|
2025-02-17 22:35:43 +03:00
|
|
|
double interval;
|
|
|
|
double accumulator = 0.0f;
|
|
|
|
std::function<void(float)> callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<system_handler> systems;
|
|
|
|
};
|
2025-02-16 20:39:41 +03:00
|
|
|
|
|
|
|
inline void system_scheduler::add_system(float freq, auto&& func)
|
|
|
|
{
|
2025-02-16 21:18:39 +03:00
|
|
|
systems.emplace_back(1.0f / freq, 0.0f,
|
|
|
|
std::forward<decltype(func)>(func));
|
2025-02-16 20:39:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-02-17 22:35:43 +03:00
|
|
|
|
|
|
|
class world final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
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-17 22:35:43 +03:00
|
|
|
bool has(entity_id e) const;
|
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
template<Component T>
|
2025-02-17 22:35:43 +03:00
|
|
|
T& get(entity_id e);
|
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
template<Component... T>
|
2025-02-17 22:35:43 +03:00
|
|
|
void set(entity_id e);
|
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
template<Component... T>
|
2025-02-17 22:35:43 +03:00
|
|
|
void set(entity_id e, const T&... comps);
|
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
template<Component... T>
|
2025-02-17 22:35:43 +03:00
|
|
|
void remove(entity_id e);
|
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
template<Component... T>
|
2025-02-18 22:44:21 +03:00
|
|
|
void query(std::invocable<T&...> auto&& system);
|
|
|
|
|
2025-02-17 22:35:43 +03:00
|
|
|
private:
|
|
|
|
entities_set alive_entities;
|
|
|
|
entity_id entity_counter = 0;
|
|
|
|
component_storage storage;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline entity_id world::make_entity()
|
|
|
|
{
|
|
|
|
auto id = ++entity_counter;
|
|
|
|
alive_entities.emplace(id);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void world::destroy_entity(entity_id e)
|
|
|
|
{
|
|
|
|
alive_entities.erase(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool world::is_alive(entity_id e) const
|
|
|
|
{
|
|
|
|
return alive_entities.contains(e);
|
|
|
|
}
|
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
template<Component... T>
|
2025-02-17 22:35:43 +03:00
|
|
|
inline bool world::has(entity_id e) const
|
|
|
|
{
|
|
|
|
return storage.has<T...>(e);
|
|
|
|
}
|
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
template<Component T>
|
2025-02-17 22:35:43 +03:00
|
|
|
inline T& world::get(entity_id e)
|
|
|
|
{
|
|
|
|
return storage.get<T>(e);
|
|
|
|
}
|
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
template<Component... T>
|
2025-02-17 22:35:43 +03:00
|
|
|
inline void world::set(entity_id e)
|
|
|
|
{
|
|
|
|
storage.set<T...>(e);
|
|
|
|
}
|
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
template<Component... T>
|
2025-02-17 22:35:43 +03:00
|
|
|
inline void world::set(entity_id e, const T&... comps)
|
|
|
|
{
|
|
|
|
storage.set(e, comps...);
|
|
|
|
}
|
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
template<Component... T>
|
2025-02-17 22:35:43 +03:00
|
|
|
inline void world::remove(entity_id e)
|
|
|
|
{
|
|
|
|
storage.remove<T...>(e);
|
|
|
|
}
|
|
|
|
|
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
|