Pythonic sound generator
[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 = (ACTION_GLYPHS.NOTHING,)
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
70     def perform_action(self, board, location):
71         pass
72
73
74 class LoseHealthOrMSB(LocationAction):
75     TEXT = "Lose {HEALTH} or {MSB}."
76     MSB_GLYPH = ACTION_GLYPHS.DAMAGE
77
78     def perform_action(self, board, location):
79         if not self.check_and_clear_MSB(board.player):
80             board.lose_health()
81
82
83 class SetBits(LocationAction):
84     TEXT = "Set %(location_bits)s."
85     GLYPHS = (ACTION_GLYPHS.SET_BITS,)
86
87     def perform_action(self, board, location):
88         board.player.bits.set_bits(location.bitwise_operand)
89
90
91 class ToggleBits(LocationAction):
92     TEXT = "Toggle %(location_bits)s."
93     GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
94
95     def perform_action(self, board, location):
96         board.player.bits.toggle_bits(location.bitwise_operand)
97
98
99 class LoseHealthOrMSBAndSetBits(LocationAction):
100     TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
101     GLYPHS = (ACTION_GLYPHS.SET_BITS,)
102     MSB_GLYPH = ACTION_GLYPHS.DAMAGE
103
104     def perform_action(self, board, location):
105         if not self.check_and_clear_MSB(board.player):
106             board.lose_health()
107         board.player.bits.set_bits(location.bitwise_operand)
108
109
110 class AcquireWinToken(LocationAction):
111     TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
112     GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
113
114     def perform_action(self, board, location):
115         board.acquire_win_token()
116         board.player.bits.clear_bits(set([
117             BITS.RED, BITS.GREEN, BITS.BLUE,
118         ]))
119
120
121 class GainHealth(LocationAction):
122     TEXT = "Gain {HEALTH}."
123     GLYPHS = (ACTION_GLYPHS.HEAL,)
124
125     def perform_action(self, board, location):
126         board.gain_health()
127
128
129 class GainHealthAndClearBitsOrMSB(LocationAction):
130     TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
131     GLYPHS = (ACTION_GLYPHS.HEAL,)
132     MSB_GLYPH = ACTION_GLYPHS.CLEAR_BITS
133
134     def perform_action(self, board, location):
135         board.gain_health()
136         if not self.check_and_clear_MSB(board.player):
137             board.player.bits.clear_bits(location.bitwise_operand)
138
139
140 class ShiftLocations(LocationAction):
141     TEXT = "Shift current %(rowcol)s %(direction)s."
142     GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
143
144     def perform_action(self, board, location):
145         sound.play_sound('change.ogg')
146         board.shift_locations(self.data['direction'])
147
148
149 class RotateLocations(LocationAction):
150     TEXT = "Rotate adjacent locations %(rot_direction_name)s."
151     GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
152
153     def perform_action(self, board, location):
154         sound.play_sound('change.ogg')
155         board.rotate_locations(self.data['rot_direction'])
156
157
158 class AllowChessMove(LocationAction):
159     TEXT = "Move like a %(chesspiece_name)s for one turn."
160     GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
161
162     def perform_action(self, board, location):
163         if self.data['chesspiece'] in CHESS_PIECES:
164             chesspiece = CHESS_PIECES[self.data['chesspiece']]
165             board.allow_chess_move(chesspiece)