comparison pytouhou/game/enemymanager.py @ 20:6ebf9539c077

Handle more enemies types and movements
author Thibaut Girka <thib@sitedethib.com>
date Thu, 11 Aug 2011 12:13:48 +0200
parents ca26a84916cb
children bf225780973f
comparison
equal deleted inserted replaced
19:ca7886296d4a 20:6ebf9539c077
1 from itertools import chain 1 from itertools import chain
2 from io import BytesIO 2 from io import BytesIO
3 import os 3 import os
4 from struct import unpack, pack 4 from struct import unpack, pack
5 from pytouhou.utils.interpolator import Interpolator
5 from pytouhou.game.sprite import Sprite 6 from pytouhou.game.sprite import Sprite
6 from math import cos, sin 7 from math import cos, sin, atan2
7 8
8 9
9 class Enemy(object): 10 class Enemy(object):
10 def __init__(self, pos, life, _type, script, anms): 11 def __init__(self, pos, life, _type, script, anms):
11 self.anms = tuple(anms) 12 self.anms = tuple(anms)
15 self.life = life 16 self.life = life
16 self.type = _type 17 self.type = _type
17 self.frame = 0 18 self.frame = 0
18 self.sprite = None 19 self.sprite = None
19 20
21 self.interpolator = None #TODO
20 self.angle = 0. 22 self.angle = 0.
21 self.speed = 0. 23 self.speed = 0.
22 self.rotation_speed = 0. 24 self.rotation_speed = 0.
23 self.acceleration = 0. 25 self.acceleration = 0.
24 26
36 self.sprite = Sprite(self.anms[0], script_index) 38 self.sprite = Sprite(self.anms[0], script_index)
37 self.anm = self.anms[0] 39 self.anm = self.anms[0]
38 else: 40 else:
39 self.sprite = Sprite(self.anms[1], script_index) 41 self.sprite = Sprite(self.anms[1], script_index)
40 self.anm = self.anms[1] 42 self.anm = self.anms[1]
43 elif instr_type == 43: # set_pos
44 self.x, self.y, z = unpack('<fff', args)
45 self.interpolator = Interpolator((self.x, self.y)) #TODO: better interpolation
46 self.interpolator.set_interpolation_start(self.frame, (self.x, self.y))
41 elif instr_type == 45: # set_angle_speed 47 elif instr_type == 45: # set_angle_speed
42 self.angle, self.speed = unpack('<ff', args) 48 self.angle, self.speed = unpack('<ff', args)
43 elif instr_type == 46: # set_angle 49 elif instr_type == 46: # set_angle
44 self.rotation_speed, = unpack('<f', args) 50 self.rotation_speed, = unpack('<f', args)
45 elif instr_type == 47: # set_speed 51 elif instr_type == 47: # set_speed
46 self.speed, = unpack('<f', args) 52 self.speed, = unpack('<f', args)
47 elif instr_type == 48: # set_acceleration 53 elif instr_type == 48: # set_acceleration
48 self.acceleration, = unpack('<f', args) 54 self.acceleration, = unpack('<f', args)
55 elif instr_type == 51: # move_towards_player #TODO: main
56 unknown, self.speed = unpack('<If', args) #TODO: unknown
57 player_x, player_y = 192., 400.#TODO
58 self.angle = atan2(player_y - self.y, player_x - self.x)
59 elif instr_type == 57:
60 duration, x, y, z = unpack('<Ifff', args)
61 self.interpolator.set_interpolation_end(self.frame + duration, (x, y))
49 if self.sprite: 62 if self.sprite:
50 self.sprite.update() 63 self.sprite.update()
51 64
65 if self.interpolator:
66 self.interpolator.update(self.frame)
67 x, y = self.interpolator.values
68 dx, dy = x - self.x, y - self.y
69 #TODO: animations
70 if abs(dx) > abs(dy):
71 pass #TODO
72 else:
73 pass #TODO
74 self.x, self.y = x, y
52 self.speed += self.acceleration #TODO: units? Execution order? 75 self.speed += self.acceleration #TODO: units? Execution order?
53 self.angle += self.rotation_speed #TODO: units? Execution order? 76 self.angle += self.rotation_speed #TODO: units? Execution order?
54 77
55 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed 78 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed
56 if self.type == 2: 79 if self.type & 2:
57 self.x -= dx 80 self.x -= dx
58 else: 81 else:
59 self.x += dx 82 self.x += dx
60 self.y += dy 83 self.y += dy
61 84
93 116
94 117
95 def update(self, frame): 118 def update(self, frame):
96 if self.main and self.main[0][0] == frame: 119 if self.main and self.main[0][0] == frame:
97 for sub, instr_type, args in self.main.pop(0)[1]: 120 for sub, instr_type, args in self.main.pop(0)[1]:
98 if instr_type in (0, 2): # Normal/mirrored enemy 121 if instr_type in (0, 2, 4, 6): # Normal/mirrored enemy
99 x, y, z, life, unknown1, unknown2, unknown3 = args 122 x, y, z, life, unknown1, unknown2, unknown3 = args
100 self.enemies.append(Enemy((x, y), life, instr_type, self.subs[sub], self.anims)) 123 self.enemies.append(Enemy((x, y), life, instr_type, self.subs[sub], self.anims))
101 124
102 # Update enemies 125 # Update enemies
103 for enemy in tuple(self.enemies): 126 for enemy in tuple(self.enemies):