chore: format code

This commit is contained in:
rus07tam 2025-11-23 10:49:54 +00:00
parent a49fe64be8
commit 7b34426c82
5 changed files with 26 additions and 63 deletions

View file

@ -2,8 +2,12 @@ import sys
from snakia.core.tui import CanvasChar, RenderContext from snakia.core.tui import CanvasChar, RenderContext
from snakia.core.tui.render import ANSIRenderer from snakia.core.tui.render import ANSIRenderer
from snakia.core.tui.widgets import (BoxWidget, HorizontalSplitWidget, from snakia.core.tui.widgets import (
TextWidget, VerticalSplitWidget) BoxWidget,
HorizontalSplitWidget,
TextWidget,
VerticalSplitWidget,
)
class StdoutTarget: class StdoutTarget:

View file

@ -82,9 +82,7 @@ class System:
self.__processors.remove(processor) self.__processors.remove(processor)
@overload @overload
def get_components( def get_components(self, c1: type[A], /) -> Iterable[tuple[int, tuple[A]]]: ...
self, c1: type[A], /
) -> Iterable[tuple[int, tuple[A]]]: ...
@overload @overload
def get_components( def get_components(
@ -117,10 +115,7 @@ class System:
) -> Iterable[tuple[int, tuple[Component, ...]]]: ) -> Iterable[tuple[int, tuple[Component, ...]]]:
"""Returns all entities with the given components.""" """Returns all entities with the given components."""
entity_set = set.intersection( entity_set = set.intersection(
*( *(self.__components[component_type] for component_type in component_types)
self.__components[component_type]
for component_type in component_types
)
) )
for entity in entity_set: for entity in entity_set:
yield ( yield (
@ -181,9 +176,7 @@ class System:
), ),
) )
def get_component( def get_component(self, component_type: type[C], /) -> Iterable[tuple[int, C]]:
self, component_type: type[C], /
) -> Iterable[tuple[int, C]]:
"""Returns all entities with the given component.""" """Returns all entities with the given component."""
for entity in self.__components[component_type].copy(): for entity in self.__components[component_type].copy():
yield entity, cast(C, self.__entitites[entity][component_type]) yield entity, cast(C, self.__entitites[entity][component_type])
@ -214,24 +207,16 @@ class System:
self.__components[component_type].add(entity) self.__components[component_type].add(entity)
self.__entitites[entity][component_type] = component self.__entitites[entity][component_type] = component
def has_component( def has_component(self, entity: int, component_type: type[Component]) -> bool:
self, entity: int, component_type: type[Component]
) -> bool:
"""Returns True if the entity has the given component.""" """Returns True if the entity has the given component."""
return component_type in self.__entitites[entity] return component_type in self.__entitites[entity]
def has_components( def has_components(self, entity: int, *component_types: type[Component]) -> bool:
self, entity: int, *component_types: type[Component]
) -> bool:
"""Returns True if the entity has all the given components.""" """Returns True if the entity has all the given components."""
components_dict = self.__entitites[entity] components_dict = self.__entitites[entity]
return all( return all(comp_type in components_dict for comp_type in component_types)
comp_type in components_dict for comp_type in component_types
)
def remove_component( def remove_component(self, entity: int, component_type: type[C]) -> C | None:
self, entity: int, component_type: type[C]
) -> C | None:
"""Removes a component from an entity.""" """Removes a component from an entity."""
self.__components[component_type].discard(entity) self.__components[component_type].discard(entity)
if not self.__components[component_type]: if not self.__components[component_type]:
@ -263,9 +248,7 @@ class System:
def entity_exists(self, entity: int) -> bool: def entity_exists(self, entity: int) -> bool:
"""Returns True if the entity exists.""" """Returns True if the entity exists."""
return ( return entity in self.__entitites and entity not in self.__dead_entities
entity in self.__entitites and entity not in self.__dead_entities
)
def start(self) -> None: def start(self) -> None:
"""Starts the system.""" """Starts the system."""

View file

