Ejemplo n.º 1
0
 def __init__(self,
              *args,
              depth=0,
              z_shift=(0, 0),
              face_position=(0, 0),
              face_size=(0, 0),
              passable=False,
              **kwargs):
     super().__init__(*args, name='collision', **kwargs)
     self.dispatcher.register_listener(self, 'ecs_collision')
     self.depth = depth
     self.passable = passable
     if len(z_shift) != 2 or not all(isinstance(x, int) for x in z_shift):
         raise BearECSException(
             'z_shift for a CollisionComponent should be a tuple of 2 ints')
     self.z_shift = z_shift
     if len(face_position) != 2 or not all(
             isinstance(x, int) for x in face_position):
         raise BearECSException(
             'face_position for a CollisionComponent should be a tuple of 2 ints'
         )
     self.face_position = face_position
     if len(face_size) != 2 or not all(
             isinstance(x, int) for x in face_size):
         raise BearECSException(
             'z_shift for a CollisionComponent should be a tuple of 2 ints')
     self.face_size = face_size
Ejemplo n.º 2
0
 def __init__(self, dispatcher, name='Root', owner=None):
     super().__init__()
     if not name:
         raise BearECSException('Cannot create a component without a name')
     if dispatcher and not isinstance(dispatcher, BearEventDispatcher):
         raise BearECSException(f'Attempted to use {type(dispatcher)} as dispatcher')
     self.dispatcher = dispatcher
     self.name = name
     self.owner = None
     self.set_owner(owner)
Ejemplo n.º 3
0
 def __repr__(self):
     # It's unlikely that repr(layout) is ever gonna be necessary.
     # And it's very bad to try and serialize them. Widget serialization is
     # only intended for use with the Widgets that can be part of a
     # WidgetComponent. Layouts are sorta frontend for the entire ECS system,
     # not a part of it.
     raise BearECSException(
         'ECSLayout and its children are not meant to be stored via repr')
Ejemplo n.º 4
0
 def hitpoints(self, value):
     if not isinstance(value, int):
         raise BearECSException(
             f'Attempting to set hitpoints of {self.owner.id} to non-integer {value}'
         )
     self._hitpoints = value
     if self._hitpoints < 0:
         self._hitpoints = 0
     self.process_hitpoint_update()
Ejemplo n.º 5
0
    def add_entity(self, entity):
        """
        Register the entity to be displayed. Assumes that the entity has a
        widget already.

        The entity is not actually shown until the `'ecs_add'` event is emitted
        :return:
        """
        if not isinstance(entity, Entity):
            raise BearECSException('Cannot add non-Entity to ECSLayout')
        self.entities[entity.id] = entity
        self.widgets[entity.id] = entity.widget.widget
Ejemplo n.º 6
0
    def add_component(self, component):
        """
        Add a single component to the Entity.

        Raises exception if ``Component.name`` is already in ``self.__dict__``
        and not in ``self.components``. This allows overwriting
        components (should you want to change eg the entity's widget), while
        protecting the non-Component properties.

        :param component: A Component instance.
        """
        if not isinstance(component, Component):
            raise BearECSException('Only Component instance can be added' +
                                   ' as an entity\'s component')
        if component.name not in self.components and \
                component.name in self.__dict__:
            raise BearECSException('Cannot add component' +
                   '{} that shadows builtin attribute'.format(component.name))
        self.__dict__[component.name] = component
        self.components.append(component.name)
        component.owner = self
Ejemplo n.º 7
0
    def set_owner(self, owner):
        """
        Register a component owner.
        
        This is only useful if the component is passed from one owner to
        another, or if the component is created with the `owner` argument (thus
        attaching it immediately upon creation). This method calls owner's
        ``add_component``

        :param owner: an Entity to attach to.
        """
        if owner:
            if not isinstance(owner, Entity):
                raise BearECSException('Only an Entity can be Component owner')
            owner.add_component(self)
Ejemplo n.º 8
0
    def remove_entity(self, entity_id):
        """
        Forget about the registered entity and its widget.

        Does not imply or cause the destruction of Entity object itself or any
        of its Component objects. Making sure that the entity is removed cleanly
        is not the Layout's job.

        :param entity_id: Entity ID
        """
        if entity_id not in self.entities:
            raise BearECSException(
                'Attempting to remove nonexistent entity {} from ESCLayout'.
                format(entity_id))
        self.remove_child(self.entities[entity_id].widget.widget)
        del self.entities[entity_id]
Ejemplo n.º 9
0
    def remove_component(self, component_name):
        """
        Remove a single component from this entity.

        Uses the ``Component.name``, not an actual instance, as an argument. If
        the Entity doesn't have such a component, raises ``BearECSException``

        :param component_name: The name of a component to remove
        :return:
        """
        if component_name in self.components:
            del(self.__dict__[component_name])
            self.components.remove(component_name)
        else:
            raise BearECSException('Cannot remove component ' +
                          '{} that Entity doesn\'t have'.format(component_name))
Ejemplo n.º 10
0
    def remove_entity(self, entity_id):
        """
        Forget about the registered entity and its widget.
        Does not imply or cause the destruction of Entity object or any of its
        Component objects (except if this was the last reference). Making sure
        that the entity is removed cleanly is someone else's job.

        :param entity_id: ID of the removed entity.

        """
        if entity_id not in self.entities:
            raise BearECSException(
                'Attempting to remove nonexistent entity {} from ESCLayout'.
                format(entity_id))
        try:
            self.remove_child(self.entities[entity_id].widget.widget)
            del self.widget_to_entity[id(
                self.entities[entity_id].widget.widget)]
        except BearLayoutException:
            # Silently ignore any attempt to remove entities which weren't ever
            # actually placed on the layout (such as eg hands which were never
            # used during the whole level)
            pass
        del self.entities[entity_id]
Ejemplo n.º 11
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     if not isinstance(self.widget, SwitchingWidget):
         raise BearECSException(
             'SwitchWidgetComponent can only be used with SwitchingWidget')