Dummy sound should know about foreground as well
[naja.git] / naja / actions.py
1 from naja.constants import ACTION_GLYPHS, BITS, CHESS_PIECES
2 from naja.sound import sound
3 from naja.utils import bit_glyphs, move_glyph
4
5
6 class LocationAction(object):
7     """
8     An action that may be performed on a location.
9     """
10
11     TEXT = None
12     GLYPHS = tuple()
13     MSB_GLYPH = None
14
15     def __init__(self, required_bits, **data):
16         self.required_bits = required_bits
17         self.data = data
18
19     def get_text(self, location=None):
20         substitutions = self.data.copy()
21
22         if 'direction' in self.data:
23             substitutions['rowcol'] = {
24                 'NORTH': 'column',
25                 'SOUTH': 'column',
26                 'EAST': 'row',
27                 'WEST': 'row',
28             }[self.data['direction']]
29             substitutions['direction'] = '{%s}' % (substitutions['direction'],)
30
31         if 'chesspiece' in self.data:
32             substitutions['chesspiece_name'] = move_glyph(
33                 self.data['chesspiece'])
34
35         if 'rot_direction' in self.data:
36             substitutions['rot_direction_name'] = '{%s}' % (
37                 substitutions['rot_direction'],)
38
39         if location is None:
40             substitutions['location_bits'] = 'bits specified by this location'
41         else:
42             substitutions['location_bits'] = bit_glyphs(
43                 location.bitwise_operand)
44
45         return self.TEXT % substitutions
46
47     def check_available(self, player):
48         return player.bits.check_bits(self.required_bits)
49
50     def perform_action(self, board, location):
51         raise NotImplementedError(
52             "%s does not implement perform_action()." % (type(self).__name__,))
53
54     def check_and_clear_MSB(self, player):
55         if player.bits.check_bit(BITS.MSB):
56             player.bits.clear_bit(BITS.MSB)
57             return True
58         else:
59             return False
60
61     def export(self):
62         return {'required_bits': list(self.required_bits),
63                 'data': self.data,
64                 'action_class': self.__class__.__name__}
65
66
67 class DoNothing(LocationAction):
68     TEXT = "No effect."
69     GLYPHS = (ACTION_GLYPHS.NOTHING,)
70
71     def perform_action(self, board, location):
72         pass
73
74
75 class LoseHealthOrMSB(LocationAction):
76     TEXT = "Lose {HEALTH} or {MSB}."
77     MSB_GLYPH = ACTION_GLYPHS.DAMAGE
78
79     def perform_action(self, board, location):
80         if not self.check_and_clear_MSB(board.player):
81             board.lose_health()
82
83
84 class SetBits(LocationAction):
85     TEXT = "Set %(location_bits)s."
86     GLYPHS = (ACTION_GLYPHS.SET_BITS,)
87
88     def perform_action(self, board, location):
89         board.player.bits.set_bits(location.bitwise_operand)
90
91
92 class ToggleBits(LocationAction):
93     TEXT = "Toggle %(location_bits)s."
94     GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
95
96     def perform_action(self, board, location):
97         board.player.bits.toggle_bits(location.bitwise_operand)
98
99
100 class LoseHealthOrMSBAndSetBits(LocationAction):
101     TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
102     GLYPHS = (ACTION_GLYPHS.SET_BITS,)
103     MSB_GLYPH = ACTION_GLYPHS.DAMAGE
104
105     def perform_action(self, board, location):
106         if not self.check_and_clear_MSB(board.player):
107             board.lose_health()
108         board.player.bits.set_bits(location.bitwise_operand)
109
110
111 class AcquireWinToken(LocationAction):
112     TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
113     GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
114
115     def perform_action(self, board, location):
116         board.acquire_win_token()
117         board.player.bits.clear_bits(set([
118             BITS.RED, BITS.GREEN, BITS.BLUE,
119         ]))
120
121
122 class GainHealth(LocationAction):
123     TEXT = "Gain {HEALTH}."
124     GLYPHS = (ACTION_GLYPHS.HEAL,)
125
126     def perform_action(self, board, location):
127         board.gain_health()
128
129
130 class GainHealthAndClearBitsOrMSB(LocationAction):
131     TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
132     GLYPHS = (ACTION_GLYPHS.HEAL,)
133     MSB_GLYPH = ACTION_GLYPHS.CLEAR_BITS
134
135     def perform_action(self, board, location):
136         board.gain_health()
137         if not self.check_and_clear_MSB(board.player):
138             board.player.bits.clear_bits(location.bitwise_operand)
139
140
141 class ShiftLocations(LocationAction):
142     TEXT = "Shift current %(rowcol)s %(direction)s."
143     GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
144
145     def perform_action(self, board, location):
146         sound.play_sound('grind.ogg')
147         board.shift_locations(self.data['direction'])
148
149
150 class RotateLocations(LocationAction):
151     TEXT = "Rotate adjacent locations %(rot_direction_name)s."
152     GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
153
154     def perform_action(self, board, location):
155         sound.play_sound('grind.ogg')
156         board.rotate_locations(self.data['rot_direction'])
157
158
159 class AllowChessMove(LocationAction):
160     TEXT = "Move like a %(chesspiece_name)s for one turn."
161     GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
162
163     def perform_action(self, board, location):
164         if self.data['chesspiece'] in CHESS_PIECES:
165             chesspiece = CHESS_PIECES[self.data['chesspiece']]
166             board.allow_chess_move(chesspiece)