45 lines
1.2 KiB
C++
45 lines
1.2 KiB
C++
#include <psemek/ecs/entity_container.hpp>
|
|
|
|
namespace psemek::ecs
|
|
{
|
|
|
|
bool entity_container::alive(entity_handle const & entity) const
|
|
{
|
|
return entity_list_.get_entities()[entity.id].epoch == entity.epoch;
|
|
}
|
|
|
|
void entity_container::destroy(entity_handle const & entity)
|
|
{
|
|
auto entities = entity_list_.get_entities();
|
|
auto & data = entities[entity.id];
|
|
auto & iteration_data = data.table->get_iteration_data();
|
|
|
|
if (!iteration_data || iteration_data->current_row < data.row)
|
|
{
|
|
remove_row(*data.table, data.row, entities);
|
|
entity_list_.destroy(entity.id);
|
|
}
|
|
else
|
|
{
|
|
data.table->push_remove(data.row);
|
|
entity_list_.destroy(entity.id);
|
|
}
|
|
}
|
|
|
|
entity_accessor entity_container::get(entity_handle const & entity)
|
|
{
|
|
auto const & data = entity_list_.get_entities()[entity.id];
|
|
return {data.table, data.row};
|
|
}
|
|
|
|
void entity_container::remove_row(detail::table & table, std::uint32_t row, util::span<detail::entity_data> entities)
|
|
{
|
|
// Swap with the last row in that table
|
|
auto table_entity_handles = table.entity_handles();
|
|
table.swap_rows(row, table_entity_handles.size() - 1);
|
|
table.pop_row();
|
|
auto swap_handle = table_entity_handles[row];
|
|
entities[swap_handle.id].row = row;
|
|
}
|
|
|
|
}
|