data/components/textbox.py (view raw)
1__author__ = 'justinarmstrong'
2import copy
3import pygame as pg
4from .. import setup
5from .. import constants as c
6
7
8
9class NextArrow(pg.sprite.Sprite):
10 """Flashing arrow indicating more dialogue"""
11 def __init__(self):
12 super(NextArrow, self).__init__()
13 self.image = setup.GFX['fancyarrow']
14 self.rect = self.image.get_rect(right=780,
15 bottom=135)
16
17
18class DialogueBox(object):
19 """Text box used for dialogue"""
20 def __init__(self, dialogue, index=0, image_key='dialoguebox', item=None):
21 self.item = item
22 self.bground = setup.GFX[image_key]
23 self.rect = self.bground.get_rect(centerx=400)
24 self.arrow_timer = 0.0
25 self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
26 self.dialogue_list = dialogue
27 self.index = index
28 self.image = self.make_dialogue_box_image()
29 self.arrow = NextArrow()
30 self.check_to_draw_arrow()
31 self.done = False
32 self.allow_input = False
33 self.name = image_key
34
35
36 def make_dialogue_box_image(self):
37 """Make the image of the dialogue box"""
38 image = pg.Surface(self.rect.size)
39 image.set_colorkey(c.BLACK)
40 image.blit(self.bground, (0, 0))
41
42 dialogue_image = self.font.render(self.dialogue_list[self.index],
43 True,
44 c.NEAR_BLACK)
45 dialogue_rect = dialogue_image.get_rect(left=50, top=50)
46 image.blit(dialogue_image, dialogue_rect)
47
48 return image
49
50
51
52 def update(self, keys, current_time):
53 """Updates scrolling text"""
54 self.current_time = current_time
55 self.draw_box(current_time)
56 self.terminate_check(keys)
57
58
59 def draw_box(self, current_time, x=400):
60 """Reveal dialogue on textbox"""
61 self.image = self.make_dialogue_box_image()
62 self.check_to_draw_arrow()
63
64
65 def terminate_check(self, keys):
66 """Remove textbox from sprite group after 2 seconds"""
67 if keys[pg.K_SPACE] and self.allow_input:
68 self.done = True
69
70 if not keys[pg.K_SPACE]:
71 self.allow_input = True
72
73
74 def check_to_draw_arrow(self):
75 """Blink arrow if more text needs to be read"""
76 if self.index < len(self.dialogue_list) - 1:
77 self.image.blit(self.arrow.image, self.arrow.rect)
78 else:
79 pass
80
81
82class ItemBox(DialogueBox):
83 """Text box for information like obtaining new items"""
84 def __init__(self, dialogue, item=None):
85 super(ItemBox, self).__init__(None, 0, 'infobox', item)
86
87
88 def make_dialogue_box_image(self):
89 """Make the image of the dialogue box"""
90 image = pg.Surface(self.rect.size)
91 image.set_colorkey(c.BLACK)
92 image.blit(self.bground, (0, 0))
93
94 if self.item:
95 type = list(self.item.keys())[0]
96 total = str(self.item[type])
97 dialogue = 'You received ' + total + ' ' + type + '.'
98 self.dialogue_list = [dialogue]
99 self.item = None
100
101 dialogue_image = self.font.render(self.dialogue_list[self.index],
102 False,
103 c.NEAR_BLACK)
104 dialogue_rect = dialogue_image.get_rect(left=50, top=50)
105 image.blit(dialogue_image, dialogue_rect)
106
107 return image
108
109
110
111class TextHandler(object):
112 """Handles interaction between sprites to create dialogue boxes"""
113
114 def __init__(self, level):
115 self.player = level.player
116 self.sprites = level.sprites
117 self.talking_sprite = None
118 self.textbox = None
119 self.allow_input = False
120 self.level = level
121 self.last_textbox_timer = 0.0
122 self.game_data = level.game_data
123
124
125 def update(self, keys, current_time):
126 """Checks for the creation of Dialogue boxes"""
127 if keys[pg.K_SPACE] and not self.textbox and self.allow_input:
128 for sprite in self.sprites:
129 if (current_time - self.last_textbox_timer) > 300:
130 if self.player.state == 'resting':
131 self.allow_input = False
132 self.check_for_dialogue(sprite)
133
134 if self.textbox:
135 self.textbox.update(keys, current_time)
136
137 if self.textbox.done:
138
139 if self.textbox.index < (len(self.textbox.dialogue_list) - 1):
140 index = self.textbox.index + 1
141 dialogue = self.textbox.dialogue_list
142 if self.textbox.name == 'dialoguebox':
143 self.textbox = DialogueBox(dialogue, index)
144 elif self.textbox.name == 'infobox':
145 self.textbox = ItemBox(dialogue, index)
146 elif self.talking_sprite.item:
147 item = self.check_for_item()
148 self.textbox = ItemBox(None, item)
149 else:
150 self.level.state = 'normal'
151 self.textbox = None
152 self.last_textbox_timer = current_time
153 self.reset_sprite_direction()
154
155 if not keys[pg.K_SPACE]:
156 self.allow_input = True
157
158
159 def check_for_dialogue(self, sprite):
160 """Checks if a sprite is in the correct location to give dialogue"""
161 player = self.player
162 tile_x, tile_y = player.location
163
164 if player.direction == 'up':
165 if sprite.location == [tile_x, tile_y - 1]:
166 self.textbox = DialogueBox(sprite.dialogue)
167 sprite.direction = 'down'
168 self.talking_sprite = sprite
169 elif player.direction == 'down':
170 if sprite.location == [tile_x, tile_y + 1]:
171 self.textbox = DialogueBox(sprite.dialogue)
172 sprite.direction = 'up'
173 self.talking_sprite = sprite
174 elif player.direction == 'left':
175 if sprite.location == [tile_x - 1, tile_y]:
176 self.textbox = DialogueBox(sprite.dialogue)
177 sprite.direction = 'right'
178 self.talking_sprite = sprite
179 elif player.direction == 'right':
180 if sprite.location == [tile_x + 1, tile_y]:
181 self.textbox = DialogueBox(sprite.dialogue)
182 sprite.direction = 'left'
183 self.talking_sprite = sprite
184
185
186 def check_for_item(self):
187 """Checks if sprite has an item to give to the player"""
188 item = self.talking_sprite.item
189 if item:
190 if 'gold' in item:
191 self.game_data['player inventory']['gold'] += item['gold']
192 self.talking_sprite.item = None
193 if self.talking_sprite.name == 'king':
194 self.game_data['king item'] = None
195
196 return item
197
198
199
200 def reset_sprite_direction(self):
201 """Reset sprite to default direction"""
202 for sprite in self.sprites:
203 if sprite.state == 'resting':
204 sprite.direction = sprite.default_direction
205
206
207 def draw(self, surface):
208 """Draws textbox to surface"""
209 if self.textbox:
210 surface.blit(self.textbox.image, self.textbox.rect)
211
212
213 def make_textbox(self, name, dialogue, item=None):
214 """Make textbox on demand"""
215 if name == 'itembox':
216 textbox = ItemBox(dialogue, item)
217 elif name == 'dialoguebox':
218 textbox = DialogueBox(dialogue)
219 else:
220 textbox = None
221
222 return textbox
223