data/components/person.py (view raw)
1from __future__ import division
2import math, random, copy
3import pygame as pg
4from .. import setup, observer
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.alpha = 255
15 self.name = sheet_key
16 self.get_image = setup.tools.get_image
17 self.spritesheet_dict = self.create_spritesheet_dict(sheet_key)
18 self.animation_dict = self.create_animation_dict()
19 self.index = index
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.origin_pos = self.rect.topleft
25 self.state_dict = self.create_state_dict()
26 self.vector_dict = self.create_vector_dict()
27 self.x_vel = 0
28 self.y_vel = 0
29 self.timer = 0.0
30 self.move_timer = 0.0
31 self.current_time = 0.0
32 self.state = state
33 self.blockers = self.set_blockers()
34 self.location = self.get_tile_location()
35 self.dialogue = ['Location: ' + str(self.location)]
36 self.default_direction = direction
37 self.item = None
38 self.wander_box = self.make_wander_box()
39 self.observers = [observer.SoundEffects()]
40 self.health = 0
41 self.death_image = pg.transform.scale2x(self.image)
42 self.battle = None
43
44 def create_spritesheet_dict(self, sheet_key):
45 """Implemented by inheriting classes"""
46 image_list = []
47 image_dict = {}
48 sheet = setup.GFX[sheet_key]
49
50 image_keys = ['facing up 1', 'facing up 2',
51 'facing down 1', 'facing down 2',
52 'facing left 1', 'facing left 2',
53 'facing right 1', 'facing right 2']
54
55 for row in range(2):
56 for column in range(4):
57 image_list.append(
58 self.get_image(column*32, row*32, 32, 32, sheet))
59
60 for key, image in zip(image_keys, image_list):
61 image_dict[key] = image
62
63 return image_dict
64
65 def create_animation_dict(self):
66 """Return a dictionary of image lists for animation"""
67 image_dict = self.spritesheet_dict
68
69 left_list = [image_dict['facing left 1'], image_dict['facing left 2']]
70 right_list = [image_dict['facing right 1'], image_dict['facing right 2']]
71 up_list = [image_dict['facing up 1'], image_dict['facing up 2']]
72 down_list = [image_dict['facing down 1'], image_dict['facing down 2']]
73
74 direction_dict = {'left': left_list,
75 'right': right_list,
76 'up': up_list,
77 'down': down_list}
78
79 return direction_dict
80
81 def create_state_dict(self):
82 """Return a dictionary of all state methods"""
83 state_dict = {'resting': self.resting,
84 'moving': self.moving,
85 'animated resting': self.animated_resting,
86 'autoresting': self.auto_resting,
87 'automoving': self.auto_moving,
88 'battle resting': self.battle_resting,
89 'attack': self.attack,
90 'enemy attack': self.enemy_attack,
91 c.RUN_AWAY: self.run_away,
92 c.VICTORY_DANCE: self.victory_dance,
93 c.KNOCK_BACK: self.knock_back,
94 c.FADE_DEATH: self.fade_death}
95
96 return state_dict
97
98 def create_vector_dict(self):
99 """Return a dictionary of x and y velocities set to
100 direction keys."""
101 vector_dict = {'up': (0, -1),
102 'down': (0, 1),
103 'left': (-1, 0),
104 'right': (1, 0)}
105
106 return vector_dict
107
108 def update(self, current_time, *args):
109 """Implemented by inheriting classes"""
110 self.blockers = self.set_blockers()
111 self.current_time = current_time
112 self.image_list = self.animation_dict[self.direction]
113 state_function = self.state_dict[self.state]
114 state_function()
115 self.location = self.get_tile_location()
116
117 def set_blockers(self):
118 """Sets blockers to prevent collision with other sprites"""
119 blockers = []
120
121 if self.state == 'resting' or self.state == 'autoresting':
122 blockers.append(pg.Rect(self.rect.x, self.rect.y, 32, 32))
123
124 elif self.state == 'moving' or self.state == 'automoving':
125 if self.rect.x % 32 == 0:
126 tile_float = self.rect.y / float(32)
127 tile1 = (self.rect.x, math.ceil(tile_float)*32)
128 tile2 = (self.rect.x, math.floor(tile_float)*32)
129 tile_rect1 = pg.Rect(tile1[0], tile1[1], 32, 32)
130 tile_rect2 = pg.Rect(tile2[0], tile2[1], 32, 32)
131 blockers.extend([tile_rect1, tile_rect2])
132
133 elif self.rect.y % 32 == 0:
134 tile_float = self.rect.x / float(32)
135 tile1 = (math.ceil(tile_float)*32, self.rect.y)
136 tile2 = (math.floor(tile_float)*32, self.rect.y)
137 tile_rect1 = pg.Rect(tile1[0], tile1[1], 32, 32)
138 tile_rect2 = pg.Rect(tile2[0], tile2[1], 32, 32)
139 blockers.extend([tile_rect1, tile_rect2])
140
141 return blockers
142
143 def get_tile_location(self):
144 """
145 Convert pygame coordinates into tile coordinates.
146 """
147 if self.rect.x == 0:
148 tile_x = 0
149 elif self.rect.x % 32 == 0:
150 tile_x = (self.rect.x / 32)
151 else:
152 tile_x = 0
153
154 if self.rect.y == 0:
155 tile_y = 0
156 elif self.rect.y % 32 == 0:
157 tile_y = (self.rect.y / 32)
158 else:
159 tile_y = 0
160
161 return [tile_x, tile_y]
162
163
164 def make_wander_box(self):
165 """
166 Make a list of rects that surround the initial location
167 of a sprite to limit his/her wandering.
168 """
169 x = int(self.location[0])
170 y = int(self.location[1])
171 box_list = []
172 box_rects = []
173
174 for i in range(x-3, x+4):
175 box_list.append([i, y-3])
176 box_list.append([i, y+3])
177
178 for i in range(y-2, y+3):
179 box_list.append([x-3, i])
180 box_list.append([x+3, i])
181
182 for box in box_list:
183 left = box[0]*32
184 top = box[1]*32
185 box_rects.append(pg.Rect(left, top, 32, 32))
186
187 return box_rects
188
189
190 def resting(self):
191 """
192 When the Person is not moving between tiles.
193 Checks if the player is centered on a tile.
194 """
195 self.image = self.image_list[self.index]
196
197 assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
198 + str(self.rect.y) + " : " + str(self.name))
199 assert(self.rect.x % 32 == 0), ('Player not centered on tile'
200 + str(self.rect.x))
201
202 def moving(self):
203 """
204 Increment index and set self.image for animation.
205 """
206 self.animation()
207 assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
208 'Not centered on tile'
209
210 def animated_resting(self):
211 self.animation(500)
212
213 def animation(self, freq=100):
214 """
215 Adjust sprite image frame based on timer.
216 """
217 if (self.current_time - self.timer) > freq:
218 if self.index < (len(self.image_list) - 1):
219 self.index += 1
220 else:
221 self.index = 0
222 self.timer = self.current_time
223
224 self.image = self.image_list[self.index]
225
226 def begin_moving(self, direction):
227 """
228 Transition the player into the 'moving' state.
229 """
230 self.direction = direction
231 self.image_list = self.animation_dict[direction]
232 self.timer = self.current_time
233 self.move_timer = self.current_time
234 self.state = 'moving'
235
236 if self.rect.x % 32 == 0:
237 self.y_vel = self.vector_dict[self.direction][1]
238 if self.rect.y % 32 == 0:
239 self.x_vel = self.vector_dict[self.direction][0]
240
241
242 def begin_resting(self):
243 """
244 Transition the player into the 'resting' state.
245 """
246 self.state = 'resting'
247 self.index = 1
248 self.x_vel = self.y_vel = 0
249
250 def begin_auto_moving(self, direction):
251 """
252 Transition sprite to a automatic moving state.
253 """
254 self.direction = direction
255 self.image_list = self.animation_dict[direction]
256 self.state = 'automoving'
257 self.x_vel = self.vector_dict[direction][0]
258 self.y_vel = self.vector_dict[direction][1]
259 self.move_timer = self.current_time
260
261 def begin_auto_resting(self):
262 """
263 Transition sprite to an automatic resting state.
264 """
265 self.state = 'autoresting'
266 self.index = 1
267 self.x_vel = self.y_vel = 0
268 self.move_timer = self.current_time
269
270
271 def auto_resting(self):
272 """
273 Determine when to move a sprite from resting to moving in a random
274 direction.
275 """
276 #self.image = self.image_list[self.index]
277 self.image_list = self.animation_dict[self.direction]
278 self.image = self.image_list[self.index]
279
280 if self.rect.y % 32 != 0:
281 self.correct_position(self.rect.y)
282 if self.rect.x % 32 != 0:
283 self.correct_position(self.rect.x)
284
285 if (self.current_time - self.move_timer) > 2000:
286 direction_list = ['up', 'down', 'left', 'right']
287 random.shuffle(direction_list)
288 direction = direction_list[0]
289 self.begin_auto_moving(direction)
290 self.move_timer = self.current_time
291
292 def correct_position(self, rect_pos):
293 """
294 Adjust sprite position to be centered on tile.
295 """
296 diff = rect_pos % 32
297 if diff <= 16:
298 rect_pos - diff
299 else:
300 rect_pos + diff
301
302
303 def battle_resting(self):
304 """
305 Player stays still during battle state unless he attacks.
306 """
307 pass
308
309 def enter_attack_state(self, enemy):
310 """
311 Set values for attack state.
312 """
313 self.notify(c.SWORD)
314 self.attacked_enemy = enemy
315 self.x_vel = -5
316 self.state = 'attack'
317
318
319 def attack(self):
320 """
321 Player does an attack animation.
322 """
323 FAST_FORWARD = -5
324 FAST_BACK = 5
325
326 self.rect.x += self.x_vel
327
328 if self.x_vel == FAST_FORWARD:
329 self.image = self.spritesheet_dict['facing left 1']
330 self.image = pg.transform.scale2x(self.image)
331 if self.rect.x <= self.origin_pos[0] - 110:
332 self.x_vel = FAST_BACK
333 self.notify(c.ENEMY_DAMAGED)
334 else:
335 if self.rect.x >= self.origin_pos[0]:
336 self.rect.x = self.origin_pos[0]
337 self.x_vel = 0
338 self.state = 'battle resting'
339 self.image = self.spritesheet_dict['facing left 2']
340 self.image = pg.transform.scale2x(self.image)
341 self.notify(c.PLAYER_FINISHED_ATTACK)
342
343 def enter_enemy_attack_state(self):
344 """
345 Set values for enemy attack state.
346 """
347 self.x_vel = -5
348 self.state = 'enemy attack'
349 self.origin_pos = self.rect.topleft
350 self.move_counter = 0
351
352 def enemy_attack(self):
353 """
354 Enemy does an attack animation.
355 """
356 FAST_LEFT = -5
357 FAST_RIGHT = 5
358 STARTX = self.origin_pos[0]
359
360 self.rect.x += self.x_vel
361
362 if self.move_counter == 3:
363 self.x_vel = 0
364 self.state = 'battle resting'
365 self.rect.x = STARTX
366 self.notify(c.PLAYER_DAMAGED)
367
368 elif self.x_vel == FAST_LEFT:
369 if self.rect.x <= (STARTX - 15):
370 self.x_vel = FAST_RIGHT
371 elif self.x_vel == FAST_RIGHT:
372 if self.rect.x >= (STARTX + 15):
373 self.move_counter += 1
374 self.x_vel = FAST_LEFT
375
376 def auto_moving(self):
377 """
378 Animate sprite and check to stop.
379 """
380 self.animation()
381
382 assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
383 'Not centered on tile'
384
385 def notify(self, event):
386 """
387 Notify all observers of events.
388 """
389 for observer in self.observers:
390 observer.on_notify(event)
391
392 def calculate_hit(self, armor_list, inventory):
393 """
394 Calculate hit strength based on attack stats.
395 """
396 armor_power = 0
397 for armor in armor_list:
398 armor_power += inventory[armor]['power']
399 max_strength = max(1, (self.level * 5) - armor_power)
400 min_strength = 0
401 return random.randint(min_strength, max_strength)
402
403 def run_away(self):
404 """
405 Run away from battle state.
406 """
407 X_VEL = 5
408 self.rect.x += X_VEL
409 self.direction = 'right'
410 self.small_image_list = self.animation_dict[self.direction]
411 self.image_list = []
412 for image in self.small_image_list:
413 self.image_list.append(pg.transform.scale2x(image))
414 self.animation()
415
416 def victory_dance(self):
417 """
418 Post Victory Dance.
419 """
420 self.small_image_list = self.animation_dict[self.direction]
421 self.image_list = []
422 for image in self.small_image_list:
423 self.image_list.append(pg.transform.scale2x(image))
424 self.animation(500)
425
426 def knock_back(self):
427 """
428 Knock back when hit.
429 """
430 FORWARD_VEL = -2
431
432 self.rect.x += self.x_vel
433
434 if self.name == 'player':
435 if self.rect.x >= (self.origin_pos[0] + 10):
436 self.x_vel = FORWARD_VEL
437 elif self.rect.x <= self.origin_pos[0]:
438 self.rect.x = self.origin_pos[0]
439 self.state = 'battle resting'
440 self.x_vel = 0
441 else:
442 if self.rect.x <= (self.origin_pos[0] - 10):
443 self.x_vel = 2
444 elif self.rect.x >= self.origin_pos[0]:
445 self.rect.x = self.origin_pos[0]
446 self.state = 'battle resting'
447 self.x_vel = 0
448
449 def fade_death(self):
450 """
451 Make character become transparent in death.
452 """
453 self.image = pg.Surface((64, 64)).convert()
454 self.image.set_colorkey(c.BLACK)
455 self.image.set_alpha(self.alpha)
456 self.image.blit(self.death_image, (0, 0))
457 self.alpha -= 8
458 if self.alpha <= 0:
459 self.kill()
460 self.notify(c.ENEMY_DEAD)
461
462
463 def enter_knock_back_state(self):
464 """
465 Set values for entry to knock back state.
466 """
467 if self.name == 'player':
468 self.x_vel = 4
469 else:
470 self.x_vel = -4
471
472 self.state = c.KNOCK_BACK
473 self.origin_pos = self.rect.topleft
474
475
476class Player(Person):
477 """
478 User controlled character.
479 """
480
481 def __init__(self, direction, game_data, x=0, y=0, state='resting', index=0):
482 super(Player, self).__init__('player', x, y, direction, state, index)
483 self.damaged = False
484 self.healing = False
485 self.damage_alpha = 0
486 self.healing_alpha = 0
487 self.fade_in = True
488 self.game_data = game_data
489 self.index = 1
490 self.image = self.image_list[self.index]
491
492 @property
493 def level(self):
494 """
495 Make level property equal to player level in game_data.
496 """
497 return self.game_data['player stats']['Level']
498
499
500 def create_vector_dict(self):
501 """Return a dictionary of x and y velocities set to
502 direction keys."""
503 vector_dict = {'up': (0, -2),
504 'down': (0, 2),
505 'left': (-2, 0),
506 'right': (2, 0)}
507
508 return vector_dict
509
510 def update(self, keys, current_time):
511 """Updates player behavior"""
512 self.current_time = current_time
513 self.damage_animation()
514 self.healing_animation()
515 self.blockers = self.set_blockers()
516 self.keys = keys
517 self.check_for_input()
518 state_function = self.state_dict[self.state]
519 state_function()
520 self.location = self.get_tile_location()
521
522 def damage_animation(self):
523 """
524 Put a red overlay over sprite to indicate damage.
525 """
526 if self.damaged:
527 self.image = copy.copy(self.spritesheet_dict['facing left 2'])
528 self.image = pg.transform.scale2x(self.image).convert_alpha()
529 damage_image = copy.copy(self.image).convert_alpha()
530 damage_image.fill((255, 0, 0, self.damage_alpha), special_flags=pg.BLEND_RGBA_MULT)
531 self.image.blit(damage_image, (0, 0))
532 if self.fade_in:
533 self.damage_alpha += 25
534 if self.damage_alpha >= 255:
535 self.fade_in = False
536 self.damage_alpha = 255
537 elif not self.fade_in:
538 self.damage_alpha -= 25
539 if self.damage_alpha <= 0:
540 self.damage_alpha = 0
541 self.damaged = False
542 self.fade_in = True
543 self.image = self.spritesheet_dict['facing left 2']
544 self.image = pg.transform.scale2x(self.image)
545
546 def healing_animation(self):
547 """
548 Put a green overlay over sprite to indicate healing.
549 """
550 if self.healing:
551 self.image = copy.copy(self.spritesheet_dict['facing left 2'])
552 self.image = pg.transform.scale2x(self.image).convert_alpha()
553 healing_image = copy.copy(self.image).convert_alpha()
554 healing_image.fill((0, 255, 0, self.healing_alpha), special_flags=pg.BLEND_RGBA_MULT)
555 self.image.blit(healing_image, (0, 0))
556 if self.fade_in:
557 self.healing_alpha += 25
558 if self.healing_alpha >= 255:
559 self.fade_in = False
560 self.healing_alpha = 255
561 elif not self.fade_in:
562 self.healing_alpha -= 25
563 if self.healing_alpha <= 0:
564 self.healing_alpha = 0
565 self.healing = False
566 self.fade_in = True
567 self.image = self.spritesheet_dict['facing left 2']
568 self.image = pg.transform.scale2x(self.image)
569
570 def check_for_input(self):
571 """Checks for player input"""
572 if self.state == 'resting':
573 if self.keys[pg.K_UP]:
574 self.begin_moving('up')
575 elif self.keys[pg.K_DOWN]:
576 self.begin_moving('down')
577 elif self.keys[pg.K_LEFT]:
578 self.begin_moving('left')
579 elif self.keys[pg.K_RIGHT]:
580 self.begin_moving('right')
581
582 def calculate_hit(self):
583 """
584 Calculate hit strength based on attack stats.
585 """
586 weapon = self.game_data['player inventory']['equipped weapon']
587 if not weapon:
588 weapon_power = 0
589 else:
590 weapon_power = self.game_data['player inventory'][weapon]['power']
591 max_strength = weapon_power + (self.level * 5)
592 min_strength = max_strength // 4
593 return random.randint(min_strength, max_strength)
594
595
596class Enemy(Person):
597 """
598 Enemy sprite.
599 """
600 def __init__(self, sheet_key, x, y, direction='down', state='resting', index=0):
601 super(Enemy, self).__init__(sheet_key, x, y, direction, state, index)
602 self.level = 1
603 self.type = 'enemy'
604
605
606class Chest(Person):
607 """
608 Treasure chest that contains items to collect.
609 """
610 def __init__(self, x, y, id):
611 super(Chest, self).__init__('treasurechest', x, y)
612 self.spritesheet_dict = self.make_image_dict()
613 self.image_list = self.make_image_list()
614 self.image = self.image_list[self.index]
615 self.rect = self.image.get_rect(x=x, y=y)
616 self.id = id
617
618 def make_image_dict(self):
619 """
620 Make a dictionary for the sprite's images.
621 """
622 sprite_sheet = setup.GFX['treasurechest']
623 image_dict = {'closed': self.get_image(0, 0, 32, 32, sprite_sheet),
624 'opened': self.get_image(32, 0, 32, 32, sprite_sheet)}
625
626 return image_dict
627
628 def make_image_list(self):
629 """
630 Make the list of two images for the chest.
631 """
632 image_list = [self.spritesheet_dict['closed'],
633 self.spritesheet_dict['opened']]
634
635 return image_list
636
637 def update(self, current_time, *args):
638 """Implemented by inheriting classes"""
639 self.blockers = self.set_blockers()
640 self.current_time = current_time
641 state_function = self.state_dict[self.state]
642 state_function()
643 self.location = self.get_tile_location()
644
645
646
647