1 # These will probably need to go away when we have images
4 from naja.constants import TILE_SIZE, BITS, LOCK_HEIGHT, EXAMINE, ACTION_GLYPHS
5 from naja.resources import resources
6 from naja.resources.mutators import EIGHT_BIT
7 from naja.widgets.base import Widget
11 frozenset([BITS.RED]): 'board/tile_red.png',
12 frozenset([BITS.GREEN]): 'board/tile_green.png',
13 frozenset([BITS.BLUE]): 'board/tile_blue.png',
14 frozenset([BITS.RED, BITS.GREEN]): 'board/tile_red_green.png',
15 frozenset([BITS.RED, BITS.BLUE]): 'board/tile_red_blue.png',
16 frozenset([BITS.GREEN, BITS.BLUE]): 'board/tile_green_blue.png',
17 frozenset([BITS.RED, BITS.GREEN, BITS.BLUE]):
18 'board/tile_red_green_blue.png',
23 ACTION_GLYPHS.CLEAR_BITS: 'glyphs/bits_clear.png',
24 ACTION_GLYPHS.TOGGLE_BITS: 'glyphs/bits_toggle.png',
25 ACTION_GLYPHS.SET_BITS: 'glyphs/bits_set.png',
26 ACTION_GLYPHS.CHANGE_BOARD: 'glyphs/board.png',
27 ACTION_GLYPHS.DAMAGE: 'glyphs/damage.png',
28 ACTION_GLYPHS.HEAL: 'glyphs/health.png',
29 ACTION_GLYPHS.MOVEMENT: 'glyphs/move.png',
30 ACTION_GLYPHS.WINTOKEN: 'glyphs/win.png',
31 ACTION_GLYPHS.MSB: 'glyphs/msb.png',
32 ACTION_GLYPHS.NOTHING: 'glyphs/do_nothing.png',
36 class TileWidget(Widget):
37 """Widget which holds a tile on the game board."""
38 def __init__(self, pos, state=None, board_pos=None):
39 super(TileWidget, self).__init__(pos, TILE_SIZE)
41 self.board_pos = board_pos
42 self.highlighted = False
46 x, y = abs(self.board_pos[0] - 2), abs(self.board_pos[1] - 2)
49 bg = resources.get_image('board/tile_2.png',
50 transforms=(EIGHT_BIT,))
52 bg = resources.get_image('board/tile_1.png',
53 transforms=(EIGHT_BIT,))
56 legal_move = (self.board_pos in self.state.player.legal_moves())
58 if self.state.gameboard.player_mode == EXAMINE and legal_move:
59 overlays.append(resources.get_image('board/tile_available.png',
60 transforms=(EIGHT_BIT,)))
62 overlays.append(resources.get_image('board/tile_selected.png',
63 transforms=(EIGHT_BIT,)))
65 self.surface = pygame.surface.Surface(TILE_SIZE)
66 self.surface.blit(bg, (0, 0))
67 for overlay in overlays:
68 self.surface.blit(overlay, (0, 0))
69 # Look up the required bits on the board location
70 card = self.state.board_locations[self.board_pos]
71 y_offset = TILE_SIZE[1] - LOCK_HEIGHT * len(card.actions)
72 for action in card.actions:
73 y_offset = self._prepare_action(action, y_offset)
75 def _prepare_lock(self, action, y_offset):
76 if not action.required_bits:
79 img_name = BIT_MAP[action.required_bits]
80 if self.board_pos != self.state.player.position:
83 img_name = img_name.replace('.png', '_small.png')
85 if y_offset >= TILE_SIZE[1] // 2:
87 # Bump the lock down by some hand-tuned factor
88 # to not overlap with the robot
89 y_offset += LOCK_HEIGHT // 2 - 4
90 img = resources.get_image(img_name, transforms=(EIGHT_BIT,))
91 self.surface.blit(img, (x_offset, y_offset))
92 return x_offset + img.get_width() + 2
94 def _prepare_action(self, action, y_offset):
95 x_offset = self._prepare_lock(action, y_offset)
96 if self.board_pos != self.state.player.position:
97 for glyph in action.GLYPHS:
98 img = resources.get_image(
99 GLYPH_MAP[glyph], transforms=(EIGHT_BIT,))
100 self.surface.blit(img, (x_offset, y_offset + 4))
101 x_offset += img.get_width()
102 return y_offset + LOCK_HEIGHT
104 def set_highlight(self, pos):
105 self.highlighted = False
106 if (self.state.gameboard.player_mode == EXAMINE and
107 self.board_pos == pos):
108 self.highlighted = True
110 def draw(self, surface):
111 surface.blit(self.surface, self.pos)