Created
January 23, 2019 16:43
-
-
Save ItsMeThom-zz/ad48588441d648f0638ee9dab963cbb1 to your computer and use it in GitHub Desktop.
signaling test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from abc import abstractmethod | |
| from random import randint | |
| from enum import Enum, IntEnum | |
| class Signals(Enum): | |
| HP_REDUCE = 0 | |
| HP_INCREASE = 1 | |
| HP_EXHAUSTED = 2 | |
| FIRE = 3 | |
| WET = 4 | |
| BURNING = 5 | |
| class Entity: | |
| def __init__(self, name, *components): | |
| self.name = name | |
| self.components = {} | |
| for component in components: | |
| self.set(component) | |
| def __str__(self): | |
| return self.name | |
| def set(self, component): | |
| key = type(component) | |
| self.components[key] = component | |
| component.parent = self | |
| def get(self, component): | |
| return self.components.get(component) | |
| def has(self, component): | |
| return self.get(component) is not None | |
| def signal(self, signal, data): | |
| for component in self.components: | |
| self.get(component).signal(signal, data) | |
| def update(self): | |
| for component in self.components: | |
| self.get(component).update() | |
| class Component: | |
| def __init(self, parent): | |
| self.parent = parent | |
| @abstractmethod | |
| def signal(self, signal, data): | |
| pass | |
| @abstractmethod | |
| def update(self): | |
| pass | |
| class Burnable(Component): | |
| def __init__(self, ignition_point=100): | |
| self.ignition_point = ignition_point | |
| self.is_burning = False | |
| super().__init__() | |
| def signal(self, signal, data): | |
| if signal is Signals.FIRE: | |
| if data.get('temp') >= self.ignition_point: | |
| print("{} caught fire!".format(self.parent)) | |
| self.is_burning = True | |
| self.parent.signal(Signals.BURNING, data) | |
| else: | |
| print("{} didnt catch fire this time.".format(self.parent)) | |
| if signal is Signals.WET: | |
| print("{} is quenched.".format(self.parent)) | |
| self.is_burning = False | |
| def update(self): | |
| if self.is_burning: | |
| self.parent.signal(Signals.HP_REDUCE, {'amount': 10, 'by': 'fire'}) | |
| self.parent.signal(Signals.BURNING, {}) | |
| class Health(Component): | |
| def __init__(self, hit_points): | |
| self.hit_points = hit_points | |
| super().__init__() | |
| def signal(self, signal, data): | |
| if signal is Signals.HP_REDUCE: | |
| print("{} damaged by {}".format(self.parent, data['by'])) | |
| self.hit_points -= data.get('amount') | |
| elif signal is Signals.HP_INCREASE: | |
| self.hit_points += data.get('amount') | |
| def update(self): | |
| if self.hit_points <= 0: | |
| pass # signal entity kill to self.parent | |
| wood = Entity("Piece of wood", | |
| Burnable(ignition_point=40)) | |
| stone = Entity(name = "Rock") | |
| oil_barrel = Entity("Oil Barrel", | |
| Burnable(ignition_point=5), | |
| Health(hit_points=60)) | |
| entity_container = [wood, stone, oil_barrel] | |
| # fire system | |
| fire_temp = randint(1,200) | |
| print("Temperature is: {} degrees".format(fire_temp)) | |
| for entity in entity_container: | |
| print(entity) | |
| entity.signal(Signals.FIRE, {'temp': fire_temp}) | |
| entity.update() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment