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
7from .. import constants as c
8
9
10
11class Town(tools._State):
12 def __init__(self):
13 super(Town, self).__init__()
14
15
16 def startup(self, current_time, persist):
17 """Called when the State object is created"""
18 self.persist = persist
19 self.current_time = current_time
20 self.state = 'normal'
21 self.town_map = tm.create_town_map()
22 self.viewport = tm.create_viewport(self.town_map)
23 self.blockers = tm.create_blockers()
24 self.level_surface = tm.create_level_surface(self.town_map)
25 self.level_rect = self.level_surface.get_rect()
26 self.player = person.Player('up')
27 self.town_sprites = pg.sprite.Group()
28 self.start_positions = tm.set_sprite_positions(self.player,
29 self.town_sprites)
30 self.set_sprite_dialogue()
31 self.collision_handler = collision.CollisionHandler(self.player,
32 self.blockers,
33 self.town_sprites)
34 self.dialogue_handler = textbox.DialogueHandler(self.player,
35 self.town_sprites,
36 self)
37 self.state_dict = self.make_state_dict()
38 self.portals = tm.make_level_portals()
39
40
41 def set_sprite_dialogue(self):
42 """Sets unique dialogue for each sprite"""
43 for sprite in self.town_sprites:
44 if sprite.location == (10, 47):
45 sprite.dialogue = ['Welcome to our town, Mr. Traveller!',
46 'The King is loved by all!',
47 'You should go visit him in his castle.']
48 elif sprite.location == (16, 42):
49 sprite.dialogue = ['You seem tired, why not rest at our Inn?']
50 sprite.begin_auto_resting()
51 elif sprite.location == (14, 14):
52 sprite.dialogue = ['Be careful. There are monsters surrounding our town.',
53 'Make sure to equip sufficient armour and weapons.',
54 'Spells and potions are useful too.']
55 elif sprite.location == (11, 14):
56 sprite.dialogue = ['I have heard rumours that the King has lost something...',
57 'Perhaps you should pay him a visit.']
58 elif sprite.location == (11, 8):
59 sprite.dialogue = ['Welcome to the castle, citizen.']
60 elif sprite.location == (14, 8):
61 sprite.dialogue = ['Move along.']
62
63
64 def make_state_dict(self):
65 """Make a dictionary of states the level can be in"""
66 state_dict = {'normal': self.running_normally,
67 'dialogue': self.handling_dialogue}
68
69 return state_dict
70
71
72 def running_normally(self, surface, keys, current_time):
73 """Update level normally"""
74 self.check_for_dialogue()
75 self.check_for_portals()
76 self.player.update(keys, current_time)
77 self.town_sprites.update(current_time)
78 self.collision_handler.update(keys, current_time)
79 self.dialogue_handler.update(keys, current_time)
80 self.viewport_update()
81
82 self.draw_level(surface)
83
84
85 def check_for_portals(self):
86 """Check if the player walks into a door, requiring a level change"""
87 portal = pg.sprite.spritecollideany(self.player, self.portals)
88
89 if portal and self.player.state == 'resting':
90 #self.next = portal.name
91 self.next = c.TOWN
92 self.done = True
93
94
95 def handling_dialogue(self, surface, keys, current_time):
96 """Update only dialogue boxes"""
97 self.dialogue_handler.update(keys, current_time)
98 self.draw_level(surface)
99
100
101 def check_for_dialogue(self):
102 """Check if the level needs to freeze"""
103 if self.dialogue_handler.textbox:
104 self.state = 'dialogue'
105
106
107 def update(self, surface, keys, current_time):
108 """Updates state"""
109 state_function = self.state_dict[self.state]
110 state_function(surface, keys, current_time)
111
112
113 def viewport_update(self):
114 """Viewport stays centered on character, unless at edge of map"""
115 self.viewport.center = self.player.rect.center
116 self.viewport.clamp_ip(self.level_rect)
117
118
119 def draw_level(self, surface):
120 """Blits all images to screen"""
121 self.level_surface.blit(self.town_map['surface'], self.viewport, self.viewport)
122 self.level_surface.blit(self.player.image, self.player.rect)
123 self.town_sprites.draw(self.level_surface)
124
125 surface.blit(self.level_surface, (0, 0), self.viewport)
126 self.dialogue_handler.draw(surface)
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141