コード例 #1
0
    def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]:
        action: Optional[Action] = None

        key = event.sym

        player = self.engine.player

        if key in MOVE_KEYS:
            dx, dy = MOVE_KEYS[key]
            action = BumpAction(player, dx, dy)
        elif key in WAIT_KEYS:
            action = WaitAction(player)
        elif key == tcod.event.K_ESCAPE:
            raise SystemExit()
        elif key == tcod.event.K_v:
            self.engine.event_handler = HistoryViewer(self.engine)

        elif key == tcod.event.K_g:
            action = PickupAction(player)
        elif key == tcod.event.K_o:
            self.engine.event_handler = InventoryActivateHandler(self.engine)
        elif key == tcod.event.K_d:
            self.engine.event_handler = InventoryDropHandler(self.engine)
        elif key == tcod.event.K_SLASH:
            self.engine.event_handler = LookHandler(self.engine)

        return action
コード例 #2
0
    def ev_keydown(self,
                   event: tcod.event.KeyDown) -> Optional[ActionOrHandler]:
        action: Optional[Action] = None
        key = event.sym
        modifier = event.mod
        player = self.engine.player

        if key == tcod.event.K_PERIOD and modifier & (tcod.event.KMOD_LSHIFT |
                                                      tcod.event.KMOD_RSHIFT):
            return actions.TakeStairsAction(player)

        if key in MOVE_KEYS:
            dx, dy = MOVE_KEYS[key]
            action = BumpAction(player, dx, dy)
        elif key in WAIT_KEYS:
            action = WaitAction(player)
        elif key == tcod.event.K_ESCAPE:
            raise SystemExit()
        elif key == tcod.event.K_v:
            return HistoryViewer(self.engine)
        elif key == tcod.event.K_g:
            action = PickupAction(player)

        elif key == tcod.event.K_i:
            return InventoryActivateHandler(self.engine)
        elif key == tcod.event.K_d:
            return InventoryDropHandler(self.engine)
        elif key == tcod.event.K_c:
            return CharacterScreenEventHandler(self.engine)
        elif key == tcod.event.K_SLASH:
            return LookHandler(self.engine)

        return action
コード例 #3
0
ファイル: input_handler.py プロジェクト: Niarit/rogue_like
    def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[ActionOrHandler]:
        action: Optional[Action] = None

        key = event.sym

        player = self.engine.player

        if key in MOVE_KEYS:
            dx, dy = MOVE_KEYS[key]
            action = BumpAction(player, dx, dy)
        elif key in WAIT_KEYS:
            action = WaitAction(player)

        elif key == tcod.event.K_ESCAPE:
            raise SystemExit()
        elif key == tcod.event.K_v:
            return HistoryViewer(self.engine)
        elif key == tcod.event.K_g:
            action = PickupAction(player)
        elif key == tcod.event.K_i:
            return InventoryActivateHandler(self.engine)
        elif key == tcod.event.K_d:
            return InventoryDropHandler(self.engine)
        elif key == tcod.event.K_c:
            return CharacterScreenEventHandler(self.engine)
        elif key == tcod.event.K_l:
            return LookHandler(self.engine)
        elif key == tcod.event.K_y:
            return actions.TakeStairsAction(player)

        # No valid key was pressed
        return action
コード例 #4
0
    def ev_keydown(self,
                   event: tcod.event.KeyDown) -> Optional[ActionOrHandler]:
        action: Optional[Action] = None

        key = event.sym
        modifier = event.mod

        player = self.engine.player

        if key == tcod.event.K_SPACE:
            return actions.TakeStairsAction(player)

        if key in MOVE_KEYS:
            dx, dy = MOVE_KEYS[key]
            action = BumpAction(player, dx, dy)
        elif key in WAIT_KEYS:
            action = WaitAction(player)

        elif key == tcod.event.K_ESCAPE:
            raise SystemExit()
        elif key == tcod.event.K_m:
            return HistoryViewer(self.engine)
        elif key == tcod.event.K_g:
            action = PickupAction(player)
        elif key == tcod.event.K_i:
            return InventoryActivateHandler(self.engine)
        elif key == tcod.event.K_SLASH:
            return LookHandler(self.engine)
        elif key == tcod.event.K_TAB:
            return ControlsEventHandler(self.engine)

        # Был нажат невалидный ключ.
        return action
