Fix component pool entity removal

This commit is contained in:
Hugo Mårdbrink 2025-08-26 22:01:28 +02:00
parent 77c5c23cf5
commit 6d43f30c09
5 changed files with 48 additions and 18 deletions

View file

@ -2,7 +2,7 @@ package ecs
import "core:log"
ComponentPool :: struct($T: typeid) {
ComponentPool :: struct(T: typeid) {
data: []T,
entity_to_index: map[EntityID]uintptr,
index_to_entity: map[uintptr]EntityID,
@ -20,7 +20,7 @@ component_pool_create :: proc($T: typeid) -> ComponentPool(T) {
return component_pool
}
component_pool_insert_data :: proc($T: typeid, component_pool: ^ComponentPool(T), entity_id: EntityID, component: T) {
component_pool_insert_data :: proc(component_pool: ^ComponentPool($T), entity_id: EntityID, component: T) {
log.assertf(entity_id not_in component_pool.entity_to_index, "Component already added to entity")
new_idx := component_pool.size
@ -31,7 +31,7 @@ component_pool_insert_data :: proc($T: typeid, component_pool: ^ComponentPool(T)
component_pool.size += 1
}
component_pool_remove_data :: proc(component_pool: ^ComponentPool(any), entity_id: EntityID) {
component_pool_remove_data :: proc(component_pool: ^ComponentPool($T), entity_id: EntityID) {
log.assertf(entity_id in component_pool.entity_to_index, "Entity doesn't have component")
removed_entity_idx := component_pool.entity_to_index[entity_id]
@ -48,20 +48,20 @@ component_pool_remove_data :: proc(component_pool: ^ComponentPool(any), entity_i
component_pool.size -= 1
}
component_pool_get :: proc($T: typeid, component_pool: ^ComponentPool(T), entity_id: EntityID) -> ^T {
component_pool_get :: proc(component_pool: ^ComponentPool($T), entity_id: EntityID) -> ^T {
log.assertf(entity_id in component_pool.entity_to_index, "Entity doesn't have component")
idx := component_pool.entity_to_index[entity_id]
return &component_pool.data[idx]
}
component_pool_destroy_entity :: proc(component_pool: ^ComponentPool(any), entity_id: EntityID) {
component_pool_destroy_entity :: proc(component_pool: ^ComponentPool($T), entity_id: EntityID) {
if entity_id in component_pool.entity_to_index {
component_pool_remove_data(component_pool, entity_id)
}
}
component_pool_destroy :: proc(component_pool: ^ComponentPool(any)) {
component_pool_destroy :: proc(component_pool: ^ComponentPool($T)) {
delete(component_pool.entity_to_index)
delete(component_pool.index_to_entity)
delete(component_pool.data)