Optional sanity check for actions.
[naja.git] / naja / widgets / tile.py
1 # These will probably need to go away when we have images
2 import pygame
3
4 from naja.constants import (
5     TILE_SIZE, BITS, LOCK_HEIGHT, SMALL_LOCK_HEIGHT, EXAMINE, PALETTE)
6 from naja.resources import resources
7 from naja.resources.mutators import EIGHT_BIT, blender
8 from naja.widgets.base import Widget
9
10
11 BIT_MAP = {
12     frozenset([BITS.RED]): 'board/tile_red.png',
13     frozenset([BITS.GREEN]): 'board/tile_green.png',
14     frozenset([BITS.BLUE]): 'board/tile_blue.png',
15     frozenset([BITS.RED, BITS.GREEN]): 'board/tile_red_green.png',
16     frozenset([BITS.RED, BITS.BLUE]): 'board/tile_red_blue.png',
17     frozenset([BITS.GREEN, BITS.BLUE]): 'board/tile_green_blue.png',
18     frozenset([BITS.RED, BITS.GREEN, BITS.BLUE]):
19         'board/tile_red_green_blue.png',
20 }
21
22
23 class TileWidget(Widget):
24     """Widget which holds a tile on the game board."""
25     def __init__(self, pos, state=None, board_pos=None):
26         super(TileWidget, self).__init__(pos, TILE_SIZE)
27         self.state = state
28         self.current_card = None
29         self.board_pos = board_pos
30         self.highlighted = False
31         self.animation = TILE_SIZE[0]
32
33     def prepare(self):
34         # Draw background
35         x, y = abs(self.board_pos[0] - 2), abs(self.board_pos[1] - 2)
36
37         if self.state.gameboard.puzzle:
38             tile_1_name = 'board/tile_1.png'
39             tile_2_name = 'board/tile_2_puzzle.png'
40             tile_available_name = 'board/tile_available_puzzle.png'
41         else:
42             tile_1_name = 'board/tile_1.png'
43             tile_2_name = 'board/tile_2.png'
44             tile_available_name = 'board/tile_available.png'
45
46         if (x + y) % 2 == 0:
47             bg_name = tile_2_name
48         else:
49             bg_name = tile_1_name
50         bg = resources.get_image(bg_name, transforms=(EIGHT_BIT,))
51         overlays = []
52
53         legal_move = (self.board_pos in self.state.player.legal_moves())
54
55         if self.state.gameboard.player_mode == EXAMINE and legal_move:
56             overlays.append(resources.get_image(
57                 tile_available_name, transforms=(EIGHT_BIT,)))
58         if self.highlighted:
59             overlays.append(resources.get_image(
60                 'board/tile_selected.png',
61                 transforms=(EIGHT_BIT,)))
62
63         self.surface = pygame.surface.Surface(TILE_SIZE)
64         self.surface.blit(bg, (0, 0))
65         for overlay in overlays:
66             self.surface.blit(overlay, (0, 0))
67         # Look up the required bits on the board location
68         card = self.state.board_locations[self.board_pos]
69         if card is not self.current_card:
70             self.animation = TILE_SIZE[0]
71             self.current_card = card
72
73         y_offset = TILE_SIZE[1] - LOCK_HEIGHT * len(card.actions)
74         for action in card.actions:
75             y_offset = self._prepare_action(action, y_offset)
76
77         self._prepare_countdown(card)
78
79     def _prepare_countdown(self, card):
80         if card.replacement_time is None:
81             return
82         elif card.replacement_time <= 1:
83             glyph = 'glyphs/countdown_1.png'
84         elif card.replacement_time == 2:
85             glyph = 'glyphs/countdown_2.png'
86         elif card.replacement_time == 3:
87             glyph = 'glyphs/countdown_3.png'
88         elif card.replacement_time < 8:
89             glyph = 'glyphs/countdown_4.png'
90         else:
91             glyph = 'glyphs/countdown_5.png'
92         img = resources.get_image(
93             glyph, transforms=(EIGHT_BIT, blender(PALETTE.DARK_VIOLET)))
94         self.surface.blit(img, (TILE_SIZE[0] - 20, 0))
95
96     def _prepare_lock(self, action, y_offset):
97         required_keys = action.required_bits & frozenset([
98             BITS.RED, BITS.GREEN, BITS.BLUE])
99         if required_keys not in BIT_MAP:
100             return 4
101
102         img_name = BIT_MAP[required_keys]
103
104         if self.board_pos != self.state.player.position:
105             x_offset = 0
106         else:
107             img_name = img_name.replace('.png', '_small.png')
108             x_offset = 2
109             if y_offset == LOCK_HEIGHT:
110                 y_offset = 0  # middle -> top
111             elif y_offset == 2 * LOCK_HEIGHT:
112                 # bottom -> further down
113                 y_offset += LOCK_HEIGHT - SMALL_LOCK_HEIGHT - 2
114
115         img = resources.get_image(img_name, transforms=(EIGHT_BIT,))
116         img_rect = img.get_rect()
117         self.surface.blit(img, (x_offset, y_offset))
118
119         if BITS.MSB in action.required_bits:
120             msb = resources.get_image('board/msb_lock_decoration.png',
121                                       transforms=(EIGHT_BIT,))
122             msb_rect = msb.get_rect()
123             self.surface.blit(
124                 msb, (x_offset + img_rect.width - msb_rect.width, y_offset)
125             )
126
127         return x_offset + img_rect.width + 2
128
129     def _prepare_action(self, action, y_offset):
130         x_offset = self._prepare_lock(action, y_offset)
131         if self.board_pos != self.state.player.position:
132             for glyph in action.get_glyphs():
133                 img = resources.get_image(glyph, transforms=(EIGHT_BIT,))
134                 self.surface.blit(img, (x_offset, y_offset + 4))
135                 x_offset += img.get_width()
136             if action.get_msb_glyph() is not None:
137                 img = resources.get_image(
138                     action.get_msb_glyph(),
139                     transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
140                 self.surface.blit(img, (x_offset, y_offset + 4))
141                 x_offset += img.get_width()
142         return y_offset + LOCK_HEIGHT
143
144     def set_highlight(self, pos):
145         self.highlighted = False
146         if (self.state.gameboard.player_mode == EXAMINE and
147                 self.board_pos == pos):
148             self.highlighted = True
149
150     def draw(self, surface):
151         scaled_width = self.surface.get_width() - self.animation
152         scaled_height = self.surface.get_height() - self.animation
153         scaled_position = (self.pos[0] + (self.animation / 2),
154                            self.pos[1] + (self.animation / 2))
155         if self.animation > 0:
156             self.animation = max(0, self.animation - 4)
157         scaled_surface = pygame.transform.scale(
158             self.surface, (scaled_width, scaled_height))
159         surface.blit(scaled_surface, scaled_position)