omniecs 0.2.0__tar.gz

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-0.2.0/LICENSE ADDED
@@ -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.
omniecs-0.2.0/PKG-INFO ADDED
@@ -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,2 @@
1
+ # OmniECS
2
+ OmniECS is a lightweight, modular, and extensible Entity-Component-System framework implemented in Python for game development, designed to separate logic, data, and rendering.
@@ -0,0 +1,15 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "omniecs"
7
+ version = "0.2.0"
8
+ description = "A lightweight, modular, and extensible Entity-Component-System framework."
9
+ authors = [{ name = "Matheus Brenner Lehmann", email = "legus.codes@gmail.com" }]
10
+ license = { text = "MIT" }
11
+ requires-python = ">=3.9"
12
+ dependencies = []
13
+
14
+ [project.optional-dependencies]
15
+ test = ["pytest>=7.0", "pytest-cov"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
File without changes
@@ -0,0 +1,7 @@
1
+ from dataclasses import dataclass
2
+
3
+ from omniecs.types import Component
4
+
5
+
6
+ @dataclass
7
+ class Spawned(Component): ...
@@ -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()
@@ -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)
@@ -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")
@@ -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,22 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/omniecs/__init__.py
5
+ src/omniecs/components.py
6
+ src/omniecs/managers.py
7
+ src/omniecs/system.py
8
+ src/omniecs/types.py
9
+ src/omniecs/world.py
10
+ src/omniecs.egg-info/PKG-INFO
11
+ src/omniecs.egg-info/SOURCES.txt
12
+ src/omniecs.egg-info/dependency_links.txt
13
+ src/omniecs.egg-info/requires.txt
14
+ src/omniecs.egg-info/top_level.txt
15
+ tests/test_component_manager.py
16
+ tests/test_entity_manager.py
17
+ tests/test_event_manager.py
18
+ tests/test_render_manager.py
19
+ tests/test_resource_manager.py
20
+ tests/test_system.py
21
+ tests/test_system_manager.py
22
+ tests/test_world.py
@@ -0,0 +1,4 @@
1
+
2
+ [test]
3
+ pytest>=7.0
4
+ pytest-cov
@@ -0,0 +1 @@
1
+ omniecs
@@ -0,0 +1,189 @@
1
+ from omniecs.managers import ComponentManager, ComponentStorage
2
+ from omniecs.types import Component, EntityId
3
+
4
+
5
+ class MockComponent(Component): ...
6
+
7
+ class MockComponentA(Component): ...
8
+ class MockComponentB(Component): ...
9
+ class MockComponentC(Component): ...
10
+ class MockComponentD(Component): ...
11
+ class MockComponentE(Component): ...
12
+
13
+
14
+ def test_add_component_storage():
15
+ component_storage = ComponentStorage[MockComponent]()
16
+ entity_id = EntityId(1)
17
+ component = MockComponent()
18
+ component_storage.add(entity_id, component)
19
+
20
+ assert component_storage.has(entity_id)
21
+ assert component_storage.get(entity_id) == component
22
+
23
+ def test_remove_component_storage():
24
+ component_storage = ComponentStorage[MockComponent]()
25
+ entity_id = EntityId(1)
26
+ component = MockComponent()
27
+ component_storage.add(entity_id, component)
28
+ component_storage.remove(entity_id)
29
+
30
+ assert not component_storage.has(entity_id)
31
+ assert component_storage.get(entity_id) is None
32
+
33
+ def test_remove_non_existing_entity():
34
+ component_storage = ComponentStorage[MockComponent]()
35
+ entity_id = EntityId(1)
36
+ component_storage.remove(entity_id)
37
+
38
+ assert not component_storage.has(entity_id)
39
+ assert component_storage.get(entity_id) is None
40
+
41
+ def test_component_storage_entities():
42
+ component_storage = ComponentStorage[MockComponent]()
43
+ entity_ids = set([EntityId(1), EntityId(2), EntityId(3), EntityId(4)])
44
+ component = MockComponent()
45
+ for entity_id in entity_ids:
46
+ component_storage.add(entity_id, component)
47
+
48
+ assert component_storage.entities() == entity_ids
49
+
50
+ def test_add_component_manager():
51
+ component_manager = ComponentManager()
52
+ entity_id = EntityId(1)
53
+ component_manager.add(entity_id, MockComponentA())
54
+
55
+ assert component_manager.has(entity_id, MockComponentA)
56
+
57
+ def test_add_multiple_components_component_manager():
58
+ component_manager = ComponentManager()
59
+ entity_id = EntityId(1)
60
+ component_manager.add(entity_id, MockComponentA())
61
+ component_manager.add(entity_id, MockComponentB())
62
+ component_manager.add(entity_id, MockComponentC())
63
+ component_manager.add(entity_id, MockComponentD())
64
+
65
+ assert component_manager.has(entity_id, MockComponentA)
66
+ assert component_manager.has(entity_id, MockComponentB)
67
+ assert component_manager.has(entity_id, MockComponentC)
68
+ assert component_manager.has(entity_id, MockComponentD)
69
+
70
+ def test_remove_component_manager():
71
+ component_manager = ComponentManager()
72
+ entity_id = EntityId(1)
73
+ component_manager.add(entity_id, MockComponentA())
74
+ component_manager.remove(entity_id, MockComponentA)
75
+
76
+ assert not component_manager.has(entity_id, MockComponentA)
77
+
78
+ def test_destroy_component_manager():
79
+ component_manager = ComponentManager()
80
+ entity_id = EntityId(1)
81
+ component_manager.add(entity_id, MockComponentA())
82
+ component_manager.add(entity_id, MockComponentB())
83
+ component_manager.add(entity_id, MockComponentC())
84
+ component_manager.add(entity_id, MockComponentD())
85
+ component_manager.destroy(entity_id)
86
+
87
+ assert not component_manager.has(entity_id, MockComponentA)
88
+ assert not component_manager.has(entity_id, MockComponentB)
89
+ assert not component_manager.has(entity_id, MockComponentC)
90
+ assert not component_manager.has(entity_id, MockComponentD)
91
+
92
+ def test_query_all_of_entities_component_manager():
93
+ component_manager = ComponentManager()
94
+ entity_id1 = EntityId(1)
95
+ entity_id2 = EntityId(2)
96
+ entity_id3 = EntityId(3)
97
+ entity_id4 = EntityId(4)
98
+ entity_id5 = EntityId(5)
99
+ component_manager.add(entity_id1, MockComponentA())
100
+ component_manager.add(entity_id2, MockComponentB())
101
+ component_manager.add(entity_id3, MockComponentC())
102
+ component_manager.add(entity_id4, MockComponentD())
103
+ component_manager.add(entity_id5, MockComponentE())
104
+ component_manager.add(entity_id2, MockComponentE())
105
+
106
+ entities = component_manager.query_entities(all_of=(MockComponentB, MockComponentE))
107
+ assert entities == set([entity_id2])
108
+
109
+ def test_query_any_of_entities_component_manager():
110
+ component_manager = ComponentManager()
111
+ entity_id1 = EntityId(1)
112
+ entity_id2 = EntityId(2)
113
+ entity_id3 = EntityId(3)
114
+ entity_id4 = EntityId(4)
115
+ entity_id5 = EntityId(5)
116
+ component_manager.add(entity_id1, MockComponentA())
117
+ component_manager.add(entity_id2, MockComponentB())
118
+ component_manager.add(entity_id3, MockComponentC())
119
+ component_manager.add(entity_id4, MockComponentD())
120
+ component_manager.add(entity_id5, MockComponentE())
121
+ component_manager.add(entity_id3, MockComponentA())
122
+
123
+ entities = component_manager.query_entities(any_of=(MockComponentA, MockComponentD))
124
+ assert entities == set([entity_id1, entity_id3, entity_id4])
125
+
126
+ def test_query_none_of_entities_component_manager():
127
+ component_manager = ComponentManager()
128
+ entity_id1 = EntityId(1)
129
+ entity_id2 = EntityId(2)
130
+ entity_id3 = EntityId(3)
131
+ entity_id4 = EntityId(4)
132
+ entity_id5 = EntityId(5)
133
+ component_manager.add(entity_id1, MockComponentA())
134
+ component_manager.add(entity_id2, MockComponentB())
135
+ component_manager.add(entity_id3, MockComponentC())
136
+ component_manager.add(entity_id4, MockComponentD())
137
+ component_manager.add(entity_id5, MockComponentE())
138
+ component_manager.add(entity_id1, MockComponentB())
139
+
140
+ entities = component_manager.query_entities(none_of=(MockComponentB, MockComponentC))
141
+ assert entities == set([entity_id4, entity_id5])
142
+
143
+ def test_query_entities_component_manager():
144
+ component_manager = ComponentManager()
145
+ entity_id1 = EntityId(1)
146
+ entity_id2 = EntityId(2)
147
+ entity_id3 = EntityId(3)
148
+ entity_id4 = EntityId(4)
149
+ entity_id5 = EntityId(5)
150
+ component_manager.add(entity_id1, MockComponentA())
151
+ component_manager.add(entity_id1, MockComponentB())
152
+ component_manager.add(entity_id1, MockComponentC())
153
+ component_manager.add(entity_id2, MockComponentB())
154
+ component_manager.add(entity_id2, MockComponentD())
155
+ component_manager.add(entity_id3, MockComponentC())
156
+ component_manager.add(entity_id3, MockComponentE())
157
+ component_manager.add(entity_id4, MockComponentB())
158
+ component_manager.add(entity_id4, MockComponentE())
159
+ component_manager.add(entity_id5, MockComponentA())
160
+ component_manager.add(entity_id5, MockComponentE())
161
+
162
+ entities = component_manager.query_entities(all_of=(MockComponentB,), any_of=(MockComponentA, MockComponentD), none_of=(MockComponentE,))
163
+ assert entities == set([entity_id1, entity_id2])
164
+
165
+ def test_query_component_manager():
166
+ component_manager = ComponentManager()
167
+ entity_id1 = EntityId(1)
168
+ entity_id2 = EntityId(2)
169
+ entity_id3 = EntityId(3)
170
+ entity_id4 = EntityId(4)
171
+ entity_id5 = EntityId(5)
172
+ component = MockComponentC()
173
+ component_manager.add(entity_id1, MockComponentA())
174
+ component_manager.add(entity_id1, MockComponentB())
175
+ component_manager.add(entity_id1, component)
176
+ component_manager.add(entity_id2, MockComponentB())
177
+ component_manager.add(entity_id2, MockComponentD())
178
+ component_manager.add(entity_id3, MockComponentC())
179
+ component_manager.add(entity_id3, MockComponentE())
180
+ component_manager.add(entity_id4, MockComponentB())
181
+ component_manager.add(entity_id4, MockComponentE())
182
+ component_manager.add(entity_id5, MockComponentA())
183
+ component_manager.add(entity_id5, MockComponentE())
184
+
185
+ entities = list(component_manager.query(MockComponentC, all_of=(MockComponentB,), any_of=(MockComponentA, MockComponentD), none_of=(MockComponentE,)))
186
+ assert len(entities) == 1
187
+ entity, (queried_component,) = entities[0]
188
+ assert entity == entity_id1
189
+ assert queried_component == component
@@ -0,0 +1,30 @@
1
+ from omniecs.managers import EntityManager
2
+ from omniecs.types import EntityId
3
+
4
+
5
+ def test_create_entity():
6
+ entity_manager = EntityManager()
7
+ entity = entity_manager.create()
8
+ assert entity_manager.exists(entity)
9
+ assert isinstance(entity, EntityId)
10
+
11
+
12
+ def test_create_multiple_entities():
13
+ entity_manager = EntityManager()
14
+ entity1 = entity_manager.create()
15
+ entity2 = entity_manager.create()
16
+ assert entity1 != entity2
17
+
18
+
19
+ def test_destroy_entity():
20
+ entity_manager = EntityManager()
21
+ entity = entity_manager.create()
22
+ entity_manager.destroy(entity)
23
+ assert not entity_manager.exists(entity)
24
+
25
+
26
+ def test_destroy_non_existing_entity():
27
+ entity_manager = EntityManager()
28
+ entity = entity_manager.create()
29
+ entity_manager.destroy(EntityId(12))
30
+ assert entity_manager.exists(entity)
@@ -0,0 +1,27 @@
1
+ from omniecs.managers import EventManager
2
+ from omniecs.types import Event
3
+
4
+
5
+ def test_create_event():
6
+ event_manager = EventManager()
7
+ event_manager.push(Event())
8
+ assert len(event_manager._events) == 1
9
+
10
+
11
+ def test_get_events():
12
+ event_manager = EventManager()
13
+ event_manager.push(Event())
14
+ event_manager.push(Event())
15
+ event_manager.push(Event())
16
+ events = event_manager.get()
17
+ assert len(events) == 3
18
+ assert len(event_manager._events) == 3
19
+
20
+
21
+ def test_clear_events():
22
+ event_manager = EventManager()
23
+ event_manager.push(Event())
24
+ event_manager.push(Event())
25
+ event_manager.push(Event())
26
+ event_manager.clear()
27
+ assert len(event_manager._events) == 0
@@ -0,0 +1,38 @@
1
+ from dataclasses import dataclass
2
+
3
+ from omniecs.managers import RenderManager
4
+ from omniecs.types import DrawCommand
5
+
6
+
7
+
8
+ @dataclass(kw_only=True)
9
+ class MockDrawCommand(DrawCommand):
10
+ text: str
11
+
12
+
13
+ def test_create_command():
14
+ render_manager = RenderManager()
15
+ render_manager.push(MockDrawCommand(global_layer=1, text=''))
16
+ assert len(render_manager._queue) == 1
17
+
18
+
19
+ def test_get_commands():
20
+ render_manager = RenderManager()
21
+ render_manager.push(MockDrawCommand(global_layer=3, text='third'))
22
+ render_manager.push(MockDrawCommand(global_layer=1, text='first'))
23
+ render_manager.push(MockDrawCommand(global_layer=2, text='second'))
24
+ commands = render_manager.get()
25
+ assert len(commands) == 3
26
+ assert commands[0].text == 'first'
27
+ assert commands[1].text == 'second'
28
+ assert commands[2].text == 'third'
29
+ assert len(render_manager._queue) == 3
30
+
31
+
32
+ def test_clear_commands():
33
+ render_manager = RenderManager()
34
+ render_manager.push(MockDrawCommand(global_layer=3, text='third'))
35
+ render_manager.push(MockDrawCommand(global_layer=1, text='first'))
36
+ render_manager.push(MockDrawCommand(global_layer=2, text='second'))
37
+ render_manager.clear()
38
+ assert len(render_manager._queue) == 0
@@ -0,0 +1,35 @@
1
+ from dataclasses import dataclass
2
+
3
+ from omniecs.managers import ResourceManager
4
+ from omniecs.types import Resource
5
+
6
+
7
+ class ResourceA(Resource): ...
8
+
9
+
10
+ @dataclass
11
+ class ResourceB(Resource):
12
+ value: str
13
+
14
+
15
+ def test_set_resource():
16
+ resource_manager = ResourceManager()
17
+ resource_manager.set(ResourceA())
18
+ resource = resource_manager.get(ResourceA)
19
+ assert isinstance(resource, ResourceA)
20
+
21
+
22
+ def test_overwrite_resource():
23
+ resource_manager = ResourceManager()
24
+ resource_manager.set(ResourceB('old'))
25
+ resource_manager.set(ResourceB('new'))
26
+ resource = resource_manager.get(ResourceB)
27
+ assert resource.value == 'new'
28
+
29
+
30
+ def test_remove_resource():
31
+ resource_manager = ResourceManager()
32
+ resource_manager.set(ResourceA())
33
+ resource_manager.remove(ResourceA)
34
+ resource = resource_manager.get(ResourceA)
35
+ assert resource is None
@@ -0,0 +1,62 @@
1
+ from dataclasses import dataclass
2
+ from omniecs.system import ClearEventSystem, ClearRenderQueueSystem, ClearTemporaryComponentSystem, System
3
+ from omniecs.types import Component, DrawCommand, Event
4
+ from omniecs.world import WorldFactory
5
+
6
+
7
+ class MockEvent(Event): ...
8
+ class MockTemporaryComponent(Component): ...
9
+ class MockDrawCommand(DrawCommand): ...
10
+
11
+ class MockSystem(System):
12
+ def on_register(self):
13
+ self.world.register_temporary_component(MockTemporaryComponent)
14
+
15
+
16
+ def test_system_on_register_temporary_component():
17
+ world = WorldFactory.create_world()
18
+ world.register_system(MockSystem())
19
+
20
+ world.spawn(MockTemporaryComponent())
21
+ world.spawn(MockTemporaryComponent())
22
+ world.spawn(MockTemporaryComponent())
23
+ assert len(list(world.query_entities(all_of=(MockTemporaryComponent,)))) == 3
24
+
25
+ world.execute(0)
26
+ assert len(list(world.query_entities(all_of=(MockTemporaryComponent,)))) == 0
27
+
28
+
29
+ def test_clear_event_system():
30
+ world = WorldFactory.create_world()
31
+ assert any(isinstance(system, ClearEventSystem) for system in world._systems.all_systems)
32
+
33
+ world.push_event(MockEvent())
34
+ assert len(world.get_events()) == 1
35
+
36
+ world.execute(0)
37
+ assert len(world.get_events()) == 0
38
+
39
+
40
+ def test_clear_render_queue_system():
41
+ world = WorldFactory.create_world()
42
+ assert any(isinstance(system, ClearRenderQueueSystem) for system in world._systems.all_systems)
43
+
44
+ world.add_draw_command(MockDrawCommand(global_layer=3))
45
+ assert len(world.get_draw_commands()) == 1
46
+
47
+ world.execute(0)
48
+ assert len(world.get_draw_commands()) == 0
49
+
50
+
51
+ def test_clear_temporary_component_system():
52
+ world = WorldFactory.create_world()
53
+ assert any(isinstance(system, ClearTemporaryComponentSystem) for system in world._systems.all_systems)
54
+
55
+ world.spawn(MockTemporaryComponent())
56
+ world.spawn(MockTemporaryComponent())
57
+ world.spawn(MockTemporaryComponent())
58
+ world.register_temporary_component(MockTemporaryComponent)
59
+ assert len(list(world.query_entities(all_of=(MockTemporaryComponent,)))) == 3
60
+
61
+ world.execute(0)
62
+ assert len(list(world.query_entities(all_of=(MockTemporaryComponent,)))) == 0
@@ -0,0 +1,61 @@
1
+ import pytest
2
+ from dataclasses import dataclass
3
+
4
+ from omniecs.managers import SystemManager
5
+ from omniecs.system import System
6
+ from omniecs.types import ExecutionStage
7
+
8
+
9
+ @dataclass
10
+ class SystemA(System):
11
+ executed: bool = False
12
+
13
+ def execute(self, delta_time):
14
+ self.executed = True
15
+
16
+ @dataclass
17
+ class SystemB(System):
18
+ executed: bool = False
19
+
20
+ def execute(self, delta_time):
21
+ self.executed = True
22
+
23
+ @dataclass
24
+ class SystemC(System):
25
+ executed: bool = False
26
+
27
+ def execute(self, delta_time):
28
+ self.executed = True
29
+
30
+
31
+ @pytest.mark.parametrize('stage', [ExecutionStage.reset, ExecutionStage.update, ExecutionStage.cleanup])
32
+ def test_add_system_frame_start(stage: ExecutionStage):
33
+ system_manager = SystemManager()
34
+ system_manager.add(SystemA(), stage)
35
+ system_manager.add(SystemB(), stage)
36
+ system_manager.add(SystemC(), stage)
37
+ assert len(system_manager._systems[stage]) == 3
38
+
39
+ def test_add_system_of_same_type():
40
+ system_manager = SystemManager()
41
+ system_manager.add(SystemA(), ExecutionStage.update)
42
+ with pytest.raises(ValueError) as exception:
43
+ system_manager.add(SystemA(), ExecutionStage.cleanup)
44
+
45
+ def test_execute_systems():
46
+ system_manager = SystemManager()
47
+ system_manager.add(SystemA(), ExecutionStage.reset)
48
+ system_manager.add(SystemB(), ExecutionStage.update)
49
+ system_manager.add(SystemC(), ExecutionStage.cleanup)
50
+ system_manager.execute(0)
51
+ for system in system_manager.all_systems:
52
+ assert system.executed
53
+
54
+ def test_remove_system():
55
+ system_manager = SystemManager()
56
+ system_manager.add(SystemA(), ExecutionStage.reset)
57
+ system_manager.add(SystemB(), ExecutionStage.update)
58
+ system_manager.add(SystemC(), ExecutionStage.cleanup)
59
+ system_manager.remove(SystemA)
60
+ system_manager.remove(SystemB)
61
+ assert len(list(system_manager.all_systems)) == 1
@@ -0,0 +1,136 @@
1
+ from omniecs.managers import ComponentManager, EntityManager, EventManager, RenderManager, ResourceManager, SystemManager
2
+ from omniecs.system import System
3
+ from omniecs.types import Component, DrawCommand, Event, Resource
4
+ from omniecs.world import WorldFactory
5
+
6
+
7
+ class MockComponentA(Component): ...
8
+ class MockComponentB(Component): ...
9
+ class MockComponentC(Component): ...
10
+
11
+ class MockSystem(System):
12
+ executed: bool = False
13
+ def execute(self, delta_time: float) -> None:
14
+ self.executed = True
15
+
16
+ class MockResource(Resource): ...
17
+
18
+ class MockEvent(Event): ...
19
+
20
+ class MockDrawCommand(DrawCommand): ...
21
+
22
+ def test_world_has_all_managers():
23
+ world = WorldFactory.create_world()
24
+ assert isinstance(world._entities, EntityManager)
25
+ assert isinstance(world._components, ComponentManager)
26
+ assert isinstance(world._systems, SystemManager)
27
+ assert len(list(world._systems.all_systems)) == 3
28
+ assert isinstance(world._resources, ResourceManager)
29
+ assert isinstance(world._events, EventManager)
30
+ assert isinstance(world._render, RenderManager)
31
+
32
+ def test_spawn_entity():
33
+ world = WorldFactory.create_world()
34
+ componentA = MockComponentA()
35
+ componentB = MockComponentB()
36
+ entity_id = world.spawn(componentA, componentB)
37
+
38
+ entities = list(world.query(MockComponentA, MockComponentB))
39
+ assert len(entities) == 1
40
+ entity, (compA, compB) = entities[0]
41
+ assert entity == entity_id
42
+ assert compA == componentA
43
+ assert compB == componentB
44
+
45
+ def test_destroy_entity():
46
+ world = WorldFactory.create_world()
47
+ componentA = MockComponentA()
48
+ componentB = MockComponentB()
49
+ entity_id = world.spawn(componentA, componentB)
50
+ world.destroy(entity_id)
51
+
52
+ assert world.query_entities(any_of=(MockComponentA, MockComponentB)) == set()
53
+
54
+ def test_add_component():
55
+ world = WorldFactory.create_world()
56
+ componentC = MockComponentC()
57
+ entity_id = world.spawn(MockComponentA(), MockComponentB())
58
+ world.add_component(entity_id, componentC)
59
+
60
+ entities = list(world.query(MockComponentC))
61
+ assert len(entities) == 1
62
+ entity, (compC,) = entities[0]
63
+ assert entity == entity_id
64
+ assert compC == componentC
65
+
66
+ def test_remove_component():
67
+ world = WorldFactory.create_world()
68
+ entity_id = world.spawn(MockComponentA(), MockComponentB())
69
+ world.remove_component(entity_id, MockComponentA)
70
+
71
+ assert world.query_entities(all_of=(MockComponentA,)) == set()
72
+
73
+ def test_register_temporary_component():
74
+ world = WorldFactory.create_world()
75
+ world.register_temporary_component(MockComponentA)
76
+ world.spawn(MockComponentA())
77
+ world.spawn(MockComponentA())
78
+ world.spawn(MockComponentA())
79
+ world.execute(0)
80
+
81
+ assert world.query_entities(all_of=(MockComponentA,)) == set()
82
+
83
+ def test_execute_system():
84
+ world = WorldFactory.create_world()
85
+ system = MockSystem()
86
+ world.register_system(system)
87
+ assert not system.executed
88
+
89
+ world.execute(0)
90
+ assert system.executed
91
+
92
+ def test_execute_unregistered_system():
93
+ world = WorldFactory.create_world()
94
+ system = MockSystem()
95
+ world.register_system(system)
96
+ assert not system.executed
97
+
98
+ world.unregister_system(MockSystem)
99
+ world.execute(0)
100
+ assert not system.executed
101
+
102
+ def test_set_resource():
103
+ world = WorldFactory.create_world()
104
+ resource = MockResource()
105
+ world.set_resource(resource)
106
+
107
+ assert world.get_resource(MockResource) == resource
108
+
109
+ def test_overwrite_resource():
110
+ world = WorldFactory.create_world()
111
+ resource = MockResource()
112
+ world.set_resource(MockResource())
113
+ world.set_resource(resource)
114
+
115
+ assert world.get_resource(MockResource) == resource
116
+
117
+ def test_push_events():
118
+ world = WorldFactory.create_world()
119
+ event = MockEvent()
120
+ world.push_event(event)
121
+ world.push_event(event)
122
+ world.push_event(event)
123
+
124
+ assert world.get_events() == [event, event, event]
125
+ assert world.get_events() == [event, event, event]
126
+
127
+ def test_add_draw_commands():
128
+ world = WorldFactory.create_world()
129
+ draw_command = MockDrawCommand(global_layer=1)
130
+ world.add_draw_command(draw_command)
131
+ world.add_draw_command(draw_command)
132
+ world.add_draw_command(draw_command)
133
+
134
+ assert world.get_draw_commands() == [draw_command, draw_command, draw_command]
135
+ assert world.get_draw_commands() == [draw_command, draw_command, draw_command]
136
+