def close_inventory(self):
        base = utils.fetch(utils.qualify_controller_name("game_controller"))

        imports = base.GameController.get_imports()

        view_module = utils.fetch(imports[base.GameController.VIEWS]["game_view"])

        view = view_module.GameView()

        controller = base.GameController(self.model, view)

        pygame.event.post(pygame.event.Event(
            event_types.UPDATE_GAME_STATE,
            {"Controller": controller, "View": view}))
Ejemplo n.º 2
0
    def _handle_trigger(self, trigger, position, is_previous, pressed_key):
        # We support triggers being fired when entering or leaving a tile.
        valid_previous_trigger = trigger.triggered_on == Trigger.EXIT and is_previous
        valid_current_trigger = trigger.triggered_on == Trigger.ENTER and not is_previous

        valid_action_trigger = trigger.triggered_on == Trigger.KEY_ACTION and \
                               pressed_key == pygame.K_e

        if not (valid_previous_trigger or valid_current_trigger or valid_action_trigger):
            return

        # Make sure the character is facing the appropriate direction
        if not (self.model.character.direction == trigger.direction_facing or \
                trigger.direction_facing == Trigger.DIRECTION_ANY):
            return

        if trigger.is_one_time:
            trigger.is_active = False

        if trigger.action_type == Trigger.CHANGE_MAP:
            self._change_map(trigger.action_data['map_name'])
            position = Position(
                trigger.action_data['character_x'],
                trigger.action_data['character_y'])

            self.view.set_visible_model_position(self.model.character, position)

        if trigger.action_type == Trigger.START_BATTLE:
            self._start_battle(trigger.action_data['enemy'], position)

        if trigger.action_type == Trigger.GET_ITEM:
            self.model.character.inventory.\
                    add_item(self.model.items[trigger.action_data['item']])
                # Use a dialog here to show that an item is acquired

        if trigger.action_type == Trigger.SHOW_DIALOG:
            if not self.unmoved:
                self.unmoved = True
                new_dialog = Dialog(
                    Position(trigger.action_data['dialog_x'], trigger.action_data['dialog_y']),
                    trigger.action_data['dialog_text'],
                    trigger.action_data['timed'],
                    trigger.action_data['timeout'],
                    self)
                self.view.add_model(
                    new_dialog, GameView.render_dialog, new_dialog.location, GameView.OVERLAY)
                
                if not new_dialog.timed:
                    # TODO: Render icon to indicate manual procession
                    base = utils.fetch(utils.qualify_controller_name("dialog_controller"))
                    
                    controller = base.DialogController(new_dialog, self.view)

                    pygame.event.post(
                        pygame.event.Event(
                            event_types.UPDATE_GAME_STATE,
                            {"Controller": controller,
                             "View": self.view}))

        print("Action occurred with data: " + str(trigger.action_data))
Ejemplo n.º 3
0
    def end_battle(self):
        base = utils.fetch(utils.qualify_controller_name("game_controller"))

        imports = base.GameController.get_imports()

        view_module = utils.fetch(
            imports[base.GameController.VIEWS]["game_view"])

        view = view_module.GameView()

        controller = base.GameController(self.model, view)

        pygame.event.post(
            pygame.event.Event(event_types.UPDATE_GAME_STATE, {
                "Controller": controller,
                "View": view
            }))
Ejemplo n.º 4
0
    def open_inventory(self):
        base = utils.fetch(
            utils.qualify_controller_name("inventory_controller"))

        imports = base.InventoryController.get_imports()

        view_module = utils.fetch(
            imports[base.InventoryController.VIEWS]["inventory_view"])

        view = view_module.InventoryView()
        controller = base.InventoryController(self.model, view)

        pygame.event.post(
            pygame.event.Event(event_types.UPDATE_GAME_STATE, {
                "Controller": controller,
                "View": view
            }))
Ejemplo n.º 5
0
    def _start_battle(self, enemy_name, position):
        base = utils.fetch(utils.qualify_controller_name("battle_controller"))

        imports = base.BattleController.get_imports()

        view_module = utils.fetch(
            imports[base.BattleController.VIEWS]["battle_view"])

        view = view_module.BattleView()

        controller = base.BattleController(self.model, view, enemy_name)

        pygame.event.post(
            pygame.event.Event(event_types.UPDATE_GAME_STATE, {
                "Controller": controller,
                "View": view
            }))
Ejemplo n.º 6
0
    def open_main_menu(self, source_view):
        base = utils.fetch(utils.qualify_controller_name(
            "menu_controller"))

        imports = base.MenuController.get_imports()

        view_module = utils.fetch(imports[base.MenuController.VIEWS]["main_menu"])

        model = base.MenuController.build_menu()
        view = view_module.MainMenu()
        controller = base.MenuController(model, view, self, source_view)

        pygame.event.post(
            pygame.event.Event(
                event_types.UPDATE_GAME_STATE,
                {"Controller": controller,
                 "View": view}))
