From 722bf6d55fa775c3086c3e9c12cd0c67d4475035 Mon Sep 17 00:00:00 2001 From: nejdetckenobi Date: Sat, 3 Oct 2026 23:55:52 +0300 Subject: [PATCH] Adds Death and Sickness systems --- components.py | 20 ++++++-------------- imp.py | 14 ++------------ systems.py | 29 +++++++++++++++++++++++++++-- test.py | 17 +++++++++++------ 4 files changed, 46 insertions(+), 34 deletions(-) diff --git a/components.py b/components.py index de0ceb5..a7277cf 100644 --- a/components.py +++ b/components.py @@ -37,19 +37,11 @@ class Capacity(BaseStat): pass +class Tag(StrEnum): + SICK = "SICK" + DEAD = "DEAD" + + @dataclass class Tags: - values: list = field(default_factory=list) - - -class LocationType(StrEnum): - NO_WHERE = "no_where" - PADDOCK = "paddock" - STORE = "store" - INVENTORY = "inventory" - - -@dataclass -class Location: - type: "LocationType" = LocationType.NO_WHERE - id: int | None = None + values: set[Tag] = field(default_factory=set) diff --git a/imp.py b/imp.py index e03e2c5..2f8fc6f 100644 --- a/imp.py +++ b/imp.py @@ -3,14 +3,13 @@ from dataclasses import dataclass, field from components import ( Health, Satiety, - Location, - LocationType, Name, Hydration, Hygiene, Tags, ) -from ecs import World + +from libs.ecs import World def create_pet(world: World, name: str): @@ -22,12 +21,3 @@ def create_pet(world: World, name: str): Hygiene(80), Tags(), ) - - -def move_pet_to(world: World, pet_id, container_entity=None) -> bool: - component_map = world.get_components(pet_id, pop=True) - if component_map is None: - return False - component_map[Location].id = container_entity - component_map[Location].type = LocationType.PADDOCK - return True diff --git a/systems.py b/systems.py index 25fa783..008208b 100644 --- a/systems.py +++ b/systems.py @@ -1,5 +1,5 @@ -from components import Health, Hydration, Hygiene, Satiety -from ecs import World +from components import Health, Hydration, Hygiene, Satiety, Tag, Tags +from libs.ecs import World class SatietySystem: @@ -39,3 +39,28 @@ class HygieneSystem: hygiene.value = 0 else: hygiene.value -= 1 + + +class SicknessSystem: + @classmethod + def process(cls, world: World): + for _, component_map in world.query(Health, Tags): + health = component_map[Health] + tags = component_map[Tags] + if Tag.SICK in tags.values and health.value > 0: + health.value -= 1 + if health.value <= 0: + health.value = 0 + tags.values.add(Tag.DEAD) + + +class DeathSystem: + @classmethod + def process(cls, world: World): + marked = set() + for entity, component_map in world.query(Health, Tags): + tags = component_map[Tags] + if Tag.DEAD in tags.values: + marked.add(entity) + for entity in marked: + world.get_components(entity, pop=True) diff --git a/test.py b/test.py index 041cde2..db89a4d 100644 --- a/test.py +++ b/test.py @@ -1,11 +1,19 @@ from imp import World, create_pet -from systems import SatietySystem, HydrationSystem, HygieneSystem +from systems import ( + DeathSystem, + SicknessSystem, + SatietySystem, + HydrationSystem, + HygieneSystem, +) def test_systems_tick(): world = World() world.systems.extend( ( + DeathSystem, + SicknessSystem, HydrationSystem, SatietySystem, HygieneSystem, @@ -14,12 +22,9 @@ def test_systems_tick(): entity = create_pet(world, "karabas") - for _ in range(81): + for _ in range(81 + 35): world.tick() - - components = world.get_components(entity) - - print(components) + print(world.registry) if __name__ == "__main__":