comparison pytouhou/game/enemy.py @ 151:5cf927cbd9c5

Merge GameState into Game. TODO: Merge PlayerState into Player
author Thibaut Girka <thib@sitedethib.com>
date Tue, 04 Oct 2011 23:32:02 +0200
parents ea21bb37febe
children 86807b8a63bd
comparison
equal deleted inserted replaced
150:4f46717390aa 151:5cf927cbd9c5
20 from pytouhou.game.bullet import Bullet 20 from pytouhou.game.bullet import Bullet
21 from math import cos, sin, atan2, pi 21 from math import cos, sin, atan2, pi
22 22
23 23
24 class Enemy(object): 24 class Enemy(object):
25 def __init__(self, pos, life, _type, anm_wrapper, game_state, pop_enemy): 25 def __init__(self, pos, life, _type, anm_wrapper, game, pop_enemy):
26 self._game_state = game_state 26 self._game = game
27 self._anm_wrapper = anm_wrapper 27 self._anm_wrapper = anm_wrapper
28 self._sprite = None 28 self._sprite = None
29 self._anmrunner = None 29 self._anmrunner = None
30 self._removed = False 30 self._removed = False
31 self._type = _type 31 self._type = _type
84 84
85 def fire(self): 85 def fire(self):
86 (type_, type_idx, sprite_idx_offset, bullets_per_shot, number_of_shots, 86 (type_, type_idx, sprite_idx_offset, bullets_per_shot, number_of_shots,
87 speed, speed2, launch_angle, angle, flags) = self.bullet_attributes 87 speed, speed2, launch_angle, angle, flags) = self.bullet_attributes
88 88
89 bullet_type = self._game_state.bullet_types[type_idx] 89 bullet_type = self._game.bullet_types[type_idx]
90 90
91 ox, oy = self.bullet_launch_offset 91 ox, oy = self.bullet_launch_offset
92 launch_pos = self.x + ox, self.y + oy 92 launch_pos = self.x + ox, self.y + oy
93 93
94 if speed2 < 0.3: 94 if speed2 < 0.3:
105 if type_ == 71: 105 if type_ == 71:
106 launch_angle += pi / bullets_per_shot 106 launch_angle += pi / bullets_per_shot
107 if type_ != 75: 107 if type_ != 75:
108 launch_angle -= angle * (bullets_per_shot - 1) / 2. 108 launch_angle -= angle * (bullets_per_shot - 1) / 2.
109 109
110 bullets = self._game_state.bullets 110 bullets = self._game.bullets
111 nb_bullets_max = self._game_state.nb_bullets_max 111 nb_bullets_max = self._game.nb_bullets_max
112 112
113 for shot_nb in range(number_of_shots): 113 for shot_nb in range(number_of_shots):
114 shot_speed = speed if shot_nb == 0 else speed + (speed2 - speed) * float(shot_nb) / float(number_of_shots) 114 shot_speed = speed if shot_nb == 0 else speed + (speed2 - speed) * float(shot_nb) / float(number_of_shots)
115 bullet_angle = launch_angle 115 bullet_angle = launch_angle
116 for bullet_nb in range(bullets_per_shot): 116 for bullet_nb in range(bullets_per_shot):
117 if nb_bullets_max is not None and len(bullets) == nb_bullets_max: 117 if nb_bullets_max is not None and len(bullets) == nb_bullets_max:
118 break 118 break
119 119
120 if type_ == 75: # 102h.exe@0x4138cf 120 if type_ == 75: # 102h.exe@0x4138cf
121 bullet_angle = self._game_state.prng.rand_double() * (launch_angle - angle) + angle 121 bullet_angle = self._game.prng.rand_double() * (launch_angle - angle) + angle
122 if type_ in (74, 75): # 102h.exe@0x4138cf 122 if type_ in (74, 75): # 102h.exe@0x4138cf
123 shot_speed = self._game_state.prng.rand_double() * (speed - speed2) + speed2 123 shot_speed = self._game.prng.rand_double() * (speed - speed2) + speed2
124 bullets.append(Bullet(launch_pos, bullet_type, sprite_idx_offset, 124 bullets.append(Bullet(launch_pos, bullet_type, sprite_idx_offset,
125 bullet_angle, shot_speed, 125 bullet_angle, shot_speed,
126 self.extended_bullet_attributes, 126 self.extended_bullet_attributes,
127 flags, player, self._game_state)) 127 flags, player, self._game))
128 bullet_angle += angle 128 bullet_angle += angle
129 129
130 130
131 def select_player(self, players=None): 131 def select_player(self, players=None):
132 return (players or self._game_state.players)[0] #TODO 132 return (players or self._game.players)[0] #TODO
133 133
134 134
135 def get_player_angle(self, player=None, pos=None): 135 def get_player_angle(self, player=None, pos=None):
136 player = player or self.select_player() 136 player = player or self.select_player()
137 x, y = pos or (self.x, self.y) 137 x, y = pos or (self.x, self.y)
145 145
146 146
147 def set_pos(self, x, y, z): 147 def set_pos(self, x, y, z):
148 self.x, self.y = x, y 148 self.x, self.y = x, y
149 self.interpolator = Interpolator((x, y)) 149 self.interpolator = Interpolator((x, y))
150 self.interpolator.set_interpolation_start(self._game_state.frame, (x, y)) 150 self.interpolator.set_interpolation_start(self._game.frame, (x, y))
151 151
152 152
153 def move_to(self, duration, x, y, z, formula): 153 def move_to(self, duration, x, y, z, formula):
154 if not self.interpolator: 154 if not self.interpolator:
155 frame = self._game_state.frame 155 frame = self._game.frame
156 self.interpolator = Interpolator((self.x, self.y), formula) 156 self.interpolator = Interpolator((self.x, self.y), formula)
157 self.interpolator.set_interpolation_start(frame, (self.x, self.y)) 157 self.interpolator.set_interpolation_start(frame, (self.x, self.y))
158 self.interpolator.set_interpolation_end(frame + duration - 1, (x, y)) 158 self.interpolator.set_interpolation_end(frame + duration - 1, (x, y))
159 159
160 self.speed = 0. 160 self.speed = 0.
161 self.angle = atan2(y - self.y, x - self.x) 161 self.angle = atan2(y - self.y, x - self.x)
162 162
163 163
164 def stop_in(self, duration, formula): 164 def stop_in(self, duration, formula):
165 if not self.speed_interpolator: 165 if not self.speed_interpolator:
166 frame = self._game_state.frame 166 frame = self._game.frame
167 self.speed_interpolator = Interpolator((self.speed,), formula) 167 self.speed_interpolator = Interpolator((self.speed,), formula)
168 self.speed_interpolator.set_interpolation_start(frame, (self.speed,)) 168 self.speed_interpolator.set_interpolation_start(frame, (self.speed,))
169 self.speed_interpolator.set_interpolation_end(frame + duration - 1, (0.,)) 169 self.speed_interpolator.set_interpolation_end(frame + duration - 1, (0.,))
170 170
171 self.speed = 0. 171 self.speed = 0.
192 192
193 193
194 def update(self): 194 def update(self):
195 x, y = self.x, self.y 195 x, y = self.x, self.y
196 if self.interpolator: 196 if self.interpolator:
197 self.interpolator.update(self._game_state.frame) 197 self.interpolator.update(self._game.frame)
198 x, y = self.interpolator.values 198 x, y = self.interpolator.values
199 199
200 self.speed += self.acceleration #TODO: units? Execution order? 200 self.speed += self.acceleration #TODO: units? Execution order?
201 self.angle += self.rotation_speed #TODO: units? Execution order? 201 self.angle += self.rotation_speed #TODO: units? Execution order?
202 202
203 203
204 if self.speed_interpolator: 204 if self.speed_interpolator:
205 self.speed_interpolator.update(self._game_state.frame) 205 self.speed_interpolator.update(self._game.frame)
206 self.speed, = self.speed_interpolator.values 206 self.speed, = self.speed_interpolator.values
207 207
208 208
209 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed 209 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed
210 if self._type & 2: 210 if self._type & 2: