def main(): emanager = EntityManager() entity1, entity2 = setup_entities(emanager, 2) print(emanager, entity1, entity2) cmanager = ComponentManager(Component) component1 = Component() component2 = Component() print(cmanager, component1, component2) cmanager.add(entity1, component1) cmanager.add(entity2, component2) print(cmanager) print(f"Find entity1(id=1): {cmanager.find(entity1)}") print(f"Find entity2(id=2): {cmanager.find(entity2)}") print(f"Remove entity1") cmanager.remove(entity1) print(f"Find entity1(id=1): {cmanager.find(entity1)}") print(cmanager, cmanager.components) pmanager = ComponentManager(Position) position1 = Position(3, 5) position2 = Position(6, 1) print(pmanager, position1, position2) pmanager.add(entity1, position1) pmanager.add(entity2, position2) print(pmanager) for eid in emanager.entities: print(eid, pmanager.find(eid)) while not all((p.x, p.y) == (0, 0) for p in pmanager.components.values()): move_system(emanager, pmanager) print(pmanager.components) hmanager = ComponentManager(Health) health1 = Health() health2 = Health() print(hmanager, repr(health1), repr(health2)) hmanager.add(entity1, health1) hmanager.add(entity2, health2) print(hmanager) for eid in emanager.entities: print(eid, hmanager.find(eid)) while not all(h.cur_hp == 0 for h in hmanager.components.values()): health_system(emanager, hmanager) print(hmanager.components)
def add_player(self): if self.player: return self.player = self.new_entity() self.positions.add(self.player, Position(1, 1)) self.infos.add(self.player, Information("player")) self.inputs.add(self.player, Input(True))
def check_component_type(): manager = ComponentManager(Position) e = Entity(1) p = Position() h = Health() manager.add(e, p) print(manager, manager.components) try: manager.add(e, h) print(manager, manager.components) except: raise
def test_manager(): m = ComponentManager(Render) n = ComponentManager(Position) render = Render() position = Position() m.add(0, render) n.add(0, position) for r, p in join_drop_key(m, n): assert r == render assert p == position
def spawn_item(self, space) -> None: item = self.engine.entities.create() self.engine.positions.add( item, Position(*space, map_id=self.engine.world.id, movement_type=Position.MovementType.NONE, blocks=False)) r = random.choice(self.engine.renders.shared['food']) self.engine.renders.add(item, r) self.engine.infos.add(item, self.engine.infos.shared['food']) self.engine.items.add(item, Item('food', effect=HealEffect(3))) self.engine.decays.add(item, Decay()) return item
def spawn_unit(self, space) -> None: # create unit entity computer = self.engine.entities.create() self.engine.inputs.add(computer, Input()) self.engine.positions.add( computer, Position(*space, map_id=self.engine.world.id, movement_type=Position.MovementType.GROUND)) self.engine.ais.add(computer, AI()) render = random.choice(self.engine.renders.shared['goblin']) self.engine.renders.add(computer, render) info = self.engine.infos.shared['goblin'] self.engine.infos.add(computer, info) self.engine.healths.add(computer, Health(2, 2)) # add items to inventory item = self.engine.entities.create() self.engine.items.add(item, Item('weapon', ('hand', 'missile'))) r = random.choice(self.engine.renders.shared['spear']) self.engine.renders.add(item, r) self.engine.infos.add(item, self.engine.infos.shared['spear']) self.engine.inventories.add(computer, Inventory(items=[item]))
def spawn_player(self) -> int: self.engine.player = player = self.engine.entities.create() # add components self.engine.inputs.add(player, Input()) self.engine.positions.add( player, Position(*self.find_valid_space(), map_id=self.engine.world.id, movement_type=Position.MovementType.GROUND)) self.engine.renders.add(player, Render('@')) self.engine.healths.add(player, Health(10, 20)) self.engine.manas.add(player, Mana(10, 20)) self.engine.infos.add(player, Information("Hero")) self.engine.inputs.add(player, Input(needs_input=True)) # create armor items for player to equip # head helmet = self.engine.entities.create() self.engine.items.add(helmet, Item('armor', ('head', ))) self.engine.renders.add(helmet, Render('[')) self.engine.infos.add( helmet, Information('iron helmet', 'Helps protect your head.')) self.engine.armors.add(helmet, Armor(2)) # body platemail = self.engine.entities.create() self.engine.items.add(platemail, Item('armor', ('body', ))) self.engine.renders.add(platemail, Render('[')) self.engine.infos.add( platemail, Information('platemail', 'Armor made from sheets of metal. Heavy but durable.')) self.engine.armors.add(platemail, Armor(5)) # feet ironboots = self.engine.entities.create() self.engine.items.add(ironboots, Item('armor', ('feet', ))) self.engine.renders.add(ironboots, Render('[')) self.engine.infos.add( ironboots, Information('iron boots', 'Reinforced footwear.')) self.engine.armors.add(ironboots, Armor(3)) # create a weapon for player spear = self.engine.entities.create() self.engine.items.add(spear, Item('weapon', ('hand', 'missiles'))) self.engine.renders.add( spear, random.choice(self.engine.renders.shared['spear'])) self.engine.infos.add(spear, self.engine.infos.shared['spear']) self.engine.weapons.add(spear, Weapon(4, 3)) # create some missiles for player stone = self.engine.entities.create() self.engine.items.add(stone, Item('weapon', ('hand', 'missiles'))) self.engine.renders.add(stone, Render('*')) self.engine.infos.add( stone, Information('stone', 'A common item useful for throwing.')) self.engine.weapons.add(stone, Weapon(1)) # add created items to an equipment component e = Equipment(head=helmet, body=platemail, hand=spear, feet=ironboots, missiles=stone) self.engine.equipments.add(player, e) # add an inventory i = Inventory() self.engine.inventories.add(player, Inventory()) # add a spellbook with a spell spellbook = Spellbook(spells=[0, 1]) self.engine.spellbooks.add(player, spellbook) return player
def spawn_player_cursor(self) -> int: self.engine.cursor = cursor = self.engine.entities.create() self.engine.cursors.add(cursor, Cursor(self.engine.player)) self.engine.positions.add(cursor, Position(blocks=False)) return cursor
def add_enemy(self): enemy = self.new_entity() self.positions.add(enemy, Position(5, 5)) self.ais.add(enemy, AI()) self.infos.add(enemy, Information("enemy"))