microecs 0.2.1__tar.gz → 0.2.2__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.1
2
2
  Name: microecs
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: MicroECS: Minimal Entity Component System (ECS) in python and numpy
5
5
  Home-page: https://gitlab.com/meehai/microecs
6
6
  License: MIT
@@ -101,6 +101,12 @@ class QueryResult:
101
101
  return _Field(data[name] or [np.empty((0, *self._field_shapes[name]), self._field_dtypes[name])])
102
102
  raise AttributeError(name)
103
103
 
104
+ def __setattr__(self, name, value):
105
+ if (data := self.__dict__.get("_data")) is not None and name in data:
106
+ getattr(self, name)[:] = value # recarray semantics: assigning a field scatters into it
107
+ return
108
+ super().__setattr__(name, value)
109
+
104
110
  def __len__(self):
105
111
  return self._len
106
112
 
@@ -30,6 +30,7 @@ class World:
30
30
  self._live_ids: set[EntityId] = set() # 'eager' mode ids so e.g. calling remove_entity twice before update fails
31
31
  # command buffer management. {add/remove}_{entity/component} are lazy. Taken into account after update().
32
32
  self._command_buffer: list[Callable] = []
33
+ self._cache: dict[PoolKey, QueryResult] = {}
33
34
  logger.debug(f"Created scene with components: {self.component_names}")
34
35
 
35
36
  # public api
@@ -38,7 +39,10 @@ class World:
38
39
  """commits the underlying pool changes from the systems between two updates. Should be called in main loop."""
39
40
  for fn in self._command_buffer:
40
41
  fn()
41
- self._command_buffer.clear()
42
+
43
+ if len(self._command_buffer) > 0:
44
+ self._command_buffer.clear()
45
+ self._cache.clear()
42
46
 
43
47
  def add_entity(self, components: list[ComponentType], **kwargs) -> EntityId:
44
48
  """Adds an entity to the world based on components (data->kwargs). Returns an entity id. Lazy; call update()"""
@@ -77,7 +81,10 @@ class World:
77
81
 
78
82
  def query_and(self, component_types: list[ComponentType]) -> QueryResult:
79
83
  """returns A QueryResult object with the entities that have all the requested components (entity ids too)."""
80
- key = self._make_key(component_types)
84
+ # Note: we can cache the queries. The only time it can get invalidated (via public API) is at update().
85
+ if (key := self._make_key(component_types)) in self._cache:
86
+ return self._cache[key]
87
+
81
88
  res = []
82
89
  for archetype_key, archetype_pool in self.pools.items():
83
90
  if (archetype_key & key) == key: # key is subset of archetype_key
@@ -87,7 +94,9 @@ class World:
87
94
  field_shapes = dict(zip(field_names, sum([self.component_to_shapes[c] for c in component_types], [])))
88
95
  field_dtypes = dict(zip(field_names, sum([self.component_to_dtypes[c] for c in component_types], [])))
89
96
  entity_ids = np.array(sum((self._pool_ids[p] for p in res), []), dtype="int64")
90
- return QueryResult(res, field_shapes=field_shapes, field_dtypes=field_dtypes, entity_ids=entity_ids)
97
+
98
+ self._cache[key] = QueryResult(res, field_shapes=field_shapes, field_dtypes=field_dtypes, entity_ids=entity_ids)
99
+ return self._cache[key]
91
100
 
92
101
  # private stuff
93
102
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: microecs
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: MicroECS: Minimal Entity Component System (ECS) in python and numpy
5
5
  Home-page: https://gitlab.com/meehai/microecs
6
6
  License: MIT
@@ -3,7 +3,7 @@ from pathlib import Path
3
3
  from setuptools import setup, find_packages
4
4
 
5
5
  NAME = "microecs"
6
- VERSION = "0.2.1"
6
+ VERSION = "0.2.2"
7
7
  DESCRIPTION = "MicroECS: Minimal Entity Component System (ECS) in python and numpy"
8
8
  URL = "https://gitlab.com/meehai/microecs"
9
9
 
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes