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,82 +1,70 @@
package ecs
Coordinator :: struct {
component_manager: ^ComponentManager,
entity_manager: ^EntityManager,
system_manager: ^SystemManager,
component_manager: ComponentManager,
entity_manager: EntityManager,
system_manager: SystemManager,
}
coordinator_create :: proc() -> Coordinator {
coordinator := Coordinator{}
coordinator.component_manager = new(ComponentManager, context.allocator)
coordinator.component_manager^ = component_manager_create()
coordinator.entity_manager = new(EntityManager, context.allocator)
coordinator.entity_manager^ = entity_manager_create()
coordinator.system_manager = new(SystemManager, context.allocator)
coordinator.system_manager^ = system_manager_create()
return coordinator
coordinator_init:: proc(coordinator: ^Coordinator) {
component_manager_init(&coordinator.component_manager)
entity_manager_init(&coordinator.entity_manager)
system_manager_init(&coordinator.system_manager)
}
coordinator_create_entity :: proc(coordinator: ^Coordinator) -> EntityID {
return entity_manager_create_entity(coordinator.entity_manager)
return entity_manager_create_entity(&coordinator.entity_manager)
}
coordinator_destroy_entity :: proc(coordinator: ^Coordinator, entity_id: EntityID) {
component_manager_destroy_entity(coordinator.component_manager, entity_id)
entity_manager_destroy_entity(coordinator.entity_manager, entity_id)
system_manager_destroy_entity(coordinator.system_manager, entity_id)
component_manager_destroy_entity(&coordinator.component_manager, entity_id)
entity_manager_destroy_entity(&coordinator.entity_manager, entity_id)
system_manager_destroy_entity(&coordinator.system_manager, entity_id)
}
coordinator_register_component :: proc($T: typeid, coordinator: ^Coordinator) {
component_manager_register_component(T, coordinator.component_manager)
coordinator_register_component :: proc(coordinator: ^Coordinator, $Component: typeid) {
component_manager_register_component(Component, &coordinator.component_manager)
}
coordinator_add_component :: proc($T: typeid, coordinator: ^Coordinator, entity_id: EntityID, component: T) {
component_manager_add_component(T, coordinator.component_manager, entity_id, component)
coordinator_add_component :: proc(coordinator: ^Coordinator, component: $Component, entity_id: EntityID) {
component_manager_add_component(&coordinator.component_manager, component, entity_id)
signature := entity_manager_get_signature(coordinator.entity_manager, entity_id)
signature_set(&signature, component_manager_get_component_type(T, coordinator.component_manager))
entity_manager_set_signature(coordinator.entity_manager, entity_id, signature)
signature := entity_manager_get_signature(&coordinator.entity_manager, entity_id)
signature_set(&signature, component_manager_get_component_id(&coordinator.component_manager, Component))
entity_manager_set_signature(&coordinator.entity_manager, entity_id, signature)
system_manager_change_entity_signature(coordinator.system_manager, entity_id, signature)
system_manager_change_entity_signature(&coordinator.system_manager, entity_id, signature)
}
coordinator_remove_component :: proc($T: typeid, coordinator: ^Coordinator, entity_id: EntityID) {
component_manager_remove_component(T, coordinator.component_manager, entity_id)
coordinator_remove_component :: proc(coordinator: ^Coordinator, $Component: typeid, entity_id: EntityID) {
component_manager_remove_component(&coordinator.component_manager, Component, entity_id)
signature := entity_manager_get_signature(coordinator.entity_manager, entity_id)
signature_unset(&signature, component_manager_get_component_type(T, coordinator.component_manager))
entity_manager_set_signature(coordinator.entity_manager, entity_id, signature)
signature := entity_manager_get_signature(&coordinator.entity_manager, entity_id)
signature_unset(&signature, component_manager_get_component_id(&coordinator.component_manager, Component))
entity_manager_set_signature(&coordinator.entity_manager, entity_id, signature)
system_manager_change_entity_signature(coordinator.system_manager, entity_id, signature)
system_manager_change_entity_signature(&coordinator.system_manager, entity_id, signature)
}
coordinator_get_component :: proc($T: typeid, coordinator: ^Coordinator, entity_id: EntityID) -> ^T {
return component_manager_get_component(T, coordinator.component_manager, entity_id)
coordinator_get_component :: proc(coordinator: ^Coordinator, $Component: typeid, entity_id: EntityID) -> ^Component {
return component_manager_get_component(&coordinator.component_manager, Component, entity_id)
}
coordinator_get_component_type :: proc($T: typeid, coordinator: ^Coordinator) -> ComponentType {
return component_manager_get_component_type(T, coordinator.component_manager)
coordinator_get_component_id :: proc(coordinator: ^Coordinator, $Component: typeid) -> ComponentID {
return component_manager_get_component_id(&coordinator.component_manager, Component)
}
coordinator_register_system :: proc($T: typeid, coordinator: ^Coordinator) -> ^T {
return system_manager_register_system(T, coordinator.system_manager)
coordinator_register_system :: proc(coordinator: ^Coordinator, $System: typeid) -> ^System {
return system_manager_register_system(&coordinator.system_manager, System)
}
coordinator_set_system_signature :: proc($T: typeid, coordinator: ^Coordinator, signature: Signature) {
system_manager_set_signature(T, coordinator.system_manager, signature)
coordinator_set_system_signature :: proc(coordinator: ^Coordinator, $System: typeid, signature: Signature) {
system_manager_set_signature(&coordinator.system_manager, System, signature)
}
coordinator_destroy :: proc(coordinator: ^Coordinator) {
component_manager_destroy(coordinator.component_manager)
entity_manager_destroy(coordinator.entity_manager)
system_manager_destroy(coordinator.system_manager)
free(coordinator.component_manager)
free(coordinator.entity_manager)
free(coordinator.system_manager)
coordinator_delete :: proc(coordinator: ^Coordinator) {
component_manager_delete(&coordinator.component_manager)
entity_manager_delete(&coordinator.entity_manager)
system_manager_delete(&coordinator.system_manager)
}