How to use the flax.component.IContainer function in flax

To help you get started, we’ve selected a few flax examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github eevee / flax / flax / ui / console / game.py View on Github external
def __init__(self, player):
        walker = urwid.SimpleListWalker([])
        self.listbox = urwid.ListBox(walker)

        from flax.component import IContainer
        for item in IContainer(player).inventory:
            item_w = InventoryItem(item)

            urwid.connect_signal(item_w, 'fire', lambda *a: self._emit('close', *a))

            self.listbox.body.append(item_w)

        super().__init__(urwid.LineBox(self.listbox))
github eevee / flax / flax / world.py View on Github external
"""
        from flax.event import Walk, MeleeAttack, Open, Unlock

        # TODO i sure do this a lot!  maybe write a method for it!
        new_pos = self.current_map.find(self.player).position + direction
        if new_pos not in self.current_map:
            return None
        tile = self.current_map.tiles[new_pos]

        if tile.creature:
            return MeleeAttack(self.player, direction)

        arch = tile.architecture

        if ILockable in arch and ILockable(arch).locked:
            key = next((item for item in IContainer(self.player).inventory if item.isa(Key)), None)
            if key:
                return Unlock(self.player, arch, key)

        if IOpenable in arch and not IOpenable(arch).open:
            return Open(self.player, arch)

        return Walk(self.player, direction)
github eevee / flax / flax / world.py View on Github external
def change_map(self, map_name):
        # TODO this is so stupidly special-casey, but i'm not really sure how
        # or when a Win should be fired.  i'd copy what Die does, but that's
        # kinda broken too, lol.  maybe the ladder should contain this
        # logic?
        if map_name == '__exit__':
            # TODO starting to look like IContainer needs a .has()
            from flax.entity import Crown
            if any(item.isa(Crown) for item in IContainer(self.player).inventory):
                raise GameOver(
                    "you found the Crown of Meeting Expectations and escaped "
                    "the dungeon!  good for you, seriously.",
                    success=True)
            else:
                raise GameOver(
                    "you leave empty-handed.  well, okay then.", success=True)

        # TODO refund time?  or only eat it after the events succeed
        self.event_queue.clear()

        self.floor_plan.change_map(map_name)
github eevee / flax / flax / ui / console / game.py View on Github external
elif key == '>':
            event = Descend(self.world.player)
        elif key == '<':
            event = Ascend(self.world.player)
        elif key == ',':
            tile = self.world.current_map.find(self.world.player)
            # TODO might consolidate this to a single event later if it fucks
            # up the sense of time.  or maybe it should!
            for item in tile.items:
                self.world.push_player_action(PickUp(self.world.player, item))
        elif key == 'e':
            # TODO menu prompt plz; identifying items is gonna be pretty
            # important later
            from flax.component import IContainer
            from flax.entity import Armor
            for item in IContainer(self.world.player).inventory:
                if item.type is Armor:
                    break
            else:
                return key
            event = Equip(self.world.player, item)
        elif key == 'r':
            # TODO menu prompt plz; identifying items is gonna be pretty
            # important later
            from flax.relation import Wearing
            rels = self.world.player.relates_to[Wearing]
            if rels:
                rel = next(iter(rels))
                event = Unequip(self.world.player, rel.to_entity)
            else:
                pass
        else:
github eevee / flax / flax / component.py View on Github external
def do_pick_up(event, portable):
    from flax.entity import Layer
    assert portable.entity.type.layer is Layer.item
    event.world.current_map.remove(portable.entity)
    IContainer(event.actor).inventory.append(portable.entity)
github eevee / flax / flax / component.py View on Github external
def do_unlock(event, lockable):
    # TODO check that the key is a key, player holds it, etc.  (inform has
    # touchability rules for all this...)
    lockable.locked = False

    # Destroy the key.  TODO: need to be able to tell an entity that i'm taking
    # it away from whatever owns it, whatever that may mean!  inform's "now"
    # does this
    IContainer(event.actor).inventory.remove(event.agent)