Ejemplo n.º 7
0
    def _start_battle(self, enemy_name, position):
        base = utils.fetch(utils.qualify_controller_name(
            "battle_controller"))

        imports = base.BattleController.get_imports()

        view_module = utils.fetch(imports[base.BattleController.VIEWS]["battle_view"])

        view = view_module.BattleView()

        controller = base.BattleController(self.model, view, enemy_name)
        
        pygame.event.post(
            pygame.event.Event(
                event_types.UPDATE_GAME_STATE,
                {"Controller": controller,
                 "View": view}))
Ejemplo n.º 8
0
    def open_main_menu(self, source_view):
        base = utils.fetch(utils.qualify_controller_name("menu_controller"))

        imports = base.MenuController.get_imports()

        view_module = utils.fetch(
            imports[base.MenuController.VIEWS]["main_menu"])

        model = base.MenuController.build_menu()
        view = view_module.MainMenu()
        controller = base.MenuController(model, view, self, source_view)

        pygame.event.post(
            pygame.event.Event(event_types.UPDATE_GAME_STATE, {
                "Controller": controller,
                "View": view
            }))
Ejemplo n.º 9
0
    def handle_key_press(self, pressed_key):
        position = self.view.get_visible_model_position(self.model.character)
        destination_tile = None

        if pressed_key == pygame.K_LEFT or pressed_key == pygame.K_a:
            self.model.character.direction = Character.LEFT
            self.unmoved = False
            destination_tile = self.model.current_map.get_map_tile(
                position.x_coord - 1, position.y_coord, 0)
            if (position.x_coord - 1) >= 0 and destination_tile.walkable == 1:
                position.set_x_coord(position.x_coord - 1)
        if pressed_key == pygame.K_RIGHT or pressed_key == pygame.K_d:
            self.model.character.direction = Character.RIGHT
            self.unmoved = False
            destination_tile = self.model.current_map.get_map_tile(
                position.x_coord + 1, position.y_coord, 0)
            if (position.x_coord +
                    1) < Map.GRID_SIZE and destination_tile.walkable == 1:
                position.set_x_coord(position.x_coord + 1)
        if pressed_key == pygame.K_UP or pressed_key == pygame.K_w:
            self.model.character.direction = Character.UP
            self.unmoved = False
            destination_tile = self.model.current_map.get_map_tile(
                position.x_coord, position.y_coord - 1, 0)
            if (position.y_coord - 1) >= 0 and destination_tile.walkable == 1:
                position.set_y_coord(position.y_coord - 1)
        if pressed_key == pygame.K_DOWN or pressed_key == pygame.K_s:
            self.model.character.direction = Character.DOWN
            self.unmoved = False
            destination_tile = self.model.current_map.get_map_tile(
                position.x_coord, position.y_coord + 1, 0)
            if (position.y_coord +
                    1) < Map.GRID_SIZE and destination_tile.walkable == 1:
                position.set_y_coord(position.y_coord + 1)
        if pressed_key == pygame.K_b:
            self._start_battle('Cultist', position)
        if pressed_key == pygame.K_ESCAPE:
            base = utils.fetch(
                utils.qualify_controller_name("menu_controller"))

            imports = base.MenuController.get_imports()

            view_module = utils.fetch(
                imports[base.MenuController.VIEWS]["main_menu"])

            view = view_module.MainMenu()
            controller = base.MenuController(self.model, view, self, self.view)

            pygame.event.post(
                pygame.event.Event(event_types.UPDATE_GAME_STATE, {
                    "Controller": controller,
                    "View": view
                }))
        if pressed_key == pygame.K_i:
            self.open_inventory()

        self.view.set_visible_model_position(self.model.character, position)
        self.model.character.position = position

        # Check if any triggers have been activated.
        if position in self.triggers and self.triggers[position].is_active:
            # TODO Handle chance here.
            self._handle_trigger(self.triggers[position], position, False,
                                 pressed_key)
        if self.previous_position in self.triggers and self.triggers[
                self.previous_position].is_active:
            # TODO Handle chance here.
            self._handle_trigger(self.triggers[self.previous_position],
                                 self.previous_position, True, pressed_key)

        self.previous_position = copy.copy(position)
        if self.changed_map:
            # If we changed maps we need to reset our previous position such that we don't fire the
            # trigger twice.
            self.previous_position = None
            self.changed_map = False
