data/components/person.py (view raw)
1__author__ = 'justinarmstrong'
2import math
3import pygame as pg
4from .. import setup
5
6
7class Person(pg.sprite.Sprite):
8 """Base class for all world characters
9 controlled by the computer"""
10
11 def __init__(self, sheet_key, x, y, direction='down'):
12 super(Person, self).__init__()
13 self.get_image = setup.tools.get_image
14 self.spritesheet_dict = self.create_spritesheet_dict(sheet_key)
15 self.animation_dict = self.create_animation_dict()
16 self.index = 0
17 self.direction = direction
18 self.image_list = self.animation_dict[self.direction]
19 self.image = self.image_list[self.index]
20 self.rect = self.image.get_rect(left=x, top=y)
21 self.old_rect = self.rect
22 self.state_dict = self.create_state_dict()
23 self.vector_dict = self.create_vector_dict()
24 self.x_vel = 0
25 self.y_vel = 0
26 self.timer = 0.0
27 self.current_time = 0.0
28 self.state = 'resting'
29 self.blockers = self.set_blockers()
30 self.location = self.get_tile_location()
31
32
33 def create_spritesheet_dict(self, sheet_key):
34 """Implemented by inheriting classes"""
35 image_list = []
36 image_dict = {}
37 sheet = setup.GFX[sheet_key]
38
39 image_keys = ['facing up 1', 'facing up 2',
40 'facing down 1', 'facing down 2',
41 'facing left 1', 'facing left 2',
42 'facing right 1', 'facing right 2']
43
44 for row in range(2):
45 for column in range(4):
46 image_list.append(
47 self.get_image(column*32, row*32, 32, 32, sheet))
48
49 for key, image in zip(image_keys, image_list):
50 image_dict[key] = image
51
52 return image_dict
53
54
55 def create_animation_dict(self):
56 """Return a dictionary of image lists for animation"""
57 image_dict = self.spritesheet_dict
58
59 left_list = [image_dict['facing left 1'], image_dict['facing left 2']]
60 right_list = [image_dict['facing right 1'], image_dict['facing right 2']]
61 up_list = [image_dict['facing up 1'], image_dict['facing up 2']]
62 down_list = [image_dict['facing down 1'], image_dict['facing down 2']]
63
64 direction_dict = {'left': left_list,
65 'right': right_list,
66 'up': up_list,
67 'down': down_list}
68
69 return direction_dict
70
71
72 def create_state_dict(self):
73 """Return a dictionary of all state methods"""
74 state_dict = {'resting': self.resting,
75 'moving': self.moving}
76
77 return state_dict
78
79
80 def create_vector_dict(self):
81 """Return a dictionary of x and y velocities set to
82 direction keys."""
83 vector_dict = {'up': (0, -2),
84 'down': (0, 2),
85 'left': (-2, 0),
86 'right': (2, 0)}
87
88 return vector_dict
89
90
91 def update(self, keys, current_time):
92 """Implemented by inheriting classes"""
93 self.blockers = self.set_blockers()
94 self.current_time = current_time
95 self.check_for_input()
96 state_function = self.state_dict[self.state]
97 state_function()
98 self.location = self.get_tile_location()
99
100
101 def set_blockers(self):
102 """Sets blockers to prevent collision with other sprites"""
103 blockers = []
104
105 if self.state == 'resting':
106 blockers.append(pg.Rect(self.rect.x, self.rect.y, 32, 32))
107
108 elif self.state == 'moving':
109 if self.rect.x % 32 == 0:
110 tile_float = self.rect.y / float(32)
111 tile1 = (self.rect.x, math.ceil(tile_float)*32)
112 tile2 = (self.rect.x, math.floor(tile_float)*32)
113 tile_rect1 = pg.Rect(tile1[0], tile1[1], 32, 32)
114 tile_rect2 = pg.Rect(tile2[0], tile2[1], 32, 32)
115 blockers.extend([tile_rect1, tile_rect2])
116
117 elif self.rect.y % 32 == 0:
118 tile_float = self.rect.x / float(32)
119 tile1 = (math.ceil(tile_float)*32, self.rect.y)
120 tile2 = (math.floor(tile_float)*32, self.rect.y)
121 tile_rect1 = pg.Rect(tile1[0], tile1[1], 32, 32)
122 tile_rect2 = pg.Rect(tile2[0], tile2[1], 32, 32)
123 blockers.extend([tile_rect1, tile_rect2])
124
125 return blockers
126
127
128 def get_tile_location(self):
129 """Converts pygame coordinates into tile coordinates"""
130 if self.rect.x == 0:
131 tile_x = 1
132 elif self.rect.x % 32 == 0:
133 tile_x = (self.rect.x / 32) + 1
134 else:
135 tile_x = 0
136
137 if self.rect.y == 0:
138 tile_y = 1
139 elif self.rect.y % 32 == 0:
140 tile_y = (self.rect.y / 32) + 1
141 else:
142 tile_y = 0
143
144 return (tile_x, tile_y)
145
146
147 def resting(self):
148 """
149 When the Person is not moving between tiles.
150 Checks if the player is centered on a tile.
151 """
152 self.image = self.image_list[self.index]
153
154 assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
155 + str(self.rect.y))
156 assert(self.rect.x % 32 == 0), ('Player not centered on tile'
157 + str(self.rect.x))
158
159
160 def moving(self):
161 """Increment index and set self.image for animation."""
162 self.animation()
163
164 assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
165 'Not centered on tile'
166
167
168 def animation(self):
169 """Adjust sprite image frame based on timer"""
170 if (self.current_time - self.timer) > 100:
171 if self.index < (len(self.image_list) - 1):
172 self.index += 1
173 else:
174 self.index = 0
175 self.timer = self.current_time
176
177 self.image = self.image_list[self.index]
178
179
180
181 def begin_moving(self, direction):
182 """Transition the player into the 'moving' state."""
183 self.direction = direction
184 self.image_list = self.animation_dict[direction]
185 self.timer = self.current_time
186 self.state = 'moving'
187 self.old_rect = self.rect
188
189 if self.rect.x % 32 == 0:
190 self.y_vel = self.vector_dict[self.direction][1]
191 if self.rect.y % 32 == 0:
192 self.x_vel = self.vector_dict[self.direction][0]
193
194
195 def begin_resting(self):
196 """Transition the player into the 'resting' state."""
197 self.state = 'resting'
198 self.index = 1
199 self.x_vel = self.y_vel = 0
200
201
202class Player(Person):
203 """User controlled character"""
204
205 def __init__(self, direction):
206 super(Player, self).__init__('player', 0, 0, direction)
207
208
209 def update(self, keys, current_time):
210 """Updates player behavior"""
211 self.blockers = self.set_blockers()
212 self.keys = keys
213 self.current_time = current_time
214 self.check_for_input()
215 state_function = self.state_dict[self.state]
216 state_function()
217 self.location = self.get_tile_location()
218
219
220 def check_for_input(self):
221 """Checks for player input"""
222 if self.state == 'resting':
223 if self.keys[pg.K_UP]:
224 self.begin_moving('up')
225 elif self.keys[pg.K_DOWN]:
226 self.begin_moving('down')
227 elif self.keys[pg.K_LEFT]:
228 self.begin_moving('left')
229 elif self.keys[pg.K_RIGHT]:
230 self.begin_moving('right')
231
232
233
234class Soldier(Person):
235 """Soldier for the castle"""
236
237 def __init__(self, x, y):
238 super(Soldier, self).__init__('soldier', x, y)
239 self.dialogue = 'Welcome to the castle, citizen.'
240
241
242
243class FemaleVillager(Person):
244 """Female Person for town"""
245
246 def __init__(self, x, y):
247 super(FemaleVillager, self).__init__('femalevillager', x, y)
248 self.dialogue = 'Hey there, Mr. Traveller. What brings you to our town?'
249
250
251class MaleVillager(Person):
252 """Male Person for town"""
253
254 def __init__(self):
255 super(MaleVillager, self).__init__('male villager', x, y)
256 self.dialogue = 'Good morrow to you, Sir.'
257