@ -23,9 +23,7 @@ class Engine:
self.__dispatcher_thread: threading.Thread | None = None self.__dispatcher_thread: threading.Thread | None = None
def start(self) -> None: def start(self) -> None:
self.__system_thread = threading.Thread( self.__system_thread = threading.Thread(target=self.system.start, daemon=False)
target=self.system.start, daemon=False
)
self.__dispatcher_thread = threading.Thread( self.__dispatcher_thread = threading.Thread(
target=self.dispatcher.start, daemon=False target=self.dispatcher.start, daemon=False
) )

View file

@ -23,9 +23,9 @@ class Dispatcher:
def __init__(self) -> None: def __init__(self) -> None:
self.__queue: Final = queue.Queue[Event]() self.__queue: Final = queue.Queue[Event]()
self.__subscribers: Final[ self.__subscribers: Final[dict[type[Event], list[Subscriber[Event]]]] = (
dict[type[Event], list[Subscriber[Event]]] defaultdict(list)
] = defaultdict(list) )
self.__running: bool = False self.__running: bool = False
@property @property
@ -33,15 +33,11 @@ class Dispatcher:
"""Returns True if the dispatcher is running.""" """Returns True if the dispatcher is running."""
return self.__running return self.__running
def subscribe( def subscribe(self, event_type: type[T], subscriber: Subscriber[T]) -> None:
self, event_type: type[T], subscriber: Subscriber[T]
) -> None:
"""Subscribe to an event type.""" """Subscribe to an event type."""
self.__subscribers[event_type].append(subscriber) # type: ignore self.__subscribers[event_type].append(subscriber) # type: ignore
def unsubscribe( def unsubscribe(self, event_type: type[T], subscriber: Subscriber[T]) -> None:
self, event_type: type[T], subscriber: Subscriber[T]
) -> None:
"""Unsubscribe from an event type.""" """Unsubscribe from an event type."""
for sub in self.__subscribers[event_type].copy(): for sub in self.__subscribers[event_type].copy():
if sub.handler != subscriber.handler: if sub.handler != subscriber.handler:
@ -97,9 +93,7 @@ class Dispatcher:
i = 0 i = 0
while i < len(subscribers): while i < len(subscribers):
subscriber = subscribers[i] subscriber = subscribers[i]
if subscriber.filters is not None and not subscriber.filters( if subscriber.filters is not None and not subscriber.filters(event):
event
):
continue continue
action = subscriber.handler(event) action = subscriber.handler(event)

View file

@ -1,12 +1,4 @@
from typing import ( from typing import Any, Awaitable, Callable, Generic, Literal, TypeVar, overload
Any,
Awaitable,
Callable,
Generic,
Literal,
TypeVar,
overload,
)
from .base_bindable import BaseBindable, BindableSubscriber, ValueChanged from .base_bindable import BaseBindable, BindableSubscriber, ValueChanged
@ -66,25 +58,19 @@ class AsyncBindable(BaseBindable[T], Generic[T]):
if run_now: if run_now:
async def _run() -> None: async def _run() -> None:
await subscriber( await subscriber(ValueChanged(self.__default_value, self.value))
ValueChanged(self.__default_value, self.value)
)
return _run() return _run()
return None return None
def unsubscribe( def unsubscribe(self, subscriber: BindableSubscriber[T, Awaitable[Any]]) -> None:
self, subscriber: BindableSubscriber[T, Awaitable[Any]]
) -> None:
"""Unsubscribe from an value.""" """Unsubscribe from an value."""
self.__subscribers.remove(subscriber) self.__subscribers.remove(subscriber)
@overload @overload
def on( def on(
self, run_now: Literal[True] self, run_now: Literal[True]
) -> Callable[ ) -> Callable[[BindableSubscriber[T, Awaitable[Any]]], Awaitable[None]]: ...
[BindableSubscriber[T, Awaitable[Any]]], Awaitable[None]
]: ...
@overload @overload
def on( def on(
@ -104,9 +90,7 @@ class AsyncBindable(BaseBindable[T], Generic[T]):
if run_now: if run_now:
async def _run() -> None: async def _run() -> None:
await subscriber( await subscriber(ValueChanged(self.__default_value, self.value))
ValueChanged(self.__default_value, self.value)
)
return _run() return _run()
return None return None