No entity_id reusage yet
This commit is contained in:
parent
4e3ae245b8
commit
f1f965a284
1 changed files with 7 additions and 6 deletions
13
zecsy.hpp
13
zecsy.hpp
|
@ -2,7 +2,7 @@
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <exception>
|
#include <stdexcept>
|
||||||
|
|
||||||
#ifndef MAX_ZECSY_ENTITIES
|
#ifndef MAX_ZECSY_ENTITIES
|
||||||
#define MAX_ZECSY_ENTITIES 65536
|
#define MAX_ZECSY_ENTITIES 65536
|
||||||
|
@ -44,7 +44,7 @@ namespace zecsy
|
||||||
void destroy_entity(entity_id e);
|
void destroy_entity(entity_id e);
|
||||||
bool is_alive(entity_id e) const;
|
bool is_alive(entity_id e) const;
|
||||||
private:
|
private:
|
||||||
std::bitset<MAX_ZECSY_ENTITIES> entities_bitset;
|
std::bitset<MAX_ZECSY_ENTITIES + 1> entities_bitset;
|
||||||
entity_id entity_counter = 0;
|
entity_id entity_counter = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -65,11 +65,12 @@ namespace zecsy
|
||||||
|
|
||||||
inline entity world::make_entity()
|
inline entity world::make_entity()
|
||||||
{
|
{
|
||||||
auto id = (++entity_counter)%MAX_ZECSY_ENTITIES;
|
auto id = ++entity_counter;
|
||||||
if(is_alive(id))
|
|
||||||
|
if(id > MAX_ZECSY_ENTITIES)
|
||||||
{
|
{
|
||||||
throw std::runtime_error(std::format("entity_id #{} already in use,"
|
throw std::runtime_error(std::format("Entity id {} exceeds "
|
||||||
" can't make a new entity", id));
|
"MAX_ZECSY_ENTITIES ({})", id, MAX_ZECSY_ENTITIES));
|
||||||
}
|
}
|
||||||
|
|
||||||
entities_bitset.set(id);
|
entities_bitset.set(id);
|
||||||
|
|
Loading…
Reference in a new issue