Skip to content

Commit 3339ef3

Browse files
committed
Add query any_of method
1 parent 3e36748 commit 3339ef3

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- New query `.any_of` method. This was possible before but it is easier with this method.
13+
1014
## [5.2.4] - 2025-03-07
1115

1216
### Fixed

tcod/ecs/query.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,30 @@ def none_of(
404404
& self._query,
405405
)
406406

407+
def any_of(
408+
self,
409+
components: Iterable[ComponentKey[object]] = (),
410+
*,
411+
tags: Iterable[object] = (),
412+
relations: Iterable[_RelationQuery] = (),
413+
traverse: Iterable[object] = (IsA,),
414+
depth: int | None = None,
415+
) -> Self:
416+
"""Filter entities based on having at least one of the provided elements.
417+
418+
.. versionadded:: Unreleased
419+
"""
420+
_check_suspicious_tags(tags, stacklevel=2)
421+
return self.__class__(
422+
self.registry,
423+
_QueryLogicalAnd(
424+
all_of=frozenset(
425+
[_QueryLogicalOr(any_of=frozenset(self.__as_queries(components, tags, relations, traverse, depth)))]
426+
)
427+
)
428+
& self._query,
429+
)
430+
407431
def __iter__(self) -> Iterator[Entity]:
408432
"""Iterate over the matching entities."""
409433
return iter(self.get_entities())

tests/test_ecs.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,11 @@ def test_world_query_bool() -> None:
282282
assert not world.Q.all_of(tags=["Foo"])
283283
world[None].tags.add("Foo")
284284
assert world.Q.all_of(tags=["Foo"])
285+
286+
287+
def test_any_of() -> None:
288+
world = tcod.ecs.Registry()
289+
world[None].tags.add("foo")
290+
assert world.Q.any_of(tags=["foo"])
291+
assert world.Q.any_of(tags=["foo", "bar"])
292+
assert not world.Q.any_of(tags=["bar"])

0 commit comments

Comments
 (0)