diff --git a/src/ecs.odin b/src/ecs.odin index cfd30ef..821e8d3 100644 --- a/src/ecs.odin +++ b/src/ecs.odin @@ -13,15 +13,18 @@ App :: struct { entities: [dynamic]Entity, } +// add a new system to the app that will run based on the provided schedule add_system :: proc(app: ^App, schedule: int, system: proc()) { append(&app.systems[schedule], system) } +// add a new plugin to the app. a modular way to add new functionality from beyond the main procedure add_plugin :: proc(app: ^App, plugin: proc(app: ^App)) { append(&app.plugins, plugin) } -run :: proc(app: ^App, entity_limit: int = 1000) { +// run the app +run :: proc(app: ^App) { for p in app.plugins { p(app) } diff --git a/src/entities.odin b/src/entities.odin index 03ecc71..a94e8cf 100644 --- a/src/entities.odin +++ b/src/entities.odin @@ -8,6 +8,7 @@ Storage :: struct($T: typeid) { data: map[Entity]T, } +// adds a new entity id to app and returns it spawn_entity :: proc(app: ^App) -> Entity { entity_num := len(&app.entities) entity: Entity = Entity(entity_num) @@ -15,23 +16,30 @@ spawn_entity :: proc(app: ^App) -> Entity { return entity } +// create a new component of type and add to Storage add_component :: proc { add_fresh_component, add_component_with_data, } +// initializes new component of type and add to Storage add_fresh_component :: proc(entity: Entity, storage: ^Storage($T)) { storage.data[entity] = T{} } +// creates a copy of component type with data and add to Storage add_component_with_data :: proc(entity: Entity, storage: ^Storage($T), component: T) { storage.data[entity] = component } +// get components saved in Storage +// returns storage.data slice :: proc(storage: ^Storage($T)) -> map[Entity]T { return storage.data } +// get component saved in Storage +// returns Entity and component of type single :: proc(storage: ^Storage($T)) -> (Entity, ^T) { for i, &v in slice(storage) { return i, &v @@ -39,6 +47,7 @@ single :: proc(storage: ^Storage($T)) -> (Entity, ^T) { return Entity{}, new(T) } +// get component of type from entity get_component :: proc(entity: Entity, storage: ^Storage($T)) -> ^T { if c, exists := &storage.data[entity]; exists { return c diff --git a/src/systems.odin b/src/systems.odin index a06e137..12a6941 100644 --- a/src/systems.odin +++ b/src/systems.odin @@ -13,7 +13,7 @@ DefaultPlugin :: proc(app: ^App) { ) } - +@(private) draw_sprite :: proc() { for i, &v in slice(&Sprite) { sprite_transform := get_component(i, &Transform)