data/components/person.py (view raw)
1import math, random
2import pygame as pg
3from .. import setup
4
5
6class Person(pg.sprite.Sprite):
7 """Base class for all world characters
8 controlled by the computer"""
9
10 def __init__(self, sheet_key, x, y, direction='right', state='resting'):
11 super(Person, self).__init__()
12 self.name = sheet_key
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 if direction == 'left':
17 self.index = 1
18 else:
19 self.index = 0
20 self.direction = direction
21 self.image_list = self.animation_dict[self.direction]
22 self.image = self.image_list[self.index]
23 self.rect = self.image.get_rect(left=x, top=y)
24 self.state_dict = self.create_state_dict()
25 self.vector_dict = self.create_vector_dict()
26 self.x_vel = 0
27 self.y_vel = 0
28 self.timer = 0.0
29 self.move_timer = 0.0
30 self.current_time = 0.0
31 self.state = state
32 self.blockers = self.set_blockers()
33 self.location = self.get_tile_location()
34 self.dialogue = ['Location: ' + str(self.location)]
35 self.default_direction = direction
36 self.item = None
37 self.wander_box = self.make_wander_box()
38
39
40 def create_spritesheet_dict(self, sheet_key):
41 """Implemented by inheriting classes"""
42 image_list = []
43 image_dict = {}
44 sheet = setup.GFX[sheet_key]
45
46 image_keys = ['facing up 1', 'facing up 2',
47 'facing down 1', 'facing down 2',
48 'facing left 1', 'facing left 2',
49 'facing right 1', 'facing right 2']
50
51 for row in range(2):
52 for column in range(4):
53 image_list.append(
54 self.get_image(column*32, row*32, 32, 32, sheet))
55
56 for key, image in zip(image_keys, image_list):
57 image_dict[key] = image
58
59 return image_dict
60
61
62 def create_animation_dict(self):
63 """Return a dictionary of image lists for animation"""
64 image_dict = self.spritesheet_dict
65
66 left_list = [image_dict['facing left 1'], image_dict['facing left 2']]
67 right_list = [image_dict['facing right 1'], image_dict['facing right 2']]
68 up_list = [image_dict['facing up 1'], image_dict['facing up 2']]
69 down_list = [image_dict['facing down 1'], image_dict['facing down 2']]
70
71 direction_dict = {'left': left_list,
72 'right': right_list,
73 'up': up_list,
74 'down': down_list}
75
76 return direction_dict
77
78
79 def create_state_dict(self):
80 """Return a dictionary of all state methods"""
81 state_dict = {'resting': self.resting,
82 'moving': self.moving,
83 'animated resting': self.animated_resting,
84 'autoresting': self.auto_resting,
85 'automoving': self.auto_moving}
86
87 return state_dict
88
89
90 def create_vector_dict(self):
91 """Return a dictionary of x and y velocities set to
92 direction keys."""
93 vector_dict = {'up': (0, -1),
94 'down': (0, 1),
95 'left': (-1, 0),
96 'right': (1, 0)}
97
98 return vector_dict
99
100
101 def update(self, current_time, *args):
102 """Implemented by inheriting classes"""
103 self.blockers = self.set_blockers()
104 self.current_time = current_time
105 self.image_list = self.animation_dict[self.direction]
106 state_function = self.state_dict[self.state]
107 state_function()
108 self.location = self.get_tile_location()
109
110
111
112 def set_blockers(self):
113 """Sets blockers to prevent collision with other sprites"""
114 blockers = []
115
116 if self.state == 'resting' or self.state == 'autoresting':
117 blockers.append(pg.Rect(self.rect.x, self.rect.y, 32, 32))
118
119 elif self.state == 'moving' or self.state == 'automoving':
120 if self.rect.x % 32 == 0:
121 tile_float = self.rect.y / float(32)
122 tile1 = (self.rect.x, math.ceil(tile_float)*32)
123 tile2 = (self.rect.x, math.floor(tile_float)*32)
124 tile_rect1 = pg.Rect(tile1[0], tile1[1], 32, 32)
125 tile_rect2 = pg.Rect(tile2[0], tile2[1], 32, 32)
126 blockers.extend([tile_rect1, tile_rect2])
127
128 elif self.rect.y % 32 == 0:
129 tile_float = self.rect.x / float(32)
130 tile1 = (math.ceil(tile_float)*32, self.rect.y)
131 tile2 = (math.floor(tile_float)*32, self.rect.y)
132 tile_rect1 = pg.Rect(tile1[0], tile1[1], 32, 32)
133 tile_rect2 = pg.Rect(tile2[0], tile2[1], 32, 32)
134 blockers.extend([tile_rect1, tile_rect2])
135
136 return blockers
137
138
139
140 def get_tile_location(self):
141 """Converts pygame coordinates into tile coordinates"""
142 if self.rect.x == 0:
143 tile_x = 0
144 elif self.rect.x % 32 == 0:
145 tile_x = (self.rect.x / 32)
146 else:
147 tile_x = 0
148
149
150 if self.rect.y == 0:
151 tile_y = 0
152 elif self.rect.y % 32 == 0:
153 tile_y = (self.rect.y / 32)
154
155 else:
156 tile_y = 0
157
158 return [tile_x, tile_y]
159
160
161 def make_wander_box(self):
162 """Make a list of rects that surround the initial location
163 of a sprite to limit his/her wandering"""
164 x = self.location[0]
165 y = self.location[1]
166 box_list = []
167 box_rects = []
168
169 for i in range(x-3, x+4):
170 box_list.append([i, y-3])
171 box_list.append([i, y+3])
172
173 for i in range(y-2, y+3):
174 box_list.append([x-3, i])
175 box_list.append([x+3, i])
176
177 for box in box_list:
178 left = box[0]*32
179 top = box[1]*32
180 box_rects.append(pg.Rect(left, top, 32, 32))
181
182 return box_rects
183
184
185 def resting(self):
186 """
187 When the Person is not moving between tiles.
188 Checks if the player is centered on a tile.
189 """
190 self.image = self.image_list[self.index]
191
192 assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
193 + str(self.rect.y))
194 assert(self.rect.x % 32 == 0), ('Player not centered on tile'
195 + str(self.rect.x))
196
197
198 def moving(self):
199 """Increment index and set self.image for animation."""
200 self.animation()
201
202 assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
203 'Not centered on tile'
204
205
206 def animated_resting(self):
207 self.animation(500)
208
209
210 def animation(self, freq=100):
211 """Adjust sprite image frame based on timer"""
212 if (self.current_time - self.timer) > freq:
213 if self.index < (len(self.image_list) - 1):
214 self.index += 1
215 else:
216 self.index = 0
217 self.timer = self.current_time
218
219 self.image = self.image_list[self.index]
220
221
222
223 def begin_moving(self, direction):
224 """Transition the player into the 'moving' state."""
225 self.direction = direction
226 self.image_list = self.animation_dict[direction]
227 self.timer = self.current_time
228 self.move_timer = self.current_time
229 self.state = 'moving'
230
231 if self.rect.x % 32 == 0:
232 self.y_vel = self.vector_dict[self.direction][1]
233 if self.rect.y % 32 == 0:
234 self.x_vel = self.vector_dict[self.direction][0]
235
236
237 def begin_resting(self):
238 """Transition the player into the 'resting' state."""
239 self.state = 'resting'
240 self.index = 1
241 self.x_vel = self.y_vel = 0
242
243
244 def begin_auto_moving(self, direction):
245 """Transition sprite to a automatic moving state"""
246 self.direction = direction
247 self.image_list = self.animation_dict[direction]
248 self.state = 'automoving'
249 self.x_vel = self.vector_dict[direction][0]
250 self.y_vel = self.vector_dict[direction][1]
251 self.move_timer = self.current_time
252
253
254 def begin_auto_resting(self):
255 """Transition sprite to an automatic resting state"""
256 self.state = 'autoresting'
257 self.index = 1
258 self.x_vel = self.y_vel = 0
259 self.move_timer = self.current_time
260
261
262 def auto_resting(self):
263 """
264 Determine when to move a sprite from resting to moving in a random
265 direction.
266 """
267 #self.image = self.image_list[self.index]
268 self.image_list = self.animation_dict[self.direction]
269 self.image = self.image_list[self.index]
270
271 assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
272 + str(self.rect.y))
273 assert(self.rect.x % 32 == 0), ('Player not centered on tile'
274 + str(self.rect.x))
275
276 if (self.current_time - self.move_timer) > 2000:
277 direction_list = ['up', 'down', 'left', 'right']
278 random.shuffle(direction_list)
279 direction = direction_list[0]
280 self.begin_auto_moving(direction)
281 self.move_timer = self.current_time
282
283
284
285 def auto_moving(self):
286 """Animate sprite and check to stop"""
287 self.animation()
288
289 assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
290 'Not centered on tile'
291
292
293
294class Player(Person):
295 """User controlled character"""
296
297 def __init__(self, direction, x=0, y=0):
298 super(Player, self).__init__('player', x, y, direction)
299
300
301 def create_vector_dict(self):
302 """Return a dictionary of x and y velocities set to
303 direction keys."""
304 vector_dict = {'up': (0, -2),
305 'down': (0, 2),
306 'left': (-2, 0),
307 'right': (2, 0)}
308
309 return vector_dict
310
311
312 def update(self, keys, current_time):
313 """Updates player behavior"""
314 self.blockers = self.set_blockers()
315 self.keys = keys
316 self.current_time = current_time
317 self.check_for_input()
318 state_function = self.state_dict[self.state]
319 state_function()
320 self.location = self.get_tile_location()
321
322
323 def check_for_input(self):
324 """Checks for player input"""
325 if self.state == 'resting':
326 if self.keys[pg.K_UP]:
327 self.begin_moving('up')
328 elif self.keys[pg.K_DOWN]:
329 self.begin_moving('down')
330 elif self.keys[pg.K_LEFT]:
331 self.begin_moving('left')
332 elif self.keys[pg.K_RIGHT]:
333 self.begin_moving('right')
334
335
336
337class Soldier(Person):
338 """Soldier for the castle"""
339
340 def __init__(self, x, y, direction='down', state='resting'):
341 super(Soldier, self).__init__('soldier', x, y, direction, state)
342
343
344
345class FemaleVillager(Person):
346 """Female Person for town"""
347
348 def __init__(self, x, y):
349 super(FemaleVillager, self).__init__('femalevillager', x, y)
350 self.index = 1
351
352
353class FemaleVillager2(Person):
354 """A second female person for town"""
355 def __init__(self, x, y):
356 super(FemaleVillager2, self).__init__('femvillager2', x, y)
357 self.index = 1
358
359
360class MaleVillager(Person):
361 """Male Person for town"""
362
363 def __init__(self):
364 super(MaleVillager, self).__init__('male villager', x, y)
365
366
367class King(Person):
368 """King of the town"""
369 def __init__(self, x, y, direction='down', state='resting'):
370 super(King, self).__init__('king', x, y, direction, state)
371
372
373class Devil(Person):
374 """Devil-like villager"""
375 def __init__(self, x, y, direction='down', state='autoresting'):
376 super(Devil, self).__init__('devil', x, y, direction, state)
377
378
379class OldMan(Person):
380 """Old man villager"""
381 def __init__(self, x, y, direction='down', state='resting'):
382 super(OldMan, self).__init__('oldman', x, y, direction, state)
383
384
385class Well(pg.sprite.Sprite):
386 """Talking well"""
387 def __init__(self, x, y):
388 super(Well, self).__init__()
389 self.image = pg.Surface((32, 32))
390 self.image.set_colorkey((0,0,0))
391 self.rect = self.image.get_rect(left=x, top=y)
392 self.location = self.get_location()
393 self.dialogue = ["I'm a well!"]
394 self.blockers = [self.rect]
395 self.x_vel = self.y_vel = 0
396 self.state = 'resting'
397 self.direction = 'down'
398 self.default_direction = self.direction
399 self.item = None
400 self.wander_box = []
401
402 def get_location(self):
403 """Get tile location"""
404 x = self.rect.x / 32
405 y = self.rect.y / 32
406
407 return [x, y]
408
409 def begin_auto_resting(self):
410 """Placeholder"""
411 pass
412
413