/// @file ecs/entity.h /// @brief Compact generational entity handle. #pragma once #include #include "core/types.h" namespace kumo { /// @brief Opaque handle identifying one entity in a World. /// /// The low 20 bits hold the slot index (up to 1,048,576 concurrent entities) /// and the high 12 bits hold a generation counter that advances each time a /// slot is reused. Zero is always the null/invalid entity, so valid generations /// start at 1 (wrapping from kMaxGeneration back to 1). struct Entity { static constexpr uint32_t kIndexBits = 20; static constexpr uint32_t kGenerationBits = 32 - kIndexBits; static constexpr uint32_t kIndexMask = (uint32_t{1} << kIndexBits) - 1; static constexpr uint32_t kMaxIndex = kIndexMask; static constexpr uint32_t kMaxGeneration = (uint32_t{1} << kGenerationBits) - 1; uint32_t raw = 0; static constexpr Entity null() { return Entity{0}; } static constexpr Entity make(uint32_t index, uint32_t generation) { assert(index <= kMaxIndex); assert(generation >= 1 && generation <= kMaxGeneration); return Entity{(generation << kIndexBits) | index}; } constexpr uint32_t index() const { return raw & kIndexMask; } constexpr uint32_t generation() const { return raw >> kIndexBits; } constexpr bool valid() const { return raw != 0; } constexpr explicit operator bool() const { return valid(); } constexpr bool operator==(const Entity&) const = default; }; /// @brief Advances a generation counter, skipping 0 so Entity::null() stays /// unique. constexpr uint32_t next_generation(uint32_t generation) { return generation >= Entity::kMaxGeneration ? 1u : generation + 1u; } } // namespace kumo