Mercurial > touhou
comparison pytouhou/game/item.py @ 153:37df8c618c2e
Add falling items.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 09 Oct 2011 16:54:49 -0700 |
parents | 86807b8a63bd |
children | ebfd328e700c |
comparison
equal
deleted
inserted
replaced
152:86807b8a63bd | 153:37df8c618c2e |
---|---|
13 ## | 13 ## |
14 | 14 |
15 | 15 |
16 from math import cos, sin, atan2, pi | 16 from math import cos, sin, atan2, pi |
17 | 17 |
18 from pytouhou.utils.interpolator import Interpolator | |
19 | |
18 | 20 |
19 class Item(object): | 21 class Item(object): |
20 def __init__(self, pos, item_type, angle, speed, player, game): | 22 def __init__(self, start_pos, item_type, game, angle=pi/2, speed=8., player=None, end_pos=None): |
21 self._game = game | 23 self._game = game |
22 self._sprite = item_type.sprite | 24 self._sprite = item_type.sprite |
23 self._removed = False | 25 self._removed = False |
24 self._item_type = item_type | 26 self._item_type = item_type |
25 | 27 |
27 | 29 |
28 self.frame = 0 | 30 self.frame = 0 |
29 | 31 |
30 self.player = player | 32 self.player = player |
31 | 33 |
32 self.x, self.y = pos | 34 self.x, self.y = start_pos |
33 self.angle = angle | 35 self.angle = angle |
34 self.speed = speed | 36 self.speed = speed |
35 dx, dy = cos(angle) * speed, sin(angle) * speed | 37 dx, dy = cos(angle) * speed, sin(angle) * speed |
36 self.delta = dx, dy | 38 self.delta = dx, dy |
39 | |
40 if not player: | |
41 #TODO: find the formulae in the binary. | |
42 self.speed_interpolator = None | |
43 if end_pos: | |
44 self.pos_interpolator = Interpolator(start_pos, 0, | |
45 end_pos, 60) | |
46 else: | |
47 self.speed_interpolator = Interpolator((-2.,), 0, | |
48 (0.,), 60) | |
37 | 49 |
38 self._sprite.angle = angle | 50 self._sprite.angle = angle |
39 | 51 |
40 | 52 |
41 def collect(self, player): | 53 def collect(self, player): |
44 | 56 |
45 | 57 |
46 def update(self): | 58 def update(self): |
47 dx, dy = self.delta | 59 dx, dy = self.delta |
48 | 60 |
61 if self.frame == 60: | |
62 self.speed_interpolator = Interpolator((0.,), 60, | |
63 (3.,), 180) | |
64 | |
49 if self.player is not None: | 65 if self.player is not None: |
50 self.angle = atan2(self.player.y - self.y, self.player.x - self.x) | 66 self.angle = atan2(self.player.y - self.y, self.player.x - self.x) |
67 self.x += cos(self.angle) * self.speed | |
68 self.y += sin(self.angle) * self.speed | |
69 elif self.speed_interpolator is None: | |
70 self.pos_interpolator.update(self.frame) | |
71 self.x, self.y = self.pos_interpolator.values | |
72 else: | |
73 self.speed_interpolator.update(self.frame) | |
74 self.speed, = self.speed_interpolator.values | |
51 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed | 75 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed |
52 else: | 76 self.delta = dx, dy |
53 pass #TODO: item falls! | 77 self.x += dx |
78 self.y += dy | |
54 | 79 |
55 self.x += dx | 80 self.frame += 1 |
56 self.y += dy | |
57 | 81 |