Skip to content

Instantly share code, notes, and snippets.

@krummja
Created March 31, 2021 18:28
Show Gist options
  • Save krummja/33194b4ad853483ecde628220e3eb399 to your computer and use it in GitHub Desktop.
Save krummja/33194b4ad853483ecde628220e3eb399 to your computer and use it in GitHub Desktop.
from uuid import uuid1
class Entity:
def __init__(self):
self.uid = uuid1().hex
class componentmeta(type):
def __new__(mcs, clsname, bases, clsdict):
clsobj = super().__new__(mcs, clsname, bases, clsdict)
clsobj.name = str(clsname).upper()
return clsobj
class Component(metaclass=componentmeta):
entity = None
@property
def is_attached(self):
return bool(self.entity)
class Position(Component):
def __init__(self, x: int = 0, y: int = 0) -> None:
self.x = x
self.y = y
def create_entity(identifier):
class_name = "".join(x.capitalize() for x in identifier.split())
def __init__(self):
self.uid = uuid1()
globals()[class_name] = type(class_name, (Entity,), dict(__init__ = __init__))
if __name__ == '__main__':
initializations = ["Player()"]
create_entity('Player')
for init in initializations:
exec(init)
entities = {}
entities['player'] = globals()['Player']
entities['player'].position = Position(x = 10, y = 12)
player = entities['player']()
print(player.uid)
print(player.position.x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment