Exemple #1
0
    def setUp(self):
        self.world = World()

        self.human = Entity()
        self.human.components.add(Container())

        self.food = Entity()
        self.food.components.add(Portable())
Exemple #2
0
    def make(self, **kwargs):
        """ Makes an Entity with the Assemblage's combination of Components.

            Can initialize the Entity's components with particular values.
            If the Assemblage has been created with initial values for the parameters of its Components,
                those initial values are applied.
            If this method is called with kwargs, it will pass that initial value to the appropriate Component.
            Calling `make` with kwargs will override the initial values set on the Assemblage.

            Returns an Entity.
        """

        entity = Entity()

        for component_type, init_kwargs in six.iteritems(self.component_types):
            instance_kwargs = init_kwargs
            instance_kwargs.update({k:v for k,v in six.iteritems(kwargs) if k in component_type.__slots__})
            kwargs = {k:v for k,v in six.iteritems(kwargs) if k not in component_type.__slots__}

            component = component_type(**instance_kwargs)
            entity.components.add(component)

        if kwargs:
            raise ValueError("Unknown initial properties: {}".format(', '.join(six.iterkeys(kwargs))))

        return entity
Exemple #3
0
    def setUp(self):
        self.cat = Entity()
        self.cat.components |= set([Alive(), Portable(), Container()])

        self.plant = Entity()
        self.plant.components.add(Alive())

        self.bathtub = Entity()
        self.bathtub.components.add(Container())

        self.brains = Entity()
        self.brains.components |= set([Portable(), Location()])

        self.zombie = Entity()
        self.zombie.components |= set([Moveable(), Location(), Container()])

        self.entities = set(
            [self.cat, self.plant, self.bathtub, self.brains, self.zombie])
Exemple #4
0
    def test_destroy_entity_raises_if_not_entity(self):
        unrelated_entity = Entity()
        self.assertNotIn(unrelated_entity, self.world.entities)

        with self.assertRaises(ValueError) as e:
            self.world.destroy_entity(unrelated_entity)

        if six.PY2:
            exception_message = e.exception.message
        if six.PY3:
            exception_message = str(e.exception)

        self.assertEqual(
            exception_message,
            "{0} does not contain {1}".format(repr(self.world),
                                              repr(unrelated_entity)))