omniecs 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
omniecs/__init__.py ADDED
File without changes
omniecs/components.py ADDED
@@ -0,0 +1,7 @@
1
+ from dataclasses import dataclass
2
+
3
+ from omniecs.types import Component
4
+
5
+
6
+ @dataclass
7
+ class Spawned(Component): ...
omniecs/managers.py ADDED
@@ -0,0 +1,187 @@
1
+ from collections import defaultdict
2
+ from itertools import chain, count
3
+ from typing import Iterable
4
+
5
+ from omniecs.system import System
6
+ from omniecs.types import R, Component, Cs, DrawCommand, EntityId, Event, ExecutionStage, Resource
7
+
8
+
9
+ class EntityManager:
10
+
11
+ def __init__(self):
12
+ self._next_entity_id = count()
13
+ self._entities: set[EntityId] = set()
14
+
15
+ def create(self) -> EntityId:
16
+ entity_id = EntityId(next(self._next_entity_id))
17
+ self._entities.add(entity_id)
18
+ return entity_id
19
+
20
+ def destroy(self, entity_id: EntityId) -> None:
21
+ self._entities.discard(entity_id)
22
+
23
+ def exists(self, entity_id: EntityId) -> bool:
24
+ return entity_id in self._entities
25
+
26
+
27
+ class ComponentStorage[C: Component]:
28
+
29
+ def __init__(self):
30
+ self._components: dict[EntityId, C] = {}
31
+
32
+ def add(self, entity_id: EntityId, component: C) -> None:
33
+ self._components[entity_id] = component
34
+
35
+ def remove(self, entity_id: EntityId) -> None:
36
+ self._components.pop(entity_id, None)
37
+
38
+ def get(self, entity_id: EntityId) -> C | None:
39
+ return self._components.get(entity_id, None)
40
+
41
+ def has(self, entity_id: EntityId) -> bool:
42
+ return entity_id in self._components
43
+
44
+ def entities(self) -> set[EntityId]:
45
+ return set(self._components.keys())
46
+
47
+
48
+ class ComponentManager:
49
+
50
+ def __init__(self):
51
+ self._component_storage: dict[type[Component], ComponentStorage[Component]] = defaultdict(ComponentStorage)
52
+ self._temporary_components: set[type[Component]] = set()
53
+
54
+ def add(self, entity_id: EntityId, component: Component) -> None:
55
+ self._component_storage[type(component)].add(entity_id, component)
56
+
57
+ def remove(self, entity_id: EntityId, component_type: type[Component]) -> None:
58
+ self._component_storage.get(component_type, ComponentStorage()).remove(entity_id)
59
+
60
+ def remove_all(self, component_type: type[Component]) -> None:
61
+ self._component_storage.pop(component_type, None)
62
+
63
+ def destroy(self, entity_id: EntityId) -> None:
64
+ for storage in self._component_storage.values():
65
+ storage.remove(entity_id)
66
+
67
+ def has(self, entity_id: EntityId, component_type: type[Component]) -> bool:
68
+ return component_type in self._component_storage and self._component_storage[component_type].has(entity_id)
69
+
70
+ def get(self, entity_id: EntityId, component_type: type[Component]) -> Component | None:
71
+ return self._component_storage[component_type].get(entity_id)
72
+
73
+ def query_entities(self,
74
+ all_of: tuple[type[Component], ...] = (),
75
+ any_of: tuple[type[Component], ...] = (),
76
+ none_of: tuple[type[Component], ...] = ()
77
+ ) -> set[EntityId]:
78
+ if len(self._component_storage) == 0:
79
+ return set()
80
+
81
+ entities = set.union(*(storage.entities() for storage in self._component_storage.values()))
82
+
83
+ if all_of:
84
+ for component in all_of:
85
+ entities.intersection_update(self._component_storage[component].entities())
86
+ if not entities:
87
+ return entities
88
+
89
+ if any_of:
90
+ candidates = set.union(*(self._component_storage[c].entities() for c in any_of))
91
+ entities.intersection_update(candidates)
92
+ if not entities:
93
+ return entities
94
+
95
+ if none_of:
96
+ candidates = set.union(*(self._component_storage[c].entities() for c in none_of))
97
+ entities.difference_update(candidates)
98
+
99
+ return entities
100
+
101
+ def query(self,
102
+ *component_types: *Cs,
103
+ all_of: tuple[type[Component], ...] = (),
104
+ any_of: tuple[type[Component], ...] = (),
105
+ none_of: tuple[type[Component], ...] = ()
106
+ ) -> Iterable[tuple[EntityId, tuple[*Cs]]]:
107
+ entities = self.query_entities(all_of + component_types, any_of, none_of)
108
+
109
+ for entity in entities:
110
+ components = tuple(self._component_storage[c].get(entity) for c in component_types)
111
+ yield entity, components
112
+
113
+ def register_temporary_component(self, component_type: type[Component]) -> None:
114
+ self._temporary_components.add(component_type)
115
+
116
+ def get_temporary_components(self) -> set[type[Component]]:
117
+ return self._temporary_components
118
+
119
+
120
+ class SystemManager:
121
+
122
+ def __init__(self):
123
+ self._systems: dict[ExecutionStage, list[System]] = defaultdict(list)
124
+
125
+ @property
126
+ def all_systems(self) -> Iterable[System]:
127
+ return chain(*self._systems.values())
128
+
129
+ def add(self, system: System, stage: ExecutionStage) -> None:
130
+ if any(isinstance(s, type(system)) for s in self.all_systems):
131
+ raise ValueError(f'System {type(system).__name__} already registered')
132
+ self._systems[stage].append(system)
133
+
134
+ def remove(self, system_type: type[System]) -> None:
135
+ for stage, systems in self._systems.items():
136
+ self._systems[stage] = [system for system in systems if not isinstance(system, system_type)]
137
+
138
+ def execute(self, delta_time: float) -> None:
139
+ for stage in ExecutionStage:
140
+ for system in self._systems[stage]:
141
+ system.execute(delta_time)
142
+
143
+
144
+ class ResourceManager:
145
+
146
+ def __init__(self):
147
+ self._resources: dict[type[Resource], Resource] = {}
148
+
149
+ def set(self, resource: Resource) -> None:
150
+ self._resources[type(resource)] = resource
151
+
152
+ def get(self, resource_type: type[R]) -> R | None:
153
+ return self._resources.get(resource_type, None)
154
+
155
+ def remove(self, resource_type: type[Resource]) -> None:
156
+ self._resources.pop(resource_type, None)
157
+
158
+
159
+ class EventManager:
160
+
161
+ def __init__(self):
162
+ self._events: list[Event] = []
163
+
164
+ def push(self, event: Event) -> None:
165
+ self._events.append(event)
166
+
167
+ def get(self) -> list[Event]:
168
+ return self._events
169
+
170
+ def clear(self) -> None:
171
+ self._events.clear()
172
+
173
+
174
+
175
+ class RenderManager:
176
+
177
+ def __init__(self):
178
+ self._queue: list[DrawCommand] = []
179
+
180
+ def push(self, command: DrawCommand) -> None:
181
+ self._queue.append(command)
182
+
183
+ def get(self) -> list[DrawCommand]:
184
+ return sorted(self._queue, key=lambda c: (c.global_layer, c.local_layer))
185
+
186
+ def clear(self) -> None:
187
+ self._queue.clear()
omniecs/system.py ADDED
@@ -0,0 +1,39 @@
1
+ from __future__ import annotations
2
+ from typing import TYPE_CHECKING
3
+
4
+
5
+ if TYPE_CHECKING:
6
+ from omniecs.world import World
7
+
8
+
9
+ class System:
10
+
11
+ def __init__(self):
12
+ self.world: World | None = None
13
+
14
+ def register(self, world: World) -> None:
15
+ self.world = world
16
+ self.on_register()
17
+
18
+ def on_register(self) -> None: ...
19
+
20
+ def execute(self, delta_time: float) -> None: ...
21
+
22
+
23
+ class ClearEventSystem(System):
24
+
25
+ def execute(self, _: float) -> None:
26
+ self.world.clear_events()
27
+
28
+
29
+ class ClearRenderQueueSystem(System):
30
+
31
+ def execute(self, _: float) -> None:
32
+ self.world.clear_draw_commands()
33
+
34
+
35
+ class ClearTemporaryComponentSystem(System):
36
+
37
+ def execute(self, _: float) -> None:
38
+ for component_type in self.world.get_temporary_components():
39
+ self.world.remove_all(component_type)
omniecs/types.py ADDED
@@ -0,0 +1,37 @@
1
+ from dataclasses import dataclass
2
+ from enum import Enum
3
+ from typing import Iterable, Protocol, TypeVar, TypeVarTuple
4
+
5
+
6
+ @dataclass(slots=True, frozen=True)
7
+ class EntityId:
8
+ value: int
9
+
10
+
11
+ class Component: ...
12
+
13
+
14
+ class Bundle(Protocol):
15
+ def components(self) -> Iterable[Component]: ...
16
+
17
+
18
+ class ExecutionStage(Enum):
19
+ reset = 0
20
+ update = 1
21
+ cleanup = 2
22
+
23
+
24
+ class Resource: ...
25
+
26
+
27
+ class Event: ...
28
+
29
+
30
+ @dataclass(kw_only=True)
31
+ class DrawCommand:
32
+ global_layer: int
33
+ local_layer: int = 0
34
+
35
+
36
+ R = TypeVar("R", bound=Resource)
37
+ Cs = TypeVarTuple("Cs")
omniecs/world.py ADDED
@@ -0,0 +1,141 @@
1
+ from typing import Iterable
2
+
3
+ from omniecs.components import Spawned
4
+ from omniecs.managers import ComponentManager, EntityManager, EventManager, RenderManager, ResourceManager, SystemManager
5
+ from omniecs.system import ClearEventSystem, ClearRenderQueueSystem, ClearTemporaryComponentSystem, System
6
+ from omniecs.types import R, Component, Cs, DrawCommand, EntityId, Event, ExecutionStage, Resource
7
+
8
+
9
+ class World:
10
+
11
+ def __init__(self,
12
+ entity_manager: EntityManager,
13
+ component_manager: ComponentManager,
14
+ system_manager: SystemManager,
15
+ resource_manager: ResourceManager,
16
+ event_manager: EventManager,
17
+ render_manager: RenderManager):
18
+ self._entities = entity_manager
19
+ self._components = component_manager
20
+ self._systems = system_manager
21
+ self._resources = resource_manager
22
+ self._events = event_manager
23
+ self._render = render_manager
24
+ self._running = True
25
+
26
+ def quit(self) -> None:
27
+ self._running = False
28
+
29
+ @property
30
+ def is_running(self) -> bool:
31
+ return self._running
32
+
33
+ def spawn(self, *components: Component) -> EntityId:
34
+ entity_id = self._entities.create()
35
+ for component in components:
36
+ self.add_component(entity_id, component)
37
+ self.add_component(entity_id, Spawned())
38
+ return entity_id
39
+
40
+ def destroy(self, entity_id: EntityId) -> None:
41
+ self._components.destroy(entity_id)
42
+ self._entities.destroy(entity_id)
43
+
44
+ def exists(self, entity_id: EntityId) -> bool:
45
+ return self._entities.exists(entity_id)
46
+
47
+ def add_component(self, entity_id: EntityId, component: Component) -> None:
48
+ self._components.add(entity_id, component)
49
+
50
+ def add_components(self, entity_id: EntityId, components: list[Component]) -> None:
51
+ for component in components:
52
+ self.add_component(entity_id, component)
53
+
54
+ def remove_component(self, entity_id: EntityId, component_type: type[Component]) -> None:
55
+ self._components.remove(entity_id, component_type)
56
+
57
+ def get_component(self, entity_id: EntityId, component_type: type[Component]) -> Component | None:
58
+ return self._components.get(entity_id, component_type)
59
+
60
+ def remove_all(self, component_type: type[Component]) -> None:
61
+ self._components.remove_all(component_type)
62
+
63
+ def query_entities(self,
64
+ all_of: tuple[type[Component], ...] = (),
65
+ any_of: tuple[type[Component], ...] = (),
66
+ none_of: tuple[type[Component], ...] = ()
67
+ ) -> set[EntityId]:
68
+ return self._components.query_entities(all_of, any_of, none_of)
69
+
70
+
71
+ def query(self,
72
+ *component_types: *Cs,
73
+ all_of: tuple[type[Component], ...] = (),
74
+ any_of: tuple[type[Component], ...] = (),
75
+ none_of: tuple[type[Component], ...] = ()
76
+ ) -> Iterable[tuple[EntityId, tuple[*Cs]]]:
77
+ yield from self._components.query(*component_types, all_of=all_of, any_of=any_of, none_of=none_of)
78
+
79
+ def register_temporary_component(self, component_type: type[Component]) -> None:
80
+ self._components.register_temporary_component(component_type)
81
+
82
+ def get_temporary_components(self) -> set[type[Component]]:
83
+ return self._components.get_temporary_components()
84
+
85
+ def register_system(self, system: System, stage: ExecutionStage=ExecutionStage.update) -> None:
86
+ system.register(self)
87
+ self._systems.add(system, stage)
88
+
89
+ def unregister_system(self, system_type: type[System]) -> None:
90
+ self._systems.remove(system_type)
91
+
92
+ def execute(self, delta_time: float = 0) -> None:
93
+ self._systems.execute(delta_time)
94
+
95
+ def set_resource(self, resource: Resource) -> None:
96
+ self._resources.set(resource)
97
+
98
+ def get_resource(self, resource: type[R]) -> R:
99
+ return self._resources.get(resource)
100
+
101
+ def push_event(self, event: Event) -> None:
102
+ self._events.push(event)
103
+
104
+ def get_events(self) -> list[Event]:
105
+ return self._events.get()
106
+
107
+ def clear_events(self) -> None:
108
+ self._events.clear()
109
+
110
+ def add_draw_command(self, draw_command: DrawCommand) -> None:
111
+ self._render.push(draw_command)
112
+
113
+ def get_draw_commands(self ) -> list[DrawCommand]:
114
+ return self._render.get()
115
+
116
+ def clear_draw_commands(self) -> None:
117
+ self._render.clear()
118
+
119
+
120
+ class WorldFactory:
121
+
122
+ @staticmethod
123
+ def create_world() -> World:
124
+ world = WorldFactory.create_empty_world()
125
+ world.register_system(ClearRenderQueueSystem(), ExecutionStage.reset)
126
+ world.register_system(ClearEventSystem(), ExecutionStage.cleanup)
127
+ world.register_system(ClearTemporaryComponentSystem(), ExecutionStage.cleanup)
128
+ world.register_temporary_component(Spawned)
129
+
130
+ return world
131
+
132
+ @staticmethod
133
+ def create_empty_world() -> World:
134
+ entity_manager = EntityManager()
135
+ component_manager = ComponentManager()
136
+ system_manager = SystemManager()
137
+ resource_manager = ResourceManager()
138
+ event_manager = EventManager()
139
+ render_manager = RenderManager()
140
+
141
+ return World(entity_manager, component_manager, system_manager, resource_manager, event_manager, render_manager)
@@ -0,0 +1,12 @@
1
+ Metadata-Version: 2.4
2
+ Name: omniecs
3
+ Version: 0.2.0
4
+ Summary: A lightweight, modular, and extensible Entity-Component-System framework.
5
+ Author-email: Matheus Brenner Lehmann <legus.codes@gmail.com>
6
+ License: MIT
7
+ Requires-Python: >=3.9
8
+ License-File: LICENSE
9
+ Provides-Extra: test
10
+ Requires-Dist: pytest>=7.0; extra == "test"
11
+ Requires-Dist: pytest-cov; extra == "test"
12
+ Dynamic: license-file
@@ -0,0 +1,11 @@
1
+ omniecs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ omniecs/components.py,sha256=JcRU_V0XwMYPDXi_vAZjvliq14O1MdRlhpj_WW-vgeo,121
3
+ omniecs/managers.py,sha256=JHs4eTk9LZSCxsL-Yy40t2Mx9KkFqB8sjpGbYvj04_I,6513
4
+ omniecs/system.py,sha256=jkLAOqnnRyfn87nIUWPDqFdCkBiX5hoZCGgW_xXKUxM,897
5
+ omniecs/types.py,sha256=iESX-i0OAc4W6BuOMJjCOWFA8lX_GyJMu7zCc8vkySQ,597
6
+ omniecs/world.py,sha256=wa81geyiAz0PriaK9ZV0_vp2pYqFeGTfURCTJbTEMcw,5494
7
+ omniecs-0.2.0.dist-info/licenses/LICENSE,sha256=Os0WYgx_nC5tqtt85RnEz52Fk0XCrJZehYTfPQaGSeo,1083
8
+ omniecs-0.2.0.dist-info/METADATA,sha256=OINkOnb3JzKYGCCqPGcqRUtaloiR20OpDgAH4Cs9Rbw,396
9
+ omniecs-0.2.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
10
+ omniecs-0.2.0.dist-info/top_level.txt,sha256=4-GuB6YmotfbiwLSjAVkDKsmjNvX2QiGt0_PcQt2Rpo,8
11
+ omniecs-0.2.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Legus
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ omniecs