Standardise and make ECS more robust

This commit is contained in:
Hugo Mårdbrink 2025-08-28 14:31:55 +02:00
parent b9aaeb62c9
commit 2ba1022f79
13 changed files with 245 additions and 232 deletions

View file

@ -1,26 +1,23 @@
#+private
package ecs
import "core:log"
ComponentPool :: struct(T: typeid) {
data: []T,
ComponentPool :: struct(Component: typeid) {
data: []Component,
entity_to_index: map[EntityID]uintptr,
index_to_entity: map[uintptr]EntityID,
size: uintptr,
}
component_pool_create :: proc($T: typeid) -> ComponentPool(T) {
component_pool := ComponentPool(T) {
data = make([]T, ENTITY_MAX),
entity_to_index = make(map[EntityID]uintptr, context.allocator),
index_to_entity = make(map[uintptr]EntityID, context.allocator),
size = 0,
}
return component_pool
component_pool_init :: proc(component_pool: ^ComponentPool($Component)) {
component_pool.data = make([]Component, ENTITY_MAX, context.allocator)
component_pool.entity_to_index = make(map[EntityID]uintptr, context.allocator)
component_pool.index_to_entity = make(map[uintptr]EntityID, context.allocator)
component_pool.size = 0
}
component_pool_insert_data :: proc(component_pool: ^ComponentPool($T), entity_id: EntityID, component: T) {
component_pool_insert_data :: proc(component_pool: ^ComponentPool($Component), entity_id: EntityID, component: Component) {
log.assertf(entity_id not_in component_pool.entity_to_index, "Component already added to entity")
new_idx := component_pool.size
@ -31,7 +28,7 @@ component_pool_insert_data :: proc(component_pool: ^ComponentPool($T), entity_id
component_pool.size += 1
}
component_pool_remove_data :: proc(component_pool: ^ComponentPool($T), entity_id: EntityID) {
component_pool_remove_data :: proc(component_pool: ^ComponentPool($Component), 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,21 +45,21 @@ component_pool_remove_data :: proc(component_pool: ^ComponentPool($T), entity_id
component_pool.size -= 1
}
component_pool_get :: proc(component_pool: ^ComponentPool($T), entity_id: EntityID) -> ^T {
component_pool_get :: proc(component_pool: ^ComponentPool($Component), entity_id: EntityID) -> ^Component {
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($T), entity_id: EntityID) {
component_pool_destroy_entity :: proc(component_pool: ^ComponentPool($Component), 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($T)) {
component_pool_delete :: proc(component_pool: ^ComponentPool($Component)) {
delete(component_pool.data)
delete(component_pool.entity_to_index)
delete(component_pool.index_to_entity)
delete(component_pool.data)
}