data/components/person.py (view raw)
1import math, random
2import pygame as pg
3from .. import setup
4
5
6class Person(pg.sprite.Sprite):
7 """Base class for all world characters
8 controlled by the computer"""
9
10 def __init__(self, sheet_key, x, y, direction='down', state='resting', index=0):
11 super(Person, self).__init__()
12 self.name = sheet_key
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 = index
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.origin_pos = self.rect.topleft
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.move_timer = 0.0
28 self.current_time = 0.0
29 self.state = state
30 self.blockers = self.set_blockers()
31 self.location = self.get_tile_location()
32 self.dialogue = ['Location: ' + str(self.location)]
33 self.default_direction = direction
34 self.item = None
35 self.wander_box = self.make_wander_box()
36
37 def create_spritesheet_dict(self, sheet_key):
38 """Implemented by inheriting classes"""
39 image_list = []
40 image_dict = {}
41 sheet = setup.GFX[sheet_key]
42
43 image_keys = ['facing up 1', 'facing up 2',
44 'facing down 1', 'facing down 2',
45 'facing left 1', 'facing left 2',
46 'facing right 1', 'facing right 2']
47
48 for row in range(2):
49 for column in range(4):
50 image_list.append(
51 self.get_image(column*32, row*32, 32, 32, sheet))
52
53 for key, image in zip(image_keys, image_list):
54 image_dict[key] = image
55
56 return image_dict
57
58 def create_animation_dict(self):
59 """Return a dictionary of image lists for animation"""
60 image_dict = self.spritesheet_dict
61
62 left_list = [image_dict['facing left 1'], image_dict['facing left 2']]
63 right_list = [image_dict['facing right 1'], image_dict['facing right 2']]
64 up_list = [image_dict['facing up 1'], image_dict['facing up 2']]
65 down_list = [image_dict['facing down 1'], image_dict['facing down 2']]
66
67 direction_dict = {'left': left_list,
68 'right': right_list,
69 'up': up_list,
70 'down': down_list}
71
72 return direction_dict
73
74 def create_state_dict(self):
75 """Return a dictionary of all state methods"""
76 state_dict = {'resting': self.resting,
77 'moving': self.moving,
78 'animated resting': self.animated_resting,
79 'autoresting': self.auto_resting,
80 'automoving': self.auto_moving,
81 'battle resting': self.battle_resting,
82 'attack': self.attack}
83
84 return state_dict
85
86 def create_vector_dict(self):
87 """Return a dictionary of x and y velocities set to
88 direction keys."""
89 vector_dict = {'up': (0, -1),
90 'down': (0, 1),
91 'left': (-1, 0),
92 'right': (1, 0)}
93
94 return vector_dict
95
96 def update(self, current_time, *args):
97 """Implemented by inheriting classes"""
98 self.blockers = self.set_blockers()
99 self.current_time = current_time
100 self.image_list = self.animation_dict[self.direction]
101 state_function = self.state_dict[self.state]
102 state_function()
103 self.location = self.get_tile_location()
104
105
106
107 def set_blockers(self):
108 """Sets blockers to prevent collision with other sprites"""
109 blockers = []
110
111 if self.state == 'resting' or self.state == 'autoresting':
112 blockers.append(pg.Rect(self.rect.x, self.rect.y, 32, 32))
113
114 elif self.state == 'moving' or self.state == 'automoving':
115 if self.rect.x % 32 == 0:
116 tile_float = self.rect.y / float(32)
117 tile1 = (self.rect.x, math.ceil(tile_float)*32)
118 tile2 = (self.rect.x, math.floor(tile_float)*32)
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 elif self.rect.y % 32 == 0:
124 tile_float = self.rect.x / float(32)
125 tile1 = (math.ceil(tile_float)*32, self.rect.y)
126 tile2 = (math.floor(tile_float)*32, self.rect.y)
127 tile_rect1 = pg.Rect(tile1[0], tile1[1], 32, 32)
128 tile_rect2 = pg.Rect(tile2[0], tile2[1], 32, 32)
129 blockers.extend([tile_rect1, tile_rect2])
130
131 return blockers
132
133 def get_tile_location(self):
134 """
135 Convert pygame coordinates into tile coordinates.
136 """
137 if self.rect.x == 0:
138 tile_x = 0
139 elif self.rect.x % 32 == 0:
140 tile_x = (self.rect.x / 32)
141 else:
142 tile_x = 0
143
144 if self.rect.y == 0:
145 tile_y = 0
146 elif self.rect.y % 32 == 0:
147 tile_y = (self.rect.y / 32)
148 else:
149 tile_y = 0
150
151 return [tile_x, tile_y]
152
153
154 def make_wander_box(self):
155 """
156 Make a list of rects that surround the initial location
157 of a sprite to limit his/her wandering.
158 """
159 x = int(self.location[0])
160 y = int(self.location[1])
161 box_list = []
162 box_rects = []
163
164 for i in range(x-3, x+4):
165 box_list.append([i, y-3])
166 box_list.append([i, y+3])
167
168 for i in range(y-2, y+3):
169 box_list.append([x-3, i])
170 box_list.append([x+3, i])
171
172 for box in box_list:
173 left = box[0]*32
174 top = box[1]*32
175 box_rects.append(pg.Rect(left, top, 32, 32))
176
177 return box_rects
178
179
180 def resting(self):
181 """
182 When the Person is not moving between tiles.
183 Checks if the player is centered on a tile.
184 """
185 self.image = self.image_list[self.index]
186
187 assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
188 + str(self.rect.y) + " : " + str(self.name))
189 assert(self.rect.x % 32 == 0), ('Player not centered on tile'
190 + str(self.rect.x))
191
192 def moving(self):
193 """
194 Increment index and set self.image for animation.
195 """
196 self.animation()
197 assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
198 'Not centered on tile'
199
200 def animated_resting(self):
201 self.animation(500)
202
203 def animation(self, freq=100):
204 """
205 Adjust sprite image frame based on timer.
206 """
207 if (self.current_time - self.timer) > freq:
208 if self.index < (len(self.image_list) - 1):
209 self.index += 1
210 else:
211 self.index = 0
212 self.timer = self.current_time
213
214 self.image = self.image_list[self.index]
215
216 def begin_moving(self, direction):
217 """
218 Transition the player into the 'moving' state.
219 """
220 self.direction = direction
221 self.image_list = self.animation_dict[direction]
222 self.timer = self.current_time
223 self.move_timer = self.current_time
224 self.state = 'moving'
225
226 if self.rect.x % 32 == 0:
227 self.y_vel = self.vector_dict[self.direction][1]
228 if self.rect.y % 32 == 0:
229 self.x_vel = self.vector_dict[self.direction][0]
230
231
232 def begin_resting(self):
233 """
234 Transition the player into the 'resting' state.
235 """
236 self.state = 'resting'
237 self.index = 1
238 self.x_vel = self.y_vel = 0
239
240 def begin_auto_moving(self, direction):
241 """
242 Transition sprite to a automatic moving state.
243 """
244 self.direction = direction
245 self.image_list = self.animation_dict[direction]
246 self.state = 'automoving'
247 self.x_vel = self.vector_dict[direction][0]
248 self.y_vel = self.vector_dict[direction][1]
249 self.move_timer = self.current_time
250
251 def begin_auto_resting(self):
252 """
253 Transition sprite to an automatic resting state.
254 """
255 self.state = 'autoresting'
256 self.index = 1
257 self.x_vel = self.y_vel = 0
258 self.move_timer = self.current_time
259
260
261 def auto_resting(self):
262 """
263 Determine when to move a sprite from resting to moving in a random
264 direction.
265 """
266 #self.image = self.image_list[self.index]
267 self.image_list = self.animation_dict[self.direction]
268 self.image = self.image_list[self.index]
269
270 assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
271 + str(self.rect.y))
272 assert(self.rect.x % 32 == 0), ('Player not centered on tile'
273 + str(self.rect.x))
274
275 if (self.current_time - self.move_timer) > 2000:
276 direction_list = ['up', 'down', 'left', 'right']
277 random.shuffle(direction_list)
278 direction = direction_list[0]
279 self.begin_auto_moving(direction)
280 self.move_timer = self.current_time
281
282 def battle_resting(self):
283 """
284 Player stays still during battle state unless he attacks.
285 """
286 pass
287
288 def enter_attack_state(self, enemy):
289 """
290 Set values for attack state.
291 """
292 self.attacked_enemy = enemy
293 self.x_vel = 1
294 self.state = 'attack'
295
296 def attack(self):
297 """
298 Player does an attack animation.
299 """
300 SLOW_BACK = 1
301 FAST_FORWARD = -5
302 FAST_BACK = 5
303
304 self.rect.x += self.x_vel
305
306 if self.x_vel == SLOW_BACK:
307 if self.rect.x >= self.origin_pos[0] + 20:
308 self.x_vel = FAST_FORWARD
309 elif self.x_vel == FAST_FORWARD:
310 if self.rect.topleft >= self.origin_pos:
311 self.image = self.spritesheet_dict['facing left 1']
312 self.image = pg.transform.scale2x(self.image)
313 elif self.rect.x <= self.origin_pos[0] - 110:
314 self.x_vel = FAST_BACK
315 self.observer.on_notify('attack animation')
316 else:
317 if self.rect.x >= self.origin_pos[0]:
318 self.rect.x = self.origin_pos[0]
319 self.x_vel = 0
320 self.state = 'battle resting'
321 self.image = self.spritesheet_dict['facing left 2']
322 self.image = pg.transform.scale2x(self.image)
323 self.observer.on_notify('select action')
324
325
326
327 def auto_moving(self):
328 """
329 Animate sprite and check to stop.
330 """
331 self.animation()
332
333 assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
334 'Not centered on tile'
335
336
337class Player(Person):
338 """
339 User controlled character.
340 """
341
342 def __init__(self, direction, x=0, y=0, state='resting', index=0):
343 super(Player, self).__init__('player', x, y, direction, state, index)
344
345 def create_vector_dict(self):
346 """Return a dictionary of x and y velocities set to
347 direction keys."""
348 vector_dict = {'up': (0, -2),
349 'down': (0, 2),
350 'left': (-2, 0),
351 'right': (2, 0)}
352
353 return vector_dict
354
355 def update(self, keys, current_time):
356 """Updates player behavior"""
357 self.blockers = self.set_blockers()
358 self.keys = keys
359 self.current_time = current_time
360 self.check_for_input()
361 state_function = self.state_dict[self.state]
362 state_function()
363 self.location = self.get_tile_location()
364
365 def check_for_input(self):
366 """Checks for player input"""
367 if self.state == 'resting':
368 if self.keys[pg.K_UP]:
369 self.begin_moving('up')
370 elif self.keys[pg.K_DOWN]:
371 self.begin_moving('down')
372 elif self.keys[pg.K_LEFT]:
373 self.begin_moving('left')
374 elif self.keys[pg.K_RIGHT]:
375 self.begin_moving('right')
376
377
378
379
380class Well(pg.sprite.Sprite):
381 """Talking well"""
382 def __init__(self, x, y):
383 super(Well, self).__init__()
384 self.image = pg.Surface((32, 32))
385 self.image.set_colorkey((0,0,0))
386 self.rect = self.image.get_rect(left=x, top=y)
387 self.location = self.get_location()
388 self.dialogue = ["I'm a well!"]
389 self.blockers = [self.rect]
390 self.x_vel = self.y_vel = 0
391 self.state = 'resting'
392 self.direction = 'down'
393 self.default_direction = self.direction
394 self.item = None
395 self.wander_box = []
396
397 def get_location(self):
398 """Get tile location"""
399 x = self.rect.x / 32
400 y = self.rect.y / 32
401
402 return [x, y]
403
404 def begin_auto_resting(self):
405 """Placeholder"""
406 pass
407
408