More hardness descriptions.
[naja.git] / naja / widgets / info_area.py
1 """
2 Widget for the game board information area.
3 """
4 import pygame
5 import pygame.locals as pgl
6
7 from naja.constants import (
8     INFO_SIZE, ACT, KEYS, EXAMINE, PALETTE,
9     ACTION_TEXT_OFFSET, INFO_LEFT_PADDING,
10     INFO_RIGHT_PADDING, BIT_SIZE, BITS)
11 from naja.events import finish_event
12 from naja.resources import resources
13 from naja.resources.mutators import EIGHT_BIT, blender
14 from naja.sound import sound
15 from naja.utils import bit_glyphs, Flashlight
16
17 from naja.widgets.base import Widget
18 from naja.widgets.tile import BIT_MAP
19 from naja.widgets.text import TextBoxWidget
20 from naja import constants
21
22
23 HINTS = {
24     ACT: "Choose an action using {NORTH,SOUTH}."
25          " Press {RETURN} to execute it.",
26     EXAMINE: "Browse the tiles with {NORTH,SOUTH,EAST,WEST} keys.",
27 }
28
29 HINT_LEGAL_MOVE = "\nPress {RETURN} to move."
30
31 TITLES = {
32     ACT: "Choose an Action",
33     EXAMINE: "Select a Tile",
34 }
35
36
37 class InfoAreaWidget(Widget):
38     """
39     Widget for the game board information area.
40     """
41     def __init__(self, pos, state):
42         super(InfoAreaWidget, self).__init__(pos, INFO_SIZE)
43         self.state = state
44         self.chosen = None
45         self.card_position = state.player.position
46         self.set_position(state.player.position)
47         self.flash_light = Flashlight(constants.FPS // 2)
48
49     def prepare(self):
50         if self.state.gameboard.player_mode == ACT:
51             self.set_position(self.state.player.position)
52         self.surface = pygame.surface.Surface(INFO_SIZE)
53         self.surface.fill((0, 0, 0))
54
55         box_width = INFO_SIZE[0] - INFO_LEFT_PADDING - INFO_RIGHT_PADDING
56         y_offset = 0
57         pos = lambda: (INFO_LEFT_PADDING, y_offset)
58
59         # Bits
60         y_offset += 12
61         bits_text = ''.join('1' if bit in self.card.bitwise_operand else '0'
62                             for bit in reversed(range(8)))
63         if self.card.bitwise_operand:
64             bits_text = '%s %s' % (
65                 bits_text, bit_glyphs(self.card.bitwise_operand))
66         card_bits = TextBoxWidget(
67             (0, 0), bits_text, padding=4, centre=True,
68             colour=PALETTE.WHITE, border=2,
69             bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
70             box_width=box_width)
71         card_bits.prepare()
72         self.surface.blit(card_bits.surface, pos())
73         y_offset += card_bits.surface.get_rect().height + 12
74
75         # Actions
76         for choice, action in enumerate(self.card.actions):
77             y_offset = self.prepare_action(choice, action, y_offset, box_width)
78
79         # Hint text
80         hint_text = HINTS[self.state.gameboard.player_mode]
81         if self.state.gameboard.player_mode == EXAMINE:
82             if self.card_position in self.state.player.legal_moves():
83                 hint_text += HINT_LEGAL_MOVE
84
85         hint = TextBoxWidget(
86             (0, 0), hint_text, padding=4,
87             colour=PALETTE.WHITE, border=2,
88             bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
89             box_width=box_width)
90         hint.prepare()
91         y_offset = (INFO_SIZE[1] - hint.surface.get_rect().height
92                     - BIT_SIZE[1] - 2)
93         self.surface.blit(hint.surface, pos())
94
95         # Game mode (puzzle, easy, standard, hard, etc ...)
96         game_mode = TextBoxWidget(
97             (0, 0), self.mode_hint(), padding=4, centre=True,
98             colour=PALETTE.WHITE, border=2,
99             bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
100             box_width=box_width)
101         game_mode.prepare()
102
103         y_offset = (INFO_SIZE[1] - BIT_SIZE[1] + 12)
104         self.surface.blit(game_mode.surface, pos())
105
106     def mode_hint(self):
107         gameboard = self.state.gameboard
108         if gameboard.puzzle:
109             return "PUZZLE"
110         return {
111             0: "DEATH", 1: "NINTENDO HARD",
112             2: "VERY HARD", 3: "HARD",
113             4: "STANDARD", 5: "EASY",
114         }.get(gameboard.max_health, "UNKNOWN")
115
116     def prepare_action(self, choice, action, y_offset, box_width):
117         x_offset = INFO_LEFT_PADDING
118         glyphs_x_offset = 2
119         glyphs_y_offset = y_offset
120         y_offset += ACTION_TEXT_OFFSET
121         action_viable = action.check_available(self.state.player)
122         text_colour = PALETTE.BLACK if action_viable else PALETTE.GREY
123
124         text = TextBoxWidget(
125             (x_offset, y_offset), action.get_text(self.card),
126             box_width=box_width,
127             fontsize=28, colour=text_colour,
128             border=2, border_colour=PALETTE.GREY)
129         text.render(self.surface)
130
131         border_colour = None
132         if choice == self.chosen:
133             if self.flash_light.on:
134                 border_colour = (PALETTE.GREEN if action_viable else
135                                  PALETTE.ORANGE)
136             else:
137                 border_colour = (PALETTE.DARK_GREEN if action_viable else
138                                  PALETTE.DARK_RED)
139
140         if border_colour:
141             bottom = y_offset + text.surface.get_rect().height
142             right = text.surface.get_rect().width + x_offset
143             pygame.draw.lines(self.surface, border_colour, True,
144                               [(x_offset, y_offset), (right, y_offset),
145                                (right, bottom), (x_offset, bottom)], 4)
146
147         required_keys = action.required_bits & frozenset([
148             BITS.RED, BITS.GREEN, BITS.BLUE])
149         if required_keys in BIT_MAP:
150             img_name = BIT_MAP[required_keys].replace(
151                 '.png', '_small.png')
152             img = resources.get_image(img_name,
153                                       transforms=(EIGHT_BIT,))
154             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset))
155             glyphs_x_offset += img.get_width() + 4
156         else:
157             glyphs_x_offset = INFO_LEFT_PADDING
158
159         if BITS.MSB in action.required_bits:
160             msb = resources.get_image('board/msb_lock_decoration.png',
161                                       transforms=(EIGHT_BIT,))
162             msb_rect = msb.get_rect()
163             self.surface.blit(
164                 msb, (glyphs_x_offset - msb_rect.width - 4, glyphs_y_offset)
165             )
166
167         for glyph in action.get_glyphs():
168             img = resources.get_image(
169                 glyph, transforms=(EIGHT_BIT, blender(PALETTE.GREY)))
170             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
171             glyphs_x_offset += img.get_width()
172         if action.get_msb_glyph() is not None:
173             img = resources.get_image(
174                 action.get_msb_glyph(),
175                 transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
176             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
177
178         return y_offset + text.surface.get_rect().height + 16
179
180     def set_position(self, position):
181         self.card_position = position
182         self.card = self.state.board_locations[self.card_position]
183         if self.state.gameboard.player_mode == ACT:
184             if self.chosen is None:
185                 self.chosen = 0
186         else:
187             self.chosen = None
188
189     def draw(self, surface):
190         if self.flash_light.tick():
191             self.prepare()
192         surface.blit(self.surface, self.pos)
193
194     def next_action(self, viable_only=False, step=1):
195         num_actions = len(self.card.actions)
196         if num_actions == 0:
197             return
198         player = self.state.player
199         chosen = self.chosen
200         for i in range(num_actions - 1):
201             # loop through each action at most once.
202             chosen = (chosen + step) % num_actions
203             action = self.card.actions[chosen]
204             if not viable_only or action.check_available(player):
205                 sound.play_sound('zoop.ogg', volume=0.05)
206                 self.chosen = chosen
207
208     def prev_action(self, viable_only=False):
209         return self.next_action(viable_only=viable_only, step=-1)
210
211     def try_perform_action(self):
212         player = self.state.player
213         action = self.card.actions[self.chosen]
214         if not action.check_available(player):
215             sound.play_sound('error.ogg')
216         else:
217             sound.play_sound('chirp.ogg', volume=0.5)
218             action.perform_action(self.state.gameboard, self.card)
219             self.state.gameboard.card_used(player.position)
220             self.state.gameboard.change_mode(EXAMINE)
221             self.set_position(player.position)
222
223     def handle_event(self, ev):
224         if self.state.gameboard.player_mode == EXAMINE:
225             return super(InfoAreaWidget, self).handle_event(ev)
226         if ev.type == pgl.KEYDOWN:
227             if ev.key in KEYS.SELECT:
228                 self.try_perform_action()
229                 return finish_event()
230             if ev.key in KEYS.UP:
231                 self.next_action()
232                 return finish_event()
233             if ev.key in KEYS.DOWN:
234                 self.prev_action()
235                 return finish_event()
236             if ev.key in KEYS.SWITCH:
237                 self.next_action(viable_only=True)
238                 return finish_event()
239         return super(InfoAreaWidget, self).handle_event(ev)