Created
January 23, 2019 16:03
-
-
Save ItsMeThom-zz/5985264f7f104a9713814a7c021e3e05 to your computer and use it in GitHub Desktop.
Entity Component Composition tests
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 | |
| #Signals = Enum('Signals', ['HP_REDUCE', 'HP_INCREASE']) | |
| class Signals(IntEnum): | |
| HP_REDUCE = 0 | |
| HP_INCREASE = 1 | |
| 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, c): | |
| return self.get(c) is not None | |
| def update(self): | |
| print("I am {}".format(self)) | |
| for component in self.components: | |
| self.get(component).update() | |
| class Component: | |
| def __init(self, parent): | |
| self.parent = parent | |
| @abstractmethod | |
| def message(self, 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 message(self, data): | |
| if data.get('temp') >= self.ignition_point: | |
| print("{} caught fire!".format(self.parent)) | |
| self.is_burning = True | |
| else: | |
| print("{} didnt catch fire this time.".format(self.parent)) | |
| def update(self): | |
| if self.is_burning: | |
| if self.parent.has(Damageable): | |
| print("Damageable") | |
| damage = self.parent.get(Damageable) | |
| damage.message(data={'amount': 10, | |
| 'action': Signals.HP_REDUCE, | |
| 'verb': 'burns' | |
| }) | |
| class Explodable(Component): | |
| def __init__(self, ignition_point): | |
| self.ignition_point = ignition_point | |
| super().__init__() | |
| def message(self, data): | |
| pass | |
| def update(self): | |
| pass # no idea | |
| class Damageable(Component): | |
| def __init__(self, hit_points): | |
| self.hit_points = hit_points | |
| super().__init__() | |
| def message(self, data): | |
| if data.get('action') is Signals.HP_REDUCE: | |
| print("{} {} for {}".format(self.parent, | |
| data.get('verb'), | |
| data.get('amount'))) | |
| self.hit_points -= data.get('amount') | |
| elif data.get('action' 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=40), | |
| Damageable(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: | |
| if entity.has(Burnable): | |
| print("{} is burnable".format(entity)) | |
| data = {'temp': fire_temp} | |
| burn = entity.get(Burnable) | |
| burn.message(data) | |
| else: | |
| print("{} is not burnable".format(entity)) | |
| entity.update() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment