90
|
1 """Class for dealing with the player"""
|
|
2
|
|
3 import pygame.transform
|
|
4 import os
|
|
5
|
|
6 from skaapsteker.sprites.base import TILE_SIZE
|
|
7 from skaapsteker.physics import Sprite
|
|
8 from skaapsteker.constants import Layers
|
|
9 from skaapsteker.data import get_files, load_image
|
|
10
|
|
11
|
|
12 class Player(Sprite):
|
|
13
|
|
14 def __init__(self):
|
|
15 Sprite.__init__(self)
|
|
16 self.image = None
|
|
17 self._image_dict = {}
|
|
18 # State flags and such
|
|
19 self.running = False
|
|
20 self.jumping = False
|
|
21 self.flying = False
|
|
22 self._load_images()
|
|
23 # We muck with these in load for convience, so ensure they're right
|
|
24 self.tails = 0
|
|
25 self.set_facing('left')
|
|
26 self.set_image()
|
|
27 self.set_pos((0, 0))
|
|
28 self._layer = Layers.PLAYER
|
|
29
|
|
30 def set_image(self):
|
|
31 key = self._make_key()
|
|
32 # TODO: handle animations
|
|
33 self.image = self._image_dict[key][0]
|
|
34
|
|
35 def set_facing(self, new_facing):
|
|
36 self.facing = new_facing
|
|
37
|
|
38 def set_pos(self, pos):
|
|
39 self.starting_tile_pos = pos
|
|
40 self.rect = self.image.get_rect(topleft=(pos[0]*TILE_SIZE[0], pos[1]*TILE_SIZE[1]))
|
|
41
|
|
42
|
|
43 def dispatch(self, ev):
|
|
44 pass
|
|
45
|
|
46 def _get_action(self):
|
|
47 if self.running:
|
|
48 return 'running'
|
|
49 if self.jumping:
|
|
50 return 'jumpin'
|
|
51 return 'standing'
|
|
52
|
|
53
|
|
54 def _make_key(self, action=None):
|
|
55 if action is None:
|
|
56 action = self._get_action()
|
|
57 tails = self.tails
|
|
58 if self.tails >= 4:
|
|
59 tails = 4
|
|
60 elif self.tails >= 2:
|
|
61 tails = 2
|
|
62 return '%s %s %d' % (action, self.facing, tails)
|
|
63
|
|
64 def _load_images(self):
|
|
65 for action in ['standing', 'running', 'jumping']:
|
|
66 for tails in [0, 1, 2, 4]:
|
|
67 self.tails = tails
|
|
68 directory = os.path.join('sprites',
|
|
69 'kitsune_%s' % action,
|
|
70 'kitsune_%s_%dtail' % (action, tails))
|
|
71 for facing in ['left', 'right']:
|
|
72 self.facing = facing
|
|
73 key = self._make_key(action)
|
|
74 self._image_dict[key] = []
|
|
75 for image_file in get_files(directory):
|
|
76 if image_file.startswith('.'):
|
|
77 # Skip extra junk for now
|
|
78 continue
|
|
79 image = load_image(os.path.join(directory, image_file))
|
|
80 if facing == 'right':
|
|
81 image = pygame.transform.flip(image, True, False)
|
|
82 self._image_dict[key].append(image)
|
|
83
|
|
84
|