b478584f4330aac871503443d40cc9d779de8b53
[naja.git] / naja / widgets / tile.py
1 # These will probably need to go away when we have images
2 import pygame
3 import pygame.locals as pgl
4
5 from naja.constants import TILE_SIZE, BITS, LOCK_HEIGHT
6 from naja.resources import resources
7 from naja.resources.mutators import EIGHT_BIT
8 from naja.widgets.base import Widget
9
10
11 BIT_MAP = {
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',
19     }
20
21
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)
26         self.state = state
27         self.board_pos = board_pos
28
29     def prepare(self):
30         # Draw background
31         x, y = abs(self.board_pos[0] - 2), abs(self.board_pos[1] - 2)
32         if (x == 1 and y in [0, 1]) or (y == 1 and x in [0, 1]):
33             # Inner ring
34             bg = resources.get_image('board/tile_2.png',
35                                      transforms=(EIGHT_BIT,))
36         else:
37             bg = resources.get_image('board/tile_1.png',
38                                      transforms=(EIGHT_BIT,))
39         self.surface = pygame.surface.Surface(TILE_SIZE)
40         self.surface.blit(bg, (0, 0))
41         # Look up the required bits on the board location
42         card = self.state.board_locations[self.board_pos]
43         player_pos = self.state.player.position
44         bits = []
45         for action in card.actions:
46             if action.required_bits:
47                 bits.append(action.required_bits)
48         # Sort so we have longer lists of bits later
49         self.size = self.surface.get_rect().size
50         if bits:
51             bits.sort(key=lambda x: len(x))
52             y_offset = TILE_SIZE[1] - LOCK_HEIGHT * len(bits)
53             for pattern in bits:
54                 if self.board_pos != player_pos:
55                     img_name = BIT_MAP[pattern]
56                 else:
57                     img_name = BIT_MAP[pattern].replace('.png', '_small.png')
58                     if y_offset >= TILE_SIZE[1] // 2:
59                         # FIXME: Hack'ish
60                         # Bump the lock down by some hand-tuned factor
61                         # to not overlap with the robot
62                         y_offset += LOCK_HEIGHT // 2 - 6
63                 img = resources.get_image(img_name,
64                                           transforms=(EIGHT_BIT,))
65                 self.surface.blit(img, (5, y_offset))
66                 y_offset += LOCK_HEIGHT
67
68     def draw(self, surface):
69         surface.blit(self.surface, self.pos)