data/components/person.py (view raw)
1from __future__ import division
2import math, random, copy
3import pygame as pg
4from .. import setup
5from .. import constants as c
6
7
8class Person(pg.sprite.Sprite):
9 """Base class for all world characters
10 controlled by the computer"""
11
12 def __init__(self, sheet_key, x, y, direction='down', state='resting', index=0):
13 super(Person, self).__init__()
14 self.name = sheet_key
15 self.get_image = setup.tools.get_image
16 self.spritesheet_dict = self.create_spritesheet_dict(sheet_key)
17 self.animation_dict = self.create_animation_dict()
18 self.index = index
19 self.direction = direction
20 self.image_list = self.animation_dict[self.direction]
21 self.image = self.image_list[self.index]
22 self.rect = self.image.get_rect(left=x, top=y)
23 self.origin_pos = self.rect.topleft
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 self.observers = []
39 self.level = 1
40 self.health = 0
41
42 def create_spritesheet_dict(self, sheet_key):
43 """Implemented by inheriting classes"""
44 image_list = []
45 image_dict = {}
46 sheet = setup.GFX[sheet_key]
47
48 image_keys = ['facing up 1', 'facing up 2',
49 'facing down 1', 'facing down 2',
50 'facing left 1', 'facing left 2',
51 'facing right 1', 'facing right 2']
52
53 for row in range(2):
54 for column in range(4):
55 image_list.append(
56 self.get_image(column*32, row*32, 32, 32, sheet))
57
58 for key, image in zip(image_keys, image_list):
59 image_dict[key] = image
60
61 return image_dict
62
63 def create_animation_dict(self):
64 """Return a dictionary of image lists for animation"""
65 image_dict = self.spritesheet_dict
66
67 left_list = [image_dict['facing left 1'], image_dict['facing left 2']]
68 right_list = [image_dict['facing right 1'], image_dict['facing right 2']]
69 up_list = [image_dict['facing up 1'], image_dict['facing up 2']]
70 down_list = [image_dict['facing down 1'], image_dict['facing down 2']]
71
72 direction_dict = {'left': left_list,
73 'right': right_list,
74 'up': up_list,
75 'down': down_list}
76
77 return direction_dict
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 'battle resting': self.battle_resting,
87 'attack': self.attack,
88 'enemy attack': self.enemy_attack,
89 c.RUN_AWAY: self.run_away,
90 c.VICTORY_DANCE: self.victory_dance}
91
92 return state_dict
93
94 def create_vector_dict(self):
95 """Return a dictionary of x and y velocities set to
96 direction keys."""
97 vector_dict = {'up': (0, -1),
98 'down': (0, 1),
99 'left': (-1, 0),
100 'right': (1, 0)}
101
102 return vector_dict
103
104 def update(self, current_time, *args):
105 """Implemented by inheriting classes"""
106 self.blockers = self.set_blockers()
107 self.current_time = current_time
108 self.image_list = self.animation_dict[self.direction]
109 state_function = self.state_dict[self.state]
110 state_function()
111 self.location = self.get_tile_location()
112
113
114
115 def set_blockers(self):
116 """Sets blockers to prevent collision with other sprites"""
117 blockers = []
118
119 if self.state == 'resting' or self.state == 'autoresting':
120 blockers.append(pg.Rect(self.rect.x, self.rect.y, 32, 32))
121
122 elif self.state == 'moving' or self.state == 'automoving':
123 if self.rect.x % 32 == 0:
124 tile_float = self.rect.y / float(32)
125 tile1 = (self.rect.x, math.ceil(tile_float)*32)
126 tile2 = (self.rect.x, math.floor(tile_float)*32)
127 tile_rect1 = pg.Rect(tile1[0], tile1[1], 32, 32)
128 tile_rect2 = pg.Rect(tile2[0], tile2[1], 32, 32)
129 blockers.extend([tile_rect1, tile_rect2])
130
131 elif self.rect.y % 32 == 0:
132 tile_float = self.rect.x / float(32)
133 tile1 = (math.ceil(tile_float)*32, self.rect.y)
134 tile2 = (math.floor(tile_float)*32, self.rect.y)
135 tile_rect1 = pg.Rect(tile1[0], tile1[1], 32, 32)
136 tile_rect2 = pg.Rect(tile2[0], tile2[1], 32, 32)
137 blockers.extend([tile_rect1, tile_rect2])
138
139 return blockers
140
141 def get_tile_location(self):
142 """
143 Convert pygame coordinates into tile coordinates.
144 """
145 if self.rect.x == 0:
146 tile_x = 0
147 elif self.rect.x % 32 == 0:
148 tile_x = (self.rect.x / 32)
149 else:
150 tile_x = 0
151
152 if self.rect.y == 0:
153 tile_y = 0
154 elif self.rect.y % 32 == 0:
155 tile_y = (self.rect.y / 32)
156 else:
157 tile_y = 0
158
159 return [tile_x, tile_y]
160
161
162 def make_wander_box(self):
163 """
164 Make a list of rects that surround the initial location
165 of a sprite to limit his/her wandering.
166 """
167 x = int(self.location[0])
168 y = int(self.location[1])
169 box_list = []
170 box_rects = []
171
172 for i in range(x-3, x+4):
173 box_list.append([i, y-3])
174 box_list.append([i, y+3])
175
176 for i in range(y-2, y+3):
177 box_list.append([x-3, i])
178 box_list.append([x+3, i])
179
180 for box in box_list:
181 left = box[0]*32
182 top = box[1]*32
183 box_rects.append(pg.Rect(left, top, 32, 32))
184
185 return box_rects
186
187
188 def resting(self):
189 """
190 When the Person is not moving between tiles.
191 Checks if the player is centered on a tile.
192 """
193 self.image = self.image_list[self.index]
194
195 assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
196 + str(self.rect.y) + " : " + str(self.name))
197 assert(self.rect.x % 32 == 0), ('Player not centered on tile'
198 + str(self.rect.x))
199
200 def moving(self):
201 """
202 Increment index and set self.image for animation.
203 """
204 self.animation()
205 assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
206 'Not centered on tile'
207
208 def animated_resting(self):
209 self.animation(500)
210
211 def animation(self, freq=100):
212 """
213 Adjust sprite image frame based on timer.
214 """
215 if (self.current_time - self.timer) > freq:
216 if self.index < (len(self.image_list) - 1):
217 self.index += 1
218 else:
219 self.index = 0
220 self.timer = self.current_time
221
222 self.image = self.image_list[self.index]
223
224 def begin_moving(self, direction):
225 """
226 Transition the player into the 'moving' state.
227 """
228 self.direction = direction
229 self.image_list = self.animation_dict[direction]
230 self.timer = self.current_time
231 self.move_timer = self.current_time
232 self.state = 'moving'
233
234 if self.rect.x % 32 == 0:
235 self.y_vel = self.vector_dict[self.direction][1]
236 if self.rect.y % 32 == 0:
237 self.x_vel = self.vector_dict[self.direction][0]
238
239
240 def begin_resting(self):
241 """
242 Transition the player into the 'resting' state.
243 """
244 self.state = 'resting'
245 self.index = 1
246 self.x_vel = self.y_vel = 0
247
248 def begin_auto_moving(self, direction):
249 """
250 Transition sprite to a automatic moving state.
251 """
252 self.direction = direction
253 self.image_list = self.animation_dict[direction]
254 self.state = 'automoving'
255 self.x_vel = self.vector_dict[direction][0]
256 self.y_vel = self.vector_dict[direction][1]
257 self.move_timer = self.current_time
258
259 def begin_auto_resting(self):
260 """
261 Transition sprite to an automatic resting state.
262 """
263 self.state = 'autoresting'
264 self.index = 1
265 self.x_vel = self.y_vel = 0
266 self.move_timer = self.current_time
267
268
269 def auto_resting(self):
270 """
271 Determine when to move a sprite from resting to moving in a random
272 direction.
273 """
274 #self.image = self.image_list[self.index]
275 self.image_list = self.animation_dict[self.direction]
276 self.image = self.image_list[self.index]
277
278 assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
279 + str(self.rect.y))
280 assert(self.rect.x % 32 == 0), ('Player not centered on tile'
281 + str(self.rect.x))
282
283 if (self.current_time - self.move_timer) > 2000:
284 direction_list = ['up', 'down', 'left', 'right']
285 random.shuffle(direction_list)
286 direction = direction_list[0]
287 self.begin_auto_moving(direction)
288 self.move_timer = self.current_time
289
290 def battle_resting(self):
291 """
292 Player stays still during battle state unless he attacks.
293 """
294 pass
295
296 def enter_attack_state(self, enemy):
297 """
298 Set values for attack state.
299 """
300 self.attacked_enemy = enemy
301 self.x_vel = -5
302 self.state = 'attack'
303
304
305 def attack(self):
306 """
307 Player does an attack animation.
308 """
309 FAST_FORWARD = -5
310 FAST_BACK = 5
311
312 self.rect.x += self.x_vel
313
314 if self.x_vel == FAST_FORWARD:
315 self.image = self.spritesheet_dict['facing left 1']
316 self.image = pg.transform.scale2x(self.image)
317 if self.rect.x <= self.origin_pos[0] - 110:
318 self.x_vel = FAST_BACK
319 self.notify('attack animation')
320 else:
321 if self.rect.x >= self.origin_pos[0]:
322 self.rect.x = self.origin_pos[0]
323 self.x_vel = 0
324 self.state = 'battle resting'
325 self.image = self.spritesheet_dict['facing left 2']
326 self.image = pg.transform.scale2x(self.image)
327 self.notify(c.PLAYER_FINISHED_ATTACK)
328
329 def enter_enemy_attack_state(self):
330 """
331 Set values for enemy attack state.
332 """
333 self.x_vel = -5
334 self.state = 'enemy attack'
335 self.origin_pos = self.rect.topleft
336 self.move_counter = 0
337
338 def enemy_attack(self):
339 """
340 Enemy does an attack animation.
341 """
342 FAST_LEFT = -5
343 FAST_RIGHT = 5
344 STARTX = self.origin_pos[0]
345
346 self.rect.x += self.x_vel
347
348 if self.move_counter == 3:
349 self.x_vel = 0
350 self.state = 'battle resting'
351 self.rect.x = STARTX
352 self.notify(c.ENEMY_ATTACK_DAMAGE)
353
354 elif self.x_vel == FAST_LEFT:
355 if self.rect.x <= (STARTX - 15):
356 self.x_vel = FAST_RIGHT
357 elif self.x_vel == FAST_RIGHT:
358 if self.rect.x >= (STARTX + 15):
359 self.move_counter += 1
360 self.x_vel = FAST_LEFT
361
362 def auto_moving(self):
363 """
364 Animate sprite and check to stop.
365 """
366 self.animation()
367
368 assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
369 'Not centered on tile'
370
371 def notify(self, event):
372 """
373 Notify all observers of events.
374 """
375 for observer in self.observers:
376 observer.on_notify(event)
377
378 def calculate_hit(self):
379 """
380 Calculate hit strength based on attack stats.
381 """
382 max_strength = self.level * 5
383 min_strength = 0
384 return random.randint(min_strength, max_strength)
385
386 def run_away(self):
387 """
388 Run away from battle state.
389 """
390 X_VEL = 5
391 self.rect.x += X_VEL
392 self.direction = 'right'
393 self.small_image_list = self.animation_dict[self.direction]
394 self.image_list = []
395 for image in self.small_image_list:
396 self.image_list.append(pg.transform.scale2x(image))
397 self.animation()
398
399 def victory_dance(self):
400 """
401 Post Victory Dance.
402 """
403 self.small_image_list = self.animation_dict[self.direction]
404 self.image_list = []
405 for image in self.small_image_list:
406 self.image_list.append(pg.transform.scale2x(image))
407 self.animation(500)
408
409
410
411class Player(Person):
412 """
413 User controlled character.
414 """
415
416 def __init__(self, direction, x=0, y=0, state='resting', index=0):
417 super(Player, self).__init__('player', x, y, direction, state, index)
418 self.damaged = False
419 self.healing = False
420 self.damage_alpha = 0
421 self.healing_alpha = 0
422 self.fade_in = True
423
424 def create_vector_dict(self):
425 """Return a dictionary of x and y velocities set to
426 direction keys."""
427 vector_dict = {'up': (0, -2),
428 'down': (0, 2),
429 'left': (-2, 0),
430 'right': (2, 0)}
431
432 return vector_dict
433
434 def update(self, keys, current_time):
435 """Updates player behavior"""
436 self.current_time = current_time
437 self.damage_animation()
438 self.healing_animation()
439 self.blockers = self.set_blockers()
440 self.keys = keys
441 self.check_for_input()
442 state_function = self.state_dict[self.state]
443 state_function()
444 self.location = self.get_tile_location()
445
446 def damage_animation(self):
447 """
448 Put a red overlay over sprite to indicate damage.
449 """
450 if self.damaged:
451 self.image = copy.copy(self.spritesheet_dict['facing left 2'])
452 self.image = pg.transform.scale2x(self.image).convert_alpha()
453 damage_image = copy.copy(self.image).convert_alpha()
454 damage_image.fill((255, 0, 0, self.damage_alpha), special_flags=pg.BLEND_RGBA_MULT)
455 self.image.blit(damage_image, (0, 0))
456 if self.fade_in:
457 self.damage_alpha += 25
458 if self.damage_alpha >= 255:
459 self.fade_in = False
460 self.damage_alpha = 255
461 elif not self.fade_in:
462 self.damage_alpha -= 25
463 if self.damage_alpha <= 0:
464 self.damage_alpha = 0
465 self.damaged = False
466 self.fade_in = True
467 self.image = self.spritesheet_dict['facing left 2']
468 self.image = pg.transform.scale2x(self.image)
469
470 def healing_animation(self):
471 """
472 Put a green overlay over sprite to indicate healing.
473 """
474 if self.healing:
475 self.image = copy.copy(self.spritesheet_dict['facing left 2'])
476 self.image = pg.transform.scale2x(self.image).convert_alpha()
477 healing_image = copy.copy(self.image).convert_alpha()
478 healing_image.fill((0, 255, 0, self.healing_alpha), special_flags=pg.BLEND_RGBA_MULT)
479 self.image.blit(healing_image, (0, 0))
480 if self.fade_in:
481 self.healing_alpha += 25
482 if self.healing_alpha >= 255:
483 self.fade_in = False
484 self.healing_alpha = 255
485 elif not self.fade_in:
486 self.healing_alpha -= 25
487 if self.healing_alpha <= 0:
488 self.healing_alpha = 0
489 self.healing = False
490 self.fade_in = True
491 self.image = self.spritesheet_dict['facing left 2']
492 self.image = pg.transform.scale2x(self.image)
493
494
495
496 def check_for_input(self):
497 """Checks for player input"""
498 if self.state == 'resting':
499 if self.keys[pg.K_UP]:
500 self.begin_moving('up')
501 elif self.keys[pg.K_DOWN]:
502 self.begin_moving('down')
503 elif self.keys[pg.K_LEFT]:
504 self.begin_moving('left')
505 elif self.keys[pg.K_RIGHT]:
506 self.begin_moving('right')
507
508 def calculate_hit(self):
509 """
510 Calculate hit strength based on attack stats.
511 """
512 max_strength = 5 + (self.level * 5)
513 min_strength = max_strength // 2
514 return random.randint(min_strength, max_strength)
515
516
517
518
519class Well(pg.sprite.Sprite):
520 """Talking well"""
521 def __init__(self, x, y):
522 super(Well, self).__init__()
523 self.image = pg.Surface((32, 32))
524 self.image.set_colorkey((0,0,0))
525 self.rect = self.image.get_rect(left=x, top=y)
526 self.location = self.get_location()
527 self.dialogue = ["I'm a well!"]
528 self.blockers = [self.rect]
529 self.x_vel = self.y_vel = 0
530 self.state = 'resting'
531 self.direction = 'down'
532 self.default_direction = self.direction
533 self.item = None
534 self.wander_box = []
535
536 def get_location(self):
537 """Get tile location"""
538 x = self.rect.x / 32
539 y = self.rect.y / 32
540
541 return [x, y]
542
543 def begin_auto_resting(self):
544 """Placeholder"""
545 pass
546
547