comparison pytouhou/game/enemymanager.py @ 18:ca26a84916cb

Add preliminary ECL viewer/interpreter.
author Thibaut Girka <thib@sitedethib.com>
date Tue, 09 Aug 2011 11:40:48 +0200
parents
children 6ebf9539c077
comparison
equal deleted inserted replaced
17:d940d004b840 18:ca26a84916cb
1 from itertools import chain
2 from io import BytesIO
3 import os
4 from struct import unpack, pack
5 from pytouhou.game.sprite import Sprite
6 from math import cos, sin
7
8
9 class Enemy(object):
10 def __init__(self, pos, life, _type, script, anms):
11 self.anms = tuple(anms)
12 self.anm = None
13 self.script = list(script)
14 self.x, self.y = pos
15 self.life = life
16 self.type = _type
17 self.frame = 0
18 self.sprite = None
19
20 self.angle = 0.
21 self.speed = 0.
22 self.rotation_speed = 0.
23 self.acceleration = 0.
24
25
26 def update(self, frame):
27 if not self.script:
28 return True
29 if self.script[0][0] == self.frame:
30 for instr_type, rank_mask, param_mask, args in self.script.pop(0)[1]:
31 if instr_type == 1: # delete
32 return False
33 elif instr_type == 97: # set_enemy_sprite
34 script_index, = unpack('<I', args)
35 if script_index in self.anms[0].scripts:
36 self.sprite = Sprite(self.anms[0], script_index)
37 self.anm = self.anms[0]
38 else:
39 self.sprite = Sprite(self.anms[1], script_index)
40 self.anm = self.anms[1]
41 elif instr_type == 45: # set_angle_speed
42 self.angle, self.speed = unpack('<ff', args)
43 elif instr_type == 46: # set_angle
44 self.rotation_speed, = unpack('<f', args)
45 elif instr_type == 47: # set_speed
46 self.speed, = unpack('<f', args)
47 elif instr_type == 48: # set_acceleration
48 self.acceleration, = unpack('<f', args)
49 if self.sprite:
50 self.sprite.update()
51
52 self.speed += self.acceleration #TODO: units? Execution order?
53 self.angle += self.rotation_speed #TODO: units? Execution order?
54
55 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed
56 if self.type == 2:
57 self.x -= dx
58 else:
59 self.x += dx
60 self.y += dy
61
62 self.frame += 1
63 return True
64
65
66
67 class EnemyManager(object):
68 def __init__(self, stage, anims, ecl):
69 self.stage = stage
70 self.anims = tuple(anims)
71 self.main = []
72 self.subs = {}
73 self.objects_by_texture = {}
74 self.enemies = []
75
76 # Populate main
77 for frame, sub, instr_type, args in ecl.main:
78 if not self.main or self.main[-1][0] < frame:
79 self.main.append((frame, [(sub, instr_type, args)]))
80 elif self.main[-1][0] == frame:
81 self.main[-1][1].append((sub, instr_type, args))
82
83
84 # Populate subs
85 for i, sub in enumerate(ecl.subs):
86 for frame, instr_type, rank_mask, param_mask, args in sub:
87 if i not in self.subs:
88 self.subs[i] = []
89 if not self.subs[i] or self.subs[i][-1][0] < frame:
90 self.subs[i].append((frame, [(instr_type, rank_mask, param_mask, args)]))
91 elif self.subs[i][-1][0] == frame:
92 self.subs[i][-1][1].append((instr_type, rank_mask, param_mask, args))
93
94
95 def update(self, frame):
96 if self.main and self.main[0][0] == frame:
97 for sub, instr_type, args in self.main.pop(0)[1]:
98 if instr_type in (0, 2): # Normal/mirrored enemy
99 x, y, z, life, unknown1, unknown2, unknown3 = args
100 self.enemies.append(Enemy((x, y), life, instr_type, self.subs[sub], self.anims))
101
102 # Update enemies
103 for enemy in tuple(self.enemies):
104 if not enemy.update(frame):
105 self.enemies.remove(enemy)
106 continue
107
108 # Add enemies to vertices/uvs
109 self.objects_by_texture = {}
110 for enemy in self.enemies:
111 ox, oy = enemy.x, enemy.y
112 if enemy.sprite:
113 key = enemy.anm.first_name, enemy.anm.secondary_name
114 if not key in self.objects_by_texture:
115 self.objects_by_texture[key] = (0, [], [])
116 vertices = tuple((x + ox, y + oy, z) for x, y, z in enemy.sprite._vertices)
117 self.objects_by_texture[key][2].extend(enemy.sprite._uvs)
118 self.objects_by_texture[key][1].extend(vertices)
119 for key, (nb_vertices, vertices, uvs) in self.objects_by_texture.items():
120 nb_vertices = len(vertices)
121 vertices = pack('f' * (3 * nb_vertices), *chain(*vertices))
122 uvs = pack('f' * (2 * nb_vertices), *chain(*uvs))
123 self.objects_by_texture[key] = (nb_vertices, vertices, uvs)
124