Mercurial > touhou
comparison pytouhou/game/laser.pyx @ 471:06f0eeb519bb
Make Laser and Orb extension types, and use that where possible.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Mon, 16 Sep 2013 18:42:04 +0200 |
parents | pytouhou/game/laser.py@5bb7d2c0ff46 |
children | 8038f1957b71 |
comparison
equal
deleted
inserted
replaced
470:98995d8ac744 | 471:06f0eeb519bb |
---|---|
1 # -*- encoding: utf-8 -*- | |
2 ## | |
3 ## Copyright (C) 2012 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 libc.math cimport cos, sin, M_PI as pi | |
16 | |
17 from pytouhou.vm.anmrunner import ANMRunner | |
18 | |
19 | |
20 cdef class LaserLaunchAnim(Element): | |
21 def __init__(self, Laser laser, anm, unsigned long index): | |
22 Element.__init__(self, (0, 0)) | |
23 | |
24 self._laser = laser | |
25 self.sprite = Sprite() | |
26 self.sprite.anm = anm | |
27 self.sprite.texcoords = anm.sprites[index] | |
28 self.sprite.blendfunc = 1 | |
29 | |
30 | |
31 cpdef update(self): | |
32 laser = self._laser | |
33 length = <double>min(laser.end_offset - laser.start_offset, laser.max_length) | |
34 offset = laser.end_offset - length | |
35 dx, dy = cos(laser.angle), sin(laser.angle) | |
36 | |
37 self.x = laser.base_pos[0] + offset * dx | |
38 self.y = laser.base_pos[1] + offset * dy | |
39 | |
40 scale = laser.width / 10. - (offset - laser.start_offset) #TODO: check | |
41 self.sprite.rescale = (scale, scale) | |
42 self.sprite.changed = True | |
43 | |
44 if laser.removed or scale <= 0.: | |
45 self.removed = True | |
46 | |
47 | |
48 | |
49 cdef class Laser(Element): | |
50 def __init__(self, tuple base_pos, laser_type, | |
51 unsigned long sprite_idx_offset, double angle, double speed, | |
52 double start_offset, double end_offset, double max_length, | |
53 double width, unsigned long start_duration, | |
54 unsigned long duration, unsigned long stop_duration, | |
55 unsigned long grazing_delay, | |
56 unsigned long grazing_extra_duration, Game game): | |
57 Element.__init__(self, (0, 0)) | |
58 | |
59 self._game = game | |
60 launch_anim = LaserLaunchAnim(self, laser_type.anm, | |
61 laser_type.launch_anim_offsets[sprite_idx_offset] | |
62 + laser_type.launch_sprite_idx) | |
63 self._game.effects.append(launch_anim) | |
64 self._laser_type = laser_type | |
65 self.state = STARTING | |
66 | |
67 #TODO: hitbox | |
68 | |
69 self.frame = 0 | |
70 self.start_duration = start_duration | |
71 self.duration = duration | |
72 self.stop_duration = stop_duration | |
73 self.grazing_delay = grazing_delay | |
74 self.grazing_extra_duration = grazing_extra_duration | |
75 | |
76 self.sprite_idx_offset = sprite_idx_offset | |
77 self.set_base_pos(base_pos[0], base_pos[1]) | |
78 self.angle = angle | |
79 self.speed = speed | |
80 self.start_offset = start_offset | |
81 self.end_offset = end_offset | |
82 self.max_length = max_length | |
83 self.width = width | |
84 | |
85 self.set_anim() | |
86 | |
87 | |
88 cdef void set_anim(self, long sprite_idx_offset=-1): | |
89 if sprite_idx_offset >= 0: | |
90 self.sprite_idx_offset = sprite_idx_offset | |
91 | |
92 lt = self._laser_type | |
93 self.sprite = Sprite() | |
94 self.sprite.angle = self.angle | |
95 self.anmrunner = ANMRunner(lt.anm, lt.anim_index, | |
96 self.sprite, self.sprite_idx_offset) | |
97 | |
98 | |
99 cpdef set_base_pos(self, double x, double y): | |
100 self.base_pos[:] = [x, y] | |
101 | |
102 | |
103 cdef bint _check_collision(self, double point[2], double border_size): | |
104 cdef double c1[2], c2[2], c3[2] | |
105 | |
106 x, y = point[0] - self.base_pos[0], point[1] - self.base_pos[1] | |
107 dx, dy = cos(self.angle), sin(self.angle) | |
108 dx2, dy2 = -dy, dx | |
109 | |
110 length = <double>min(self.end_offset - self.start_offset, self.max_length) | |
111 offset = self.end_offset - length - border_size / 2. | |
112 end_offset = self.end_offset + border_size / 2. | |
113 half_width = self.width / 4. + border_size / 2. | |
114 | |
115 c1[:] = [dx * offset - dx2 * half_width, dy * offset - dy2 * half_width] | |
116 c2[:] = [dx * offset + dx2 * half_width, dy * offset + dy2 * half_width] | |
117 c3[:] = [dx * end_offset + dx2 * half_width, dy * end_offset + dy2 * half_width] | |
118 vx, vy = x - c2[0], y - c2[1] | |
119 v1x, v1y = c1[0] - c2[0], c1[1] - c2[1] | |
120 v2x, v2y = c3[0] - c2[0], c3[1] - c2[1] | |
121 | |
122 return (0 <= vx * v1x + vy * v1y <= v1x * v1x + v1y * v1y | |
123 and 0 <= vx * v2x + vy * v2y <= v2x * v2x + v2y * v2y) | |
124 | |
125 | |
126 cdef bint check_collision(self, double point[2]): | |
127 if self.state != STARTED: | |
128 return False | |
129 | |
130 return self._check_collision(point, 2.5) | |
131 | |
132 | |
133 cdef bint check_grazing(self, double point[2]): | |
134 #TODO: quadruple check! | |
135 if self.state == STOPPING and self.frame >= self.grazing_extra_duration: | |
136 return False | |
137 if self.state == STARTING and self.frame <= self.grazing_delay: | |
138 return False | |
139 if self.frame % 12 != 0: | |
140 return False | |
141 | |
142 return self._check_collision(point, 96 + 2.5) | |
143 | |
144 | |
145 def get_bullets_pos(self): | |
146 #TODO: check | |
147 length = <double>min(self.end_offset - self.start_offset, self.max_length) | |
148 offset = self.end_offset - length | |
149 dx, dy = cos(self.angle), sin(self.angle) | |
150 while self.start_offset <= offset < self.end_offset: | |
151 yield (self.base_pos[0] + offset * dx, self.base_pos[1] + offset * dy) | |
152 offset += 48. | |
153 | |
154 | |
155 cpdef cancel(self): | |
156 self.grazing_extra_duration = 0 | |
157 if self.state != STOPPING: | |
158 self.frame = 0 | |
159 self.state = STOPPING | |
160 | |
161 | |
162 cpdef update(self): | |
163 if self.anmrunner is not None and not self.anmrunner.run_frame(): | |
164 self.anmrunner = None | |
165 | |
166 self.end_offset += self.speed | |
167 | |
168 length = <double>min(self.end_offset - self.start_offset, self.max_length) # TODO | |
169 if self.state == STARTING: | |
170 if self.frame == self.start_duration: | |
171 self.frame = 0 | |
172 self.state = STARTED | |
173 else: | |
174 width = self.width * float(self.frame) / self.start_duration #TODO | |
175 if self.state == STARTED: | |
176 width = self.width #TODO | |
177 if self.frame == self.duration: | |
178 self.frame = 0 | |
179 self.state = STOPPING | |
180 if self.state == STOPPING: | |
181 if self.frame == self.stop_duration: | |
182 width = 0. | |
183 self.removed = True | |
184 else: | |
185 width = self.width * (1. - float(self.frame) / self.stop_duration) #TODO | |
186 | |
187 offset = self.end_offset - length / 2. | |
188 self.x = self.base_pos[0] + offset * cos(self.angle) | |
189 self.y = self.base_pos[1] + offset * sin(self.angle) | |
190 self.sprite.visible = (width > 0 and length > 0) | |
191 self.sprite.width_override = width | |
192 self.sprite.height_override = length | |
193 | |
194 self.sprite.update_orientation(pi/2. - self.angle, True) | |
195 self.sprite.changed = True #TODO | |
196 | |
197 self.frame += 1 | |
198 | |
199 | |
200 cdef class PlayerLaser(Element): | |
201 def __init__(self, laser_type, unsigned long sprite_idx_offset, | |
202 tuple hitbox, unsigned long damage, double angle, | |
203 double offset, unsigned long duration, Element origin): | |
204 Element.__init__(self) | |
205 | |
206 self._laser_type = laser_type | |
207 self.origin = origin | |
208 | |
209 self.hitbox[:] = [hitbox[0], hitbox[1]] | |
210 | |
211 self.frame = 0 | |
212 self.duration = duration | |
213 | |
214 self.sprite_idx_offset = sprite_idx_offset | |
215 self.angle = angle | |
216 self.offset = offset | |
217 self.damage = damage | |
218 | |
219 self.set_anim() | |
220 | |
221 | |
222 cdef void set_anim(self, long sprite_idx_offset=-1): | |
223 if sprite_idx_offset >= 0: | |
224 self.sprite_idx_offset = sprite_idx_offset | |
225 | |
226 lt = self._laser_type | |
227 self.sprite = Sprite() | |
228 self.anmrunner = ANMRunner(lt.anm, lt.anim_index, | |
229 self.sprite, self.sprite_idx_offset) | |
230 #self.sprite.blendfunc = 1 #XXX | |
231 | |
232 | |
233 cdef void cancel(self): | |
234 self.anmrunner.interrupt(1) | |
235 | |
236 | |
237 cdef void update(self): | |
238 if self.anmrunner is not None and not self.anmrunner.run_frame(): | |
239 self.anmrunner = None | |
240 self.removed = True | |
241 | |
242 length = self.origin.y | |
243 if self.frame == self.duration: | |
244 self.cancel() | |
245 | |
246 self.sprite.visible = (length > 0) | |
247 self.sprite.height_override = length | |
248 self.sprite.changed = True #TODO | |
249 | |
250 self.x = self.origin.x + self.offset * cos(self.angle) | |
251 self.y = self.origin.y / 2. + self.offset * sin(self.angle) | |
252 | |
253 self.frame += 1 |