Beispiel #1
0
 def get_board_position_from_pixel(
         self, position: tuple) -> board_position.BoardPosition:
     column = (position[0] - self.tile_margin) // (self.tile_size +
                                                   self.tile_margin)
     row = (position[1] - self.tile_margin) // (self.tile_size +
                                                self.tile_margin)
     return board_position.BoardPosition(column, row)
Beispiel #2
0
    def add_to_board(
            self, token: token.Token,
            position: Union[tuple,
                            board_position.BoardPosition]) -> token.Token:
        """Adds token to board

        Args:
            token: The token which should be added
            position: The position the token should be added

        Returns:
            The Token

        """
        self.tokens.add(token)
        if type(position) == tuple:
            token.position = board_position.BoardPosition(
                position[0], position[1])
        elif type(position) == board_position.BoardPosition:
            token.position = position
        else:
            raise AttributeError("Position has wrong type" +
                                 str(type(position)))
        if not self.is_on_board(token.position.to_rect()):
            return None
        if token.speed == 0:
            token.speed = self.default_actor_speed
        token.add_to_board(self, position)
        self._max_diameter = max(
            hypot(*token.rect.size) for token in self.tokens)
        self.window.send_event_to_containers("Added token", token)
        return token
Beispiel #3
0
 def position(self, value: Union[board_position.BoardPosition, tuple]):
     self.last_position = self.position
     if type(value) == tuple:
         value = board_position.BoardPosition(value[0], value[1])
     self._position = value
     self.dirty = 1
     if self.board:
         self.board.window.send_event_to_containers("actor_moved", self)
Beispiel #4
0
 def get_color_at_board_position(
         self, position: Union[tuple,
                               board_position.BoardPosition]) -> list:
     if type(position == tuple):
         position = board_position.BoardPosition(position[1], position[0])
     self.dirty = 1
     return self.background.color_at(
         self.get_pixel_from_board_position(pos=position))
Beispiel #5
0
 def is_on_board(
         self, area: Union[tuple, board_position.BoardPosition,
                           pygame.Rect]) -> bool:
     if type(area) == tuple:
         area = board_position.BoardPosition(area[0], area[1])
     if type(area) == board_position.BoardPosition:
         area = area.to_rect()
     position = self.get_board_position_from_pixel(area.center)
     if position.x > self.columns - 1:
         return False
     elif position.y > self.rows - 1:
         return False
     elif position.x < 0 or position.y < 0:
         return False
     else:
         return True
Beispiel #6
0
    def is_on_board(
            self, area: Union[board_position.BoardPosition,
                              pygame.Rect]) -> bool:
        if type(area) == tuple:
            area = board_position.BoardPosition(area[0], area[1])
        if type(area) == board_position.BoardPosition:
            area = area.to_rect()

        top_left_x, top_left_y, right, top = area.topleft[0], \
                                             area.topleft[1], \
                                             area.right, \
                                             area.top
        if top_left_x < 0 or top_left_y < 0 or area.right >= self.width or area.bottom >= self.height:
            return False
        else:
            return True
Beispiel #7
0
 def borders(
     self, value: Union[tuple, board_position.BoardPosition,
                        pygame.Rect]) -> list:
     borders = []
     if type(value) == tuple:
         value = board_position.BoardPosition(value[0], value[1])
     if type(value) == board_position.BoardPosition:
         value = value.to_rect()
     position = self.get_board_position_from_pixel(value.center)
     if position.x == self.columns - 1:
         borders.append("right")
     if position.y == self.rows - 1:
         borders.append("bottom")
     if position.x == 0:
         borders.append("right")
     if position.y == 0:
         borders.append("top")
     return borders
Beispiel #8
0
 def get_tokens_in_area(self,
                        area: Union[pygame.Rect, tuple],
                        token_type=None,
                        exclude=None) -> list:
     self._dynamic_actors_dict.clear()
     self._update_token_positions()
     if type(area) == tuple:
         position = board_position.BoardPosition(area[0], area[1])
     else:
         position = self.get_board_position_from_pixel(area.topleft)
     tokens_in_area = []
     if self.is_on_board(self.rect):
         if self._dynamic_actors_dict[position.x, position.y]:
             tokens_in_area.extend(self._dynamic_actors_dict[(position.x,
                                                              position.y)])
         if self._static_tokens_dict[position.x, position.y]:
             tokens_in_area.extend(self._static_tokens_dict[(position.x,
                                                             position.y)])
     if token_type is not None:
         tokens_in_area = self.filter_actor_list(tokens_in_area, token_type)
     return tokens_in_area
Beispiel #9
0
    def look(
        self,
        distance: int = -1,
    ) -> pygame.Rect:
        """Looks *distance* steps into a *direction*.

        Args:
            direction: The direction in degrees (int) or a direction as string
            distance: Number of steps to look

        Returns:
            A destination Rectangle
        """
        direction = self.direction
        if distance < 0:
            distance = self.speed
        x = self.position[0] + round(
            math.cos(math.radians(direction)) * distance)
        y = self.position[1] - round(
            math.sin(math.radians(direction)) * distance)
        return board_position.BoardPosition(x, y).to_rect(rect=self.rect)