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