コード例 #5
0
def player_act(player, level, game):
    command = game.get_command()

    if command.name == Command.MOVE:
        return MoveAction(*command.data)
    elif command.name == Command.PICKUP:
        return PickupAction()
    elif command.name == Command.DROP:
        return DropAction()

    return WaitAction()
コード例 #6
0
    def ev_keydown(self,
                   event: tcod.event.KeyDown) -> Optional[ActionOrHandler]:
        super().ev_keydown(event)
        action: Optional[Action] = None

        key = event.sym
        modifier = event.mod

        player = self.engine.player

        if key == tcod.event.K_PERIOD and modifier & (tcod.event.KMOD_LSHIFT |
                                                      tcod.event.KMOD_RSHIFT):
            return actions.TakeStairsAction(player)

        # If fullscreen was toggled, keep input handler active
        if event.sym == tcod.event.K_RETURN and event.mod & tcod.event.KMOD_ALT:
            self.engine.toggle_fullscreen()
            return None

        if key in MOVE_KEYS:
            dx, dy = MOVE_KEYS[key]
            action = BumpAction(player, dx, dy)

        elif key in WAIT_KEYS:
            action = WaitAction(player)

        elif key == tcod.event.K_ESCAPE:
            raise SystemExit()

        elif key == tcod.event.K_v:
            return HistoryViewer(self.engine)

        elif key == tcod.event.K_g:
            action = PickupAction(player)

        elif key == tcod.event.K_i:
            return InventoryActivateHandler(self.engine)

        elif key == tcod.event.K_d:
            return InventoryDropHandler(self.engine)

        elif key == tcod.event.K_c:
            return CharacterScreenEventHandler(self.engine)

        elif key == tcod.event.K_m:
            return LookHandler(self.engine)

        return action
コード例 #7
0
    def ev_keydown(self, event: str) -> Optional[ActionOrHandler]:
        action: Optional[Action] = None

        key = event
        modifier = event

        player = self.engine.player

        # if key == tcod.event.K_PERIOD and modifier & (
        #     tcod.event.KMOD_LSHIFT | tcod.event.KMOD_RSHIFT
        # ):

        if key == "spacebar":
            return actions.TakeStairsAction(player)
        if key == "numpad0":
            return actions.KillAllAction(player)
        if key == "numpadenter":
            return actions.SwitchViewMode(player)
        if key == "numpadadd":
            return actions.CheatTakeStairsAction(player)

        if key in MOVE_KEYS:
            dx, dy = MOVE_KEYS[key]
            action = BumpAction(player, dx, dy)
        elif key in WAIT_KEYS:
            action = WaitAction(player)

        elif key == "escape":
            raise SystemExit()
        elif key == "v":
            return HistoryViewer(self.engine)

        elif key == "g":
            action = PickupAction(player)

        elif key == "i":
            return InventoryActivateHandler(self.engine)
        elif key == "d":
            return InventoryDropHandler(self.engine)
        elif key == "c":
            return CharacterScreenEventHandler(self.engine)
        elif key == "/":
            return LookHandler(self.engine)

        # No valid key was pressed
        return action
