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