data/states/level_state.py (view raw)
1__author__ = 'justinarmstrong'
2"""
3This is the base class all level states (i.e. states
4where the player can move around the screen) inherit
5from. This class inherits from the generic state class
6found in the tools.py module.
7"""
8
9import copy
10import pygame as pg
11from .. import tools, collision
12from .. import tilemap as tm
13from .. components import person, textbox
14
15
16class LevelState(tools._State):
17 def __init__(self):
18 super(LevelState, self).__init__()
19 self.name = None
20 self.map_width = None
21 self.map_height = None
22
23
24
25 def startup(self, current_time, game_data):
26 """Called when the State object is created"""
27 self.game_data = game_data
28 print(game_data['player inventory'])
29 self.current_time = current_time
30 self.state = 'normal'
31 self.background = tm.create_map_layer1(self.name,
32 self.map_width,
33 self.map_height)
34 self.foreground = tm.create_map_layer2(self.name,
35 self.map_width,
36 self.map_height)
37 self.viewport = tm.create_viewport(self.background)
38 self.blockers = tm.create_blockers(self.name)
39 self.level_surface = tm.make_level_surface(self.background)
40 self.level_rect = self.level_surface.get_rect()
41 self.player = person.Player(game_data['last direction'])
42 self.sprites = pg.sprite.Group()
43 self.start_positions = tm.set_sprite_positions(self.player,
44 self.sprites,
45 self.name,
46 self.game_data)
47 self.set_sprite_dialogue()
48 self.collision_handler = collision.CollisionHandler(self.player,
49 self.blockers,
50 self.sprites)
51 self.dialogue_handler = textbox.TextHandler(self)
52 self.state_dict = self.make_state_dict()
53 self.portals = tm.make_level_portals(self.name)
54
55
56 def set_sprite_dialogue(self):
57 """Sets unique dialogue for each sprite"""
58 raise NotImplementedError
59
60
61 def make_state_dict(self):
62 """Make a dictionary of states the level can be in"""
63 state_dict = {'normal': self.running_normally,
64 'dialogue': self.handling_dialogue}
65
66 return state_dict
67
68
69 def running_normally(self, surface, keys, current_time):
70 """Update level normally"""
71 self.check_for_dialogue()
72 self.check_for_portals()
73 self.player.update(keys, current_time)
74 self.sprites.update(current_time)
75 self.collision_handler.update(keys, current_time)
76 self.dialogue_handler.update(keys, current_time)
77 self.viewport_update()
78
79 self.draw_level(surface)
80
81
82 def check_for_portals(self):
83 """Check if the player walks into a door, requiring a level change"""
84 portal = pg.sprite.spritecollideany(self.player, self.portals)
85
86 if portal and self.player.state == 'resting':
87 self.player.location = self.player.get_tile_location()
88 self.next = portal.name
89 self.update_game_data()
90 self.done = True
91
92
93 def update_game_data(self):
94 """Update the persistant game data dictionary"""
95 self.game_data['last location'] = self.player.location
96 self.game_data['last direction'] = self.player.direction
97 self.game_data['last state'] = self.name
98
99 self.set_new_start_pos()
100
101
102 def set_new_start_pos(self):
103 """Set new start position based on previous state"""
104 location = copy.deepcopy(self.game_data['last location'])
105 direction = self.game_data['last direction']
106 state = self.game_data['last state']
107
108 if direction == 'up':
109 location[1] += 1
110 elif direction == 'down':
111 location[1] -= 1
112 elif direction == 'left':
113 location[0] += 1
114 elif direction == 'right':
115 location[0] -= 1
116
117 self.game_data[state + ' start pos'] = location
118
119
120 def handling_dialogue(self, surface, keys, current_time):
121 """Update only dialogue boxes"""
122 self.dialogue_handler.update(keys, current_time)
123 self.draw_level(surface)
124
125
126 def check_for_dialogue(self):
127 """Check if the level needs to freeze"""
128 if self.dialogue_handler.textbox:
129 self.state = 'dialogue'
130
131
132 def update(self, surface, keys, current_time):
133 """Updates state"""
134 state_function = self.state_dict[self.state]
135 state_function(surface, keys, current_time)
136
137
138 def viewport_update(self):
139 """Viewport stays centered on character, unless at edge of map"""
140 self.viewport.center = self.player.rect.center
141 self.viewport.clamp_ip(self.level_rect)
142
143
144 def draw_level(self, surface):
145 """Blits all images to screen"""
146 self.level_surface.blit(self.background, self.viewport, self.viewport)
147 self.level_surface.blit(self.player.image, self.player.rect)
148 self.sprites.draw(self.level_surface)
149 self.level_surface.blit(self.foreground, self.viewport, self.viewport)
150
151 surface.blit(self.level_surface, (0, 0), self.viewport)
152 self.dialogue_handler.draw(surface)
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167