Example #1
0
    def unregister(cls):
        """Unregister a behaviour class

        Returns:
            True if the behaviour was unregistered, false if Not
        """
        try:
            BehaviourManager.unregister_behaviour(cls.__registered_as)
            cls.__registered_as = None
        except NotRegisteredError as error:
            print(error)
            return False
Example #2
0
    def edit_behaviours(self):
        """Show the dialog to edit behaviours"""
        dialog = Behaviours(self)
        values = dialog.show_modal(self.editor_gui.editor_window,
                                   self.engine.pump)
        if not dialog.return_value:
            return False
        BehaviourManager.clear_behaviours()
        current_items = list(values["current_items"])
        self.project.set("fife-rpg", "Behaviours", current_items)
        self.register_behaviours(current_items)

        self.project_changed = True
Example #3
0
    def update_agents(self, game_map):
        """Updates the map to be in sync with the entities

        Args:
            game_map: The name of the map, or a Map instance
        """
        if isinstance(game_map, str):
            game_map = self.maps[game_map]
        if isinstance(game_map, str):  # The map is not yet loaded
            return
        object_namespace = self.settings.get("fife-rpg", "ObjectNamespace",
                                             "fife-rpg")
        fife_model = self.engine.getModel()
        game_map.update_entities()
        for entity in game_map.entities:
            agent = getattr(entity, Agent.registered_as)
            namespace = agent.namespace or object_namespace
            map_object = fife_model.getObject(agent.gfx, namespace)
            if not map_object:
                raise RuntimeError("There is no object %s in the namespace %s"
                                   % (agent.gfx, namespace))
            general = getattr(entity, General.registered_as)
            layer = game_map.get_layer(agent.layer)
            fife_instance = layer.getInstance(general.identifier)
            if fife_instance:
                fife_object = fife_instance.getObject()
                if (fife_object.getId() != map_object.getId() or
                        fife_object.getNamespace() !=
                        map_object.getNamespace()):
                    layer.deleteInstance(fife_instance)
                    fife_instance = None
            if not fife_instance:
                position = agent.position
                fife_instance = layer.createInstance(
                    map_object,
                    fife.ExactModelCoordinate(position.x,
                                              position.y,
                                              position.z),
                    general.identifier)
                fife_instance.setRotation(agent.rotation)
                visual = fife.InstanceVisual.create(fife_instance)
                if map_object.getAction('default'):
                    target = fife.Location(game_map.actor_layer)
                    fife_instance.actRepeat('default', target)
                fifeagent = getattr(entity, FifeAgent.registered_as)
                behaviour_class = BehaviourManager.get_behaviour(
                    agent.behaviour_type)
                if behaviour_class is None:
                    raise RuntimeError("There is no registered behaviour %s"
                                       % agent.behaviour_type)
                behaviour = behaviour_class(**agent.behaviour_args)
                behaviour.agent = fife_instance
                fifeagent.behaviour = behaviour
                fifeagent.layer = layer
                fifeagent.instance = fife_instance
                setup_behaviour(fifeagent)
                fifeagent.behaviour.idle()
            else:
                visual = fife_instance.get2dGfxVisual()
            visual.setStackPosition(STACK_POSITION[agent.type])
Example #4
0
    def register(cls, name="Base"):
        """Registers the class as a behaviour

        Args:
            name: The name under which the class should be registered

        Returns:
            True if the behaviour was registered, False if not.
        """
        try:
            BehaviourManager.register_behaviour(name, cls)
            cls.__registered_as = name
            return True
        except AlreadyRegisteredError as error:
            print error
            return False
