annotate pytouhou/game/enemymanager.py @ 30:e3ba2fa966f6

Various optimizations
author Thibaut Girka <thib@sitedethib.com>
date Fri, 12 Aug 2011 23:47:34 +0200
parents afa91be769ae
children 47543594ff66
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
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
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
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 #TODO
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
118 #if not self.script:
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
119 # 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
120 #if self.script[0][0] == self.frame:
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
121 # for instr_type, rank_mask, param_mask, args in self.script.pop(0)[1]:
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
122 # if instr_type == 1: # delete
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
123 # 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
124 self.ecl_runner.update()
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
125
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
126 x, y = self.x, self.y
20
6ebf9539c077 Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents: 18
diff changeset
127 if self.interpolator:
6ebf9539c077 Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents: 18
diff changeset
128 self.interpolator.update(self.frame)
6ebf9539c077 Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents: 18
diff changeset
129 x, y = self.interpolator.values
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
130
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
131 self.speed += self.acceleration #TODO: units? Execution order?
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
132 self.angle += self.rotation_speed #TODO: units? Execution order?
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
133
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
134 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
135 if self.type & 2:
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
136 x -= dx
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
137 else:
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
138 x += dx
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
139 y += dy
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
140
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
141 if self.movement_dependant_sprites:
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
142 #TODO: is that really how it works?
22
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
143 if x < self.x:
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
144 self.anm, self.sprite = self.anm_wrapper.get_sprite(self.movement_dependant_sprites[2])
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
145 self.direction = -1
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
146 elif x > self.x:
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
147 self.anm, self.sprite = self.anm_wrapper.get_sprite(self.movement_dependant_sprites[3])
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
148 self.direction = +1
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
149 #TODO: end_animation
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
150 elif self.direction is not None:
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
151 self.anm, self.sprite = self.anm_wrapper.get_sprite(self.movement_dependant_sprites[{-1: 0, +1:1}[self.direction]])
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
152 self.direction = None
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
153
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
154 self.x, self.y = x, y
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
155 if self.sprite:
29
afa91be769ae Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents: 23
diff changeset
156 if self.sprite.update() and self.is_visible(384, 448): #TODO: screen size
afa91be769ae Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents: 23
diff changeset
157 self.sprite.update_uvs_vertices()
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 self.frame += 1
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
160 return True
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
161
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
162
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
163
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
164 class EnemyManager(object):
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
165 def __init__(self, stage, anm_wrapper, ecl):
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
166 self.stage = stage
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
167 self.anm_wrapper = anm_wrapper
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
168 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
169 self.ecl = ecl
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
170 self.objects_by_texture = {}
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
171 self.enemies = []
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
172
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
173 # Populate main
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
174 for frame, sub, instr_type, args in ecl.main:
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
175 if not self.main or self.main[-1][0] < frame:
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
176 self.main.append((frame, [(sub, instr_type, args)]))
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
177 elif self.main[-1][0] == frame:
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
178 self.main[-1][1].append((sub, instr_type, args))
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
179
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
180
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
181 def update(self, frame):
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
182 if self.main and self.main[0][0] == frame:
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
183 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
184 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
185 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
186 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
187 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
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 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
190 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
191
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
192 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
193
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
194 self.enemies.append(enemy)
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 # Update enemies
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
197 for enemy in tuple(self.enemies):
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
198 if not enemy.update(frame):
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
199 self.enemies.remove(enemy)
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
200 continue
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
201
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
202 # Add enemies to vertices/uvs
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
203 self.objects_by_texture = {}
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
204 for enemy in self.enemies:
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
205 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
206 if enemy.is_visible(384, 448): #TODO
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
207 key = enemy.anm.first_name, enemy.anm.secondary_name
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
208 if not key in self.objects_by_texture:
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
209 self.objects_by_texture[key] = (0, [], [])
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
210 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
211 self.objects_by_texture[key][2].extend(enemy.sprite._uvs)
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
212 self.objects_by_texture[key][1].extend(vertices)
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
213 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
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))
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
217 self.objects_by_texture[key] = (nb_vertices, vertices, uvs)
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
218