comparison pytouhou/game/bullet.py @ 163:ee0bbde02c48

Implement player bullets in Bullet.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 16 Oct 2011 07:41:26 -0700
parents 7e7368356445
children 0595315d3880
comparison
equal deleted inserted replaced
162:959c8b312918 163:ee0bbde02c48
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 from math import cos, sin, atan2 15 from math import cos, sin, atan2, pi
16 16
17 from pytouhou.utils.interpolator import Interpolator 17 from pytouhou.utils.interpolator import Interpolator
18 from pytouhou.vm.anmrunner import ANMRunner 18 from pytouhou.vm.anmrunner import ANMRunner
19 from pytouhou.game.sprite import Sprite 19 from pytouhou.game.sprite import Sprite
20 20
21 21
22 class Bullet(object): 22 class Bullet(object):
23 def __init__(self, pos, bullet_type, sprite_idx_offset, 23 def __init__(self, pos, bullet_type, sprite_idx_offset,
24 angle, speed, attributes, flags, player, game): 24 angle, speed, attributes, flags, player, game, player_bullet=False):
25 self._game = game 25 self._game = game
26 self._sprite = None 26 self._sprite = None
27 self._anmrunner = None 27 self._anmrunner = None
28 self._removed = False 28 self._removed = False
29 self._launched = False 29 self._launched = False
45 self.x, self.y = pos 45 self.x, self.y = pos
46 self.angle = angle 46 self.angle = angle
47 self.speed = speed 47 self.speed = speed
48 dx, dy = cos(angle) * speed, sin(angle) * speed 48 dx, dy = cos(angle) * speed, sin(angle) * speed
49 self.delta = dx, dy 49 self.delta = dx, dy
50
51 self.player_bullet = player_bullet
50 52
51 #TODO 53 #TODO
52 if flags & 14: 54 if flags & 14:
53 if flags & 2: 55 if flags & 2:
54 index = bullet_type.launch_anim2_index 56 index = bullet_type.launch_anim2_index
66 bullet_type.launch_anim_offsets[sprite_idx_offset]) 68 bullet_type.launch_anim_offsets[sprite_idx_offset])
67 self._anmrunner.run_frame() 69 self._anmrunner.run_frame()
68 else: 70 else:
69 self.launch() 71 self.launch()
70 72
71 self._sprite.angle = angle 73 if self.player_bullet:
74 self._sprite.angle = angle - pi
75 else:
76 self._sprite.angle = angle
72 77
73 78
74 def is_visible(self, screen_width, screen_height): 79 def is_visible(self, screen_width, screen_height):
75 tx, ty, tw, th = self._sprite.texcoords 80 tx, ty, tw, th = self._sprite.texcoords
76 x, y = self.x, self.y 81 x, y = self.x, self.y
90 if sprite_idx_offset is not None: 95 if sprite_idx_offset is not None:
91 self.sprite_idx_offset = sprite_idx_offset 96 self.sprite_idx_offset = sprite_idx_offset
92 97
93 bt = self._bullet_type 98 bt = self._bullet_type
94 self._sprite = Sprite() 99 self._sprite = Sprite()
95 self._sprite.angle = self.angle 100 if self.player_bullet:
101 self._sprite.angle = self.angle - pi
102 else:
103 self._sprite.angle = self.angle
96 self._anmrunner = ANMRunner(bt.anm_wrapper, bt.anim_index, 104 self._anmrunner = ANMRunner(bt.anm_wrapper, bt.anim_index,
97 self._sprite, self.sprite_idx_offset) 105 self._sprite, self.sprite_idx_offset)
98 self._anmrunner.run_frame() 106 self._anmrunner.run_frame()
99 107
100 108
113 121
114 def cancel(self): 122 def cancel(self):
115 # Cancel animation 123 # Cancel animation
116 bt = self._bullet_type 124 bt = self._bullet_type
117 self._sprite = Sprite() 125 self._sprite = Sprite()
118 self._sprite.angle = self.angle 126 if self.player_bullet:
127 self._sprite.angle = self.angle - pi
128 else:
129 self._sprite.angle = self.angle
119 self._anmrunner = ANMRunner(bt.anm_wrapper, bt.cancel_anim_index, 130 self._anmrunner = ANMRunner(bt.anm_wrapper, bt.cancel_anim_index,
120 self._sprite, bt.launch_anim_offsets[self.sprite_idx_offset]) 131 self._sprite, bt.launch_anim_offsets[self.sprite_idx_offset])
121 self._anmrunner.run_frame() 132 self._anmrunner.run_frame()
133 self.delta = self.delta[0] / 2., self.delta[1] / 2.
122 134
123 # Change update method 135 # Change update method
124 self.update = self.update_cancel 136 self.update = self.update_cancel
125 137
126 # Do not use this one for collisions anymore 138 # Do not use this one for collisions anymore
127 self._game.bullets.remove(self) 139 if self.player_bullet:
140 self._game.players_bullets.remove(self)
141 else:
142 self._game.bullets.remove(self)
128 self._game.cancelled_bullets.append(self) 143 self._game.cancelled_bullets.append(self)
129 144
130 145
131 def update(self): 146 def update(self):
132 dx, dy = self.launch_delta 147 dx, dy = self.launch_delta