Mercurial > touhou
annotate pytouhou/game/bullet.pyx @ 500:777544d11578
[Boss rush] Mark removed lasers as such
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Fri, 08 Nov 2013 13:40:53 +0100 |
parents | 887de1309491 |
children | db28538cd399 |
rev | line source |
---|---|
84 | 1 # -*- encoding: utf-8 -*- |
2 ## | |
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> | |
4 ## | |
5 ## This program is free software; you can redistribute it and/or modify | |
6 ## it under the terms of the GNU General Public License as published | |
7 ## by the Free Software Foundation; version 3 only. | |
8 ## | |
9 ## This program is distributed in the hope that it will be useful, | |
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 ## GNU General Public License for more details. | |
13 ## | |
14 | |
442
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
15 from libc.math cimport cos, sin, atan2, M_PI as pi |
84 | 16 |
17 from pytouhou.vm.anmrunner import ANMRunner | |
442
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
18 from pytouhou.game.sprite cimport Sprite |
84 | 19 |
20 | |
440
b9d2db93972f
Add a base Element class for every object in pytouhou.game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
433
diff
changeset
|
21 cdef class Bullet(Element): |
472
8038f1957b71
Type bullettype, itemtype and lasertype a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
468
diff
changeset
|
22 def __init__(self, pos, BulletType bullet_type, unsigned long sprite_idx_offset, |
447
78e1c3864e73
Make pytouhou.game.game an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
442
diff
changeset
|
23 double angle, double speed, attributes, unsigned long flags, target, Game game, |
492
887de1309491
Add friendly fire in netplay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
472
diff
changeset
|
24 long player=-1, unsigned long damage=0, tuple hitbox=None): |
442
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
25 cdef double launch_mult |
257
9b699e8de4a7
Switch Bullet to Cython to improve performances.
Thibaut Girka <thib@sitedethib.com>
parents:
256
diff
changeset
|
26 |
440
b9d2db93972f
Add a base Element class for every object in pytouhou.game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
433
diff
changeset
|
27 Element.__init__(self, pos) |
b9d2db93972f
Add a base Element class for every object in pytouhou.game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
433
diff
changeset
|
28 |
151
5cf927cbd9c5
Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents:
149
diff
changeset
|
29 self._game = game |
123 | 30 self._bullet_type = bullet_type |
335
2350147cf043
Fix bullet cancellation and removal
Thibaut Girka <thib@sitedethib.com>
parents:
328
diff
changeset
|
31 self.state = LAUNCHING |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
32 self.was_visible = True |
84 | 33 |
442
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
34 if hitbox is not None: |
468
feecdb4a8928
Add “except *” to cdef void functions, and type some more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
448
diff
changeset
|
35 self.hitbox[:] = [hitbox[0], hitbox[1]] |
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
|
36 else: |
468
feecdb4a8928
Add “except *” to cdef void functions, and type some more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
448
diff
changeset
|
37 self.hitbox[:] = [bullet_type.hitbox_size, bullet_type.hitbox_size] |
142
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
127
diff
changeset
|
38 |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
39 self.speed_interpolator = None |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
40 self.frame = 0 |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
41 self.grazed = False |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
42 |
232
8843e26f80c3
Hopefully implement “accelerating” bullets
Thibaut Girka <thib@sitedethib.com>
parents:
220
diff
changeset
|
43 self.target = target |
84 | 44 |
45 self.sprite_idx_offset = sprite_idx_offset | |
46 | |
47 self.flags = flags | |
48 self.attributes = list(attributes) | |
49 | |
50 self.angle = angle | |
51 self.speed = speed | |
256
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
52 self.dx, self.dy = cos(angle) * speed, sin(angle) * speed |
84 | 53 |
492
887de1309491
Add friendly fire in netplay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
472
diff
changeset
|
54 self.player = player |
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
|
55 self.damage = damage |
163
ee0bbde02c48
Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
161
diff
changeset
|
56 |
122
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
120
diff
changeset
|
57 #TODO |
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
120
diff
changeset
|
58 if flags & 14: |
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
120
diff
changeset
|
59 if flags & 2: |
123 | 60 index = bullet_type.launch_anim2_index |
61 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
|
62 elif flags & 4: |
123 | 63 index = bullet_type.launch_anim4_index |
64 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
|
65 else: |
123 | 66 index = bullet_type.launch_anim8_index |
67 launch_mult = bullet_type.launch_anim_penalties[2] | |
256
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
68 self.dx, self.dy = self.dx * launch_mult, self.dy * launch_mult |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
69 self.sprite = Sprite() |
430
c9433188ffdb
Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
70 self.anmrunner = ANMRunner(bullet_type.anm, |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
71 index, self.sprite, |
123 | 72 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
|
73 else: |
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
120
diff
changeset
|
74 self.launch() |
88
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
75 |
492
887de1309491
Add friendly fire in netplay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
472
diff
changeset
|
76 if self.player >= 0: |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
77 self.sprite.angle = angle - pi |
163
ee0bbde02c48
Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
161
diff
changeset
|
78 else: |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
79 self.sprite.angle = angle |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
80 |
84 | 81 |
442
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
82 cdef bint is_visible(self, unsigned int screen_width, unsigned int screen_height): |
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
83 cdef double tw, th |
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
84 |
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
85 tw, th = self.sprite.texcoords[2:] |
123 | 86 x, y = self.x, self.y |
84 | 87 |
442
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
88 max_x = tw / 2 |
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
89 max_y = th / 2 |
84 | 90 |
123 | 91 if (max_x < x - screen_width |
92 or max_x < -x | |
93 or max_y < y - screen_height | |
94 or max_y < -y): | |
84 | 95 return False |
96 return True | |
97 | |
98 | |
442
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
99 cpdef set_anim(self, sprite_idx_offset=None): |
107
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
100 if sprite_idx_offset is not None: |
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
101 self.sprite_idx_offset = sprite_idx_offset |
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
102 |
122
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
120
diff
changeset
|
103 bt = self._bullet_type |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
104 self.sprite = Sprite() |
492
887de1309491
Add friendly fire in netplay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
472
diff
changeset
|
105 if self.player >= 0: |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
106 self.sprite.angle = self.angle - pi |
163
ee0bbde02c48
Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
161
diff
changeset
|
107 else: |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
108 self.sprite.angle = self.angle |
430
c9433188ffdb
Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
109 self.anmrunner = ANMRunner(bt.anm, bt.anim_index, |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
110 self.sprite, self.sprite_idx_offset) |
122
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
120
diff
changeset
|
111 |
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
120
diff
changeset
|
112 |
442
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
113 cdef void launch(self): |
335
2350147cf043
Fix bullet cancellation and removal
Thibaut Girka <thib@sitedethib.com>
parents:
328
diff
changeset
|
114 self.state = LAUNCHED |
256
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
115 self.frame = 0 |
122
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
120
diff
changeset
|
116 self.set_anim() |
256
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
117 self.dx, self.dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed |
328
56523a16db1d
Fix some replay synchronization issues and update the TODO.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
314
diff
changeset
|
118 |
122
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
120
diff
changeset
|
119 if self.flags & 1: |
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
120
diff
changeset
|
120 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
|
121 (self.speed,), 16) |
107
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
122 |
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
123 |
468
feecdb4a8928
Add “except *” to cdef void functions, and type some more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
448
diff
changeset
|
124 cdef void collide(self): |
161
7e7368356445
Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents:
158
diff
changeset
|
125 self.cancel() |
388
ac2891afb0bb
Make particles behave as in the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
384
diff
changeset
|
126 self._game.new_particle((self.x, self.y), 10, 256) #TODO: find the real size. |
161
7e7368356445
Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents:
158
diff
changeset
|
127 |
7e7368356445
Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents:
158
diff
changeset
|
128 |
468
feecdb4a8928
Add “except *” to cdef void functions, and type some more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
448
diff
changeset
|
129 cdef void cancel(self): |
161
7e7368356445
Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents:
158
diff
changeset
|
130 # Cancel animation |
7e7368356445
Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents:
158
diff
changeset
|
131 bt = self._bullet_type |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
132 self.sprite = Sprite() |
492
887de1309491
Add friendly fire in netplay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
472
diff
changeset
|
133 if self.player >= 0: |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
134 self.sprite.angle = self.angle - pi |
163
ee0bbde02c48
Implement player bullets in Bullet.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
161
diff
changeset
|
135 else: |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
136 self.sprite.angle = self.angle |
430
c9433188ffdb
Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
137 self.anmrunner = ANMRunner(bt.anm, bt.cancel_anim_index, |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
138 self.sprite, bt.launch_anim_offsets[self.sprite_idx_offset]) |
256
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
139 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
|
140 |
335
2350147cf043
Fix bullet cancellation and removal
Thibaut Girka <thib@sitedethib.com>
parents:
328
diff
changeset
|
141 self.state = CANCELLED |
161
7e7368356445
Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents:
158
diff
changeset
|
142 |
152
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
151
diff
changeset
|
143 |
468
feecdb4a8928
Add “except *” to cdef void functions, and type some more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
448
diff
changeset
|
144 cdef void update(self): |
442
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
145 cdef int frame, count, game_width, game_height |
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
146 cdef double length, angle, speed, acceleration, angular_speed |
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
147 |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
148 if self.anmrunner is not None and not self.anmrunner.run_frame(): |
335
2350147cf043
Fix bullet cancellation and removal
Thibaut Girka <thib@sitedethib.com>
parents:
328
diff
changeset
|
149 if self.state == LAUNCHING: |
256
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
150 #TODO: check if it doesn't skip a frame |
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
151 self.launch() |
335
2350147cf043
Fix bullet cancellation and removal
Thibaut Girka <thib@sitedethib.com>
parents:
328
diff
changeset
|
152 elif self.state == CANCELLED: |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
153 self.removed = True |
256
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
154 else: |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
155 self.anmrunner = None |
122
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
120
diff
changeset
|
156 |
335
2350147cf043
Fix bullet cancellation and removal
Thibaut Girka <thib@sitedethib.com>
parents:
328
diff
changeset
|
157 if self.state == LAUNCHING: |
256
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
158 pass |
335
2350147cf043
Fix bullet cancellation and removal
Thibaut Girka <thib@sitedethib.com>
parents:
328
diff
changeset
|
159 elif self.state == CANCELLED: |
256
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
160 pass |
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
161 elif self.flags & 1: |
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
162 # Initial speed burst |
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
163 #TODO: use frame instead of interpolator? |
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
164 if not self.speed_interpolator: |
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
165 self.flags &= ~1 |
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
166 elif self.flags & 16: |
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
167 # 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
|
168 length, angle = self.attributes[4:6] |
89
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
169 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
|
170 self.dx += cos(angle) * length |
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
171 self.dy += sin(angle) * length |
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
172 self.speed = (self.dx ** 2 + self.dy ** 2) ** 0.5 |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
173 self.angle = self.sprite.angle = atan2(self.dy, self.dx) |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
174 if self.sprite.automatic_orientation: |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
175 self.sprite.changed = True |
149
3673d55a8448
Fix bullet flags 16 and 32 handling
Thibaut Girka <thib@sitedethib.com>
parents:
145
diff
changeset
|
176 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
|
177 self.flags &= ~16 |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
178 elif self.flags & 32: |
256
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
179 # Each frame, accelerate and rotate |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
180 #TODO: check |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
181 acceleration, angular_speed = self.attributes[4:6] |
89
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
182 self.speed += acceleration |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
183 self.angle += angular_speed |
256
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
184 self.dx = cos(self.angle) * self.speed |
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
185 self.dy = sin(self.angle) * self.speed |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
186 self.sprite.angle = self.angle |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
187 if self.sprite.automatic_orientation: |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
188 self.sprite.changed = True |
149
3673d55a8448
Fix bullet flags 16 and 32 handling
Thibaut Girka <thib@sitedethib.com>
parents:
145
diff
changeset
|
189 if self.frame == self.attributes[0]: |
256
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
190 self.flags &= ~32 |
88
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
191 elif self.flags & 448: |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
192 #TODO: check |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
193 frame, count = self.attributes[0:2] |
89
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
194 angle, speed = self.attributes[4:6] |
122
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
120
diff
changeset
|
195 if self.frame % frame == 0: |
442
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
196 count -= 1 |
88
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
197 |
124
f06e96dbed4e
Fix a few things with special bullet flags
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
198 if self.frame != 0: |
314
0f88ae611d37
Fix Sakuya's daggers in stage 6.
Thibaut Girka <thib@sitedethib.com>
parents:
304
diff
changeset
|
199 self.speed = self.speed if speed < -900 else speed |
88
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
200 |
124
f06e96dbed4e
Fix a few things with special bullet flags
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
201 if self.flags & 64: |
f06e96dbed4e
Fix a few things with special bullet flags
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
202 self.angle += angle |
f06e96dbed4e
Fix a few things with special bullet flags
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
203 elif self.flags & 128: |
256
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
204 self.angle = atan2(self.target.y - self.y, |
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
205 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
|
206 elif self.flags & 256: |
f06e96dbed4e
Fix a few things with special bullet flags
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
207 self.angle = angle |
f06e96dbed4e
Fix a few things with special bullet flags
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
208 |
256
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
209 self.dx = cos(self.angle) * self.speed |
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
210 self.dy = sin(self.angle) * self.speed |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
211 self.sprite.angle = self.angle |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
212 if self.sprite.automatic_orientation: |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
213 self.sprite.changed = True |
122
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
120
diff
changeset
|
214 |
127 | 215 if count >= 0: |
124
f06e96dbed4e
Fix a few things with special bullet flags
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
216 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
|
217 (0.,), self.frame + frame - 1) |
89
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
218 else: |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
219 self.flags &= ~448 |
88
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
220 |
89
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
221 self.attributes[1] = count |
256
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
222 |
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
223 # Common updates |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
224 |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
225 if self.speed_interpolator: |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
226 self.speed_interpolator.update(self.frame) |
314
0f88ae611d37
Fix Sakuya's daggers in stage 6.
Thibaut Girka <thib@sitedethib.com>
parents:
304
diff
changeset
|
227 speed, = self.speed_interpolator.values |
0f88ae611d37
Fix Sakuya's daggers in stage 6.
Thibaut Girka <thib@sitedethib.com>
parents:
304
diff
changeset
|
228 self.dx = cos(self.angle) * speed |
0f88ae611d37
Fix Sakuya's daggers in stage 6.
Thibaut Girka <thib@sitedethib.com>
parents:
304
diff
changeset
|
229 self.dy = sin(self.angle) * speed |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
230 |
256
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
231 self.x += self.dx |
507dfd6efe0c
Refactor pytouhou.game.bullet.
Thibaut Girka <thib@sitedethib.com>
parents:
255
diff
changeset
|
232 self.y += self.dy |
84 | 233 |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
234 self.frame += 1 |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
235 |
442
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
236 game_width, game_height = self._game.width, self._game.height |
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
237 |
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
|
238 # 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
|
239 if self.flags & 448: |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
240 self.was_visible = False |
442
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
241 elif self.is_visible(game_width, game_height): |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
242 self.was_visible = True |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
243 elif self.was_visible: |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
244 self.removed = True |
260
e22e0d73f614
Implement bullet flags 1024 and 2048 (bouncing bullet).
Thibaut Girka <thib@sitedethib.com>
parents:
258
diff
changeset
|
245 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
|
246 # Bounce! |
442
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
247 if self.x < 0 or self.x > game_width: |
260
e22e0d73f614
Implement bullet flags 1024 and 2048 (bouncing bullet).
Thibaut Girka <thib@sitedethib.com>
parents:
258
diff
changeset
|
248 self.angle = pi - self.angle |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
249 self.removed = False |
442
6b4c3e250bd6
Type Bullet more, to improve performances.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
440
diff
changeset
|
250 if self.y < 0 or ((self.flags & 1024) and self.y > game_height): |
260
e22e0d73f614
Implement bullet flags 1024 and 2048 (bouncing bullet).
Thibaut Girka <thib@sitedethib.com>
parents:
258
diff
changeset
|
251 self.angle = -self.angle |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
252 self.removed = False |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
253 self.sprite.angle = self.angle |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
254 if self.sprite.automatic_orientation: |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
267
diff
changeset
|
255 self.sprite.changed = True |
260
e22e0d73f614
Implement bullet flags 1024 and 2048 (bouncing bullet).
Thibaut Girka <thib@sitedethib.com>
parents:
258
diff
changeset
|
256 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
|
257 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
|
258 self.attributes[0] -= 1 |