1 # These will probably need to go away when we have images
3 import pygame.locals as pgl
5 from naja.constants import TILE_SIZE, BITS, LOCK_HEIGHT, MOVE, EXAMINE
6 from naja.resources import resources
7 from naja.resources.mutators import EIGHT_BIT
8 from naja.widgets.base import Widget
12 frozenset([BITS.CYAN]): 'board/tile_cyan.png',
13 frozenset([BITS.MAGENTA]): 'board/tile_magenta.png',
14 frozenset([BITS.YELLOW]): 'board/tile_yellow.png',
15 frozenset([BITS.CYAN, BITS.MAGENTA]): 'board/tile_cyan_magenta.png',
16 frozenset([BITS.CYAN, BITS.YELLOW]): 'board/tile_cyan_yellow.png',
17 frozenset([BITS.MAGENTA, BITS.YELLOW]): 'board/tile_magenta_yellow.png',
18 frozenset([BITS.CYAN, BITS.MAGENTA, BITS.YELLOW]): 'board/tile_cyan_magenta_yellow.png',
22 class TileWidget(Widget):
23 """Widget which holds a tile on the game board."""
24 def __init__(self, pos, state=None, board_pos=None):
25 super(TileWidget, self).__init__(pos, TILE_SIZE)
27 self.board_pos = board_pos
28 self.highlighted = False
32 x, y = abs(self.board_pos[0] - 2), abs(self.board_pos[1] - 2)
33 legal_move = (self.board_pos in self.state.player.legal_moves())
34 if self.state.gameboard.player_mode in (MOVE, EXAMINE) and legal_move:
35 bg = resources.get_image('board/tile_available.png',
36 transforms=(EIGHT_BIT,))
37 elif (x + y) % 2 == 0:
38 bg = resources.get_image('board/tile_2.png',
39 transforms=(EIGHT_BIT,))
41 bg = resources.get_image('board/tile_1.png',
42 transforms=(EIGHT_BIT,))
44 bg = resources.get_image('board/tile_selected.png',
45 transforms=(EIGHT_BIT,))
46 self.surface = pygame.surface.Surface(TILE_SIZE)
47 self.surface.blit(bg, (0, 0))
48 # Look up the required bits on the board location
49 card = self.state.board_locations[self.board_pos]
50 player_pos = self.state.player.position
52 for action in card.actions:
53 if action.required_bits:
54 bits.append(action.required_bits)
55 # Sort so we have longer lists of bits later
56 self.size = self.surface.get_rect().size
58 bits.sort(key=lambda x: len(x))
59 y_offset = TILE_SIZE[1] - LOCK_HEIGHT * len(bits)
61 if self.board_pos != player_pos:
62 img_name = BIT_MAP[pattern]
65 img_name = BIT_MAP[pattern].replace('.png', '_small.png')
67 if y_offset >= TILE_SIZE[1] // 2:
69 # Bump the lock down by some hand-tuned factor
70 # to not overlap with the robot
71 y_offset += LOCK_HEIGHT // 2 - 4
72 img = resources.get_image(img_name,
73 transforms=(EIGHT_BIT,))
74 self.surface.blit(img, (x_offset, y_offset))
75 y_offset += LOCK_HEIGHT
77 def set_highlight(self, pos):
78 self.highlighted = False
79 if (self.state.gameboard.player_mode == EXAMINE and
80 self.board_pos == pos):
81 self.highlighted = True
83 def draw(self, surface):
84 surface.blit(self.surface, self.pos)