comparison pytouhou/game/enemymanager.py @ 69:a142e57218a0

Refactor. Move VMs to pytouhou.vm.
author Thibaut Girka <thib@sitedethib.com>
date Sat, 27 Aug 2011 10:58:54 +0200
parents a2459defd4b6
children 6a08f44fa01b
comparison
equal deleted inserted replaced
68:a2459defd4b6 69:a142e57218a0
16 from itertools import chain 16 from itertools import chain
17 from io import BytesIO 17 from io import BytesIO
18 import os 18 import os
19 from struct import unpack, pack 19 from struct import unpack, pack
20 from pytouhou.utils.interpolator import Interpolator 20 from pytouhou.utils.interpolator import Interpolator
21 from pytouhou.game.eclrunner import ECLRunner 21 from pytouhou.vm.eclrunner import ECLRunner
22 from pytouhou.vm.anmrunner import ANMRunner
22 from pytouhou.game.sprite import Sprite 23 from pytouhou.game.sprite import Sprite
23 from math import cos, sin, atan2 24 from math import cos, sin, atan2
24 25
25 26
26 class Enemy(object): 27 class Enemy(object):
27 def __init__(self, pos, life, _type, anm_wrapper): 28 def __init__(self, pos, life, _type, anm_wrapper):
28 self._anm_wrapper = anm_wrapper 29 self._anm_wrapper = anm_wrapper
29 self._anm = None
30 self._sprite = None 30 self._sprite = None
31 self._anmrunner = None
31 self._removed = False 32 self._removed = False
32 self._type = _type 33 self._type = _type
33 self._was_visible = False 34 self._was_visible = False
34 35
35 self.frame = 0 36 self.frame = 0
89 def get_player_angle(self, player): 90 def get_player_angle(self, player):
90 return atan2(player.y - self.y, player.x - self.x) 91 return atan2(player.y - self.y, player.x - self.x)
91 92
92 93
93 def set_anim(self, index): 94 def set_anim(self, index):
94 self._anm, self._sprite = self._anm_wrapper.get_sprite(index) 95 self._sprite = Sprite()
96 self._anmrunner = ANMRunner(self._anm_wrapper, index, self._sprite)
95 97
96 98
97 def set_pos(self, x, y, z): 99 def set_pos(self, x, y, z):
98 self.x, self.y = x, y 100 self.x, self.y = x, y
99 self.interpolator = Interpolator((x, y)) 101 self.interpolator = Interpolator((x, y))
137 return True 139 return True
138 140
139 141
140 def get_objects_by_texture(self): 142 def get_objects_by_texture(self):
141 objects_by_texture = {} 143 objects_by_texture = {}
142 key = self._anm.first_name, self._anm.secondary_name 144 key = self._sprite.anm.first_name, self._sprite.anm.secondary_name
143 if not key in objects_by_texture: 145 if not key in objects_by_texture:
144 objects_by_texture[key] = (0, [], [], []) 146 objects_by_texture[key] = (0, [], [], [])
145 vertices = tuple((x + self.x, y + self.y, z) for x, y, z in self._sprite._vertices) 147 vertices = tuple((x + self.x, y + self.y, z) for x, y, z in self._sprite._vertices)
146 objects_by_texture[key][1].extend(vertices) 148 objects_by_texture[key][1].extend(vertices)
147 objects_by_texture[key][2].extend(self._sprite._uvs) 149 objects_by_texture[key][2].extend(self._sprite._uvs)
148 objects_by_texture[key][3].extend(self._sprite._colors) 150 objects_by_texture[key][3].extend(self._sprite._colors)
149 #TODO: effects/bullet launch 151 #TODO: effects/bullet launch
150 return objects_by_texture 152 return objects_by_texture
151 153
152 154
153 def update(self, frame): 155 def update(self):
154 x, y = self.x, self.y 156 x, y = self.x, self.y
155 if self.interpolator: 157 if self.interpolator:
156 self.interpolator.update(self.frame) 158 self.interpolator.update(self.frame)
157 x, y = self.interpolator.values 159 x, y = self.interpolator.values
158 160
193 x = max(xmin, min(x, xmax)) 195 x = max(xmin, min(x, xmax))
194 y = max(ymin, min(y, ymax)) 196 y = max(ymin, min(y, ymax))
195 197
196 198
197 self.x, self.y = x, y 199 self.x, self.y = x, y
200
201 #TODO
202 if self._anmrunner and not self._anmrunner.run_frame():
203 self._anmrunner = None
204
198 if self._sprite: 205 if self._sprite:
199 changed = self._sprite.update() 206 if self._sprite._removed:
200 visible = self.is_visible(384, 448)
201 if changed and visible:
202 self._sprite.update_vertices_uvs_colors()
203 elif not self._sprite.playing:
204 visible = False
205 self._sprite = None 207 self._sprite = None
206 else: 208 else:
207 visible = False 209 self._sprite.update()
208 210 if self._sprite._changed:
211 self._sprite.update_vertices_uvs_colors()
209 212
210 self.frame += 1 213 self.frame += 1
211 return visible
212 214
213 215
214 216
215 class EnemyManager(object): 217 class EnemyManager(object):
216 def __init__(self, stage, anm_wrapper, ecl, game_state): 218 def __init__(self, stage, anm_wrapper, ecl, game_state):
253 255
254 # Filter of destroyed enemies 256 # Filter of destroyed enemies
255 self.enemies[:] = (enemy for enemy in self.enemies if not enemy._removed) 257 self.enemies[:] = (enemy for enemy in self.enemies if not enemy._removed)
256 258
257 # Update enemies 259 # Update enemies
258 visible_enemies = [enemy for enemy in self.enemies if enemy.update(frame)] 260 for enemy in self.enemies:
261 enemy.update()
262
263 # Filter out non-visible enemies
264 visible_enemies = [enemy for enemy in self.enemies if enemy.is_visible(384, 448)] #TODO
259 for enemy in visible_enemies: 265 for enemy in visible_enemies:
260 enemy._was_visible = True 266 enemy._was_visible = True
261 267
262 # Filter out-of-screen enemies 268 # Filter out-of-screen enemies
263 for enemy in tuple(self.enemies): 269 for enemy in tuple(self.enemies):
264 if enemy._was_visible and not enemy in visible_enemies: 270 if enemy._was_visible and not enemy in visible_enemies:
265 enemy._removed = True 271 enemy._removed = True
266 self.enemies.remove(enemy) 272 self.enemies.remove(enemy)
267 273
268
269 #TODO: disable boss mode if it is dead/it has timeout 274 #TODO: disable boss mode if it is dead/it has timeout
270 if self._game_state.boss and self._game_state.boss._removed: 275 if self._game_state.boss and self._game_state.boss._removed:
271 self._game_state.boss = None 276 self._game_state.boss = None
272
273 277
274 # Add enemies to vertices/uvs 278 # Add enemies to vertices/uvs
275 self.objects_by_texture = {} 279 self.objects_by_texture = {}
276 for enemy in visible_enemies: 280 for enemy in visible_enemies:
277 if enemy.is_visible(384, 448): #TODO 281 if enemy.is_visible(384, 448): #TODO