You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.3 KiB
62 lines
1.3 KiB
|
|
class ECS:
|
|
def __init__(self):
|
|
self.entities = {}
|
|
self.facts = {}
|
|
self.id_counter = 0
|
|
|
|
def entity(self):
|
|
self.id_counter += 1
|
|
return self.id_counter
|
|
|
|
def set(self, entity_id, obj):
|
|
name = obj.__class__.__qualname__
|
|
target = self.entities.get(name, {})
|
|
target[entity_id] = obj
|
|
self.entities[name] = target
|
|
|
|
def query(self, cls):
|
|
return self.entities[cls.__qualname__].items()
|
|
|
|
|
|
class Systems:
|
|
def __init__(self, ecs):
|
|
self.ecs = ecs
|
|
|
|
def play_sounds(self):
|
|
for eid, entity in ecs.query(Sound):
|
|
print("TALKING: ", entity.text)
|
|
|
|
def combat(self):
|
|
for eid, entity in ecs.query(Combat):
|
|
print("FIGHT: ", entity.hp)
|
|
|
|
def movement(self):
|
|
for eid, entity in ecs.query(Position):
|
|
print("MOVE: ", entity.x, entity.y)
|
|
|
|
class Combat:
|
|
def __init__(self, hp):
|
|
self.hp = hp
|
|
|
|
class Sound:
|
|
def __init__(self, text):
|
|
self.text = text
|
|
|
|
class Position:
|
|
def __init__(self, x, y):
|
|
self.x = x
|
|
self.y = y
|
|
|
|
|
|
ecs = ECS()
|
|
systems = Systems(ecs)
|
|
|
|
troll = ecs.entity()
|
|
ecs.set(troll, Combat(100))
|
|
ecs.set(troll, Sound("ROAR!"))
|
|
ecs.set(troll, Position(1, 2))
|
|
|
|
systems.play_sounds()
|
|
systems.combat()
|
|
systems.movement()
|
|
|