tinyecs 0.5.1__tar.gz → 0.5.3__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.
- {tinyecs-0.5.1/src/tinyecs.egg-info → tinyecs-0.5.3}/PKG-INFO +1 -1
- {tinyecs-0.5.1 → tinyecs-0.5.3}/pyproject.toml +1 -1
- {tinyecs-0.5.1 → tinyecs-0.5.3}/src/tinyecs/__init__.py +63 -24
- {tinyecs-0.5.1 → tinyecs-0.5.3/src/tinyecs.egg-info}/PKG-INFO +1 -1
- {tinyecs-0.5.1 → tinyecs-0.5.3}/tests/test_tinyecs.py +55 -16
- {tinyecs-0.5.1 → tinyecs-0.5.3}/LICENSE +0 -0
- {tinyecs-0.5.1 → tinyecs-0.5.3}/README.rst +0 -0
- {tinyecs-0.5.1 → tinyecs-0.5.3}/setup.cfg +0 -0
- {tinyecs-0.5.1 → tinyecs-0.5.3}/src/tinyecs/tutorial.py +0 -0
- {tinyecs-0.5.1 → tinyecs-0.5.3}/src/tinyecs.egg-info/SOURCES.txt +0 -0
- {tinyecs-0.5.1 → tinyecs-0.5.3}/src/tinyecs.egg-info/dependency_links.txt +0 -0
- {tinyecs-0.5.1 → tinyecs-0.5.3}/src/tinyecs.egg-info/requires.txt +0 -0
- {tinyecs-0.5.1 → tinyecs-0.5.3}/src/tinyecs.egg-info/top_level.txt +0 -0
|
@@ -14,10 +14,11 @@ __all__ = ['Component', 'ComponentID', 'DomainID', 'EntityID',
|
|
|
14
14
|
'create_entity', 'eid_has', 'eid_of_comp', 'eids_by_cids',
|
|
15
15
|
'eids_by_property', 'has', 'has_property', 'healthcheck',
|
|
16
16
|
'purge_by_property', 'remove_archetype', 'remove_component',
|
|
17
|
-
'
|
|
18
|
-
'
|
|
19
|
-
'
|
|
20
|
-
'
|
|
17
|
+
'remove_domain', 'remove_entity', 'remove_from_archetype',
|
|
18
|
+
'remove_property', 'remove_system', 'remove_system_from_domain',
|
|
19
|
+
'reset', 'run_all_systems', 'run_bulk_system', 'run_domain',
|
|
20
|
+
'run_system', 'set_properties', 'set_property',
|
|
21
|
+
'update_component',]
|
|
21
22
|
|
|
22
23
|
type EntityID = Hashable
|
|
23
24
|
type ComponentID = Hashable
|
|
@@ -44,7 +45,7 @@ eidx = {} # entity index
|
|
|
44
45
|
cidx = {} # component id index
|
|
45
46
|
sidx = {} # system index
|
|
46
47
|
didx = {} # domain index
|
|
47
|
-
oidx = defaultdict(
|
|
48
|
+
oidx = defaultdict(list) # object index
|
|
48
49
|
plist = defaultdict(set) # entity property lists
|
|
49
50
|
archetype = {}
|
|
50
51
|
|
|
@@ -226,6 +227,8 @@ def add_component(eid: EntityID, cid: ComponentID, comp: Component) -> Component
|
|
|
226
227
|
if cid not in cidx:
|
|
227
228
|
cidx[cid] = {}
|
|
228
229
|
|
|
230
|
+
t = (eid, cid)
|
|
231
|
+
|
|
229
232
|
# Make sure, when replacing a component, the the old one is removed from
|
|
230
233
|
# oidx for this entity.
|
|
231
234
|
try:
|
|
@@ -234,15 +237,19 @@ def add_component(eid: EntityID, cid: ComponentID, comp: Component) -> Component
|
|
|
234
237
|
pass
|
|
235
238
|
else:
|
|
236
239
|
old_comp_id = id(old_comp)
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
240
|
+
try:
|
|
241
|
+
oidx[old_comp_id].remove(t)
|
|
242
|
+
except ValueError:
|
|
243
|
+
pass
|
|
244
|
+
else:
|
|
245
|
+
if not oidx[old_comp_id]:
|
|
246
|
+
del oidx[old_comp_id]
|
|
247
|
+
if hasattr(old_comp, 'shutdown_'):
|
|
248
|
+
old_comp.shutdown_()
|
|
242
249
|
|
|
243
250
|
cidx[cid][eid] = comp
|
|
244
251
|
eidx[eid][cid] = comp
|
|
245
|
-
oidx[id(comp)].
|
|
252
|
+
oidx[id(comp)].append(t)
|
|
246
253
|
|
|
247
254
|
add_to_archetype(eid)
|
|
248
255
|
|
|
@@ -283,17 +290,22 @@ def remove_component(eid: EntityID, *cids: ComponentID) -> None:
|
|
|
283
290
|
# Also, no need to try each on their own
|
|
284
291
|
try:
|
|
285
292
|
obj = cidx[cid][eid]
|
|
286
|
-
obj_id = id(obj)
|
|
287
|
-
del cidx[cid][eid]
|
|
288
|
-
del eidx[eid][cid]
|
|
289
|
-
oidx[obj_id].discard(eid)
|
|
290
|
-
if not oidx[obj_id]:
|
|
291
|
-
del oidx[id(obj)]
|
|
292
293
|
except KeyError:
|
|
294
|
+
continue
|
|
295
|
+
|
|
296
|
+
obj_id = id(obj)
|
|
297
|
+
del cidx[cid][eid]
|
|
298
|
+
del eidx[eid][cid]
|
|
299
|
+
try:
|
|
300
|
+
oidx[obj_id].remove((eid, cid))
|
|
301
|
+
except ValueError:
|
|
293
302
|
pass
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
303
|
+
|
|
304
|
+
if not oidx[obj_id]:
|
|
305
|
+
del oidx[id(obj)]
|
|
306
|
+
|
|
307
|
+
if hasattr(obj, 'shutdown_'):
|
|
308
|
+
obj.shutdown_()
|
|
297
309
|
|
|
298
310
|
|
|
299
311
|
def add_system(fn: SystemFunction, *cids: ComponentID) -> None:
|
|
@@ -341,7 +353,7 @@ def remove_system(fn: SystemFunction) -> None:
|
|
|
341
353
|
pass
|
|
342
354
|
|
|
343
355
|
|
|
344
|
-
def add_system_to_domain(domain: DomainID, system: SystemFunction) -> None:
|
|
356
|
+
def add_system_to_domain(domain: DomainID, system: SystemFunction, after=None) -> None:
|
|
345
357
|
"""Create or extend a domain with the given system.
|
|
346
358
|
|
|
347
359
|
:param system: The system function
|
|
@@ -356,8 +368,12 @@ def add_system_to_domain(domain: DomainID, system: SystemFunction) -> None:
|
|
|
356
368
|
if system not in sidx:
|
|
357
369
|
raise UnknownSystemError(f'system {system} is not registered')
|
|
358
370
|
if domain not in didx:
|
|
359
|
-
didx[domain] =
|
|
360
|
-
|
|
371
|
+
didx[domain] = []
|
|
372
|
+
|
|
373
|
+
try:
|
|
374
|
+
didx[domain].insert(didx[domain].index(after) + 1, system)
|
|
375
|
+
except ValueError:
|
|
376
|
+
didx[domain].append(system)
|
|
361
377
|
|
|
362
378
|
|
|
363
379
|
def remove_system_from_domain(domain: DomainID, system: SystemFunction) -> None:
|
|
@@ -373,6 +389,21 @@ def remove_system_from_domain(domain: DomainID, system: SystemFunction) -> None:
|
|
|
373
389
|
return
|
|
374
390
|
try:
|
|
375
391
|
didx[domain].remove(system)
|
|
392
|
+
except ValueError:
|
|
393
|
+
pass
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
def remove_domain(domain: DomainID) -> None:
|
|
397
|
+
"""Remove the given domain.
|
|
398
|
+
|
|
399
|
+
:param domain: The domain to remove
|
|
400
|
+
:return: None
|
|
401
|
+
|
|
402
|
+
.. note:: A non existing domain is silently ignored
|
|
403
|
+
"""
|
|
404
|
+
|
|
405
|
+
try:
|
|
406
|
+
del didx[domain]
|
|
376
407
|
except KeyError:
|
|
377
408
|
pass
|
|
378
409
|
|
|
@@ -505,7 +536,7 @@ def eid_of_comp(comp: Component) -> set[EntityID]:
|
|
|
505
536
|
:return: A set of all entity IDs that contain the given component.
|
|
506
537
|
"""
|
|
507
538
|
|
|
508
|
-
return oidx
|
|
539
|
+
return [_[0] for _ in oidx[id(comp)]]
|
|
509
540
|
|
|
510
541
|
|
|
511
542
|
def cid_of_comp(eid: EntityID, comp: Component) -> ComponentID:
|
|
@@ -626,6 +657,14 @@ def run_all_systems(**kwargs) -> dict[SystemFunction, _RunSystemResult]:
|
|
|
626
657
|
|
|
627
658
|
This calls above run_system for all registered systems with their
|
|
628
659
|
appropriate components.
|
|
660
|
+
|
|
661
|
+
.. important:: If you pass kwargs to ``run_all_systems``, naturally all
|
|
662
|
+
systems have to accept kwargs. This is in the responsibility of the
|
|
663
|
+
programmer.
|
|
664
|
+
|
|
665
|
+
This function is provided for very simple use cases, use either well
|
|
666
|
+
ordered lists of ``run_system`` calls or the domain system, which will
|
|
667
|
+
be much less error prone.
|
|
629
668
|
"""
|
|
630
669
|
|
|
631
670
|
return {fn: run_system(fn, *comps, **kwargs)
|
|
@@ -249,16 +249,16 @@ def test_eid_of_comp():
|
|
|
249
249
|
oid = id(shared)
|
|
250
250
|
|
|
251
251
|
ecs.add_component(e1, 'shared', shared)
|
|
252
|
-
assert ecs.eid_of_comp(shared) ==
|
|
253
|
-
assert ecs.oidx[oid] ==
|
|
252
|
+
assert ecs.eid_of_comp(shared) == [e1]
|
|
253
|
+
assert ecs.oidx[oid] == [(e1, 'shared')]
|
|
254
254
|
|
|
255
255
|
ecs.add_component(e2, 'shared', shared)
|
|
256
|
-
assert ecs.eid_of_comp(shared) ==
|
|
257
|
-
assert ecs.oidx[oid] ==
|
|
256
|
+
assert ecs.eid_of_comp(shared) == [e1, e2]
|
|
257
|
+
assert ecs.oidx[oid] == [(e1, 'shared'), (e2, 'shared')]
|
|
258
258
|
|
|
259
259
|
ecs.remove_component(e1, 'shared')
|
|
260
|
-
assert ecs.eid_of_comp(shared) ==
|
|
261
|
-
assert ecs.oidx[oid] ==
|
|
260
|
+
assert ecs.eid_of_comp(shared) == [e2]
|
|
261
|
+
assert ecs.oidx[oid] == [(e2, 'shared')]
|
|
262
262
|
|
|
263
263
|
ecs.remove_component(e2, 'shared')
|
|
264
264
|
assert oid not in ecs.oidx
|
|
@@ -326,6 +326,44 @@ def test_run_domain():
|
|
|
326
326
|
assert res == {}
|
|
327
327
|
|
|
328
328
|
|
|
329
|
+
def test_stable_domain_order():
|
|
330
|
+
def dummy_system1b(eid, comp): pass
|
|
331
|
+
def dummy_system1a(eid, comp): pass
|
|
332
|
+
def dummy_system2a(eid, comp): pass
|
|
333
|
+
def dummy_system2b(eid, comp): pass
|
|
334
|
+
def dummy_system3(eid, comp): pass
|
|
335
|
+
|
|
336
|
+
ecs.reset()
|
|
337
|
+
ecs.add_system(dummy_system1a, 'dummy_comp')
|
|
338
|
+
ecs.add_system(dummy_system1b, 'dummy_comp')
|
|
339
|
+
ecs.add_system(dummy_system2a, 'dummy_comp')
|
|
340
|
+
ecs.add_system(dummy_system2b, 'dummy_comp')
|
|
341
|
+
ecs.add_system(dummy_system3, 'dummy_comp')
|
|
342
|
+
|
|
343
|
+
ecs.add_system_to_domain('ze_domain', dummy_system1a)
|
|
344
|
+
ecs.add_system_to_domain('ze_domain', dummy_system2a)
|
|
345
|
+
assert ecs.didx['ze_domain'] == [dummy_system1a, dummy_system2a]
|
|
346
|
+
|
|
347
|
+
ecs.add_system_to_domain('ze_domain', dummy_system1b, after=dummy_system1a)
|
|
348
|
+
assert ecs.didx['ze_domain'] == [dummy_system1a, dummy_system1b, dummy_system2a]
|
|
349
|
+
|
|
350
|
+
ecs.add_system_to_domain('ze_domain', dummy_system2b, after=dummy_system2a)
|
|
351
|
+
assert ecs.didx['ze_domain'] == [dummy_system1a, dummy_system1b, dummy_system2a, dummy_system2b]
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
def test_remove_domain():
|
|
355
|
+
def dummy_system(eid, comp): pass
|
|
356
|
+
|
|
357
|
+
ecs.reset()
|
|
358
|
+
ecs.add_system(dummy_system, 'dummy_comp')
|
|
359
|
+
|
|
360
|
+
ecs.add_system_to_domain('ze_domain', dummy_system)
|
|
361
|
+
ecs.remove_domain('ze_domain')
|
|
362
|
+
assert 'ze_domain' not in ecs.didx
|
|
363
|
+
|
|
364
|
+
ecs.remove_domain('ze_domain')
|
|
365
|
+
|
|
366
|
+
|
|
329
367
|
def test_eid_has():
|
|
330
368
|
ecs.reset()
|
|
331
369
|
e1, e2 = setup()
|
|
@@ -338,21 +376,22 @@ def test_eid_has():
|
|
|
338
376
|
def test_shutdown():
|
|
339
377
|
ecs.reset()
|
|
340
378
|
|
|
341
|
-
class
|
|
379
|
+
class ShutdownComp():
|
|
380
|
+
shutdown_called = False
|
|
342
381
|
|
|
343
|
-
class ShutdownEntity():
|
|
344
382
|
def shutdown_(self):
|
|
345
|
-
|
|
383
|
+
self.shutdown_called = True
|
|
346
384
|
|
|
347
385
|
eid = ecs.create_entity()
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
ecs.add_component(eid, 'shutdown-test', ShutdownEntity())
|
|
351
|
-
assert e.type == HasShutdown
|
|
386
|
+
comp_with_shutdown = ShutdownComp()
|
|
387
|
+
ecs.add_component(eid, 'shutdown-test', comp_with_shutdown)
|
|
352
388
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
assert
|
|
389
|
+
new_comp_with_shutdown = ShutdownComp()
|
|
390
|
+
ecs.add_component(eid, 'shutdown-test', new_comp_with_shutdown)
|
|
391
|
+
assert comp_with_shutdown.shutdown_called
|
|
392
|
+
|
|
393
|
+
ecs.remove_entity(eid)
|
|
394
|
+
assert new_comp_with_shutdown.shutdown_called
|
|
356
395
|
|
|
357
396
|
|
|
358
397
|
def test_cid_of_comp():
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|