Example #5
0
    def update_agents(self, game_map):
        """Updates the map to be in sync with the entities

        Args:
            game_map: The name of the map, or a Map instance
        """
        if isinstance(game_map, str):
            game_map = self.maps[game_map]
        if isinstance(game_map, str):  # The map is not yet loaded
            return
        object_namespace = self.settings.get("fife-rpg", "ObjectNamespace",
                                             "fife-rpg")
        fife_model = self.engine.getModel()
        game_map.update_entities()
        for entity in game_map.entities:
            agent = getattr(entity, Agent.registered_as)
            namespace = agent.namespace or object_namespace
            map_object = fife_model.getObject(agent.gfx, namespace)
            if not map_object:
                raise RuntimeError(
                    "There is no object %s in the namespace %s" %
                    (agent.gfx, namespace))
            general = getattr(entity, General.registered_as)
            layer = game_map.get_layer(agent.layer)
            fife_instance = layer.getInstance(general.identifier)
            if fife_instance:
                fife_object = fife_instance.getObject()
                if (fife_object.getId() != map_object.getId()
                        or fife_object.getNamespace() !=
                        map_object.getNamespace()):
                    layer.deleteInstance(fife_instance)
                    fife_instance = None
            if not fife_instance:
                position = agent.position
                fife_instance = layer.createInstance(
                    map_object,
                    fife.ExactModelCoordinate(position.x, position.y,
                                              position.z), general.identifier)
                fife_instance.setRotation(agent.rotation)
                visual = fife.InstanceVisual.create(fife_instance)
                if map_object.getAction('default'):
                    target = fife.Location(game_map.actor_layer)
                    fife_instance.actRepeat('default', target)
                fifeagent = getattr(entity, FifeAgent.registered_as)
                behaviour_class = BehaviourManager.get_behaviour(
                    agent.behaviour_type)
                if behaviour_class is None:
                    raise RuntimeError("There is no registered behaviour %s" %
                                       agent.behaviour_type)
                behaviour = behaviour_class(**agent.behaviour_args)
                behaviour.agent = fife_instance
                fifeagent.behaviour = behaviour
                fifeagent.layer = layer
                fifeagent.instance = fife_instance
                setup_behaviour(fifeagent)
                fifeagent.behaviour.idle()
            else:
                visual = fife_instance.get2dGfxVisual()
            visual.setStackPosition(STACK_POSITION[agent.type])
Example #6
0
 def clear(self):
     """Clears all data and restores saved settings"""
     self._maps = {}
     self._current_map = None
     self._components = {}
     self._actions = {}
     self._systems = {}
     self._behaviours = {}
     self.changed_maps = []
     self.project_changed = False
     self.entity_changed = False
     self.editor_gui.reset_layerlist()
     self.map_entities = None
     self.set_selected_object(None)
     tmp_settings = list(self.settings.getSettingsFromFile("fife-rpg").keys())
     for setting in tmp_settings:
         if setting in self.editor_settings:
             self.settings.set("fife-rpg", setting,
                               self.editor_settings[setting])
         else:
             self.settings.remove("fife-rpg", setting)
     ComponentManager.clear_components()
     ComponentManager.clear_checkers()
     ActionManager.clear_actions()
     ActionManager.clear_commands()
     SystemManager.clear_systems()
     BehaviourManager.clear_behaviours()
     self.editor.delete_maps()
     self.editor.delete_objects()
     if self.project_source is not None:
         self.engine.getVFS().removeSource(self.project_source)
         self.project_source = None
     if self.project_dir is not None:
         sys.path.remove(self.project_dir)
         self.project_dir = None
     self.project = None
     for callback in self._project_cleared_callbacks:
         callback()
     self.create_world()
Example #7
0
    def update_agents(self, game_map):
        """Updates the map to be in sync with the entities

        Args:
            game_map: The name of the map, or a Map instance
        """
        if isinstance(game_map, str):
            game_map = self.maps[game_map]
        if isinstance(game_map, str):  # The map is not yet loaded
            return
        object_namespace = self.settings.get("fife-rpg", "ObjectNamespace",
                                             "fife-rpg")
        fife_model = self.engine.getModel()
        for entity in game_map.entities:
            agent = getattr(entity, Agent.registered_as)
            map_object = fife_model.getObject(agent.gfx,
                object_namespace)
            general = getattr(entity, General.registered_as)
            layer = game_map.get_layer(agent.layer)
            fife_instance = layer.getInstance(general.identifier)
            if not fife_instance:
                fife_instance = layer.createInstance(
                    map_object,
                    fife.ExactModelCoordinate(*agent.position),
                    general.identifier)
                visual = fife.InstanceVisual.create(fife_instance)
                if (map_object.getAction('default')):
                    target = fife.Location(game_map.actor_layer)
                    fife_instance.act('default', target, True)
                fifeagent = getattr(entity, FifeAgent.registered_as)
                behaviour_class = BehaviourManager.get_behaviour(
                                                        agent.behaviour_type)
                behaviour = behaviour_class(**agent.behaviour_args)
                behaviour.agent = fife_instance
                fifeagent.behaviour = behaviour
                fifeagent.layer = layer
                fifeagent.instance = fife_instance
                setup_behaviour(fifeagent)
                fifeagent.behaviour.idle()
            else:
                visual = fife_instance.get2dGfxVisual()
                location = fife_instance.getLocation()
                location.setExactLayerCoordinates(fife.ExactModelCoordinate(
                                                    *agent.position))
                fife_instance.setLocation(location)
            fife_instance.setRotation(agent.rotation)
            visual.setStackPosition(STACK_POSITION[agent.type])