data/components/person.py (view raw)
1__author__ = 'justinarmstrong'
2import math
3import pygame as pg
4from .. import setup
5
6
7class Person(pg.sprite.Sprite):
8 """Base class for all world characters
9 controlled by the computer"""
10
11 def __init__(self, sheet_key, x, y, direction='down'):
12 super(Person, self).__init__()
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 self.index = 0
17 self.direction = direction
18 self.image_list = self.animation_dict[self.direction]
19 self.image = self.image_list[self.index]
20 self.rect = self.image.get_rect(left=x, top=y)
21 self.old_rect = self.rect
22 self.state_dict = self.create_state_dict()
23 self.vector_dict = self.create_vector_dict()
24 self.x_vel = 0
25 self.y_vel = 0
26 self.timer = 0.0
27 self.current_time = 0.0
28 self.state = 'resting'
29 self.blockers = self.set_blockers()
30
31
32 def create_spritesheet_dict(self, sheet_key):
33 """Implemented by inheriting classes"""
34 image_list = []
35 image_dict = {}
36 sheet = setup.GFX[sheet_key]
37
38 image_keys = ['facing up 1', 'facing up 2',
39 'facing down 1', 'facing down 2',
40 'facing left 1', 'facing left 2',
41 'facing right 1', 'facing right 2']
42
43 for row in range(2):
44 for column in range(4):
45 image_list.append(
46 self.get_image(self, column*32, row*32, 32, 32, sheet))
47
48 for key, image in zip(image_keys, image_list):
49 image_dict[key] = image
50
51 return image_dict
52
53
54 def create_animation_dict(self):
55 """Return a dictionary of image lists for animation"""
56 image_dict = self.spritesheet_dict
57
58 left_list = [image_dict['facing left 1'], image_dict['facing left 2']]
59 right_list = [image_dict['facing right 1'], image_dict['facing right 2']]
60 up_list = [image_dict['facing up 1'], image_dict['facing up 2']]
61 down_list = [image_dict['facing down 1'], image_dict['facing down 2']]
62
63 direction_dict = {'left': left_list,
64 'right': right_list,
65 'up': up_list,
66 'down': down_list}
67
68 return direction_dict
69
70
71 def create_state_dict(self):
72 """Return a dictionary of all state methods"""
73 state_dict = {'resting': self.resting,
74 'moving': self.moving}
75
76 return state_dict
77
78
79 def create_vector_dict(self):
80 """Return a dictionary of x and y velocities set to
81 direction keys."""
82 vector_dict = {'up': (0, -2),
83 'down': (0, 2),
84 'left': (-2, 0),
85 'right': (2, 0)}
86
87 return vector_dict
88
89
90 def update(self, keys, current_time):
91 """Implemented by inheriting classes"""
92 self.blockers = self.set_blockers()
93 self.current_time = current_time
94 self.check_for_input()
95 state_function = self.state_dict[self.state]
96 state_function()
97
98
99 def set_blockers(self):
100 """Sets blockers to prevent collision with other sprites"""
101 blockers = []
102
103 if self.state == 'resting':
104 blockers.append(pg.Rect(self.rect.x, self.rect.y, 32, 32))
105
106 elif self.state == 'moving':
107 if self.rect.x % 32 == 0:
108 tile_float = self.rect.y / float(32)
109 tile1 = (self.rect.x, math.ceil(tile_float)*32)
110 tile2 = (self.rect.x, math.floor(tile_float)*32)
111 tile_rect1 = pg.Rect(tile1[0], tile1[1], 32, 32)
112 tile_rect2 = pg.Rect(tile2[0], tile2[1], 32, 32)
113 blockers.extend([tile_rect1, tile_rect2])
114
115 elif self.rect.y % 32 == 0:
116 tile_float = self.rect.x / float(32)
117 tile1 = (math.ceil(tile_float)*32, self.rect.y)
118 tile2 = (math.floor(tile_float)*32, self.rect.y)
119 tile_rect1 = pg.Rect(tile1[0], tile1[1], 32, 32)
120 tile_rect2 = pg.Rect(tile2[0], tile2[1], 32, 32)
121 blockers.extend([tile_rect1, tile_rect2])
122
123 return blockers
124
125
126 def resting(self):
127 """
128 When the Person is not moving between tiles.
129 Checks if the player is centered on a tile.
130 """
131 self.image = self.image_list[self.index]
132
133 assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
134 + str(self.rect.y))
135 assert(self.rect.x % 32 == 0), ('Player not centered on tile'
136 + str(self.rect.x))
137
138
139 def moving(self):
140 """Increment index and set self.image for animation."""
141 self.animation()
142
143 assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
144 'Not centered on tile'
145
146
147 def animation(self):
148 """Adjust sprite image frame based on timer"""
149 if (self.current_time - self.timer) > 100:
150 if self.index < (len(self.image_list) - 1):
151 self.index += 1
152 else:
153 self.index = 0
154 self.timer = self.current_time
155
156 self.image = self.image_list[self.index]
157
158
159
160 def begin_moving(self, direction):
161 """Transition the player into the 'moving' state."""
162 self.direction = direction
163 self.image_list = self.animation_dict[direction]
164 self.timer = self.current_time
165 self.state = 'moving'
166 self.old_rect = self.rect
167
168 if self.rect.x % 32 == 0:
169 self.y_vel = self.vector_dict[self.direction][1]
170 if self.rect.y % 32 == 0:
171 self.x_vel = self.vector_dict[self.direction][0]
172
173
174 def begin_resting(self):
175 """Transition the player into the 'resting' state."""
176 self.state = 'resting'
177 self.index = 1
178 self.x_vel = self.y_vel = 0
179
180
181class Player(Person):
182 """User controlled character"""
183
184 def __init__(self, direction):
185 super(Player, self).__init__('player', 0, 0, direction)
186
187
188 def update(self, keys, current_time):
189 """Updates player behavior"""
190 self.blockers = self.set_blockers()
191 self.keys = keys
192 self.current_time = current_time
193 self.check_for_input()
194 state_function = self.state_dict[self.state]
195 state_function()
196
197
198 def check_for_input(self):
199 """Checks for player input"""
200 if self.state == 'resting':
201 if self.keys[pg.K_UP]:
202 self.begin_moving('up')
203 elif self.keys[pg.K_DOWN]:
204 self.begin_moving('down')
205 elif self.keys[pg.K_LEFT]:
206 self.begin_moving('left')
207 elif self.keys[pg.K_RIGHT]:
208 self.begin_moving('right')
209
210
211
212class Soldier(Person):
213 """Soldier for the castle"""
214
215 def __init__(self):
216 super(Soldier, self).__init__('soldier', x, y)
217
218
219class FemaleVillager(Person):
220 """Female Person for town"""
221
222 def __init__(self, x, y):
223 super(FemaleVillager, self).__init__('femalevillager', x, y)
224
225
226class MaleVillager(Person):
227 """Male Person for town"""
228
229 def __init__(self):
230 super(MaleVillager, self).__init__('male villager', x, y)
231