Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Crum committed Apr 10, 2021
1 parent c01ae27 commit f8c3a95
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
9 changes: 7 additions & 2 deletions ecstremity/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

if TYPE_CHECKING:
from entity import Entity, EntityEvent
from engine import Engine
from engine import Engine, GAME, EngineAdapter


class NonremovableError(Exception):
Expand All @@ -29,13 +29,18 @@ class Component(metaclass=componentmeta):
consistency.
"""

ecs: Engine
ecs: Union[Engine, EngineAdapter]
client: GAME
init_props: Dict[str, Any]
entity: Optional[Entity] = None
_name: str = ''
_is_destroyed: bool = False
_removable: bool = True

@property
def name(self):
return self._name

@property
def is_destroyed(self) -> bool:
"""Returns True if this component has been destroyed."""
Expand Down
9 changes: 7 additions & 2 deletions ecstremity/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def is_destroyed(self) -> bool:
"""Returns True if this entity has been destroyed."""
return self._is_destroyed

@property
def component_keys(self):
return self.keys()

@property
def components(self):
"""Return an iterable of all component instances attached to entity."""
Expand Down Expand Up @@ -67,9 +71,7 @@ def fire_event(self, name: str, data: Optional[EventData] = None):
component._on_event(evt)
if evt.prevented:
return evt

# TODO Logic for nested components.

return evt

def has(self, component: Union[str, Component]):
Expand Down Expand Up @@ -118,3 +120,6 @@ def __getitem__(self, component: Union[str, Component]) -> Component:
component = component.name
return super().__getitem__(component.upper())

def __repr__(self):
component_list = ", ".join(self.component_keys)
return f"Entity [{self.uid}] with [{component_list}]"
11 changes: 3 additions & 8 deletions ecstremity/entity_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class EventData:
instigator: Entity = None
target: Union[Tuple[int, int], Entity] = None
interactions: List[Dict[str, str]] = None
callback: Callable[[Any], Any] = None
cost: float = None


Expand Down Expand Up @@ -48,18 +49,12 @@ def prevent(self) -> None:
"""Callback for `_prevented` attribute."""
self._prevented = True

def route(self, target: Entity):
def route(self, new_event: str, target: Entity):
self._routed = True
name = self.name
if self.name[:4] == 'fwd_':
name = self.name[4:]
else:
print(f"Expected event prefixed with `fwd_`, got {name} instead.")

if not self.data.target:
raise ValueError("Routed events require a target entity!")
else:
return target.fire_event(name, self.data)
return target.fire_event(new_event, self.data)

def __eq__(self, other: object) -> bool:
if isinstance(other, EntityEvent):
Expand Down
2 changes: 2 additions & 0 deletions ecstremity/registries/component_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class ComponentRegistry(Registry):
def register(self, component: Component):
self[component.name] = component
self[component.name].ecs = self.ecs
if 'client' in self.ecs.__dict__:
self[component.name].client = self.ecs.client

def create(
self,
Expand Down
10 changes: 5 additions & 5 deletions ecstremity/registries/registry.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from __future__ import annotations
from typing import TYPE_CHECKING
from typing import *

from collections import defaultdict
from collections import defaultdict

if TYPE_CHECKING:
from engine import Engine
from engine import Engine, EngineAdapter


class Registry(defaultdict):

def __init__(self, ecs: Engine) -> None:
self.ecs = ecs
def __init__(self, ecs: Union[Engine, EngineAdapter]) -> None:
self.ecs: Union[Engine, EngineAdapter] = ecs

0 comments on commit f8c3a95

Please sign in to comment.