コード例 #8
0
    def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]:
        action: Optional[Action] = None

        key = event.sym

        # Common arguments for player actions.
        context = (self.engine, self.engine.player)

        if key in (tcod.event.K_UP, tcod.event.K_k):
            action = BumpAction(*context, dx=0, dy=-1)
        elif key in (tcod.event.K_DOWN, tcod.event.K_j):
            action = BumpAction(*context, dx=0, dy=1)
        elif key in (tcod.event.K_LEFT, tcod.event.K_h):
            action = BumpAction(*context, dx=-1, dy=0)
        elif key in (tcod.event.K_RIGHT, tcod.event.K_l):
            action = BumpAction(*context, dx=1, dy=0)

        elif key == tcod.event.K_y:
            action = BumpAction(*context, dx=-1, dy=-1)
        elif key == tcod.event.K_u:
            action = BumpAction(*context, dx=1, dy=-1)
        elif key == tcod.event.K_b:
            action = BumpAction(*context, dx=-1, dy=1)
        elif key == tcod.event.K_n:
            action = BumpAction(*context, dx=1, dy=1)

        elif key == tcod.event.K_PERIOD:
            action = WaitAction(*context)

        elif key == tcod.event.K_ESCAPE:
            action = EscapeAction(*context)

        elif key == tcod.event.K_g:
            action = PickupAction(*context)

        elif key == tcod.event.K_i:
            action = ShowInventoryAction(*context)

        elif key == tcod.event.K_d:
            action = ShowInventoryAction(*context, dropping=True)

        # No valid key was pressed
        return action
コード例 #9
0
ファイル: input_handlers.py プロジェクト: SamuelKyle1998/YARG
    def ev_keydown(self,
                   event: tcod.event.KeyDown) -> Optional[ActionOrHandler]:
        #action holds whatever subclass of Action we end up assigning it to. Defaults to none.
        action: Optional[Action] = None
        #key holds the actual key that we pressed
        key = event.sym
        modifier = event.mod
        player = self.engine.player

        if key == tcod.event.K_PERIOD and modifier & (tcod.event.KMOD_LSHIFT |
                                                      tcod.event.KMOD_RSHIFT):
            return actions.TakeStairsAction(player)
        #create a MovementAction for the up, down, left and right keys if any of them are pressed
        if key in MOVE_KEYS:
            dx, dy = MOVE_KEYS[key]
            action = BumpAction(player, dx, dy)
        elif key in WAIT_KEYS:
            action = WaitAction(player)

        #if the user presses the escape key, return EscapeAction, which will exit the game. This will exit menus in the future
        elif key == tcod.event.K_ESCAPE:
            raise SystemExit()

        elif key == tcod.event.K_v:
            return HistoryViewer(self.engine)

        elif key == tcod.event.K_e:
            action = PickupAction(player)

        elif key == tcod.event.K_i:
            return InventoryActivateHandler(self.engine)
        elif key == tcod.event.K_d:
            return InventoryDropHandler(self.engine)
        elif key == tcod.event.K_SLASH:
            return LookHandler(self.engine)
        elif key == tcod.event.K_c:
            return CharacterScreenEventHandler(self.engine)

        #Whether a valid key is pressed or not, we return the value of action, which is none by default.
        return action
コード例 #10
0
    def ev_keydown(self,
                   event: tcod.event.KeyDown) -> Optional[ActionOrHandler]:
        action: Optional[Action] = None

        key = event.sym
        modifier = event.mod

        player = self.engine.player

        if key == tcod.event.K_PERIOD and modifier & (tcod.event.KMOD_LSHIFT |
                                                      tcod.event.KMOD_RSHIFT):
            if self.engine.game_world.current_floor == 8:
                return WinEventHandler(self.engine)
            else:
                return actions.TakeStairsAction(player)

        if key in MOVE_KEYS:
            dx, dy = MOVE_KEYS[key]
            action = BumpAction(player, dx, dy)
        elif key in WAIT_KEYS:
            action = WaitAction(player)
        elif key == tcod.event.K_ESCAPE:
            raise SystemExit()
        elif key == tcod.event.K_v:
            return HistoryViewer(self.engine)
        elif key == tcod.event.K_g:
            action = PickupAction(player)
        elif key == tcod.event.K_i:
            return InventoryActivateHandler(self.engine)
        elif key == tcod.event.K_d:
            return InventoryDropHandler(self.engine)
        elif key == tcod.event.K_c:
            return CharacterScreenEventHandler(self.engine)
        elif key == tcod.event.K_q:
            return ControlsScreenEventHandler(self.engine)

        # No valid key was pressed
        return action
