comparison pytouhou/game/effect.py @ 388:ac2891afb0bb

Make particles behave as in the original game.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 21 Oct 2012 23:02:40 +0200
parents 690b5faaa0e6
children c9433188ffdb
comparison
equal deleted inserted replaced
387:e1f5dcd4b83e 388:ac2891afb0bb
14 14
15 15
16 from pytouhou.game.sprite import Sprite 16 from pytouhou.game.sprite import Sprite
17 from pytouhou.vm.anmrunner import ANMRunner 17 from pytouhou.vm.anmrunner import ANMRunner
18 from pytouhou.utils.interpolator import Interpolator 18 from pytouhou.utils.interpolator import Interpolator
19 from math import pi
20 19
21 20
22 21
23 class Effect(object): 22 class Effect(object):
24 def __init__(self, pos, index, anm_wrapper): 23 def __init__(self, pos, index, anm_wrapper):
40 self.sprite = None 39 self.sprite = None
41 self.removed = True 40 self.removed = True
42 41
43 42
44 43
45 class Particle(object): 44 class Particle(Effect):
46 def __init__(self, start_pos, index, anm_wrapper, size, amp, game): 45 def __init__(self, pos, index, anm_wrapper, amp, game, reverse=False, duration=24):
47 self._game = game 46 Effect.__init__(self, pos, index, anm_wrapper)
48 47
49 self.sprite = Sprite() 48 self.frame = 0
50 self.sprite.anm, self.sprite.texcoords = anm_wrapper.get_sprite(index) 49 self.duration = duration
51 self.removed = False
52 self.objects = [self]
53 50
54 self.x, self.y = start_pos 51 random_pos = (self.x + amp * game.prng.rand_double() - amp / 2,
55 self.frame = 0 52 self.y + amp * game.prng.rand_double() - amp / 2)
56 self.sprite.alpha = 128
57 self.sprite.blendfunc = 1
58 self.sprite.rescale = (size, size)
59 53
60 self.pos_interpolator = None 54 if not reverse:
61 self.scale_interpolator = None 55 self.pos_interpolator = Interpolator((self.x, self.y), 0,
62 self.rotations_interpolator = None 56 random_pos, duration, formula=(lambda x: 2. * x - x ** 2))
63 self.alpha_interpolator = None 57 else:
64 58 self.pos_interpolator = Interpolator(random_pos, 0,
65 self.amp = amp 59 (self.x, self.y), duration, formula=(lambda x: 2. * x - x ** 2))
66 60 self.x, self.y = random_pos
67
68 def set_end_pos(self, amp):
69 end_pos = (self.x + amp * self._game.prng.rand_double() - amp/2,
70 self.y + amp * self._game.prng.rand_double() - amp/2)
71
72 self.pos_interpolator = Interpolator((self.x, self.y), 0,
73 end_pos, 24, formula=(lambda x: 2. * x - x ** 2))
74 self.scale_interpolator = Interpolator(self.sprite.rescale, 0,
75 (0., 0.), 24)
76 self.rotations_interpolator = Interpolator(self.sprite.rotations_3d, 0,
77 (0., 0., 2*pi), 24)
78 self.alpha_interpolator = Interpolator((self.sprite.alpha,), 0,
79 (0.,), 24)
80 61
81 62
82 def update(self): 63 def update(self):
83 if self.frame == 0: 64 Effect.update(self)
84 self.set_end_pos(self.amp)
85 65
86 if self.pos_interpolator: 66 self.pos_interpolator.update(self.frame)
87 self.pos_interpolator.update(self.frame) 67 self.x, self.y = self.pos_interpolator.values
88 self.x, self.y = self.pos_interpolator.values
89
90 self.scale_interpolator.update(self.frame)
91 self.sprite.rescale = self.scale_interpolator.values
92
93 self.rotations_interpolator.update(self.frame)
94 self.sprite.rotations_3d = self.rotations_interpolator.values
95
96 self.alpha_interpolator.update(self.frame)
97 self.sprite.alpha, = self.alpha_interpolator.values
98
99 self.sprite.changed = True
100
101 if self.frame == 24:
102 self.removed = True
103 68
104 self.frame += 1 69 self.frame += 1
105 70