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