コード例 #11
0
    def do_player_queue_actions(self) -> None:
        """
        The game will automatically do an action for the player based on the player_path, player_dir information.
        """
        ### A. If player path exist (=if the player clicked a tile that is at least 2 tiles away)
        if self.player_path:

            # Check if there is any new actor spotted in sight
            if self.prev_actors_in_sight != self.actors_in_sight:
                # If so, stop the movement.
                # TODO: Add an option to not stop?
                # TODO: Add a feature to stop ONLY if the actor is hostile to player?
                self.update_entity_in_sight(
                )  # update actors only when it's necessary
                self.player_path = deque([])
                return False

            dest_xy = self.player_path[-1]
            dx = dest_xy[0] - self.player.x
            dy = dest_xy[1] - self.player.y

            # Check for semiactors
            collided_semiactor = self.game_map.get_semiactor_at_location(
                dest_xy[0], dest_xy[1])
            if collided_semiactor:
                if collided_semiactor.entity_id == "closed_door":
                    # We do not pop from the path, because the player has to first open the door and then move to the tile.
                    BumpAction(self.player, dx, dy).perform()
                    return True
            if self.game_map.get_actor_at_location(dest_xy[0], dest_xy[1]):
                # If the monster is in the way, the game automatically attacks it.
                # After the attack the game will delete the player's path.
                BumpAction(self.player, dx, dy).perform()
                self.player_path = deque([])
                return True
            else:
                # If something unexpectedly blocks the way, perform BumpAction and delete all paths.
                #NOTE: This part of the code is meant to prevent unexpected circumstances crashing the game.
                # So be aware that issues can be ignored here.
                try:
                    BumpAction(self.player, dx, dy).perform()
                    self.player_path.pop()
                    return True
                except Exception as e:
                    print(f"DEBUG::{e}")
                    self.player_path = deque([])
                    return False

        ### B. If the player clicked one of nearby tiles (including the tile player is at)
        elif self.player_dir:

            # B-1. Clicked the tile that player is currently at
            if self.player_dir == (0, 0):
                # Check for descending stairs
                if self.game_map.tiles[
                        self.player.x,
                        self.player.y]["tile_id"] == "descending_stair":
                    try:
                        DescendAction(entity=self.player).perform()
                        self.player_dir = None
                        return True
                    except exceptions.Impossible as exc:
                        self.message_log.add_message(exc.args[0],
                                                     color.impossible)
                        self.player_dir = None
                        return False
                    except:
                        traceback.print_exc()
                        self.message_log.add_message(traceback.format_exc(),
                                                     color.error)
                        self.player_dir = None
                        return False
                # Check for ascending stairs
                elif self.game_map.tiles[
                        self.player.x,
                        self.player.y]["tile_id"] == "ascending_stair":
                    try:
                        AscendAction(entity=self.player).perform()
                        self.player_dir = None
                        return True
                    except exceptions.Impossible as exc:
                        self.message_log.add_message(exc.args[0],
                                                     color.impossible)
                        self.player_dir = None
                        return False
                    except:
                        traceback.print_exc()
                        self.message_log.add_message(traceback.format_exc(),
                                                     color.error)
                        self.player_dir = None
                        return False
                # If there is no stair, check for items.
                # TODO: What if the item is dropped on the stair tile?
                else:
                    try:
                        PickupAction(entity=self.player).perform()
                        self.player_dir = None
                        return True
                    except exceptions.Impossible as exc:
                        self.message_log.add_message(exc.args[0],
                                                     color.impossible)
                        self.player_dir = None
                        return False
                    except:
                        traceback.print_exc()
                        self.message_log.add_message(traceback.format_exc(),
                                                     color.error)
                        self.player_dir = None
                        return False

            # B-2. Clicked one of the nearby tile
            else:
                try:
                    BumpAction(self.player, self.player_dir[0],
                               self.player_dir[1]).perform()
                    self.player_dir = None
                    return True
                except exceptions.Impossible as exc:
                    self.message_log.add_message(exc.args[0], color.impossible)
                    self.player_dir = None
                    return False
                except:
                    traceback.print_exc()
                    self.message_log.add_message(traceback.format_exc(),
                                                 color.error)
                    self.player_dir = None
                    return False

        return True  # Turn passes after every actions like normal