annotate pytouhou/game/bullet.pyx @ 300:da53bc29b94a

Add the game interface.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 10 Mar 2012 17:47:03 +0100
parents 35644a67ba4c
children f3099ebf4f61
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
84
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 ##
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 ##
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 ##
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 ## GNU General Public License for more details.
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 ##
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14
163
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
15 from math import cos, sin, atan2, pi
84
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16
86
a87a3c080585 Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
17 from pytouhou.utils.interpolator import Interpolator
84
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 from pytouhou.vm.anmrunner import ANMRunner
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 from pytouhou.game.sprite import Sprite
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
22 LAUNCHING, LAUNCHED, CANCELLED = range(3)
236
741860192b56 Implement ANM0 interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 232
diff changeset
23
257
9b699e8de4a7 Switch Bullet to Cython to improve performances.
Thibaut Girka <thib@sitedethib.com>
parents: 256
diff changeset
24 cdef class Bullet(object):
9b699e8de4a7 Switch Bullet to Cython to improve performances.
Thibaut Girka <thib@sitedethib.com>
parents: 256
diff changeset
25 cdef public unsigned int _state, flags, frame, sprite_idx_offset
9b699e8de4a7 Switch Bullet to Cython to improve performances.
Thibaut Girka <thib@sitedethib.com>
parents: 256
diff changeset
26 cdef public double dx, dy, angle, speed #TODO
9b699e8de4a7 Switch Bullet to Cython to improve performances.
Thibaut Girka <thib@sitedethib.com>
parents: 256
diff changeset
27 cdef public object player_bullet, target
9b699e8de4a7 Switch Bullet to Cython to improve performances.
Thibaut Girka <thib@sitedethib.com>
parents: 256
diff changeset
28 cdef public object _game, _sprite, _anmrunner, _removed, _bullet_type, _was_visible
9b699e8de4a7 Switch Bullet to Cython to improve performances.
Thibaut Girka <thib@sitedethib.com>
parents: 256
diff changeset
29 cdef public object attributes, damage, hitbox_half_size, speed_interpolator, grazed
9b699e8de4a7 Switch Bullet to Cython to improve performances.
Thibaut Girka <thib@sitedethib.com>
parents: 256
diff changeset
30 cdef public object x, y #TODO
9b699e8de4a7 Switch Bullet to Cython to improve performances.
Thibaut Girka <thib@sitedethib.com>
parents: 256
diff changeset
31
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
32 def __init__(self, pos, bullet_type, sprite_idx_offset,
232
8843e26f80c3 Hopefully implement “accelerating” bullets
Thibaut Girka <thib@sitedethib.com>
parents: 220
diff changeset
33 angle, speed, attributes, flags, target, game,
220
0595315d3880 Fix SHT handling; change a few things to be closer to ZUN’s mind; and first stub of PCB support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 163
diff changeset
34 player_bullet=False, damage=0, hitbox=None):
151
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 149
diff changeset
35 self._game = game
84
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 self._sprite = None
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37 self._anmrunner = None
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 self._removed = False
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
39 self._bullet_type = bullet_type
255
a0d6b1915591 Fix bullet removal condition.
Thibaut Girka <thib@sitedethib.com>
parents: 246
diff changeset
40 self._was_visible = True
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
41 self._state = LAUNCHING
84
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42
220
0595315d3880 Fix SHT handling; change a few things to be closer to ZUN’s mind; and first stub of PCB support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 163
diff changeset
43 if hitbox:
0595315d3880 Fix SHT handling; change a few things to be closer to ZUN’s mind; and first stub of PCB support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 163
diff changeset
44 self.hitbox_half_size = (hitbox[0] / 2., hitbox[1] / 2.)
0595315d3880 Fix SHT handling; change a few things to be closer to ZUN’s mind; and first stub of PCB support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 163
diff changeset
45 else:
0595315d3880 Fix SHT handling; change a few things to be closer to ZUN’s mind; and first stub of PCB support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 163
diff changeset
46 self.hitbox_half_size = (bullet_type.hitbox_size / 2., bullet_type.hitbox_size / 2.)
142
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 127
diff changeset
47
86
a87a3c080585 Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
48 self.speed_interpolator = None
a87a3c080585 Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
49 self.frame = 0
a87a3c080585 Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
50 self.grazed = False
a87a3c080585 Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
51
232
8843e26f80c3 Hopefully implement “accelerating” bullets
Thibaut Girka <thib@sitedethib.com>
parents: 220
diff changeset
52 self.target = target
84
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54 self.sprite_idx_offset = sprite_idx_offset
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56 self.flags = flags
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57 self.attributes = list(attributes)
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59 self.x, self.y = pos
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60 self.angle = angle
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61 self.speed = speed
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
62 self.dx, self.dy = cos(angle) * speed, sin(angle) * speed
84
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63
163
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
64 self.player_bullet = player_bullet
220
0595315d3880 Fix SHT handling; change a few things to be closer to ZUN’s mind; and first stub of PCB support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 163
diff changeset
65 self.damage = damage
163
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
66
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
67 #TODO
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
68 if flags & 14:
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
69 if flags & 2:
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
70 index = bullet_type.launch_anim2_index
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
71 launch_mult = bullet_type.launch_anim_penalties[0]
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
72 elif flags & 4:
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
73 index = bullet_type.launch_anim4_index
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
74 launch_mult = bullet_type.launch_anim_penalties[1]
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
75 else:
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
76 index = bullet_type.launch_anim8_index
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
77 launch_mult = bullet_type.launch_anim_penalties[2]
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
78 self.dx, self.dy = self.dx * launch_mult, self.dy * launch_mult
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
79 self._sprite = Sprite()
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
80 self._anmrunner = ANMRunner(bullet_type.anm_wrapper,
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
81 index, self._sprite,
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
82 bullet_type.launch_anim_offsets[sprite_idx_offset])
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
83 self._anmrunner.run_frame()
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
84 else:
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
85 self.launch()
88
2cc60137d368 Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 86
diff changeset
86
163
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
87 if self.player_bullet:
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
88 self._sprite.angle = angle - pi
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
89 else:
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
90 self._sprite.angle = angle
86
a87a3c080585 Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
91
84
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92
258
620134bc51f4 Move bullet visibility check to Bullet to improve performance and prepare handling of flags 1024 and 2048.
Thibaut Girka <thib@sitedethib.com>
parents: 257
diff changeset
93 cpdef is_visible(Bullet self, screen_width, screen_height):
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
94 tx, ty, tw, th = self._sprite.texcoords
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
95 x, y = self.x, self.y
84
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
96
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
97 max_x = tw / 2.
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
98 max_y = th / 2.
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
99
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
100 if (max_x < x - screen_width
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
101 or max_x < -x
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
102 or max_y < y - screen_height
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
103 or max_y < -y):
84
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
104 return False
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
105 return True
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
106
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
107
258
620134bc51f4 Move bullet visibility check to Bullet to improve performance and prepare handling of flags 1024 and 2048.
Thibaut Girka <thib@sitedethib.com>
parents: 257
diff changeset
108 def set_anim(Bullet self, sprite_idx_offset=None):
107
5d9052b9a4e8 (almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents: 106
diff changeset
109 if sprite_idx_offset is not None:
5d9052b9a4e8 (almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents: 106
diff changeset
110 self.sprite_idx_offset = sprite_idx_offset
5d9052b9a4e8 (almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents: 106
diff changeset
111
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
112 bt = self._bullet_type
107
5d9052b9a4e8 (almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents: 106
diff changeset
113 self._sprite = Sprite()
163
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
114 if self.player_bullet:
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
115 self._sprite.angle = self.angle - pi
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
116 else:
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
117 self._sprite.angle = self.angle
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
118 self._anmrunner = ANMRunner(bt.anm_wrapper, bt.anim_index,
107
5d9052b9a4e8 (almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents: 106
diff changeset
119 self._sprite, self.sprite_idx_offset)
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
120 self._anmrunner.run_frame()
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
121
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
122
258
620134bc51f4 Move bullet visibility check to Bullet to improve performance and prepare handling of flags 1024 and 2048.
Thibaut Girka <thib@sitedethib.com>
parents: 257
diff changeset
123 def launch(Bullet self):
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
124 self._state = LAUNCHED
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
125 self.frame = 0
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
126 self.set_anim()
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
127 self.dx, self.dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
128 if self.flags & 1:
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
129 self.speed_interpolator = Interpolator((self.speed + 5.,), 0,
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
130 (self.speed,), 16)
107
5d9052b9a4e8 (almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents: 106
diff changeset
131
5d9052b9a4e8 (almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents: 106
diff changeset
132
258
620134bc51f4 Move bullet visibility check to Bullet to improve performance and prepare handling of flags 1024 and 2048.
Thibaut Girka <thib@sitedethib.com>
parents: 257
diff changeset
133 def collide(Bullet self):
161
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 158
diff changeset
134 self.cancel()
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 158
diff changeset
135
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 158
diff changeset
136
258
620134bc51f4 Move bullet visibility check to Bullet to improve performance and prepare handling of flags 1024 and 2048.
Thibaut Girka <thib@sitedethib.com>
parents: 257
diff changeset
137 def cancel(Bullet self):
161
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 158
diff changeset
138 # Cancel animation
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 158
diff changeset
139 bt = self._bullet_type
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 158
diff changeset
140 self._sprite = Sprite()
163
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
141 if self.player_bullet:
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
142 self._sprite.angle = self.angle - pi
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
143 else:
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
144 self._sprite.angle = self.angle
161
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 158
diff changeset
145 self._anmrunner = ANMRunner(bt.anm_wrapper, bt.cancel_anim_index,
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 158
diff changeset
146 self._sprite, bt.launch_anim_offsets[self.sprite_idx_offset])
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 158
diff changeset
147 self._anmrunner.run_frame()
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
148 self.dx, self.dy = self.dx / 2., self.dy / 2.
161
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 158
diff changeset
149
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 158
diff changeset
150 # Change update method
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
151 self._state = CANCELLED
161
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 158
diff changeset
152
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 158
diff changeset
153 # Do not use this one for collisions anymore
163
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
154 if self.player_bullet:
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
155 self._game.players_bullets.remove(self)
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
156 else:
ee0bbde02c48 Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
157 self._game.bullets.remove(self)
161
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 158
diff changeset
158 self._game.cancelled_bullets.append(self)
152
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
159
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
160
258
620134bc51f4 Move bullet visibility check to Bullet to improve performance and prepare handling of flags 1024 and 2048.
Thibaut Girka <thib@sitedethib.com>
parents: 257
diff changeset
161 def update(Bullet self):
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
162 if self._anmrunner is not None and not self._anmrunner.run_frame():
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
163 if self._state == LAUNCHING:
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
164 #TODO: check if it doesn't skip a frame
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
165 self.launch()
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
166 elif self._state == CANCELLED:
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
167 self._removed = True
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
168 else:
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
169 self._anmrunner = None
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
170
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
171 if self._state == LAUNCHING:
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
172 pass
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
173 elif self._state == CANCELLED:
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
174 pass
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
175 elif self.flags & 1:
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
176 # Initial speed burst
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
177 #TODO: use frame instead of interpolator?
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
178 if not self.speed_interpolator:
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
179 self.flags &= ~1
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
180 elif self.flags & 16:
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
181 # Each frame, add a vector to the speed vector
86
a87a3c080585 Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
182 length, angle = self.attributes[4:6]
89
1513f5626656 Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents: 88
diff changeset
183 angle = self.angle if angle < -900.0 else angle #TODO: is that right?
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
184 self.dx += cos(angle) * length
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
185 self.dy += sin(angle) * length
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
186 self.speed = (self.dx ** 2 + self.dy ** 2) ** 0.5
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
187 self.angle = self._sprite.angle = atan2(self.dy, self.dx)
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
188 if self._sprite.automatic_orientation:
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
189 self._sprite._changed = True
149
3673d55a8448 Fix bullet flags 16 and 32 handling
Thibaut Girka <thib@sitedethib.com>
parents: 145
diff changeset
190 if self.frame == self.attributes[0]: #TODO: include last frame, or not?
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
191 self.flags &= ~16
86
a87a3c080585 Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
192 elif self.flags & 32:
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
193 # Each frame, accelerate and rotate
86
a87a3c080585 Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
194 #TODO: check
a87a3c080585 Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
195 acceleration, angular_speed = self.attributes[4:6]
89
1513f5626656 Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents: 88
diff changeset
196 self.speed += acceleration
1513f5626656 Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents: 88
diff changeset
197 self.angle += angular_speed
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
198 self.dx = cos(self.angle) * self.speed
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
199 self.dy = sin(self.angle) * self.speed
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
200 self._sprite.angle = self.angle
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
201 if self._sprite.automatic_orientation:
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
202 self._sprite._changed = True
149
3673d55a8448 Fix bullet flags 16 and 32 handling
Thibaut Girka <thib@sitedethib.com>
parents: 145
diff changeset
203 if self.frame == self.attributes[0]:
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
204 self.flags &= ~32
88
2cc60137d368 Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 86
diff changeset
205 elif self.flags & 448:
2cc60137d368 Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 86
diff changeset
206 #TODO: check
2cc60137d368 Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 86
diff changeset
207 frame, count = self.attributes[0:2]
89
1513f5626656 Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents: 88
diff changeset
208 angle, speed = self.attributes[4:6]
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
209 if self.frame % frame == 0:
89
1513f5626656 Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents: 88
diff changeset
210 count = count - 1
88
2cc60137d368 Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 86
diff changeset
211
124
f06e96dbed4e Fix a few things with special bullet flags
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
212 if self.frame != 0:
f06e96dbed4e Fix a few things with special bullet flags
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
213 self.speed = speed
88
2cc60137d368 Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 86
diff changeset
214
124
f06e96dbed4e Fix a few things with special bullet flags
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
215 if self.flags & 64:
f06e96dbed4e Fix a few things with special bullet flags
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
216 self.angle += angle
f06e96dbed4e Fix a few things with special bullet flags
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
217 elif self.flags & 128:
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
218 self.angle = atan2(self.target.y - self.y,
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
219 self.target.x - self.x) + angle
124
f06e96dbed4e Fix a few things with special bullet flags
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
220 elif self.flags & 256:
f06e96dbed4e Fix a few things with special bullet flags
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
221 self.angle = angle
f06e96dbed4e Fix a few things with special bullet flags
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
222
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
223 self.dx = cos(self.angle) * self.speed
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
224 self.dy = sin(self.angle) * self.speed
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
225 self._sprite.angle = self.angle
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
226 if self._sprite.automatic_orientation:
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
227 self._sprite._changed = True
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
228
127
81e05aed8db5 Fix bullets
Thibaut Girka <thib@sitedethib.com>
parents: 124
diff changeset
229 if count >= 0:
124
f06e96dbed4e Fix a few things with special bullet flags
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
230 self.speed_interpolator = Interpolator((self.speed,), self.frame,
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 120
diff changeset
231 (0.,), self.frame + frame - 1)
89
1513f5626656 Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents: 88
diff changeset
232 else:
1513f5626656 Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents: 88
diff changeset
233 self.flags &= ~448
88
2cc60137d368 Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 86
diff changeset
234
89
1513f5626656 Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents: 88
diff changeset
235 self.attributes[1] = count
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
236
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
237 # Common updates
86
a87a3c080585 Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
238
a87a3c080585 Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
239 if self.speed_interpolator:
a87a3c080585 Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
240 self.speed_interpolator.update(self.frame)
a87a3c080585 Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
241 self.speed, = self.speed_interpolator.values
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
242 self.dx = cos(self.angle) * self.speed
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
243 self.dy = sin(self.angle) * self.speed
86
a87a3c080585 Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
244
256
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
245 self.x += self.dx
507dfd6efe0c Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents: 255
diff changeset
246 self.y += self.dy
84
1a0c78e5a941 Oops O:)
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
247
86
a87a3c080585 Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
248 self.frame += 1
a87a3c080585 Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
249
258
620134bc51f4 Move bullet visibility check to Bullet to improve performance and prepare handling of flags 1024 and 2048.
Thibaut Girka <thib@sitedethib.com>
parents: 257
diff changeset
250 # Filter out-of-screen bullets and handle special flags
620134bc51f4 Move bullet visibility check to Bullet to improve performance and prepare handling of flags 1024 and 2048.
Thibaut Girka <thib@sitedethib.com>
parents: 257
diff changeset
251 if self.flags & 448:
620134bc51f4 Move bullet visibility check to Bullet to improve performance and prepare handling of flags 1024 and 2048.
Thibaut Girka <thib@sitedethib.com>
parents: 257
diff changeset
252 self._was_visible = False
620134bc51f4 Move bullet visibility check to Bullet to improve performance and prepare handling of flags 1024 and 2048.
Thibaut Girka <thib@sitedethib.com>
parents: 257
diff changeset
253 elif self.is_visible(self._game.width, self._game.height):
620134bc51f4 Move bullet visibility check to Bullet to improve performance and prepare handling of flags 1024 and 2048.
Thibaut Girka <thib@sitedethib.com>
parents: 257
diff changeset
254 self._was_visible = True
620134bc51f4 Move bullet visibility check to Bullet to improve performance and prepare handling of flags 1024 and 2048.
Thibaut Girka <thib@sitedethib.com>
parents: 257
diff changeset
255 elif self._was_visible:
620134bc51f4 Move bullet visibility check to Bullet to improve performance and prepare handling of flags 1024 and 2048.
Thibaut Girka <thib@sitedethib.com>
parents: 257
diff changeset
256 self._removed = True
260
e22e0d73f614 Implement bullet flags 1024 and 2048 (bouncing bullet).
Thibaut Girka <thib@sitedethib.com>
parents: 258
diff changeset
257 if self.flags & (1024 | 2048) and self.attributes[0] > 0:
e22e0d73f614 Implement bullet flags 1024 and 2048 (bouncing bullet).
Thibaut Girka <thib@sitedethib.com>
parents: 258
diff changeset
258 # Bounce!
e22e0d73f614 Implement bullet flags 1024 and 2048 (bouncing bullet).
Thibaut Girka <thib@sitedethib.com>
parents: 258
diff changeset
259 if self.x < 0 or self.x > self._game.width:
e22e0d73f614 Implement bullet flags 1024 and 2048 (bouncing bullet).
Thibaut Girka <thib@sitedethib.com>
parents: 258
diff changeset
260 self.angle = pi - self.angle
e22e0d73f614 Implement bullet flags 1024 and 2048 (bouncing bullet).
Thibaut Girka <thib@sitedethib.com>
parents: 258
diff changeset
261 self._removed = False
e22e0d73f614 Implement bullet flags 1024 and 2048 (bouncing bullet).
Thibaut Girka <thib@sitedethib.com>
parents: 258
diff changeset
262 if self.y < 0 or ((self.flags & 1024) and self.y > self._game.height):
e22e0d73f614 Implement bullet flags 1024 and 2048 (bouncing bullet).
Thibaut Girka <thib@sitedethib.com>
parents: 258
diff changeset
263 self.angle = -self.angle
e22e0d73f614 Implement bullet flags 1024 and 2048 (bouncing bullet).
Thibaut Girka <thib@sitedethib.com>
parents: 258
diff changeset
264 self._removed = False
267
35644a67ba4c Fix Sakuya's bouncing knives
Thibaut Girka <thib@sitedethib.com>
parents: 260
diff changeset
265 self._sprite.angle = self.angle
35644a67ba4c Fix Sakuya's bouncing knives
Thibaut Girka <thib@sitedethib.com>
parents: 260
diff changeset
266 if self._sprite.automatic_orientation:
35644a67ba4c Fix Sakuya's bouncing knives
Thibaut Girka <thib@sitedethib.com>
parents: 260
diff changeset
267 self._sprite._changed = True
260
e22e0d73f614 Implement bullet flags 1024 and 2048 (bouncing bullet).
Thibaut Girka <thib@sitedethib.com>
parents: 258
diff changeset
268 self.dx = cos(self.angle) * self.speed
e22e0d73f614 Implement bullet flags 1024 and 2048 (bouncing bullet).
Thibaut Girka <thib@sitedethib.com>
parents: 258
diff changeset
269 self.dy = sin(self.angle) * self.speed
e22e0d73f614 Implement bullet flags 1024 and 2048 (bouncing bullet).
Thibaut Girka <thib@sitedethib.com>
parents: 258
diff changeset
270 self.attributes[0] -= 1
258
620134bc51f4 Move bullet visibility check to Bullet to improve performance and prepare handling of flags 1024 and 2048.
Thibaut Girka <thib@sitedethib.com>
parents: 257
diff changeset
271