comparison pytouhou/game/player.py @ 172:ea2ad94c33a0

Implement player death.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 21 Oct 2011 09:29:16 -0700
parents 7769ce7be03c
children 35d850502d1f
comparison
equal deleted inserted replaced
171:2f3665a77f11 172:ea2ad94c33a0
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 15
16 from pytouhou.game.sprite import Sprite 16 from pytouhou.game.sprite import Sprite
17 from pytouhou.game.enemy import Effect
17 from pytouhou.vm.anmrunner import ANMRunner 18 from pytouhou.vm.anmrunner import ANMRunner
18 19
19 20
20 SQ2 = 2. ** 0.5 / 2. 21 SQ2 = 2. ** 0.5 / 2.
21 22
33 self.points = 0 34 self.points = 0
34 35
35 self.x = 192.0 36 self.x = 192.0
36 self.y = 384.0 37 self.y = 384.0
37 38
39 self.invulnerable_time = 60
40 self.touchable = True
41
38 42
39 class Player(object): 43 class Player(object):
40 def __init__(self, state, character): 44 def __init__(self, state, character, game):
41 self._sprite = None 45 self._sprite = None
42 self._anmrunner = None 46 self._anmrunner = None
47 self._game = game
43 48
44 self.hitbox_half_size = character.hitbox_size / 2. 49 self.hitbox_half_size = character.hitbox_size / 2.
45 50
46 self.state = state 51 self.state = state
47 self.character = character 52 self.character = character
48 self.anm_wrapper = character.anm_wrapper 53 self.anm_wrapper = character.anm_wrapper
49 self.direction = None 54 self.direction = None
50 55
51 self.set_anim(0) 56 self.set_anim(0)
57
58 self.death_time = 0
52 59
53 60
54 @property 61 @property
55 def x(self): 62 def x(self):
56 return self.state.x 63 return self.state.x
66 self._anmrunner = ANMRunner(self.anm_wrapper, index, self._sprite) 73 self._anmrunner = ANMRunner(self.anm_wrapper, index, self._sprite)
67 self._anmrunner.run_frame() 74 self._anmrunner.run_frame()
68 75
69 76
70 def collide(self): 77 def collide(self):
71 self.state.lives -= 1 78 if not self.state.invulnerable_time and not self.death_time and self.state.touchable: # Border Between Life and Death
72 self.state.x = 192.0 79 self.death_time = self._game.frame
73 self.state.y = 384.0 80 eff00 = self._game.resource_loader.get_anm_wrapper(('eff00.anm',))
74 #TODO: animation 81 self._game.effects.append(Effect((self.state.x, self.state.y), 2, eff00))
75 #TODO: set invulnerability.
76 82
77 83
78 def collect(self, item): 84 def collect(self, item):
79 #TODO 85 #TODO
80 self.state.score += item._item_type.score 86 self.state.score += item._item_type.score
81 item._removed = True 87 item._removed = True
82 88
83 89
84 def update(self, keystate): 90 def update(self, keystate):
85 try: 91 if self.death_time == 0 or self._game.frame - self.death_time > 60:
86 dx, dy = {16: (0.0, -1.0), 32: (0.0, 1.0), 64: (-1.0, 0.0), 128: (1.0, 0.0), 92 try:
87 16|64: (-SQ2, -SQ2), 16|128: (SQ2, -SQ2), 93 dx, dy = {16: (0.0, -1.0), 32: (0.0, 1.0), 64: (-1.0, 0.0), 128: (1.0, 0.0),
88 32|64: (-SQ2, SQ2), 32|128: (SQ2, SQ2)}[keystate & (16|32|64|128)] 94 16|64: (-SQ2, -SQ2), 16|128: (SQ2, -SQ2),
89 except KeyError: 95 32|64: (-SQ2, SQ2), 32|128: (SQ2, SQ2)}[keystate & (16|32|64|128)]
90 speed = 0.0 96 except KeyError:
91 dx, dy = 0.0, 0.0 97 speed = 0.0
92 else: 98 dx, dy = 0.0, 0.0
93 speed = self.character.focused_speed if keystate & 4 else self.character.speed 99 else:
94 dx, dy = dx * speed, dy * speed 100 speed = self.character.focused_speed if keystate & 4 else self.character.speed
101 dx, dy = dx * speed, dy * speed
95 102
96 if dx < 0 and self.direction != -1: 103 if dx < 0 and self.direction != -1:
97 self.set_anim(1) 104 self.set_anim(1)
98 self.direction = -1 105 self.direction = -1
99 elif dx > 0 and self.direction != +1: 106 elif dx > 0 and self.direction != +1:
100 self.set_anim(3) 107 self.set_anim(3)
101 self.direction = +1 108 self.direction = +1
102 elif dx == 0 and self.direction is not None: 109 elif dx == 0 and self.direction is not None:
103 self.set_anim({-1: 2, +1: 4}[self.direction]) 110 self.set_anim({-1: 2, +1: 4}[self.direction])
104 self.direction = None 111 self.direction = None
105 112
106 self.state.x += dx 113 self.state.x += dx
107 self.state.y += dy 114 self.state.y += dy
115
116 if self.state.invulnerable_time > 0:
117 self.state.invulnerable_time -= 1
118
119 m = self.state.invulnerable_time % 8
120 if m == 0:
121 self._sprite.color = (255, 255, 255)
122 self._sprite._changed = True
123 elif m == 2:
124 self._sprite.color = (64, 64, 64)
125 self._sprite._changed = True
126
127 if self.death_time:
128 time = self._game.frame - self.death_time
129 if time == 6: # too late, you are dead :(
130 self._game.drop_bonus(self.state.x, self.state.y, 2, end_pos=None) #TODO: find the formula
131 for i in range(5):
132 self._game.drop_bonus(self.state.x, self.state.y, 0, end_pos=None) #TODO: find the formula
133 self.state.lives -= 1
134
135 elif time == 7:
136 self.state.touchable = False
137 self._sprite.mirrored = False
138 self._sprite.fade(24, 128, lambda x: x)
139 self._sprite.blendfunc = 1
140 self._sprite.scale_in(24, 0., 2., lambda x: x)
141
142 elif time == 31:
143 self.state.x = 192.0
144 self.state.y = 384.0
145 self.direction = None
146
147 self._sprite = Sprite()
148 self._anmrunner = ANMRunner(self.anm_wrapper, 0, self._sprite)
149 self._sprite.alpha = 128
150 self._sprite.rescale = 0., 2.
151 self._sprite.fade(30, 255, lambda x: x)
152 self._sprite.blendfunc = 1
153 self._sprite.scale_in(30, 1., 1., lambda x: x)
154 self._anmrunner.run_frame()
155
156 elif time == 60: # respawned
157 self.state.touchable = True
158 self.state.invulnerable_time = 240
159 self._sprite.blendfunc = 0
160
161 if time > 30:
162 for bullet in self._game.bullets:
163 bullet.cancel()
164
165 if time > 90: # start the bullet hell again
166 self.death_time = 0
167
108 168
109 self._anmrunner.run_frame() 169 self._anmrunner.run_frame()
110 170