annotate pytouhou/game/laser.pyx @ 644:f983a4c98410

Remove unused variables.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 03 Aug 2019 22:44:01 +0200
parents a6af3ff86612
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 ##
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 ## Copyright (C) 2012 Thibaut Girka <thib@sitedethib.com>
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 ##
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 ##
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 ## GNU General Public License for more details.
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 ##
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
15 from libc.math cimport cos, sin, M_PI as pi
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16
495
b32cef75df59 Drop an useless dependency on Player from Orb, on Game from Laser.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 491
diff changeset
17 from pytouhou.game.game cimport Game
547
e35bef07290d Always import runners from pytouhou.vm, to allow their replacement.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 540
diff changeset
18 from pytouhou.vm import ANMRunner
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
21 cdef class LaserLaunchAnim(Element):
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
22 def __init__(self, Laser laser, anm, unsigned long index):
440
b9d2db93972f Add a base Element class for every object in pytouhou.game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 433
diff changeset
23 Element.__init__(self, (0, 0))
b9d2db93972f Add a base Element class for every object in pytouhou.game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 433
diff changeset
24
290
18e4ed141dd8 Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents: 281
diff changeset
25 self._laser = laser
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
26 self.sprite = Sprite()
430
c9433188ffdb Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 390
diff changeset
27 self.sprite.anm = anm
c9433188ffdb Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 390
diff changeset
28 self.sprite.texcoords = anm.sprites[index]
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
29 self.sprite.blendfunc = 1
290
18e4ed141dd8 Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents: 281
diff changeset
30
18e4ed141dd8 Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents: 281
diff changeset
31
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
32 cpdef update(self):
290
18e4ed141dd8 Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents: 281
diff changeset
33 laser = self._laser
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
34 length = <double>min(laser.end_offset - laser.start_offset, laser.max_length)
290
18e4ed141dd8 Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents: 281
diff changeset
35 offset = laser.end_offset - length
18e4ed141dd8 Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents: 281
diff changeset
36 dx, dy = cos(laser.angle), sin(laser.angle)
18e4ed141dd8 Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents: 281
diff changeset
37
18e4ed141dd8 Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents: 281
diff changeset
38 self.x = laser.base_pos[0] + offset * dx
18e4ed141dd8 Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents: 281
diff changeset
39 self.y = laser.base_pos[1] + offset * dy
18e4ed141dd8 Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents: 281
diff changeset
40
309
14c9aca8e274 Implement Remilia's laser webs.
Thibaut Girka <thib@sitedethib.com>
parents: 304
diff changeset
41 scale = laser.width / 10. - (offset - laser.start_offset) #TODO: check
527
db28538cd399 Use Sprite C arrays instead of their tuple representation where it makes sense.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 495
diff changeset
42 self.sprite._rescale[:] = [scale, scale]
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
43 self.sprite.changed = True
290
18e4ed141dd8 Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents: 281
diff changeset
44
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
45 if laser.removed or scale <= 0.:
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
46 self.removed = True
290
18e4ed141dd8 Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents: 281
diff changeset
47
18e4ed141dd8 Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents: 281
diff changeset
48
18e4ed141dd8 Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents: 281
diff changeset
49
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
50 cdef class Laser(Element):
472
8038f1957b71 Type bullettype, itemtype and lasertype a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 471
diff changeset
51 def __init__(self, tuple base_pos, LaserType laser_type,
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
52 unsigned long sprite_idx_offset, double angle, double speed,
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
53 double start_offset, double end_offset, double max_length,
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
54 double width, unsigned long start_duration,
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
55 unsigned long duration, unsigned long stop_duration,
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
56 unsigned long grazing_delay,
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
57 unsigned long grazing_extra_duration, Game game):
440
b9d2db93972f Add a base Element class for every object in pytouhou.game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 433
diff changeset
58 Element.__init__(self, (0, 0))
b9d2db93972f Add a base Element class for every object in pytouhou.game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 433
diff changeset
59
430
c9433188ffdb Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 390
diff changeset
60 launch_anim = LaserLaunchAnim(self, laser_type.anm,
290
18e4ed141dd8 Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents: 281
diff changeset
61 laser_type.launch_anim_offsets[sprite_idx_offset]
18e4ed141dd8 Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents: 281
diff changeset
62 + laser_type.launch_sprite_idx)
495
b32cef75df59 Drop an useless dependency on Player from Orb, on Game from Laser.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 491
diff changeset
63 game.effects.append(launch_anim)
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64 self._laser_type = laser_type
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 self.state = STARTING
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67 #TODO: hitbox
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69 self.frame = 0
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70 self.start_duration = start_duration
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 self.duration = duration
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72 self.stop_duration = stop_duration
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73 self.grazing_delay = grazing_delay
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74 self.grazing_extra_duration = grazing_extra_duration
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
75
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
76 self.sprite_idx_offset = sprite_idx_offset
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
77 self.set_base_pos(base_pos[0], base_pos[1])
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
78 self.angle = angle
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
79 self.speed = speed
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
80 self.start_offset = start_offset
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81 self.end_offset = end_offset
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 self.max_length = max_length
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83 self.width = width
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
84
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85 self.set_anim()
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
88 cdef bint set_anim(self, long sprite_idx_offset=-1) except True:
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
89 if sprite_idx_offset >= 0:
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
90 self.sprite_idx_offset = sprite_idx_offset
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
91
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92 lt = self._laser_type
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
93 self.sprite = Sprite()
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
94 self.sprite.angle = self.angle
430
c9433188ffdb Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 390
diff changeset
95 self.anmrunner = ANMRunner(lt.anm, lt.anim_index,
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
96 self.sprite, self.sprite_idx_offset)
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
97
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
98
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
99 cpdef set_base_pos(self, double x, double y):
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
100 self.base_pos[:] = [x, y]
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
101
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
102
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
103 cdef bint _check_collision(self, double point[2], double border_size):
540
53fa73932e9a Fix warnings introduced in Cython 0.20, when more than one pointer is defined on the same line.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 527
diff changeset
104 cdef double c1[2]
53fa73932e9a Fix warnings introduced in Cython 0.20, when more than one pointer is defined on the same line.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 527
diff changeset
105 cdef double c2[2]
53fa73932e9a Fix warnings introduced in Cython 0.20, when more than one pointer is defined on the same line.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 527
diff changeset
106 cdef double c3[2]
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
107
293
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
108 x, y = point[0] - self.base_pos[0], point[1] - self.base_pos[1]
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
109 dx, dy = cos(self.angle), sin(self.angle)
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
110 dx2, dy2 = -dy, dx
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
111
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
112 length = <double>min(self.end_offset - self.start_offset, self.max_length)
293
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
113 offset = self.end_offset - length - border_size / 2.
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
114 end_offset = self.end_offset + border_size / 2.
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
115 half_width = self.width / 4. + border_size / 2.
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
116
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
117 c1[:] = [dx * offset - dx2 * half_width, dy * offset - dy2 * half_width]
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
118 c2[:] = [dx * offset + dx2 * half_width, dy * offset + dy2 * half_width]
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
119 c3[:] = [dx * end_offset + dx2 * half_width, dy * end_offset + dy2 * half_width]
293
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
120 vx, vy = x - c2[0], y - c2[1]
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
121 v1x, v1y = c1[0] - c2[0], c1[1] - c2[1]
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
122 v2x, v2y = c3[0] - c2[0], c3[1] - c2[1]
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
123
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
124 return (0 <= vx * v1x + vy * v1y <= v1x * v1x + v1y * v1y
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
125 and 0 <= vx * v2x + vy * v2y <= v2x * v2x + v2y * v2y)
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
126
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
127
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
128 cdef bint check_collision(self, double point[2]):
293
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
129 if self.state != STARTED:
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
130 return False
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
131
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
132 return self._check_collision(point, 2.5)
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
133
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
134
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
135 cdef bint check_grazing(self, double point[2]):
293
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
136 #TODO: quadruple check!
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
137 if self.state == STOPPING and self.frame >= self.grazing_extra_duration:
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
138 return False
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
139 if self.state == STARTING and self.frame <= self.grazing_delay:
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
140 return False
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
141 if self.frame % 12 != 0:
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
142 return False
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
143
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
144 return self._check_collision(point, 96 + 2.5)
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
145
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
146
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
147 def get_bullets_pos(self):
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
148 #TODO: check
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
149 length = <double>min(self.end_offset - self.start_offset, self.max_length)
278
615c0bb6064b Fix generation of bullets/items position from lasers.
Thibaut Girka <thib@sitedethib.com>
parents: 274
diff changeset
150 offset = self.end_offset - length
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
151 dx, dy = cos(self.angle), sin(self.angle)
278
615c0bb6064b Fix generation of bullets/items position from lasers.
Thibaut Girka <thib@sitedethib.com>
parents: 274
diff changeset
152 while self.start_offset <= offset < self.end_offset:
281
13dcde917083 Fix bullet spawning along lasers.
Thibaut Girka <thib@sitedethib.com>
parents: 279
diff changeset
153 yield (self.base_pos[0] + offset * dx, self.base_pos[1] + offset * dy)
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
154 offset += 48.
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
155
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
156
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
157 cpdef cancel(self):
293
ab618c2bbce8 Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents: 290
diff changeset
158 self.grazing_extra_duration = 0
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
159 if self.state != STOPPING:
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
160 self.frame = 0
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
161 self.state = STOPPING
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
162
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
163
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
164 cpdef update(self):
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
165 if self.anmrunner is not None and not self.anmrunner.run_frame():
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
166 self.anmrunner = None
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
167
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
168 self.end_offset += self.speed
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
169
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
170 length = <double>min(self.end_offset - self.start_offset, self.max_length) # TODO
491
2276229282fd Fix gcc’s warnings with -Wall -Wextra.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 472
diff changeset
171 width = 0.
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
172 if self.state == STARTING:
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
173 if self.frame == self.start_duration:
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
174 self.frame = 0
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
175 self.state = STARTED
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
176 else:
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
177 width = self.width * float(self.frame) / self.start_duration #TODO
491
2276229282fd Fix gcc’s warnings with -Wall -Wextra.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 472
diff changeset
178 elif self.state == STARTED:
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
179 width = self.width #TODO
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
180 if self.frame == self.duration:
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
181 self.frame = 0
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
182 self.state = STOPPING
491
2276229282fd Fix gcc’s warnings with -Wall -Wextra.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 472
diff changeset
183 elif self.state == STOPPING:
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
184 if self.frame == self.stop_duration:
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
185 self.removed = True
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
186 else:
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
187 width = self.width * (1. - float(self.frame) / self.stop_duration) #TODO
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
188
279
3539520fff93 Fix sprite rotation/translation.
Thibaut Girka <thib@sitedethib.com>
parents: 278
diff changeset
189 offset = self.end_offset - length / 2.
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
190 self.x = self.base_pos[0] + offset * cos(self.angle)
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
191 self.y = self.base_pos[1] + offset * sin(self.angle)
467
5bb7d2c0ff46 Fix lasers sprite handling
Thibaut Girka <thib@sitedethib.com>
parents: 440
diff changeset
192 self.sprite.visible = (width > 0 and length > 0)
5bb7d2c0ff46 Fix lasers sprite handling
Thibaut Girka <thib@sitedethib.com>
parents: 440
diff changeset
193 self.sprite.width_override = width
5bb7d2c0ff46 Fix lasers sprite handling
Thibaut Girka <thib@sitedethib.com>
parents: 440
diff changeset
194 self.sprite.height_override = length
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
195
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
196 self.sprite.update_orientation(pi/2. - self.angle, True)
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
197 self.sprite.changed = True #TODO
274
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
198
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
199 self.frame += 1
f037bca24f2d Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
200
294
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
201
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
202 cdef class PlayerLaser(Element):
472
8038f1957b71 Type bullettype, itemtype and lasertype a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 471
diff changeset
203 def __init__(self, LaserType laser_type, unsigned long sprite_idx_offset,
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
204 tuple hitbox, unsigned long damage, double angle,
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
205 double offset, unsigned long duration, Element origin):
440
b9d2db93972f Add a base Element class for every object in pytouhou.game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 433
diff changeset
206 Element.__init__(self)
b9d2db93972f Add a base Element class for every object in pytouhou.game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 433
diff changeset
207
294
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
208 self._laser_type = laser_type
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
209 self.origin = origin
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
210
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
211 self.hitbox[:] = [hitbox[0], hitbox[1]]
294
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
212
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
213 self.frame = 0
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
214 self.duration = duration
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
215
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
216 self.sprite_idx_offset = sprite_idx_offset
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
217 self.angle = angle
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
218 self.offset = offset
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
219 self.damage = damage
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
220
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
221 self.set_anim()
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
222
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
223
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
224 cdef bint set_anim(self, long sprite_idx_offset=-1) except True:
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
225 if sprite_idx_offset >= 0:
294
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
226 self.sprite_idx_offset = sprite_idx_offset
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
227
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
228 lt = self._laser_type
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
229 self.sprite = Sprite()
430
c9433188ffdb Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 390
diff changeset
230 self.anmrunner = ANMRunner(lt.anm, lt.anim_index,
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
231 self.sprite, self.sprite_idx_offset)
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
232 #self.sprite.blendfunc = 1 #XXX
294
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
233
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
234
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
235 cdef bint cancel(self) except True:
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
236 self.anmrunner.interrupt(1)
294
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
237
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
238
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
239 cdef bint update(self) except True:
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
240 if self.anmrunner is not None and not self.anmrunner.run_frame():
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
241 self.anmrunner = None
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
242 self.removed = True
294
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
243
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
244 length = self.origin.y
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
245 if self.frame == self.duration:
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
246 self.cancel()
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
247
467
5bb7d2c0ff46 Fix lasers sprite handling
Thibaut Girka <thib@sitedethib.com>
parents: 440
diff changeset
248 self.sprite.visible = (length > 0)
5bb7d2c0ff46 Fix lasers sprite handling
Thibaut Girka <thib@sitedethib.com>
parents: 440
diff changeset
249 self.sprite.height_override = length
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 294
diff changeset
250 self.sprite.changed = True #TODO
294
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
251
471
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
252 self.x = self.origin.x + self.offset * cos(self.angle)
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
253 self.y = self.origin.y / 2. + self.offset * sin(self.angle)
06f0eeb519bb Make Laser and Orb extension types, and use that where possible.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 467
diff changeset
254
294
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 293
diff changeset
255 self.frame += 1