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
14 from naja.widgets.base import Widget
15 from naja.widgets.tile import BIT_MAP
16 from naja.widgets.text import TextBoxWidget, TextWidget
20 ACT: ("Choose an action using the Up/Down keys.\n"
21 "Press Return to execute the action."),
22 EXAMINE: "Select a card to examine using the arrow keys.",
25 HINT_LEGAL_MOVE = "\nPress Return to move to this card."
28 ACT: "Choose an Action",
29 EXAMINE: "Select a Card",
33 class InfoAreaWidget(Widget):
35 Widget for the game board information area.
37 def __init__(self, pos, state):
38 super(InfoAreaWidget, self).__init__(pos, INFO_SIZE)
41 self.card_position = state.player.position
42 self.set_position(state.player.position)
45 if self.state.gameboard.player_mode == ACT:
46 self.set_position(self.state.player.position)
47 self.surface = pygame.surface.Surface(INFO_SIZE)
48 self.surface.fill((0, 0, 0))
49 # Extract actions and such from the card
50 title = TextWidget((0, 0), TITLES[self.state.gameboard.player_mode],
51 colour=(255, 255, 255))
52 title.render(self.surface)
53 y_offset = title.surface.get_rect().height + 8
55 # TODO: Make this better.
56 bits_text = ''.join('1' if bit in self.card.bitwise_operand else '0'
57 for bit in reversed(range(8)))
58 card_bits = TextWidget((0, y_offset), bits_text, colour=(255, 255, 0))
59 card_bits.render(self.surface)
60 y_offset += card_bits.surface.get_rect().height + 8
62 for choice, action in enumerate(self.card.actions):
63 y_offset = self.prepare_action(choice, action, y_offset)
64 # We cheat horribly for layout reasons
65 hint_text = HINTS[self.state.gameboard.player_mode]
66 if self.state.gameboard.player_mode == EXAMINE:
67 if self.card_position in self.state.player.legal_moves():
68 hint_text += HINT_LEGAL_MOVE
70 hint = TextBoxWidget((4, 0), hint_text, padding=2,
71 box_width=(INFO_SIZE[0] - 4) // EIGHT_BIT_SCALE)
73 y_offset = INFO_SIZE[1] - hint.surface.get_rect().height
74 self.surface.blit(hint.surface, (4, y_offset))
76 def prepare_action(self, choice, action, y_offset):
78 action_viable = action.check_available(self.state.player)
79 text_colour = PALETTE.BLACK if action_viable else PALETTE.GREY
82 (x_offset, y_offset), action.get_text(),
83 box_width=(INFO_SIZE[0] - 16) // EIGHT_BIT_SCALE,
84 fontsize=28, colour=text_colour)
85 text.render(self.surface)
88 if choice == self.chosen:
89 border_colour = PALETTE.GREEN if action_viable else PALETTE.ORANGE
91 bottom = y_offset + text.surface.get_rect().height
92 right = text.surface.get_rect().width + x_offset
93 pygame.draw.lines(self.surface, border_colour, True,
94 [(x_offset, y_offset), (right, y_offset),
95 (right, bottom), (x_offset, bottom)], 4)
96 if action.required_bits:
97 img_name = BIT_MAP[action.required_bits].replace(
99 img = resources.get_image(img_name,
100 transforms=(EIGHT_BIT,))
101 self.surface.blit(img, (0, y_offset))
102 return y_offset + text.surface.get_rect().height + 16
104 def set_position(self, position):
105 self.card_position = position
106 self.card = self.state.board_locations[self.card_position]
107 if self.state.gameboard.player_mode == ACT:
108 if self.chosen is None:
113 def draw(self, surface):
114 surface.blit(self.surface, self.pos)
116 def next_viable_action(self, step=1):
117 num_actions = len(self.card.actions)
120 player = self.state.player
122 for i in range(num_actions - 1):
123 # loop through each action at most once.
124 chosen = (chosen + step) % num_actions
125 action = self.card.actions[chosen]
126 if action.check_available(player):
127 sound.play_sound('change_action.ogg', volume=0.05)
130 def prev_viable_action(self):
131 return self.next_viable_action(step=-1)
133 def try_perform_action(self):
134 player = self.state.player
135 action = self.card.actions[self.chosen]
136 if not action.check_available(player):
137 sound.play_sound('error.ogg')
139 action.perform_action(self.state.gameboard, self.card)
140 self.state.gameboard.replace_card(player.position)
141 self.state.gameboard.change_mode(EXAMINE)
142 self.set_position(player.position)
144 def handle_event(self, ev):
145 if self.state.gameboard.player_mode == EXAMINE:
146 return super(InfoAreaWidget, self).handle_event(ev)
147 if ev.type == pgl.KEYDOWN:
148 if ev.key in KEYS.SELECT:
149 self.try_perform_action()
150 return finish_event()
151 if ev.key in KEYS.UP:
152 self.next_viable_action()
153 return finish_event()
154 if ev.key in KEYS.DOWN:
155 self.prev_viable_action()
156 return finish_event()
157 if ev.key in KEYS.SWITCH:
158 self.next_viable_action()
159 return finish_event()
160 return super(InfoAreaWidget, self).handle_event(ev)