Ejemplo n.º 10
0
    def _handle_trigger(self, trigger, position, is_previous, pressed_key):
        # We support triggers being fired when entering or leaving a tile.
        valid_previous_trigger = trigger.triggered_on == Trigger.EXIT and is_previous
        valid_current_trigger = trigger.triggered_on == Trigger.ENTER and not is_previous

        valid_action_trigger = trigger.triggered_on == Trigger.KEY_ACTION and \
                               pressed_key == pygame.K_e

        if not (valid_previous_trigger or valid_current_trigger
                or valid_action_trigger):
            return

        # Make sure the character is facing the appropriate direction
        if not (self.model.character.direction == trigger.direction_facing or \
                trigger.direction_facing == Trigger.DIRECTION_ANY):
            return

        if trigger.is_one_time:
            trigger.is_active = False

        if trigger.action_type == Trigger.CHANGE_MAP:
            self._change_map(trigger.action_data['map_name'])
            position = Position(trigger.action_data['character_x'],
                                trigger.action_data['character_y'])

            self.view.set_visible_model_position(self.model.character,
                                                 position)

        if trigger.action_type == Trigger.START_BATTLE:
            self._start_battle(trigger.action_data['enemy'], position)

        if trigger.action_type == Trigger.GET_ITEM:
            self.model.character.inventory.\
                    add_item(self.model.items[trigger.action_data['item']])
            # Use a dialog here to show that an item is acquired

        if trigger.action_type == Trigger.SHOW_DIALOG:
            if not self.unmoved:
                self.unmoved = True
                new_dialog = Dialog(
                    Position(trigger.action_data['dialog_x'],
                             trigger.action_data['dialog_y']),
                    trigger.action_data['dialog_text'],
                    trigger.action_data['timed'],
                    trigger.action_data['timeout'], self)
                self.view.add_model(new_dialog, GameView.render_dialog,
                                    new_dialog.location, GameView.OVERLAY)

                if not new_dialog.timed:
                    # TODO: Render icon to indicate manual procession
                    base = utils.fetch(
                        utils.qualify_controller_name("dialog_controller"))

                    controller = base.DialogController(new_dialog, self.view)

                    pygame.event.post(
                        pygame.event.Event(event_types.UPDATE_GAME_STATE, {
                            "Controller": controller,
                            "View": self.view
                        }))

        print("Action occurred with data: " + str(trigger.action_data))
Ejemplo n.º 11
0
    def render_target_window(target_window, screen, position, *args, **kwargs):
        font = pygame.font.SysFont("monospace", BattleView.FONT_SIZE)

        base = utils.fetch(utils.qualify_controller_name(
                           "battle_controller"))

        if target_window.battle_state is base.BattleController.TARGET_SELECT:
            # Render the target icon over the new target
            target_image = pygame.image.load(
                os.path.join('Images', "target_icon.png"))
            target_image_scaled = pygame.transform.scale(
                target_image, (10, 10))
            screen.blit(target_image_scaled,
                        position.convert_to_pixels(BattleView.TARGET_ICON_OFFSET))

        # Render enemy stats window
        enemy_background = pygame.Surface((170, 110))
        enemy_background.fill((5, 4, 71, 100))
        enemy_stats_position = Position(11, 2)
        screen.blit(enemy_background, enemy_stats_position.convert_to_pixels(0))

        enemy_name_label = font.render("%s" % target_window.characters['Enemy'].name,
                                       1, (255, 255, 0))
        enemy_health_label = font.render("HP: %d/%d" %
                                         (target_window.characters['Enemy'].health,
                                         target_window.characters['Enemy'].total_health),
                                         1, (255, 255, 255))
        enemy_attack_label = font.render("ATK: %d" % target_window.characters['Enemy'].attack,
                                         1, (255, 255, 255))
        enemy_defense_label = font.render("DEF: %d" % target_window.characters['Enemy'].defense,
                                         1, (255, 255, 255))

        screen.blit(enemy_name_label,
                    enemy_stats_position.convert_with_offset(BattleView.PADDING,
                                                             BattleView.PADDING))
        screen.blit(enemy_health_label,
                    enemy_stats_position.convert_with_offset(BattleView.PADDING,
                                                             BattleView.LINE_HEIGHT +
                                                             BattleView.PADDING))
        screen.blit(enemy_attack_label,
                    enemy_stats_position.convert_with_offset(BattleView.PADDING,
                                                             2 * BattleView.LINE_HEIGHT +
                                                             BattleView.PADDING))
        screen.blit(enemy_defense_label,
                    enemy_stats_position.convert_with_offset(BattleView.PADDING,
                                                             3 * BattleView.LINE_HEIGHT +
                                                             BattleView.PADDING))

        # Render player stats window
        player_background = pygame.Surface((170, 110))
        player_background.fill((5, 4, 71, 100))
        player_stats_position = Position(2, 8)
        screen.blit(player_background, player_stats_position.convert_to_pixels(0))

        player_name_label = font.render("%s" % target_window.characters['Player'].name,
                                        1, (255, 255, 0))
        player_health_label = font.render("HP: %d/%d" %
                                          (target_window.characters['Player'].health,
                                          target_window.characters['Player'].total_health)
                                          , 1, (255, 255, 255))
        player_attack_label = font.render("ATK: %d" % target_window.characters['Player'].attack,
                                          1, (255, 255, 255))
        player_defense_label = font.render("DEF: %d" % target_window.characters['Player'].defense,
                                           1, (255, 255, 255))

        screen.blit(player_name_label,
                    player_stats_position.convert_with_offset(BattleView.PADDING,
                                                              BattleView.PADDING))
        screen.blit(player_health_label,
                    player_stats_position.convert_with_offset(BattleView.PADDING,
                                                              BattleView.LINE_HEIGHT +
                                                              BattleView.PADDING))
        screen.blit(player_attack_label,
                    player_stats_position.convert_with_offset(BattleView.PADDING,
                                                              2 * BattleView.LINE_HEIGHT +
                                                              BattleView.PADDING))
        screen.blit(player_defense_label,
                    player_stats_position.convert_with_offset(BattleView.PADDING,
                                                              3 * BattleView.LINE_HEIGHT +
                                                              BattleView.PADDING))
