Mercurial > touhou
annotate pytouhou/game/bullet.py @ 107:5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Mon, 05 Sep 2011 23:04:43 +0200 |
parents | c7847bfed427 |
children | 2a03940deea3 |
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 | |
15 from math import cos, sin, atan2, pi | |
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 | 18 from pytouhou.vm.anmrunner import ANMRunner |
19 from pytouhou.game.sprite import Sprite | |
20 | |
21 | |
22 class Bullet(object): | |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
23 __slots__ = ('x', 'y', 'angle', 'speed', 'frame', 'grazed', |
84 | 24 'flags', 'attributes', 'anim_idx', 'sprite_idx_offset', 'player', |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
25 'speed_interpolator', |
84 | 26 '_game_state', '_sprite', '_anmrunner', |
27 '_removed', '_launched') | |
28 | |
29 def __init__(self, pos, anim_idx, sprite_idx_offset, | |
30 angle, speed, attributes, flags, player, game_state): | |
31 self._game_state = game_state | |
32 self._sprite = None | |
33 self._anmrunner = None | |
34 self._removed = False | |
35 self._launched = False | |
36 | |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
37 self.speed_interpolator = None |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
38 self.frame = 0 |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
39 self.grazed = False |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
40 |
84 | 41 self.player = player |
42 | |
43 self.anim_idx = anim_idx | |
44 self.sprite_idx_offset = sprite_idx_offset | |
45 | |
46 #TODO | |
47 #if flags & (2|4|8): | |
106 | 48 # if flags & 2: #TODO: Huh?! |
49 # index = 14 | |
50 # elif flags & 4: | |
51 # index = 15 | |
52 # else: | |
53 # index = 19 | |
84 | 54 # self._sprite = Sprite() |
106 | 55 # self._anmrunner = ANMRunner(self._game_state.resource_loader.get_anm_wrapper(('etama3.anm',)), #TODO |
56 # index, self._sprite, 0) #TODO: offset | |
57 # self._anmrunner.run_frame() | |
84 | 58 |
59 self.flags = flags | |
60 self.attributes = list(attributes) | |
61 | |
62 self.x, self.y = pos | |
63 self.angle = angle | |
64 self.speed = speed | |
65 | |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
66 if flags & 1: |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
67 self.speed_interpolator = Interpolator((speed + 5.,)) |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
68 self.speed_interpolator.set_interpolation_start(0, (speed + 5.,)) |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
69 self.speed_interpolator.set_interpolation_end(16, (speed,)) |
88
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
70 if flags & 448: |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
71 self.speed_interpolator = Interpolator((speed,)) |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
72 self.speed_interpolator.set_interpolation_start(0, (speed,)) |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
73 self.speed_interpolator.set_interpolation_end(attributes[0] - 1, (0,)) # TODO: really -1? Without, the new speed isn’t set. |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
74 |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
75 |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
76 def get_player_angle(self): |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
77 return atan2(self.player.y - self.y, self.player.x - self.x) |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
78 |
84 | 79 |
80 def is_visible(self, screen_width, screen_height): | |
81 if self._sprite: | |
82 tx, ty, tw, th = self._sprite.texcoords | |
83 if self._sprite.corner_relative_placement: | |
84 raise Exception #TODO | |
85 else: | |
86 tx, ty, tw, th = 0., 0., 0., 0. | |
87 | |
88 max_x = tw / 2. | |
89 max_y = th / 2. | |
90 min_x = -max_x | |
91 min_y = -max_y | |
92 | |
93 if any((min_x > screen_width - self.x, | |
94 max_x < -self.x, | |
95 min_y > screen_height - self.y, | |
96 max_y < -self.y)): | |
97 return False | |
98 return True | |
99 | |
100 | |
85 | 101 def get_objects_by_texture(self, objects_by_texture): |
102 sprite = self._sprite | |
90 | 103 sprite.update_vertices_uvs_colors() |
85 | 104 key = sprite.anm.first_name, sprite.anm.secondary_name |
105 key = (key, sprite.blendfunc) | |
94
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
90
diff
changeset
|
106 rec = objects_by_texture.setdefault(key, ([], [], [])) |
85 | 107 vertices = ((x + self.x, y + self.y, z) for x, y, z in sprite._vertices) |
94
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
90
diff
changeset
|
108 rec[0].extend(vertices) |
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
90
diff
changeset
|
109 rec[1].extend(sprite._uvs) |
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
90
diff
changeset
|
110 rec[2].extend(sprite._colors) |
84 | 111 |
112 | |
107
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
113 def set_anim(self, anim_idx=None, sprite_idx_offset=None): |
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
114 if anim_idx is not None: |
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
115 self.anim_idx = anim_idx |
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
116 |
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
117 if sprite_idx_offset is not None: |
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
118 self.sprite_idx_offset = sprite_idx_offset |
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
119 |
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
120 self._sprite = Sprite() |
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
121 anm_wrapper = self._game_state.resource_loader.get_anm_wrapper(('etama3.anm',)) #TODO |
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
122 self._anmrunner = ANMRunner(anm_wrapper, self.anim_idx, |
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
123 self._sprite, self.sprite_idx_offset) |
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
124 |
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
125 |
84 | 126 def update(self): |
127 if not self._sprite or self._sprite._removed: | |
128 self._launched = True | |
107
5d9052b9a4e8
(almost) implement Cirno's freezing spellcard
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
129 self.set_anim() |
84 | 130 |
94
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
90
diff
changeset
|
131 self._anmrunner.run_frame() |
90 | 132 self._sprite.update(angle_base=self.angle) |
84 | 133 |
134 #TODO: flags | |
135 x, y = self.x, self.y | |
136 | |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
137 if self.flags & 16: |
89
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
138 frame, count = self.attributes[0:2] |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
139 length, angle = self.attributes[4:6] |
89
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
140 angle = self.angle if angle < -900.0 else angle #TODO: is that right? |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
141 dx = cos(self.angle) * self.speed + cos(angle) * length |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
142 dy = sin(self.angle) * self.speed + sin(angle) * length |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
143 self.speed = (dx ** 2 + dy ** 2) ** 0.5 |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
144 self.angle = atan2(dy, dx) |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
145 if self.frame == frame: #TODO: include last frame, or not? |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
146 if count > 0: |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
147 self.attributes[1] -= 1 |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
148 self.frame = 0 |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
149 else: |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
150 self.flags ^= 16 |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
151 elif self.flags & 32: |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
152 #TODO: check |
89
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
153 frame, count = self.attributes[0:2] |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
154 acceleration, angular_speed = self.attributes[4:6] |
89
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
155 self.speed += acceleration |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
156 self.angle += angular_speed |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
157 if self.frame == frame: |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
158 if count > 0: |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
159 self.attributes[1] -= 1 |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
160 else: |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
161 self.flags ^= 32 |
88
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
162 elif self.flags & 448: |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
163 #TODO: check |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
164 frame, count = self.attributes[0:2] |
89
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
165 angle, speed = self.attributes[4:6] |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
166 if frame == self.frame: |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
167 count = count - 1 |
88
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
168 |
89
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
169 if self.flags & 64: |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
170 self.angle = self.angle + angle |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
171 elif self.flags & 128: |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
172 self.angle = self.get_player_angle() + angle |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
173 elif self.flags & 256: |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
174 self.angle = angle |
88
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
175 |
89
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
176 if count: |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
177 self.speed_interpolator.set_interpolation_start(self.frame, (speed,)) |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
178 self.speed_interpolator.set_interpolation_end(self.frame + frame - 1, (0,)) # TODO: really -1? Without, the new speed isn’t set. |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
179 else: |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
180 self.flags &= ~448 |
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
181 self.speed = speed |
88
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
182 |
89
1513f5626656
Fix attack flags implementation
Thibaut Girka <thib@sitedethib.com>
parents:
88
diff
changeset
|
183 self.attributes[1] = count |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
184 #TODO: other flags |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
185 |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
186 if self.speed_interpolator: |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
187 self.speed_interpolator.update(self.frame) |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
188 self.speed, = self.speed_interpolator.values |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
189 |
84 | 190 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed |
191 self.x, self.y = x + dx, y + dy | |
192 | |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
193 self.frame += 1 |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
194 |