2 Widget for the game board information area.
5 import pygame.locals as pgl
7 from naja.constants import (INFO_SIZE, EIGHT_BIT_SCALE, ACT, KEYS,
9 from naja.events import finish_event
10 from naja.resources import resources
11 from naja.resources.mutators import EIGHT_BIT
12 from naja.sound import sound
13 from naja.utils import bit_glyphs
15 from naja.widgets.base import Widget
16 from naja.widgets.tile import BIT_MAP
17 from naja.widgets.text import TextBoxWidget, TextWidget
21 ACT: ("Choose an action using the Up/Down keys.\n"
22 "Press Return to execute the action."),
23 EXAMINE: "Select a card to examine using the arrow keys.",
26 HINT_LEGAL_MOVE = "\nPress Return to move to this card."
29 ACT: "Choose an Action",
30 EXAMINE: "Select a Card",
34 class InfoAreaWidget(Widget):
36 Widget for the game board information area.
38 def __init__(self, pos, state):
39 super(InfoAreaWidget, self).__init__(pos, INFO_SIZE)
42 self.card_position = state.player.position
43 self.set_position(state.player.position)
46 if self.state.gameboard.player_mode == ACT:
47 self.set_position(self.state.player.position)
48 self.surface = pygame.surface.Surface(INFO_SIZE)
49 self.surface.fill((0, 0, 0))
50 # Extract actions and such from the card
51 title = TextWidget((0, 0), TITLES[self.state.gameboard.player_mode],
53 title.render(self.surface)
54 y_offset = title.surface.get_rect().height + 8
56 # TODO: Make this better.
57 bits_text = ''.join('1' if bit in self.card.bitwise_operand else '0'
58 for bit in reversed(range(8)))
59 if self.card.bitwise_operand:
60 bits_text = '%s %s' % (
61 bits_text, bit_glyphs(self.card.bitwise_operand))
62 card_bits = TextBoxWidget((0, y_offset), bits_text,
63 box_width=INFO_SIZE[0],
64 colour=PALETTE.LIGHT_TURQUOISE,
65 bg_colour=PALETTE.BLACK)
66 card_bits.render(self.surface)
67 y_offset += card_bits.surface.get_rect().height + 8
69 for choice, action in enumerate(self.card.actions):
70 y_offset = self.prepare_action(choice, action, y_offset)
71 # We cheat horribly for layout reasons
72 hint_text = HINTS[self.state.gameboard.player_mode]
73 if self.state.gameboard.player_mode == EXAMINE:
74 if self.card_position in self.state.player.legal_moves():
75 hint_text += HINT_LEGAL_MOVE
77 hint = TextBoxWidget((4, 0), hint_text, padding=2,
78 box_width=(INFO_SIZE[0] - 4) // EIGHT_BIT_SCALE)
80 y_offset = INFO_SIZE[1] - hint.surface.get_rect().height
81 self.surface.blit(hint.surface, (4, y_offset))
83 def prepare_action(self, choice, action, y_offset):
85 action_viable = action.check_available(self.state.player)
86 text_colour = PALETTE.BLACK if action_viable else PALETTE.GREY
89 (x_offset, y_offset), action.get_text(self.card),
90 box_width=(INFO_SIZE[0] - 16) // EIGHT_BIT_SCALE,
91 fontsize=28, colour=text_colour)
92 text.render(self.surface)
95 if choice == self.chosen:
96 border_colour = PALETTE.GREEN if action_viable else PALETTE.ORANGE
98 bottom = y_offset + text.surface.get_rect().height
99 right = text.surface.get_rect().width + x_offset
100 pygame.draw.lines(self.surface, border_colour, True,
101 [(x_offset, y_offset), (right, y_offset),
102 (right, bottom), (x_offset, bottom)], 4)
103 if action.required_bits:
104 img_name = BIT_MAP[action.required_bits].replace(
105 '.png', '_small.png')
106 img = resources.get_image(img_name,
107 transforms=(EIGHT_BIT,))
108 self.surface.blit(img, (0, y_offset))
109 return y_offset + text.surface.get_rect().height + 16
111 def set_position(self, position):
112 self.card_position = position
113 self.card = self.state.board_locations[self.card_position]
114 if self.state.gameboard.player_mode == ACT:
115 if self.chosen is None:
120 def draw(self, surface):
121 surface.blit(self.surface, self.pos)
123 def next_action(self, viable_only=False, step=1):
124 num_actions = len(self.card.actions)
127 player = self.state.player
129 for i in range(num_actions - 1):
130 # loop through each action at most once.
131 chosen = (chosen + step) % num_actions
132 action = self.card.actions[chosen]
133 if not viable_only or action.check_available(player):
134 sound.play_sound('zoop.ogg', volume=0.05)
137 def prev_action(self, viable_only=False):
138 return self.next_action(viable_only=viable_only, step=-1)
140 def try_perform_action(self):
141 player = self.state.player
142 action = self.card.actions[self.chosen]
143 if not action.check_available(player):
144 sound.play_sound('error.ogg')
146 sound.play_sound('chirp.ogg', volume=0.5)
147 action.perform_action(self.state.gameboard, self.card)
148 self.state.gameboard.replace_card(player.position)
149 self.state.gameboard.change_mode(EXAMINE)
150 self.set_position(player.position)
152 def handle_event(self, ev):
153 if self.state.gameboard.player_mode == EXAMINE:
154 return super(InfoAreaWidget, self).handle_event(ev)
155 if ev.type == pgl.KEYDOWN:
156 if ev.key in KEYS.SELECT:
157 self.try_perform_action()
158 return finish_event()
159 if ev.key in KEYS.UP:
161 return finish_event()
162 if ev.key in KEYS.DOWN:
164 return finish_event()
165 if ev.key in KEYS.SWITCH:
166 self.next_action(viable_only=True)
167 return finish_event()
168 return super(InfoAreaWidget, self).handle_event(ev)