Ejemplo n.º 12
0
    def handle_key_press(self, pressed_key):
        position = self.view.get_visible_model_position(
            self.model.character)
        destination_tile = None

        if pressed_key == pygame.K_LEFT or pressed_key == pygame.K_a:
            self.model.character.direction = Character.LEFT
            self.unmoved = False
            destination_tile = self.model.current_map.get_map_tile(
                position.x_coord - 1, position.y_coord, 0)
            if (position.x_coord - 1) >= 0 and destination_tile.walkable == 1:
                position.set_x_coord(position.x_coord - 1)
        if pressed_key == pygame.K_RIGHT or pressed_key == pygame.K_d:
            self.model.character.direction = Character.RIGHT
            self.unmoved = False
            destination_tile = self.model.current_map.get_map_tile(
                position.x_coord + 1, position.y_coord, 0)
            if(position.x_coord + 1) < Map.GRID_SIZE and destination_tile.walkable == 1:
                position.set_x_coord(position.x_coord + 1)
        if pressed_key == pygame.K_UP or pressed_key == pygame.K_w:
            self.model.character.direction = Character.UP
            self.unmoved = False
            destination_tile = self.model.current_map.get_map_tile(
                position.x_coord, position.y_coord - 1, 0)
            if(position.y_coord - 1) >= 0 and destination_tile.walkable == 1:
                position.set_y_coord(position.y_coord - 1)
        if pressed_key == pygame.K_DOWN or pressed_key == pygame.K_s:
            self.model.character.direction = Character.DOWN
            self.unmoved = False
            destination_tile = self.model.current_map.get_map_tile(
                position.x_coord, position.y_coord + 1, 0)
            if(position.y_coord + 1) < Map.GRID_SIZE and destination_tile.walkable == 1:
                position.set_y_coord(position.y_coord + 1)
        if pressed_key == pygame.K_b:
            self._start_battle('Cultist', position)
        if pressed_key == pygame.K_ESCAPE:
            base = utils.fetch(utils.qualify_controller_name("menu_controller"))
            
            imports = base.MenuController.get_imports()

            view_module = utils.fetch(imports[base.MenuController.VIEWS]["main_menu"])

            view = view_module.MainMenu()
            controller = base.MenuController(self.model, view, self, self.view)

            pygame.event.post(
                pygame.event.Event(
                    event_types.UPDATE_GAME_STATE,
                    {"Controller": controller,
                     "View": view}))
        if pressed_key == pygame.K_i:
            self.open_inventory()

        self.view.set_visible_model_position(self.model.character, position)
        self.model.character.position = position

        # Check if any triggers have been activated.
        if position in self.triggers and self.triggers[position].is_active:
            # TODO Handle chance here.
            self._handle_trigger(self.triggers[position], position, False,
                                 pressed_key)
        if self.previous_position in self.triggers and self.triggers[
            self.previous_position].is_active:
            # TODO Handle chance here.
            self._handle_trigger(self.triggers[self.previous_position],
                                 self.previous_position, True, pressed_key)

        self.previous_position = copy.copy(position)
        if self.changed_map:
            # If we changed maps we need to reset our previous position such that we don't fire the
            # trigger twice.
            self.previous_position = None
            self.changed_map = False