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.done = True
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
397
398
399 def reject_insufficient_gold(self, keys, current_time):
400 """Reject player selection if they do not have enough gold"""
401 dialogue = ["You don't have enough gold!"]
402 self.dialogue_box = self.make_dialogue_box(dialogue, 0)
403
404 if keys[pg.K_SPACE] and self.allow_input:
405 self.notify(c.CLICK2)
406 self.state = self.begin_new_transaction()
407 self.selection_arrow.rect.topleft = self.arrow_pos1
408 self.allow_input = False
409
410 if not keys[pg.K_SPACE]:
411 self.allow_input = True
412
413
414 def accept_purchase(self, keys, current_time):
415 """Accept purchase and confirm with message"""
416 self.dialogue_box = self.make_dialogue_box(self.accept_dialogue, 0)
417 self.gold_box = self.make_gold_box()
418
419 if keys[pg.K_SPACE] and self.allow_input:
420 self.notify(c.CLICK2)
421 self.state = self.begin_new_transaction()
422 self.selection_arrow.rect.topleft = self.arrow_pos1
423 self.allow_input = False
424
425 if not keys[pg.K_SPACE]:
426 self.allow_input = True
427
428
429 def accept_sale(self, keys, current_time):
430 """Confirm to player that item was sold"""
431 self.dialogue_box = self.make_dialogue_box(self.accept_sale_dialogue, 0)
432 self.gold_box = self.make_gold_box()
433
434 if keys[pg.K_SPACE] and self.allow_input:
435 self.notify(c.CLICK2)
436 self.state = self.begin_new_transaction()
437 self.selection_arrow.rect.topleft = self.arrow_pos1
438 self.allow_input = False
439
440 if not keys[pg.K_SPACE]:
441 self.allow_input = True
442
443
444 def has_item(self, keys, current_time):
445 """Tell player he has item already"""
446 dialogue = ["You have that item already."]
447 self.dialogue_box = self.make_dialogue_box(dialogue, 0)
448
449 if keys[pg.K_SPACE] and self.allow_input:
450 self.state = self.begin_new_transaction()
451 self.selection_arrow.rect.topleft = self.arrow_pos1
452 self.allow_input = False
453 self.notify(c.CLICK2)
454
455 if not keys[pg.K_SPACE]:
456 self.allow_input = True
457
458
459 def buy_sell(self, keys, current_time):
460 """Ask player if they want to buy or sell something"""
461 dialogue = ["Would you like to buy or sell an item?"]
462 choices = ['Buy', 'Sell', 'Leave']
463 self.dialogue_box = self.make_dialogue_box(dialogue, 0)
464 self.selection_box = self.make_selection_box(choices)
465 self.selection_arrow.rect.topleft = self.arrow_pos_list[self.arrow_index]
466
467 if keys[pg.K_DOWN] and self.allow_input:
468 if self.arrow_index < (len(self.arrow_pos_list) - 1):
469 self.arrow_index += 1
470 self.allow_input = False
471 self.notify(c.CLICK)
472
473 elif keys[pg.K_UP] and self.allow_input:
474 if self.arrow_index > 0:
475 self.arrow_index -= 1
476 self.allow_input = False
477 self.notify(c.CLICK)
478 elif keys[pg.K_SPACE] and self.allow_input:
479 if self.arrow_index == 0:
480 self.state = 'select'
481 self.allow_input = False
482 self.arrow_index = 0
483 elif self.arrow_index == 1:
484 if self.check_for_sellable_items():
485 self.state = 'sell'
486 self.allow_input = False
487 self.arrow_index = 0
488 else:
489 self.state = 'cantsell'
490 self.allow_input = False
491 self.arrow_index = 0
492
493 else:
494
495 self.level.done = True
496 self.game_data['last state'] = self.level.name
497
498 self.arrow_index = 0
499 self.notify(c.CLICK2)
500
501 if not keys[pg.K_SPACE] and not keys[pg.K_DOWN] and not keys[pg.K_UP]:
502 self.allow_input = True
503
504
505 def check_for_sellable_items(self):
506 """Check for sellable items"""
507 for item in self.player_inventory:
508 if item in self.sellable_items:
509 return True
510 else:
511 return False
512
513
514 def sell_items(self, keys, current_time):
515 """Have player select items to sell"""
516 dialogue = ["What would you like to sell?"]
517 choices = []
518 item_list = []
519 for item in self.items:
520 if item['type'] in self.player_inventory:
521 name = item['type']
522 price = " (" + str(item['price'] / 2) + " gold)"
523 choices.append(name + price)
524 item_list.append(name)
525 choices.append('Cancel')
526 self.dialogue_box = self.make_dialogue_box(dialogue, 0)
527 self.selection_box = self.make_selection_box(choices)
528
529 if len(choices) == 2:
530 self.selection_arrow.rect.topleft = self.two_arrow_pos_list[self.arrow_index]
531 elif len(choices) == 3:
532 self.selection_arrow.rect.topleft = self.arrow_pos_list[self.arrow_index]
533
534 if keys[pg.K_DOWN] and self.allow_input:
535 if self.arrow_index < (len(self.arrow_pos_list) - 1):
536 self.arrow_index += 1
537 self.allow_input = False
538 self.notify(c.CLICK)
539 elif keys[pg.K_UP] and self.allow_input:
540 if self.arrow_index > 0:
541 self.arrow_index -= 1
542 self.allow_input = False
543 self.notify(c.CLICK)
544 elif keys[pg.K_SPACE] and self.allow_input:
545 if self.arrow_index == 0:
546 self.state = 'confirmsell'
547 self.allow_input = False
548 for item in self.items:
549 if item['type'] == item_list[0]:
550 self.item_to_be_sold = item
551
552 elif self.arrow_index == 1 and len(choices) == 3:
553 self.state = 'confirmsell'
554 self.allow_input = False
555 for item in self.items:
556 if item['type'] == item_list[1]:
557 self.item_to_be_sold = item
558 else:
559 self.state = 'buysell'
560 self.allow_input = False
561 self.arrow_index = 0
562 self.notify(c.CLICK2)
563
564 if not keys[pg.K_SPACE] and not keys[pg.K_DOWN] and not keys[pg.K_UP]:
565 self.allow_input = True
566
567
568 def cant_sell(self, keys, current_time):
569 """Do not allow player to sell anything"""
570 dialogue = ["You don't have anything to sell!"]
571 self.dialogue_box = self.make_dialogue_box(dialogue, 0)
572
573 if keys[pg.K_SPACE] and self.allow_input:
574 self.state = 'buysell'
575 self.allow_input = False
576 self.notify(c.CLICK2)
577
578
579 if not keys[pg.K_SPACE]:
580 self.allow_input = True
581
582 def cant_sell_equipped_weapon(self, keys, *args):
583 """
584 Do not sell weapon the player has equipped.
585 """
586 dialogue = ["You can't sell an equipped weapon."]
587 self.dialogue_box = self.make_dialogue_box(dialogue, 0)
588
589 if keys[pg.K_SPACE] and self.allow_input:
590 self.state = 'buysell'
591 self.allow_input = False
592 self.notify(c.CLICK2)
593
594 if not keys[pg.K_SPACE]:
595 self.allow_input = True
596
597 def cant_sell_equipped_armor(self, keys, *args):
598 """
599 Do not sell armor the player has equipped.
600 """
601 dialogue = ["You can't sell equipped armor."]
602 self.dialogue_box = self.make_dialogue_box(dialogue, 0)
603
604 if keys[pg.K_SPACE] and self.allow_input:
605 self.state = 'buysell'
606 self.allow_input = False
607
608 if not keys[pg.K_SPACE]:
609 self.allow_input = True
610
611
612
613 def update(self, keys, current_time):
614 """Updates the shop GUI"""
615 state_function = self.state_dict[self.state]
616 state_function(keys, current_time)
617
618
619 def draw(self, surface):
620 """Draw GUI to level surface"""
621 state_list1 = ['dialogue', 'reject', 'accept', 'hasitem']
622 state_list2 = ['select', 'confirmpurchase', 'buysell', 'sell', 'confirmsell']
623
624 surface.blit(self.dialogue_box.image, self.dialogue_box.rect)
625 surface.blit(self.gold_box.image, self.gold_box.rect)
626 if self.state in state_list2:
627 surface.blit(self.selection_box.image, self.selection_box.rect)
628 surface.blit(self.selection_arrow.image, self.selection_arrow.rect)
629