diff options
| author | Elizabeth Hunt <elizabeth@simponic.xyz> | 2024-08-20 18:17:33 -0700 |
|---|---|---|
| committer | Elizabeth Hunt <elizabeth@simponic.xyz> | 2024-08-20 18:17:33 -0700 |
| commit | 6f374aac7f4d553c484eecc77c9599be79f2d834 (patch) | |
| tree | 70b1db95de6eab70da2f83f139a80274b01cd1fb /kennel/engine | |
| parent | b4140460014d1fc134df9127600f6da2346376e3 (diff) | |
| download | kennel.hatecomputers.club-6f374aac7f4d553c484eecc77c9599be79f2d834.tar.gz kennel.hatecomputers.club-6f374aac7f4d553c484eecc77c9599be79f2d834.zip | |
send the initial state to clients
Diffstat (limited to 'kennel/engine')
| -rw-r--r-- | kennel/engine/components/controllable.py | 3 | ||||
| -rw-r--r-- | kennel/engine/entities/entity.py | 10 | ||||
| -rw-r--r-- | kennel/engine/systems/network.py | 1 | ||||
| -rw-r--r-- | kennel/engine/systems/system.py | 1 | ||||
| -rw-r--r-- | kennel/engine/systems/world.py | 24 |
5 files changed, 38 insertions, 1 deletions
diff --git a/kennel/engine/components/controllable.py b/kennel/engine/components/controllable.py index 49b0557..25ec835 100644 --- a/kennel/engine/components/controllable.py +++ b/kennel/engine/components/controllable.py @@ -10,4 +10,5 @@ class Controllable(Component): return f"Controllable(by={self.by})" def dict(self) -> dict: - return {"by": self.by} + # don't serialize who owns this + return {} diff --git a/kennel/engine/entities/entity.py b/kennel/engine/entities/entity.py index 0ca8666..b72e996 100644 --- a/kennel/engine/entities/entity.py +++ b/kennel/engine/entities/entity.py @@ -23,6 +23,13 @@ class Entity: def add_component(self, component: Component) -> None: self.components[component.component_type] = component + def to_dict(self) -> dict: + return { + "entity_type": self.entity_type, + "id": self.id, + "components": {k: v.dict() for k, v in self.components.items()}, + } + class EntityManager: def __init__(self): @@ -58,3 +65,6 @@ class EntityManager: def get_entity(self, entity_id: str) -> Optional[Entity]: return self.entities.get(entity_id) + + def to_dict(self) -> dict: + return {k: v.to_dict() for k, v in self.entities.items()} diff --git a/kennel/engine/systems/network.py b/kennel/engine/systems/network.py index 7df8088..c9696c8 100644 --- a/kennel/engine/systems/network.py +++ b/kennel/engine/systems/network.py @@ -6,6 +6,7 @@ import asyncio class EventType(str, Enum): + INITIAL_STATE = "INITIAL_STATE" ENTITY_BORN = "ENTITY_BORN" ENTITY_POSITION_UPDATE = "ENTITY_POSITION_UPDATE" ENTITY_DEATH = "ENTITY_DEATH" diff --git a/kennel/engine/systems/system.py b/kennel/engine/systems/system.py index 0e51f3a..78bda58 100644 --- a/kennel/engine/systems/system.py +++ b/kennel/engine/systems/system.py @@ -5,6 +5,7 @@ from abc import abstractmethod class SystemType(str, Enum): NETWORK = "NETWORK" + WORLD = "WORLD" class System: diff --git a/kennel/engine/systems/world.py b/kennel/engine/systems/world.py new file mode 100644 index 0000000..396e7cb --- /dev/null +++ b/kennel/engine/systems/world.py @@ -0,0 +1,24 @@ +from kennel.engine.systems.system import System, SystemType +from kennel.engine.entities.entity import EntityManager +from kennel.engine.components.component import ComponentType +from kennel.app import logger + + +class WorldSystem(System): + def __init__(self, width: int, height: int): + super().__init__(SystemType.WORLD) + self.width = width + self.height = height + + async def update(self, entity_manager: EntityManager, delta_time: float): + entities = entity_manager.get_entities_with_component(ComponentType.POSITION) + for entity in entities: + position = entity.get_component(ComponentType.POSITION) + if position is None: + logger.error(f"Entity {entity} has no position component") + continue + + position.x = max(0, min(self.width, position.x)) + position.y = max(0, min(self.height, position.y)) + + entity.add_component(position) |
