annotate pytouhou/game/enemymanager.py @ 49:cbe1cb50f2fd

Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
author Thibaut Girka <thib@sitedethib.com>
date Mon, 22 Aug 2011 19:23:00 +0200
parents 8353c33d53d4
children 811cefefb5c8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 from itertools import chain
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 from io import BytesIO
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 import os
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 from struct import unpack, pack
20
6ebf9539c077 Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents: 18
diff changeset
5 from pytouhou.utils.interpolator import Interpolator
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
6 from pytouhou.game.eclrunner import ECLRunner
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 from pytouhou.game.sprite import Sprite
20
6ebf9539c077 Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents: 18
diff changeset
8 from math import cos, sin, atan2
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10
40
ce662b372ee0 HANDLE RANDOM PLACEMENT (main flag 4) with results identical to 102h.exe \o/ \o/ \o/
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
11 from pytouhou.utils.random import Random
ce662b372ee0 HANDLE RANDOM PLACEMENT (main flag 4) with results identical to 102h.exe \o/ \o/ \o/
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
12 random = Random(0x39f4)
ce662b372ee0 HANDLE RANDOM PLACEMENT (main flag 4) with results identical to 102h.exe \o/ \o/ \o/
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
13
ce662b372ee0 HANDLE RANDOM PLACEMENT (main flag 4) with results identical to 102h.exe \o/ \o/ \o/
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
14
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15 class Enemy(object):
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
16 def __init__(self, pos, life, _type, anm_wrapper):
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
17 self._anm_wrapper = anm_wrapper
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
18 self._anm = None
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
19 self._sprite = None
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
20 self._removed = False
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
21 self._type = _type
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
22
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
23 self.frame = 0
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
24
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25 self.x, self.y = pos
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 self.life = life
36
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
27 self.max_life = life
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
28 self.pending_bullets = []
48
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
29 self.bullet_attributes = None
36
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
30 self.bullet_launch_offset = (0, 0)
48
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
31 self.vulnerable = True
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
32 self.death_callback = None
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
33 self.low_life_callback = None
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
34 self.low_life_trigger = None
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
35 self.timeout = None
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
36 self.remaining_lives = -1
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
37
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
38 self.bullet_launch_interval = 0
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
39 self.delay_attack = False
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
41 self.death_anim = None
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
42 self.movement_dependant_sprites = None
22
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
43 self.direction = None
20
6ebf9539c077 Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents: 18
diff changeset
44 self.interpolator = None #TODO
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 self.angle = 0.
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 self.speed = 0.
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 self.rotation_speed = 0.
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48 self.acceleration = 0.
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
50 self.hitbox = (0, 0)
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
51
48
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
52
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
53 def set_bullet_attributes(self, bullet_anim, launch_anim, bullets_per_shot,
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
54 number_of_shots, speed, unknown, launch_angle,
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
55 angle, flags):
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
56 self.bullet_attributes = (1, bullet_anim, launch_anim, bullets_per_shot,
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
57 number_of_shots, speed, unknown, launch_angle,
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
58 angle, flags)
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
59 if not self.delay_attack:
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
60 pass
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
61 #TODO: actually fire
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
62
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
63
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
64 def set_anim(self, index):
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
65 self._anm, self._sprite = self._anm_wrapper.get_sprite(index)
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
66
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
67
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
68 def set_pos(self, x, y, z):
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
69 self.x, self.y = x, y
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
70 self.interpolator = Interpolator((x, y))
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
71 self.interpolator.set_interpolation_start(self.frame, (x, y))
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
72
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
73
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
74 def move_to(self, duration, x, y, z):
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
75 self.interpolator.set_interpolation_end(self.frame + duration, (x, y))
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
76
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
77
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
78 def is_visible(self, screen_width, screen_height):
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
79 if not self._sprite:
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
80 return False
29
afa91be769ae Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents: 23
diff changeset
81
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
82 tx, ty, tw, th = self._sprite.texcoords
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
83 if self._sprite.corner_relative_placement:
29
afa91be769ae Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents: 23
diff changeset
84 raise Exception #TODO
afa91be769ae Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents: 23
diff changeset
85 else:
afa91be769ae Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents: 23
diff changeset
86 max_x = tw / 2.
afa91be769ae Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents: 23
diff changeset
87 max_y = th / 2.
afa91be769ae Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents: 23
diff changeset
88 min_x = -max_x
afa91be769ae Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents: 23
diff changeset
89 min_y = -max_y
afa91be769ae Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents: 23
diff changeset
90
afa91be769ae Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents: 23
diff changeset
91 if any((min_x >= screen_width - self.x,
afa91be769ae Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents: 23
diff changeset
92 max_x <= -self.x,
afa91be769ae Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents: 23
diff changeset
93 min_y >= screen_height - self.y,
afa91be769ae Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents: 23
diff changeset
94 max_y <= -self.y)):
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
95 return False
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
96 return True
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
97
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
98
36
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
99 def get_objects_by_texture(self):
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
100 objects_by_texture = {}
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
101 key = self._anm.first_name, self._anm.secondary_name
36
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
102 if not key in objects_by_texture:
37
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 36
diff changeset
103 objects_by_texture[key] = (0, [], [], [])
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
104 vertices = tuple((x + self.x, y + self.y, z) for x, y, z in self._sprite._vertices)
36
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
105 objects_by_texture[key][1].extend(vertices)
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
106 objects_by_texture[key][2].extend(self._sprite._uvs)
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
107 objects_by_texture[key][3].extend(self._sprite._colors)
36
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
108 #TODO: effects/bullet launch
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
109 return objects_by_texture
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
110
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
111
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
112 def update(self, frame):
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
113 x, y = self.x, self.y
20
6ebf9539c077 Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents: 18
diff changeset
114 if self.interpolator:
6ebf9539c077 Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents: 18
diff changeset
115 self.interpolator.update(self.frame)
6ebf9539c077 Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents: 18
diff changeset
116 x, y = self.interpolator.values
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
117
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
118 self.speed += self.acceleration #TODO: units? Execution order?
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
119 self.angle += self.rotation_speed #TODO: units? Execution order?
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
120
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
121 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
122 if self._type & 2:
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
123 x -= dx
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
124 else:
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
125 x += dx
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
126 y += dy
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
127
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
128 if self.movement_dependant_sprites:
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
129 #TODO: is that really how it works?
22
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
130 if x < self.x:
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
131 self.set_anim(self.movement_dependant_sprites[2])
22
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
132 self.direction = -1
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
133 elif x > self.x:
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
134 self.set_anim(self.movement_dependant_sprites[3])
22
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
135 self.direction = +1
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
136 elif self.direction is not None:
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
137 self.set_anim(self.movement_dependant_sprites[{-1: 0, +1:1}[self.direction]])
22
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
138 self.direction = None
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
139
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
140 self.x, self.y = x, y
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
141 if self._sprite:
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
142 changed = self._sprite.update()
32
47543594ff66 Can't stop optimizing!
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
143 visible = self.is_visible(384, 448)
47543594ff66 Can't stop optimizing!
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
144 if changed and visible:
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
145 self._sprite.update_vertices_uvs_colors()
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
146 elif not self._sprite.playing:
34
4d93d45ecb62 Fix animation script flow handling
Thibaut Girka <thib@sitedethib.com>
parents: 33
diff changeset
147 visible = False
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
148 self._sprite = None
32
47543594ff66 Can't stop optimizing!
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
149 else:
47543594ff66 Can't stop optimizing!
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
150 visible = False
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
151
36
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
152
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
153 self.frame += 1
32
47543594ff66 Can't stop optimizing!
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
154 return visible
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
155
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
156
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
157
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
158 class EnemyManager(object):
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
159 def __init__(self, stage, anm_wrapper, ecl, game_state):
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
160 self._game_state = game_state
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
161 self.stage = stage
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
162 self.anm_wrapper = anm_wrapper
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
163 self.main = []
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
164 self.ecl = ecl
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
165 self.objects_by_texture = {}
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
166 self.enemies = []
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
167 self.processes = []
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
168
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
169 # Populate main
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
170 for frame, sub, instr_type, args in ecl.main:
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
171 if not self.main or self.main[-1][0] < frame:
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
172 self.main.append((frame, [(sub, instr_type, args)]))
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
173 elif self.main[-1][0] == frame:
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
174 self.main[-1][1].append((sub, instr_type, args))
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
175
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
176
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
177 def update(self, frame):
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
178 if self.main and self.main[0][0] == frame:
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
179 for sub, instr_type, args in self.main.pop(0)[1]:
20
6ebf9539c077 Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents: 18
diff changeset
180 if instr_type in (0, 2, 4, 6): # Normal/mirrored enemy
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
181 x, y, z, life, unknown1, unknown2, unknown3 = args
40
ce662b372ee0 HANDLE RANDOM PLACEMENT (main flag 4) with results identical to 102h.exe \o/ \o/ \o/
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
182 if instr_type & 4:
ce662b372ee0 HANDLE RANDOM PLACEMENT (main flag 4) with results identical to 102h.exe \o/ \o/ \o/
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
183 if x < -990:
ce662b372ee0 HANDLE RANDOM PLACEMENT (main flag 4) with results identical to 102h.exe \o/ \o/ \o/
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
184 x = random.rand_double() * 368 #102h.exe@0x411820
ce662b372ee0 HANDLE RANDOM PLACEMENT (main flag 4) with results identical to 102h.exe \o/ \o/ \o/
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
185 if y < -990:
ce662b372ee0 HANDLE RANDOM PLACEMENT (main flag 4) with results identical to 102h.exe \o/ \o/ \o/
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
186 y = random.rand_double() * 416 #102h.exe@0x41184b
ce662b372ee0 HANDLE RANDOM PLACEMENT (main flag 4) with results identical to 102h.exe \o/ \o/ \o/
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
187 if z < -990:
ce662b372ee0 HANDLE RANDOM PLACEMENT (main flag 4) with results identical to 102h.exe \o/ \o/ \o/
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
188 y = random.rand_double() * 800 #102h.exe@0x411881
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
189 enemy = Enemy((x, y), life, instr_type, self.anm_wrapper)
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
190 self.enemies.append(enemy)
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
191 self.processes.append(ECLRunner(self.ecl, sub, enemy, self._game_state))
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
192
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
193
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
194 # Run processes
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
195 self.processes[:] = (process for process in self.processes if process.run_iteration())
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
196
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
197 # Filter of destroyed enemies
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
198 self.enemies[:] = (enemy for enemy in self.enemies if not enemy._removed)
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
199
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
200 # Update enemies
32
47543594ff66 Can't stop optimizing!
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
201 visible_enemies = [enemy for enemy in self.enemies if enemy.update(frame)]
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
202
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
203 # Add enemies to vertices/uvs
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
204 self.objects_by_texture = {}
32
47543594ff66 Can't stop optimizing!
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
205 for enemy in visible_enemies:
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
206 if enemy.is_visible(384, 448): #TODO
37
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 36
diff changeset
207 for key, (count, vertices, uvs, colors) in enemy.get_objects_by_texture().items():
36
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
208 if not key in self.objects_by_texture:
37
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 36
diff changeset
209 self.objects_by_texture[key] = (0, [], [], [])
36
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
210 self.objects_by_texture[key][1].extend(vertices)
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
211 self.objects_by_texture[key][2].extend(uvs)
37
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 36
diff changeset
212 self.objects_by_texture[key][3].extend(colors)
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 36
diff changeset
213 for key, (nb_vertices, vertices, uvs, colors) in self.objects_by_texture.items():
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
214 nb_vertices = len(vertices)
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
215 vertices = pack('f' * (3 * nb_vertices), *chain(*vertices))
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
216 uvs = pack('f' * (2 * nb_vertices), *chain(*uvs))
37
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 36
diff changeset
217 colors = pack('B' * (4 * nb_vertices), *chain(*colors))
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 36
diff changeset
218 self.objects_by_texture[key] = (nb_vertices, vertices, uvs, colors)
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
219