Mercurial > touhou
annotate pytouhou/game/enemymanager.py @ 32:47543594ff66
Can't stop optimizing!
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sat, 13 Aug 2011 00:14:29 +0200 |
parents | afa91be769ae |
children | 1f21bcc0845b |
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 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
11 class Enemy(object): |
23
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
12 def __init__(self, pos, life, _type, ecl_runner, anm_wrapper): |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
13 self.anm_wrapper = anm_wrapper |
18
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
14 self.anm = None |
23
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
15 self.ecl_runner = ecl_runner |
18
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
16 self.x, self.y = pos |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
17 self.life = life |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
18 self.type = _type |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
19 self.frame = 0 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
20 self.sprite = None |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
21 |
23
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
22 self.death_sprite = None |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
23 self.movement_dependant_sprites = None |
22 | 24 self.direction = None |
20
6ebf9539c077
Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents:
18
diff
changeset
|
25 self.interpolator = None #TODO |
18
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
26 self.angle = 0. |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
27 self.speed = 0. |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
28 self.rotation_speed = 0. |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
29 self.acceleration = 0. |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
30 |
23
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
31 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
|
32 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
33 self.ecl_runner.implementation.update({97: ('I', self.set_sprite), |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
34 98: ('HHHHHH', self.set_multiple_sprites), |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
35 45: ('ff', self.set_angle_speed), |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
36 43: ('fff', self.set_pos), |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
37 46: ('f', self.set_rotation_speed), |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
38 47: ('f', self.set_speed), |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
39 48: ('f', self.set_acceleration), |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
40 51: ('If', self.target_player), |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
41 57: ('Ifff', self.move_to), |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
42 100: ('I', self.set_death_sprite), |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
43 103: ('fff', self.set_hitbox)}) #TODO |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
44 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
45 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
46 def set_death_sprite(self, sprite_index): |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
47 self.death_sprite = sprite_index % 256 #TODO |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
48 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
49 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
50 def set_hitbox(self, width, height, depth): |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
51 self.hitbox = (width, height) |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
52 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
53 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
54 def set_sprite(self, sprite_index): |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
55 self.anm, self.sprite = self.anm_wrapper.get_sprite(sprite_index) |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
56 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
57 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
58 def set_multiple_sprites(self, default, end_left, end_right, left, right, unknown): |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
59 self.movement_dependant_sprites = end_left, end_right, left, right, unknown |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
60 self.anm, self.sprite = self.anm_wrapper.get_sprite(default) |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
61 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
62 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
63 def set_angle_speed(self, angle, speed): |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
64 self.angle, self.speed = angle, speed |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
65 |
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 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
|
68 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
|
69 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
|
70 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
|
71 |
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 def set_rotation_speed(self, speed): |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
74 self.rotation_speed = speed |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
75 |
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 def set_speed(self, speed): |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
78 self.speed = speed |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
79 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
80 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
81 def set_acceleration(self, acceleration): |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
82 self.acceleration = acceleration |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
83 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
84 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
85 def target_player(self, unknown, speed): |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
86 self.speed = speed #TODO: unknown |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
87 player_x, player_y = 192., 400.#TODO |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
88 self.angle = atan2(player_y - self.y, player_x - self.x) |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
89 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
90 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
91 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
|
92 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
|
93 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
94 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
95 def is_visible(self, screen_width, screen_height): |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
96 if not self.sprite: |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
97 return False |
29
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
23
diff
changeset
|
98 |
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
23
diff
changeset
|
99 tx, ty, tw, th = self.sprite.texcoords |
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
23
diff
changeset
|
100 if self.sprite.corner_relative_placement: |
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
23
diff
changeset
|
101 raise Exception #TODO |
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
23
diff
changeset
|
102 else: |
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
23
diff
changeset
|
103 max_x = tw / 2. |
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
23
diff
changeset
|
104 max_y = th / 2. |
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
23
diff
changeset
|
105 min_x = -max_x |
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
23
diff
changeset
|
106 min_y = -max_y |
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
23
diff
changeset
|
107 |
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
23
diff
changeset
|
108 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
|
109 max_x <= -self.x, |
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
23
diff
changeset
|
110 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
|
111 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
|
112 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
|
113 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
|
114 |
18
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
115 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
116 def update(self, frame): |
23
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
117 self.ecl_runner.update() |
18
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
118 |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
119 x, y = self.x, self.y |
20
6ebf9539c077
Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents:
18
diff
changeset
|
120 if self.interpolator: |
6ebf9539c077
Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents:
18
diff
changeset
|
121 self.interpolator.update(self.frame) |
6ebf9539c077
Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents:
18
diff
changeset
|
122 x, y = self.interpolator.values |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
123 |
18
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
124 self.speed += self.acceleration #TODO: units? Execution order? |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
125 self.angle += self.rotation_speed #TODO: units? Execution order? |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
126 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
127 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed |
20
6ebf9539c077
Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents:
18
diff
changeset
|
128 if self.type & 2: |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
129 x -= dx |
18
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
130 else: |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
131 x += dx |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
132 y += dy |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
133 |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
134 if self.movement_dependant_sprites: |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
135 #TODO: is that really how it works? |
22 | 136 if x < self.x: |
137 self.anm, self.sprite = self.anm_wrapper.get_sprite(self.movement_dependant_sprites[2]) | |
138 self.direction = -1 | |
139 elif x > self.x: | |
140 self.anm, self.sprite = self.anm_wrapper.get_sprite(self.movement_dependant_sprites[3]) | |
141 self.direction = +1 | |
142 #TODO: end_animation | |
143 elif self.direction is not None: | |
144 self.anm, self.sprite = self.anm_wrapper.get_sprite(self.movement_dependant_sprites[{-1: 0, +1:1}[self.direction]]) | |
145 self.direction = None | |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
146 |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
147 self.x, self.y = x, y |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
148 if self.sprite: |
32 | 149 changed = self.sprite.update() |
150 visible = self.is_visible(384, 448) | |
151 if changed and visible: | |
29
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
23
diff
changeset
|
152 self.sprite.update_uvs_vertices() |
32 | 153 else: |
154 visible = False | |
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 self.frame += 1 |
32 | 157 return visible |
18
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
158 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
159 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
160 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
161 class EnemyManager(object): |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
162 def __init__(self, stage, anm_wrapper, ecl): |
18
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
163 self.stage = stage |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
20
diff
changeset
|
164 self.anm_wrapper = anm_wrapper |
18
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
165 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
|
166 self.ecl = ecl |
18
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
167 self.objects_by_texture = {} |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
168 self.enemies = [] |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
169 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
170 # Populate main |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
171 for frame, sub, instr_type, args in ecl.main: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
172 if not self.main or self.main[-1][0] < frame: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
173 self.main.append((frame, [(sub, instr_type, args)])) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
174 elif self.main[-1][0] == frame: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
175 self.main[-1][1].append((sub, instr_type, args)) |
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 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
178 def update(self, frame): |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
179 if self.main and self.main[0][0] == frame: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
180 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
|
181 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
|
182 x, y, z, life, unknown1, unknown2, unknown3 = args |
23
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
183 ecl_runner = ECLRunner(self.ecl, sub) |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
184 enemy = Enemy((x, y), life, instr_type, ecl_runner, self.anm_wrapper) |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
185 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
186 def _enemy_deleter(unknown): #TOOD: unknown |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
187 self.enemies.remove(enemy) |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
188 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
189 ecl_runner.implementation[1] = ('I', _enemy_deleter) |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
190 |
444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents:
22
diff
changeset
|
191 self.enemies.append(enemy) |
18
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
192 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
193 # Update enemies |
32 | 194 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
|
195 |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
196 # Add enemies to vertices/uvs |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
197 self.objects_by_texture = {} |
32 | 198 for enemy in visible_enemies: |
18
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
199 ox, oy = enemy.x, enemy.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
|
200 if enemy.is_visible(384, 448): #TODO |
18
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
201 key = enemy.anm.first_name, enemy.anm.secondary_name |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
202 if not key in self.objects_by_texture: |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
203 self.objects_by_texture[key] = (0, [], []) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
204 vertices = tuple((x + ox, y + oy, z) for x, y, z in enemy.sprite._vertices) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
205 self.objects_by_texture[key][2].extend(enemy.sprite._uvs) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
206 self.objects_by_texture[key][1].extend(vertices) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
207 for key, (nb_vertices, vertices, uvs) in self.objects_by_texture.items(): |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
208 nb_vertices = len(vertices) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
209 vertices = pack('f' * (3 * nb_vertices), *chain(*vertices)) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
210 uvs = pack('f' * (2 * nb_vertices), *chain(*uvs)) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
211 self.objects_by_texture[key] = (nb_vertices, vertices, uvs) |
ca26a84916cb
Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
212 |