tcod-ecs 5.2.0__tar.gz → 5.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.
- {tcod_ecs-5.2.0 → tcod_ecs-5.2.2}/PKG-INFO +1 -1
- {tcod_ecs-5.2.0 → tcod_ecs-5.2.2}/tcod/ecs/__init__.py +0 -1
- {tcod_ecs-5.2.0 → tcod_ecs-5.2.2}/tcod/ecs/_version.py +2 -2
- {tcod_ecs-5.2.0 → tcod_ecs-5.2.2}/tcod/ecs/entity.py +62 -4
- {tcod_ecs-5.2.0 → tcod_ecs-5.2.2}/LICENSE +0 -0
- {tcod_ecs-5.2.0 → tcod_ecs-5.2.2}/README.md +0 -0
- {tcod_ecs-5.2.0 → tcod_ecs-5.2.2}/pyproject.toml +0 -0
- {tcod_ecs-5.2.0 → tcod_ecs-5.2.2}/tcod/ecs/_converter.py +0 -0
- {tcod_ecs-5.2.0 → tcod_ecs-5.2.2}/tcod/ecs/callbacks.py +0 -0
- {tcod_ecs-5.2.0 → tcod_ecs-5.2.2}/tcod/ecs/constants.py +0 -0
- {tcod_ecs-5.2.0 → tcod_ecs-5.2.2}/tcod/ecs/py.typed +0 -0
- {tcod_ecs-5.2.0 → tcod_ecs-5.2.2}/tcod/ecs/query.py +0 -0
- {tcod_ecs-5.2.0 → tcod_ecs-5.2.2}/tcod/ecs/registry.py +0 -0
- {tcod_ecs-5.2.0 → tcod_ecs-5.2.2}/tcod/ecs/typing.py +0 -0
- {tcod_ecs-5.2.0 → tcod_ecs-5.2.2}/tcod/ecs/world.py +0 -0
|
@@ -22,6 +22,7 @@ from typing import (
|
|
|
22
22
|
from weakref import WeakKeyDictionary, WeakValueDictionary
|
|
23
23
|
|
|
24
24
|
import attrs
|
|
25
|
+
from sentinel_value import sentinel
|
|
25
26
|
from typing_extensions import Self
|
|
26
27
|
|
|
27
28
|
import tcod.ecs.callbacks
|
|
@@ -41,6 +42,8 @@ T = TypeVar("T")
|
|
|
41
42
|
_T1 = TypeVar("_T1")
|
|
42
43
|
_T2 = TypeVar("_T2")
|
|
43
44
|
|
|
45
|
+
_raise: Final = sentinel("_raise")
|
|
46
|
+
|
|
44
47
|
_entity_table: WeakKeyDictionary[Registry, WeakValueDictionary[object, Entity]] = WeakKeyDictionary()
|
|
45
48
|
"""A weak table of registries and unique identifiers to entity objects.
|
|
46
49
|
|
|
@@ -134,6 +137,10 @@ class Entity:
|
|
|
134
137
|
This creates a new unique entity and assigns an :any:`IsA` relationship with `self` to the new entity.
|
|
135
138
|
The :any:`IsA` relation is the only data this new entity directly holds.
|
|
136
139
|
|
|
140
|
+
Immutable components inherited from the parent are always copy-on-write for its child instances.
|
|
141
|
+
|
|
142
|
+
Keep in mind that components/tags/relations inherited from the parent are not removable from the child instance.
|
|
143
|
+
|
|
137
144
|
Example::
|
|
138
145
|
|
|
139
146
|
# 'child = entity.instantiate()' is equivalent to the following:
|
|
@@ -149,14 +156,18 @@ class Entity:
|
|
|
149
156
|
>>> child.components[str] # Inherits components from parent
|
|
150
157
|
'baz'
|
|
151
158
|
>>> parent.components[str] = "foo"
|
|
152
|
-
>>> child.components[str] # Changes in parent
|
|
159
|
+
>>> child.components[str] # Changes in parent are reflected in children
|
|
153
160
|
'foo'
|
|
154
161
|
>>> child.components[str] += "bar" # In-place assignment operators will copy-on-write immutable objects
|
|
155
162
|
>>> child.components[str]
|
|
156
163
|
'foobar'
|
|
157
164
|
>>> parent.components[str]
|
|
158
165
|
'foo'
|
|
159
|
-
>>>
|
|
166
|
+
>>> child.components.pop(str, None) # Revert the component to the inherited value
|
|
167
|
+
'foobar'
|
|
168
|
+
>>> child.components[str]
|
|
169
|
+
'foo'
|
|
170
|
+
>>> child.components.pop(str, None) # Safe to call .pop with default when the value isn't set on the child
|
|
160
171
|
>>> child.components[str]
|
|
161
172
|
'foo'
|
|
162
173
|
|
|
@@ -529,12 +540,17 @@ class EntityComponents(MutableMapping[Union[Type[Any], Tuple[object, Type[Any]]]
|
|
|
529
540
|
self.update(value)
|
|
530
541
|
return self
|
|
531
542
|
|
|
532
|
-
|
|
543
|
+
@overload
|
|
544
|
+
def get(self, __key: ComponentKey[T], /) -> T | None: ...
|
|
545
|
+
@overload
|
|
546
|
+
def get(self, __key: ComponentKey[T], /, default: _T1) -> T | _T1: ...
|
|
547
|
+
|
|
548
|
+
def get(self, __key: ComponentKey[T], /, default: _T1 | None = None) -> T | _T1:
|
|
533
549
|
"""Return a component, returns None or a default value when the component is missing."""
|
|
534
550
|
try:
|
|
535
551
|
return self[__key]
|
|
536
552
|
except KeyError:
|
|
537
|
-
return
|
|
553
|
+
return default # type: ignore[return-value] # https://github.com/python/mypy/issues/3737
|
|
538
554
|
|
|
539
555
|
def setdefault(self, __key: ComponentKey[T], __default: T) -> T: # type: ignore[override]
|
|
540
556
|
"""Assign a default value if a component is missing, then returns the current value."""
|
|
@@ -544,6 +560,48 @@ class EntityComponents(MutableMapping[Union[Type[Any], Tuple[object, Type[Any]]]
|
|
|
544
560
|
self[__key] = __default
|
|
545
561
|
return __default
|
|
546
562
|
|
|
563
|
+
@overload
|
|
564
|
+
def pop(self, __key: ComponentKey[T], /) -> T | None: ...
|
|
565
|
+
@overload
|
|
566
|
+
def pop(self, __key: ComponentKey[T], /, default: _T1) -> T | _T1: ...
|
|
567
|
+
|
|
568
|
+
def pop(
|
|
569
|
+
self,
|
|
570
|
+
__key: ComponentKey[T],
|
|
571
|
+
/,
|
|
572
|
+
default: _T1 = _raise, # type: ignore[assignment] # https://github.com/python/mypy/issues/3737
|
|
573
|
+
) -> T | _T1:
|
|
574
|
+
"""Remove a component directly from this entity.
|
|
575
|
+
|
|
576
|
+
Returns the removed value.
|
|
577
|
+
If the value is missing returns `default`.
|
|
578
|
+
If `default` is unset then raises :any:`KeyError` instead.
|
|
579
|
+
|
|
580
|
+
Operates directly on the entity without traversal.
|
|
581
|
+
|
|
582
|
+
>>> parent = registry[object()]
|
|
583
|
+
>>> parent.components[str] = "foo"
|
|
584
|
+
>>> child = parent.instantiate()
|
|
585
|
+
>>> child.components[str] = "bar"
|
|
586
|
+
>>> child.components.pop(str, None)
|
|
587
|
+
'bar'
|
|
588
|
+
>>> child.components.pop(str, None)
|
|
589
|
+
>>> child.components[str]
|
|
590
|
+
'foo'
|
|
591
|
+
>>> child.components.pop(str)
|
|
592
|
+
Traceback (most recent call last):
|
|
593
|
+
...
|
|
594
|
+
KeyError: <class 'str'>
|
|
595
|
+
"""
|
|
596
|
+
_components = self.entity.registry._components_by_entity.get(self.entity, {})
|
|
597
|
+
if __key not in _components:
|
|
598
|
+
if default is _raise:
|
|
599
|
+
raise KeyError(__key)
|
|
600
|
+
return default
|
|
601
|
+
value: T | _T1 = _components[__key]
|
|
602
|
+
del self[__key]
|
|
603
|
+
return value
|
|
604
|
+
|
|
547
605
|
|
|
548
606
|
@attrs.define(eq=False, frozen=True, weakref_slot=False)
|
|
549
607
|
class EntityTags(MutableSet[Any]):
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|