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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tinyecs
3
- Version: 0.5.1
3
+ Version: 0.5.3
4
4
  Summary: The teeniest, tiniest ECS system
5
5
  Author-email: Michael Lamertz <michael.lamertz@gmail.com>
6
6
  License-Expression: MIT
@@ -1,7 +1,7 @@
1
1
  [project]
2
2
  name = "tinyecs"
3
3
  description = "The teeniest, tiniest ECS system"
4
- version = "0.5.1"
4
+ version = "0.5.3"
5
5
  readme = "README.rst"
6
6
 
7
7
  authors = [
@@ -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
- 'remove_entity', 'remove_from_archetype', 'remove_property',
18
- 'remove_system', 'remove_system_from_domain', 'reset',
19
- 'run_all_systems', 'run_bulk_system', 'run_domain', 'run_system',
20
- 'set_properties', 'set_property', 'update_component',]
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(set) # object index
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
- oidx[old_comp_id].discard(eid)
238
- if not oidx[old_comp_id]:
239
- del oidx[old_comp_id]
240
- if hasattr(old_comp, 'shutdown_'):
241
- old_comp.shutdown_()
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)].add(eid)
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
- else:
295
- if hasattr(obj, 'shutdown_'):
296
- obj.shutdown_()
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] = set()
360
- didx[domain].add(system)
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.get(id(comp), set())
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)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tinyecs
3
- Version: 0.5.1
3
+ Version: 0.5.3
4
4
  Summary: The teeniest, tiniest ECS system
5
5
  Author-email: Michael Lamertz <michael.lamertz@gmail.com>
6
6
  License-Expression: MIT
@@ -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) == {e1}
253
- assert ecs.oidx[oid] == {e1}
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) == {e1, e2}
257
- assert ecs.oidx[oid] == {e1, e2}
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) == {e2}
261
- assert ecs.oidx[oid] == {e2}
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 HasShutdown(Exception): ...
379
+ class ShutdownComp():
380
+ shutdown_called = False
342
381
 
343
- class ShutdownEntity():
344
382
  def shutdown_(self):
345
- raise HasShutdown()
383
+ self.shutdown_called = True
346
384
 
347
385
  eid = ecs.create_entity()
348
- ecs.add_component(eid, 'shutdown-test', ShutdownEntity())
349
- with pytest.raises(HasShutdown) as e:
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
- with pytest.raises(HasShutdown) as e:
354
- ecs.remove_entity(eid)
355
- assert e.type == HasShutdown
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