data/shopgui.py (view raw)
1"""
2This class controls the textbox GUI for any shop state.
3A Gui object is created and updated by the shop state.
4"""
5
6import pygame as pg
7from . import setup, observer
8from . components import textbox
9from . import constants as c
10
11
12class Gui(object):
13 """Class that controls the GUI of the shop state"""
14 def __init__(self, level):
15 self.level = level
16 self.game_data = self.level.game_data
17 self.level.game_data['last direction'] = 'down'
18 self.SFX_observer = observer.SoundEffects()
19 self.observers = [self.SFX_observer]
20 self.sellable_items = level.sell_items
21 self.player_inventory = level.game_data['player inventory']
22 self.name = level.name
23 self.state = 'dialogue'
24 self.no_selling = ['Inn', 'Magic Shop']
25 self.weapon_list = ['Long Sword', 'Rapier']
26 self.armor_list = ['Chain Mail', 'Wooden Shield']
27 self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
28 self.index = 0
29 self.timer = 0.0
30 self.allow_input = False
31 self.items = level.items
32 self.item_to_be_sold = None
33 self.item_to_be_purchased = None
34 self.dialogue = level.dialogue
35 self.accept_dialogue = level.accept_dialogue
36 self.accept_sale_dialogue = level.accept_sale_dialogue
37 self.arrow = textbox.NextArrow()
38 self.selection_arrow = textbox.NextArrow()
39 self.arrow_pos1 = (50, 475)
40 self.arrow_pos2 = (50, 515)
41 self.arrow_pos3 = (50, 555)
42 self.arrow_pos4 = (50, 495)
43 self.arrow_pos5 = (50, 535)
44 self.arrow_pos_list = [self.arrow_pos1, self.arrow_pos2, self.arrow_pos3]
45 self.two_arrow_pos_list = [self.arrow_pos4, self.arrow_pos5]
46 self.arrow_index = 0
47 self.selection_arrow.rect.topleft = self.arrow_pos1
48 self.dialogue_box = self.make_dialogue_box(self.dialogue, self.index)
49 self.gold_box = self.make_gold_box()
50 if self.name in self.no_selling:
51 choices = self.items[0]['dialogue']
52 else:
53 choices = ['Buy', 'Sell', 'Leave']
54 self.selection_box = self.make_selection_box(choices)
55 self.state_dict = self.make_state_dict()
56
57 def notify(self, event):
58 """
59 Notify all observers of event.
60 """
61 for observer in self.observers:
62 observer.on_notify(event)
63
64 def make_dialogue_box(self, dialogue_list, index):
65 """
66 Make the sprite that controls the dialogue.
67 """
68 image = setup.GFX['dialoguebox']
69 rect = image.get_rect()
70 surface = pg.Surface(rect.size)
71 surface.set_colorkey(c.BLACK)
72 surface.blit(image, rect)
73 dialogue = self.font.render(dialogue_list[index],
74 True,
75 c.NEAR_BLACK)
76 dialogue_rect = dialogue.get_rect(left=50, top=50)
77 surface.blit(dialogue, dialogue_rect)
78 sprite = pg.sprite.Sprite()
79 sprite.image = surface
80 sprite.rect = rect
81 self.check_to_draw_arrow(sprite)
82
83 return sprite
84
85 def check_to_draw_arrow(self, sprite):
86 """
87 Blink arrow if more text needs to be read.
88 """
89 if self.index < len(self.dialogue) - 1:
90 sprite.image.blit(self.arrow.image, self.arrow.rect)
91
92 def make_gold_box(self):
93 """Make the box to display total gold"""
94 image = setup.GFX['goldbox']
95 rect = image.get_rect(bottom=608, right=800)
96
97 surface = pg.Surface(rect.size)
98 surface.set_colorkey(c.BLACK)
99 surface.blit(image, (0, 0))
100 gold = self.player_inventory['GOLD']['quantity']
101 text = 'Gold: ' + str(gold)
102 text_render = self.font.render(text, True, c.NEAR_BLACK)
103 text_rect = text_render.get_rect(x=80, y=60)
104
105 surface.blit(text_render, text_rect)
106
107 sprite = pg.sprite.Sprite()
108 sprite.image = surface
109 sprite.rect = rect
110
111 return sprite
112
113 def make_selection_box(self, choices):
114 """Make the box for the player to select options"""
115 image = setup.GFX['shopbox']
116 rect = image.get_rect(bottom=608)
117
118 surface = pg.Surface(rect.size)
119 surface.set_colorkey(c.BLACK)
120 surface.blit(image, (0, 0))
121
122 if len(choices) == 2:
123 choice1 = self.font.render(choices[0], True, c.NEAR_BLACK)
124 choice1_rect = choice1.get_rect(x=200, y=35)
125 choice2 = self.font.render(choices[1], True, c.NEAR_BLACK)
126 choice2_rect = choice2.get_rect(x=200, y=75)
127
128 surface.blit(choice1, choice1_rect)
129 surface.blit(choice2, choice2_rect)
130
131 elif len(choices) == 3:
132 choice1 = self.font.render(choices[0], True, c.NEAR_BLACK)
133 choice1_rect = choice1.get_rect(x=200, y=15)
134 choice2 = self.font.render(choices[1], True, c.NEAR_BLACK)
135 choice2_rect = choice2.get_rect(x=200, y=55)
136 choice3 = self.font.render(choices[2], True, c.NEAR_BLACK)
137 choice3_rect = choice3.get_rect(x=200, y=95)
138
139 surface.blit(choice1, choice1_rect)
140 surface.blit(choice2, choice2_rect)
141 surface.blit(choice3, choice3_rect)
142
143 sprite = pg.sprite.Sprite()
144 sprite.image = surface
145 sprite.rect = rect
146
147 return sprite
148
149
150 def make_state_dict(self):
151 """Make the state dictionary for the GUI behavior"""
152 state_dict = {'dialogue': self.control_dialogue,
153 'select': self.make_selection,
154 'confirmpurchase': self.confirm_purchase,
155 'confirmsell': self.confirm_sell,
156 'reject': self.reject_insufficient_gold,
157 'accept': self.accept_purchase,
158 'acceptsell': self.accept_sale,
159 'hasitem': self.has_item,
160 'buysell': self.buy_sell,
161 'sell': self.sell_items,
162 'cantsell': self.cant_sell,
163 'cantsellequippedweapon': self.cant_sell_equipped_weapon,
164 'cantsellequippedarmor': self.cant_sell_equipped_armor}
165
166 return state_dict
167
168 def control_dialogue(self, keys, current_time):
169 """Control the dialogue boxes"""
170 self.dialogue_box = self.make_dialogue_box(self.dialogue, self.index)
171
172 if self.index < (len(self.dialogue) - 1) and self.allow_input:
173 if keys[pg.K_SPACE]:
174 self.index += 1
175 self.allow_input = False
176
177 if self.index == (len(self.dialogue) - 1):
178 self.state = self.begin_new_transaction()
179
180 if not keys[pg.K_SPACE]:
181 self.allow_input = True
182
183 def begin_new_transaction(self):
184 """Set state to buysell or select, depending if the shop
185 is a Inn/Magic shop or not"""
186 if self.level.name in self.no_selling:
187 state = 'select'
188 else:
189 state = 'buysell'
190
191 return state
192
193
194 def make_selection(self, keys, current_time):
195 """Control the selection"""
196 choices = []
197 for item in self.items:
198 choices.append(item['dialogue'])
199 if self.name in self.no_selling:
200 choices.append('Leave')
201 else:
202 choices.append('Cancel')
203 self.dialogue_box = self.make_dialogue_box(self.dialogue, self.index)
204 self.selection_box = self.make_selection_box(choices)
205 self.gold_box = self.make_gold_box()
206
207 if len(choices) == 2:
208 arrow_list = self.two_arrow_pos_list
209 elif len(choices) == 3:
210 arrow_list = self.arrow_pos_list
211 else:
212 arrow_list = None
213 AssertionError('Only two items supported')
214
215 self.selection_arrow.rect.topleft = arrow_list[self.arrow_index]
216
217
218 if keys[pg.K_DOWN] and self.allow_input:
219 if self.arrow_index < (len(choices) - 1):
220 self.arrow_index += 1
221 self.allow_input = False
222 self.notify(c.CLICK)
223 elif keys[pg.K_UP] and self.allow_input:
224 if self.arrow_index > 0:
225 self.arrow_index -= 1
226 self.allow_input = False
227 self.notify(c.CLICK)
228 elif keys[pg.K_SPACE] and self.allow_input:
229 if self.arrow_index == 0:
230 self.state = 'confirmpurchase'
231 self.item_to_be_purchased = self.items[0]
232
233 elif self.arrow_index == 1 and len(choices) == 3:
234 self.state = 'confirmpurchase'
235 self.item_to_be_purchased = self.items[1]
236
237 else:
238 if self.level.name in self.no_selling:
239 self.level.state = 'transition out'
240 self.game_data['last state'] = self.level.name
241 else:
242 self.state = 'buysell'
243
244 self.notify(c.CLICK2)
245 self.arrow_index = 0
246 self.allow_input = False
247
248 if not keys[pg.K_SPACE] and not keys[pg.K_UP] and not keys[pg.K_DOWN]:
249 self.allow_input = True
250
251
252
253 def confirm_purchase(self, keys, current_time):
254 """Confirm selection state for GUI"""
255 dialogue = ['Are you sure?']
256 choices = ['Yes', 'No']
257 self.selection_box = self.make_selection_box(choices)
258 self.gold_box = self.make_gold_box()
259 self.dialogue_box = self.make_dialogue_box(dialogue, 0)
260 self.selection_arrow.rect.topleft = self.two_arrow_pos_list[self.arrow_index]
261
262 if keys[pg.K_DOWN] and self.allow_input:
263 if self.arrow_index < (len(choices) - 1):
264 self.arrow_index += 1
265 self.allow_input = False
266 self.notify(c.CLICK)
267 elif keys[pg.K_UP] and self.allow_input:
268 if self.arrow_index > 0:
269 self.arrow_index -= 1
270 self.allow_input = False
271 self.notify(c.CLICK)
272 elif keys[pg.K_SPACE] and self.allow_input:
273 if self.arrow_index == 0:
274 self.buy_item()
275 elif self.arrow_index == 1:
276 self.state = self.begin_new_transaction()
277 self.notify(c.CLICK2)
278 self.arrow_index = 0
279 self.allow_input = False
280
281 if not keys[pg.K_SPACE] and not keys[pg.K_DOWN] and not keys[pg.K_UP]:
282 self.allow_input = True
283
284
285 def buy_item(self):
286 """Attempt to allow player to purchase item"""
287 item = self.item_to_be_purchased
288
289 self.player_inventory['GOLD']['quantity'] -= item['price']
290
291 if self.player_inventory['GOLD']['quantity'] < 0:
292 self.player_inventory['GOLD']['quantity'] += item['price']
293 self.state = 'reject'
294 else:
295 if (item['type'] in self.player_inventory and
296 not self.name == c.POTION_SHOP):
297 self.state = 'hasitem'
298 self.player_inventory['GOLD']['quantity'] += item['price']
299 else:
300 self.notify(c.CLOTH_BELT)
301 self.state = 'accept'
302 self.add_player_item(item)
303
304
305 def add_player_item(self, item):
306 """Add item to player's inventory"""
307 item_type = item['type']
308 quantity = item['quantity']
309 value = item['price']
310 power = item['power']
311 magic_list = ['Cure', 'Fire Blast']
312 player_items = self.level.game_data['player inventory']
313 player_health = self.level.game_data['player stats']['health']
314 player_magic = self.level.game_data['player stats']['magic']
315
316 item_to_add = {'quantity': quantity,
317 'value': value,
318 'power': power}
319
320 if item_type in magic_list:
321 item_to_add = {'magic points': item['magic points'],
322 'power': item['power']}
323 player_items[item_type] = item_to_add
324 elif item_type in player_items:
325 player_items[item_type]['quantity'] += quantity
326 elif quantity > 0:
327 player_items[item_type] = item_to_add
328 elif item_type == 'room':
329 player_health['current'] = player_health['maximum']
330 player_magic['current'] = player_magic['maximum']
331
332
333 def confirm_sell(self, keys, current_time):
334 """Confirm player wants to sell item"""
335 dialogue = ['Are you sure?']
336 choices = ['Yes', 'No']
337 self.dialogue_box = self.make_dialogue_box(dialogue, 0)
338 self.selection_box = self.make_selection_box(choices)
339 self.selection_arrow.rect.topleft = self.two_arrow_pos_list[self.arrow_index]
340
341 if keys[pg.K_DOWN] and self.allow_input:
342 if self.arrow_index < (len(choices) - 1):
343 self.arrow_index += 1
344 self.allow_input = False
345 self.notify(c.CLICK)
346 elif keys[pg.K_UP] and self.allow_input:
347 if self.arrow_index > 0:
348 self.arrow_index -= 1
349 self.allow_input = False
350 self.notify(c.CLICK)
351 elif keys[pg.K_SPACE] and self.allow_input:
352 if self.arrow_index == 0:
353 self.sell_item_from_inventory()
354 elif self.arrow_index == 1:
355 self.state = self.begin_new_transaction()
356 self.notify(c.CLICK2)
357 self.allow_input = False
358 self.arrow_index = 0
359
360 if not keys[pg.K_SPACE] and not keys[pg.K_UP] and not keys[pg.K_DOWN]:
361 self.allow_input = True
362
363
364 def sell_item_from_inventory(self):
365 """
366 Allow player to sell item to shop.
367 """
368 item_price = self.item_to_be_sold['price']
369 item_name = self.item_to_be_sold['type']
370
371 if item_name in self.weapon_list:
372 if item_name == self.game_data['player inventory']['equipped weapon']:
373 self.state = 'cantsellequippedweapon'
374 else:
375 self.notify(c.CLOTH_BELT)
376 self.sell_inventory_data_adjust(item_price, item_name)
377
378 elif item_name in self.armor_list:
379 if item_name in self.game_data['player inventory']['equipped armor']:
380 self.state = 'cantsellequippedarmor'
381 else:
382 self.notify(c.CLOTH_BELT)
383 self.sell_inventory_data_adjust(item_price, item_name)
384
385 def sell_inventory_data_adjust(self, item_price, item_name):
386 """
387 Add gold and subtract item during sale.
388 """
389 self.player_inventory['GOLD']['quantity'] += (item_price / 2)
390 self.state = 'acceptsell'
391 if self.player_inventory[item_name]['quantity'] > 1:
392 self.player_inventory[item_name]['quantity'] -= 1
393 else:
394 del self.player_inventory[self.item_to_be_sold['type']]
395
396 def reject_insufficient_gold(self, keys, current_time):
397 """Reject player selection if they do not have enough gold"""
398 dialogue = ["You don't have enough gold!"]
399 self.dialogue_box = self.make_dialogue_box(dialogue, 0)
400
401 if keys[pg.K_SPACE] and self.allow_input:
402 self.notify(c.CLICK2)
403 self.state = self.begin_new_transaction()
404 self.selection_arrow.rect.topleft = self.arrow_pos1
405 self.allow_input = False
406
407 if not keys[pg.K_SPACE]:
408 self.allow_input = True
409
410 def accept_purchase(self, keys, current_time):
411 """Accept purchase and confirm with message"""
412 self.dialogue_box = self.make_dialogue_box(self.accept_dialogue, 0)
413 self.gold_box = self.make_gold_box()
414
415 if keys[pg.K_SPACE] and self.allow_input:
416 self.notify(c.CLICK2)
417 self.state = self.begin_new_transaction()
418 self.selection_arrow.rect.topleft = self.arrow_pos1
419 self.allow_input = False
420
421 if not keys[pg.K_SPACE]:
422 self.allow_input = True
423
424 def accept_sale(self, keys, current_time):
425 """Confirm to player that item was sold"""
426 self.dialogue_box = self.make_dialogue_box(self.accept_sale_dialogue, 0)
427 self.gold_box = self.make_gold_box()
428
429 if keys[pg.K_SPACE] and self.allow_input:
430 self.notify(c.CLICK2)
431 self.state = self.begin_new_transaction()
432 self.selection_arrow.rect.topleft = self.arrow_pos1
433 self.allow_input = False
434
435 if not keys[pg.K_SPACE]:
436 self.allow_input = True
437
438
439 def has_item(self, keys, current_time):
440 """Tell player he has item already"""
441 dialogue = ["You have that item already."]
442 self.dialogue_box = self.make_dialogue_box(dialogue, 0)
443
444 if keys[pg.K_SPACE] and self.allow_input:
445 self.state = self.begin_new_transaction()
446 self.selection_arrow.rect.topleft = self.arrow_pos1
447 self.allow_input = False
448 self.notify(c.CLICK2)
449
450 if not keys[pg.K_SPACE]:
451 self.allow_input = True
452
453
454 def buy_sell(self, keys, current_time):
455 """Ask player if they want to buy or sell something"""
456 dialogue = ["Would you like to buy or sell an item?"]
457 choices = ['Buy', 'Sell', 'Leave']
458 self.dialogue_box = self.make_dialogue_box(dialogue, 0)
459 self.selection_box = self.make_selection_box(choices)
460 self.selection_arrow.rect.topleft = self.arrow_pos_list[self.arrow_index]
461
462 if keys[pg.K_DOWN] and self.allow_input:
463 if self.arrow_index < (len(self.arrow_pos_list) - 1):
464 self.arrow_index += 1
465 self.allow_input = False
466 self.notify(c.CLICK)
467
468 elif keys[pg.K_UP] and self.allow_input:
469 if self.arrow_index > 0:
470 self.arrow_index -= 1
471 self.allow_input = False
472 self.notify(c.CLICK)
473 elif keys[pg.K_SPACE] and self.allow_input:
474 if self.arrow_index == 0:
475 self.state = 'select'
476 self.allow_input = False
477 self.arrow_index = 0
478 elif self.arrow_index == 1:
479 if self.check_for_sellable_items():
480 self.state = 'sell'
481 self.allow_input = False
482 self.arrow_index = 0
483 else:
484 self.state = 'cantsell'
485 self.allow_input = False
486 self.arrow_index = 0
487 else:
488 self.level.state = 'transition out'
489 self.game_data['last state'] = self.level.name
490
491 self.arrow_index = 0
492 self.notify(c.CLICK2)
493
494 if not keys[pg.K_SPACE] and not keys[pg.K_DOWN] and not keys[pg.K_UP]:
495 self.allow_input = True
496
497 def check_for_sellable_items(self):
498 """Check for sellable items"""
499 for item in self.player_inventory:
500 if item in self.sellable_items:
501 return True
502 else:
503 return False
504
505 def sell_items(self, keys, current_time):
506 """Have player select items to sell"""
507 dialogue = ["What would you like to sell?"]
508 choices = []
509 item_list = []
510 for item in self.items:
511 if item['type'] in self.player_inventory:
512 name = item['type']
513 price = " (" + str(item['price'] / 2) + " gold)"
514 choices.append(name + price)
515 item_list.append(name)
516 choices.append('Cancel')
517 self.dialogue_box = self.make_dialogue_box(dialogue, 0)
518 self.selection_box = self.make_selection_box(choices)
519
520 if len(choices) == 2:
521 self.selection_arrow.rect.topleft = self.two_arrow_pos_list[self.arrow_index]
522 elif len(choices) == 3:
523 self.selection_arrow.rect.topleft = self.arrow_pos_list[self.arrow_index]
524
525 if keys[pg.K_DOWN] and self.allow_input:
526 if self.arrow_index < (len(self.arrow_pos_list) - 1):
527 self.arrow_index += 1
528 self.allow_input = False
529 self.notify(c.CLICK)
530 elif keys[pg.K_UP] and self.allow_input:
531 if self.arrow_index > 0:
532 self.arrow_index -= 1
533 self.allow_input = False
534 self.notify(c.CLICK)
535 elif keys[pg.K_SPACE] and self.allow_input:
536 if self.arrow_index == 0:
537 self.state = 'confirmsell'
538 self.allow_input = False
539 for item in self.items:
540 if item['type'] == item_list[0]:
541 self.item_to_be_sold = item
542
543 elif self.arrow_index == 1 and len(choices) == 3:
544 self.state = 'confirmsell'
545 self.allow_input = False
546 for item in self.items:
547 if item['type'] == item_list[1]:
548 self.item_to_be_sold = item
549 else:
550 self.state = 'buysell'
551 self.allow_input = False
552 self.arrow_index = 0
553 self.notify(c.CLICK2)
554
555 if not keys[pg.K_SPACE] and not keys[pg.K_DOWN] and not keys[pg.K_UP]:
556 self.allow_input = True
557
558
559 def cant_sell(self, keys, current_time):
560 """Do not allow player to sell anything"""
561 dialogue = ["You don't have anything to sell!"]
562 self.dialogue_box = self.make_dialogue_box(dialogue, 0)
563
564 if keys[pg.K_SPACE] and self.allow_input:
565 self.state = 'buysell'
566 self.allow_input = False
567 self.notify(c.CLICK2)
568
569
570 if not keys[pg.K_SPACE]:
571 self.allow_input = True
572
573 def cant_sell_equipped_weapon(self, keys, *args):
574 """
575 Do not sell weapon the player has equipped.
576 """
577 dialogue = ["You can't sell an equipped weapon."]
578 self.dialogue_box = self.make_dialogue_box(dialogue, 0)
579
580 if keys[pg.K_SPACE] and self.allow_input:
581 self.state = 'buysell'
582 self.allow_input = False
583 self.notify(c.CLICK2)
584
585 if not keys[pg.K_SPACE]:
586 self.allow_input = True
587
588 def cant_sell_equipped_armor(self, keys, *args):
589 """
590 Do not sell armor the player has equipped.
591 """
592 dialogue = ["You can't sell equipped armor."]
593 self.dialogue_box = self.make_dialogue_box(dialogue, 0)
594
595 if keys[pg.K_SPACE] and self.allow_input:
596 self.state = 'buysell'
597 self.allow_input = False
598
599 if not keys[pg.K_SPACE]:
600 self.allow_input = True
601
602
603
604 def update(self, keys, current_time):
605 """Updates the shop GUI"""
606 state_function = self.state_dict[self.state]
607 state_function(keys, current_time)
608
609
610 def draw(self, surface):
611 """Draw GUI to level surface"""
612 state_list1 = ['dialogue', 'reject', 'accept', 'hasitem']
613 state_list2 = ['select', 'confirmpurchase', 'buysell', 'sell', 'confirmsell']
614
615 surface.blit(self.dialogue_box.image, self.dialogue_box.rect)
616 surface.blit(self.gold_box.image, self.gold_box.rect)
617 if self.state in state_list2:
618 surface.blit(self.selection_box.image, self.selection_box.rect)
619 surface.blit(self.selection_arrow.image, self.selection_arrow.rect)
620