fixed lock repulsion
[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, ACTION_GLYPHS, 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 GLYPH_MAP = {
24     ACTION_GLYPHS.CLEAR_BITS: 'glyphs/bits_clear.png',
25     ACTION_GLYPHS.TOGGLE_BITS: 'glyphs/bits_toggle.png',
26     ACTION_GLYPHS.SET_BITS: 'glyphs/bits_set.png',
27     ACTION_GLYPHS.CHANGE_BOARD: 'glyphs/board.png',
28     ACTION_GLYPHS.DAMAGE: 'glyphs/damage.png',
29     ACTION_GLYPHS.HEAL: 'glyphs/health.png',
30     ACTION_GLYPHS.MOVEMENT: 'glyphs/move.png',
31     ACTION_GLYPHS.WINTOKEN: 'glyphs/win.png',
32     ACTION_GLYPHS.MSB: 'glyphs/msb.png',
33     ACTION_GLYPHS.NOTHING: 'glyphs/do_nothing.png',
34 }
35
36
37 class TileWidget(Widget):
38     """Widget which holds a tile on the game board."""
39     def __init__(self, pos, state=None, board_pos=None):
40         super(TileWidget, self).__init__(pos, TILE_SIZE)
41         self.state = state
42         self.board_pos = board_pos
43         self.highlighted = False
44
45     def prepare(self):
46         # Draw background
47         x, y = abs(self.board_pos[0] - 2), abs(self.board_pos[1] - 2)
48
49         if (x + y) % 2 == 0:
50             bg = resources.get_image('board/tile_2.png',
51                                      transforms=(EIGHT_BIT,))
52         else:
53             bg = resources.get_image('board/tile_1.png',
54                                      transforms=(EIGHT_BIT,))
55         overlays = []
56
57         legal_move = (self.board_pos in self.state.player.legal_moves())
58
59         if self.state.gameboard.player_mode == EXAMINE and legal_move:
60             overlays.append(resources.get_image('board/tile_available.png',
61                                      transforms=(EIGHT_BIT,)))
62         if self.highlighted:
63             overlays.append(resources.get_image('board/tile_selected.png',
64                                           transforms=(EIGHT_BIT,)))
65
66         self.surface = pygame.surface.Surface(TILE_SIZE)
67         self.surface.blit(bg, (0, 0))
68         for overlay in overlays:
69             self.surface.blit(overlay, (0, 0))
70         # Look up the required bits on the board location
71         card = self.state.board_locations[self.board_pos]
72         y_offset = TILE_SIZE[1] - LOCK_HEIGHT * len(card.actions)
73         for action in card.actions:
74             y_offset = self._prepare_action(action, y_offset)
75
76     def _prepare_lock(self, action, y_offset):
77         if not action.required_bits:
78             return 4
79
80         img_name = BIT_MAP[action.required_bits]
81
82         if self.board_pos != self.state.player.position:
83             x_offset = 0
84         else:
85             img_name = img_name.replace('.png', '_small.png')
86             x_offset = 2
87             if y_offset == LOCK_HEIGHT:
88                 y_offset = 0  # middle -> top
89             elif y_offset == 2 * LOCK_HEIGHT:
90                 y_offset += LOCK_HEIGHT - SMALL_LOCK_HEIGHT - 2 # bottom -> further down
91
92         img = resources.get_image(img_name, transforms=(EIGHT_BIT,))
93         self.surface.blit(img, (x_offset, y_offset))
94         return x_offset + img.get_width() + 2
95
96     def _prepare_action(self, action, y_offset):
97         x_offset = self._prepare_lock(action, y_offset)
98         if self.board_pos != self.state.player.position:
99             for glyph in action.GLYPHS:
100                 img = resources.get_image(
101                     GLYPH_MAP[glyph], transforms=(EIGHT_BIT,))
102                 self.surface.blit(img, (x_offset, y_offset + 4))
103                 x_offset += img.get_width()
104             if action.MSB_GLYPH is not None:
105                 img = resources.get_image(
106                     GLYPH_MAP[action.MSB_GLYPH],
107                     transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
108                 self.surface.blit(img, (x_offset, y_offset + 4))
109                 x_offset += img.get_width()
110         return y_offset + LOCK_HEIGHT
111
112     def set_highlight(self, pos):
113         self.highlighted = False
114         if (self.state.gameboard.player_mode == EXAMINE and
115                 self.board_pos == pos):
116             self.highlighted = True
117
118     def draw(self, surface):
119         surface.blit(self.surface, self.pos)