data/states/town.py (view raw)
1__author__ = 'justinarmstrong'
2
3import pygame as pg
4from .. import tools, collision
5from .. import tilemap as tm
6from .. components import person, textbox
7
8
9
10class Town(tools._State):
11 def __init__(self):
12 super(Town, self).__init__()
13
14
15 def startup(self, current_time, persist):
16 """Called when the State object is created"""
17 self.persist = persist
18 self.current_time = current_time
19 self.town_map = tm.create_town_map()
20 self.viewport = tm.create_viewport(self.town_map)
21 self.blockers = tm.create_blockers()
22 self.level_surface = tm.create_level_surface(self.town_map)
23 self.level_rect = self.level_surface.get_rect()
24 self.player = person.Player('up')
25 self.town_sprites = pg.sprite.Group()
26 self.start_positions = tm.set_sprite_positions(self.player,
27 self.town_sprites)
28 self.set_sprite_dialogue()
29 self.collision_handler = collision.CollisionHandler(self.player,
30 self.blockers,
31 self.town_sprites)
32 self.dialogue_handler = textbox.DialogueHandler(self.player,
33 self.town_sprites)
34
35
36 def set_sprite_dialogue(self):
37 """Sets unique dialogue for each sprite"""
38 for sprite in self.town_sprites:
39 if sprite.location == (9, 49):
40 sprite.dialogue = 'Welcome to our town, Mr. Traveller!'
41 elif sprite.location == (16, 42):
42 sprite.dialogue = 'You seem tired, why not rest at our Inn?'
43 sprite.begin_auto_resting()
44 elif sprite.location == (14, 14):
45 sprite.dialogue = 'Welcome to the castle, citizen.'
46 elif sprite.location == (11, 14):
47 sprite.dialogue = 'I have heard rumours that the King has lost something...'
48 elif sprite.location == (11, 8):
49 sprite.dialogue = 'Be careful. There are monsters surrounding our town.'
50 elif sprite.location == (14, 8):
51 sprite.dialogue = 'Move along, citizen.'
52
53
54 def update(self, surface, keys, current_time):
55 """Updates state"""
56 self.player.update(keys, current_time)
57 self.town_sprites.update(current_time)
58 self.collision_handler.update(keys, current_time)
59 self.dialogue_handler.update(keys, current_time)
60 self.viewport_update()
61
62 self.draw_level(surface)
63
64
65 def viewport_update(self):
66 """Viewport stays centered on character, unless at edge of map"""
67 self.viewport.center = self.player.rect.center
68 self.viewport.clamp_ip(self.level_rect)
69
70
71 def draw_level(self, surface):
72 """Blits all images to screen"""
73 self.level_surface.blit(self.town_map['surface'], self.viewport, self.viewport)
74 self.level_surface.blit(self.player.image, self.player.rect)
75 self.town_sprites.draw(self.level_surface)
76
77 surface.blit(self.level_surface, (0, 0), self.viewport)
78 self.dialogue_handler.draw(surface)
79
80
81
82
83
84
85
86
87
88
89
90
91