Mercurial > touhou
annotate pytouhou/game/laser.py @ 464:36bc577b2392
Make the window resizable, and scale its content correctly.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 11 Sep 2013 15:55:19 +0200 |
parents | b9d2db93972f |
children | 5bb7d2c0ff46 |
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 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
15 from math import cos, sin, pi |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
16 |
440
b9d2db93972f
Add a base Element class for every object in pytouhou.game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
433
diff
changeset
|
17 from pytouhou.game.element import Element |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
18 from pytouhou.vm.anmrunner import ANMRunner |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
19 from pytouhou.game.sprite import Sprite |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
20 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
21 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
22 STARTING, STARTED, STOPPING = range(3) |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
23 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
24 |
440
b9d2db93972f
Add a base Element class for every object in pytouhou.game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
433
diff
changeset
|
25 class LaserLaunchAnim(Element): |
430
c9433188ffdb
Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
390
diff
changeset
|
26 def __init__(self, laser, anm, index): |
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, (0, 0)) |
b9d2db93972f
Add a base Element class for every object in pytouhou.game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
433
diff
changeset
|
28 |
290
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
29 self._laser = laser |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
30 self.sprite = Sprite() |
430
c9433188ffdb
Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
390
diff
changeset
|
31 self.sprite.anm = anm |
c9433188ffdb
Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
390
diff
changeset
|
32 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
|
33 self.sprite.blendfunc = 1 |
290
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
34 |
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
35 |
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
36 def update(self): |
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
37 laser = self._laser |
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
38 length = min(laser.end_offset - laser.start_offset, laser.max_length) |
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
39 offset = laser.end_offset - length |
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
40 dx, dy = cos(laser.angle), sin(laser.angle) |
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
41 |
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
42 self.x = laser.base_pos[0] + offset * dx |
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
43 self.y = laser.base_pos[1] + offset * dy |
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
44 |
309
14c9aca8e274
Implement Remilia's laser webs.
Thibaut Girka <thib@sitedethib.com>
parents:
304
diff
changeset
|
45 scale = laser.width / 10. - (offset - laser.start_offset) #TODO: check |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
46 self.sprite.rescale = (scale, scale) |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
47 self.sprite.changed = True |
290
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
48 |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
49 if laser.removed or scale <= 0.: |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
50 self.removed = True |
290
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
51 |
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
52 |
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
53 |
440
b9d2db93972f
Add a base Element class for every object in pytouhou.game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
433
diff
changeset
|
54 class Laser(Element): |
279
3539520fff93
Fix sprite rotation/translation.
Thibaut Girka <thib@sitedethib.com>
parents:
278
diff
changeset
|
55 def __init__(self, base_pos, laser_type, sprite_idx_offset, |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
56 angle, speed, start_offset, end_offset, max_length, width, |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
57 start_duration, duration, stop_duration, |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
58 grazing_delay, grazing_extra_duration, |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
59 game): |
440
b9d2db93972f
Add a base Element class for every object in pytouhou.game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
433
diff
changeset
|
60 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
|
61 |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
62 self._game = game |
430
c9433188ffdb
Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
390
diff
changeset
|
63 launch_anim = LaserLaunchAnim(self, laser_type.anm, |
290
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
64 laser_type.launch_anim_offsets[sprite_idx_offset] |
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
65 + laser_type.launch_sprite_idx) |
18e4ed141dd8
Display lasers' “launch anim”.
Thibaut Girka <thib@sitedethib.com>
parents:
281
diff
changeset
|
66 self._game.effects.append(launch_anim) |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
67 self._laser_type = laser_type |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
68 self.state = STARTING |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
69 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
70 #TODO: hitbox |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
71 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
72 self.frame = 0 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
73 self.start_duration = start_duration |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
74 self.duration = duration |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
75 self.stop_duration = stop_duration |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
76 self.grazing_delay = grazing_delay |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
77 self.grazing_extra_duration = grazing_extra_duration |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
78 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
79 self.sprite_idx_offset = sprite_idx_offset |
279
3539520fff93
Fix sprite rotation/translation.
Thibaut Girka <thib@sitedethib.com>
parents:
278
diff
changeset
|
80 self.base_pos = base_pos |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
81 self.angle = angle |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
82 self.speed = speed |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
83 self.start_offset = start_offset |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
84 self.end_offset = end_offset |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
85 self.max_length = max_length |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
86 self.width = width |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
87 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
88 self.set_anim() |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
89 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
90 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
91 def set_anim(self, sprite_idx_offset=None): |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
92 if sprite_idx_offset is not None: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
93 self.sprite_idx_offset = sprite_idx_offset |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
94 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
95 lt = self._laser_type |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
96 self.sprite = Sprite() |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
97 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
|
98 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
|
99 self.sprite, self.sprite_idx_offset) |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
100 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
101 |
293
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
102 def _check_collision(self, point, border_size): |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
103 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
|
104 dx, dy = cos(self.angle), sin(self.angle) |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
105 dx2, dy2 = -dy, dx |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
106 |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
107 length = min(self.end_offset - self.start_offset, self.max_length) |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
108 offset = self.end_offset - length - border_size / 2. |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
109 end_offset = self.end_offset + border_size / 2. |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
110 half_width = self.width / 4. + border_size / 2. |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
111 |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
112 c1 = dx * offset - dx2 * half_width, dy * offset - dy2 * half_width |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
113 c2 = dx * offset + dx2 * half_width, dy * offset + dy2 * half_width |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
114 c3 = dx * end_offset + dx2 * half_width, dy * end_offset + dy2 * half_width |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
115 vx, vy = x - c2[0], y - c2[1] |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
116 v1x, v1y = c1[0] - c2[0], c1[1] - c2[1] |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
117 v2x, v2y = c3[0] - c2[0], c3[1] - c2[1] |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
118 |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
119 return (0 <= vx * v1x + vy * v1y <= v1x * v1x + v1y * v1y |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
120 and 0 <= vx * v2x + vy * v2y <= v2x * v2x + v2y * v2y) |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
121 |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
122 |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
123 def check_collision(self, point): |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
124 if self.state != STARTED: |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
125 return False |
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 return self._check_collision(point, 2.5) |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
128 |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
129 |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
130 def check_grazing(self, point): |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
131 #TODO: quadruple check! |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
132 if self.state == STOPPING and self.frame >= self.grazing_extra_duration: |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
133 return False |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
134 if self.state == STARTING and self.frame <= self.grazing_delay: |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
135 return False |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
136 if self.frame % 12 != 0: |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
137 return False |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
138 |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
139 return self._check_collision(point, 96 + 2.5) |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
140 |
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
141 |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
142 def get_bullets_pos(self): |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
143 #TODO: check |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
144 length = 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
|
145 offset = self.end_offset - length |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
146 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
|
147 while self.start_offset <= offset < self.end_offset: |
281
13dcde917083
Fix bullet spawning along lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
279
diff
changeset
|
148 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
|
149 offset += 48. |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
150 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
151 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
152 def cancel(self): |
293
ab618c2bbce8
Implement laser collision.
Thibaut Girka <thib@sitedethib.com>
parents:
290
diff
changeset
|
153 self.grazing_extra_duration = 0 |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
154 if self.state != STOPPING: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
155 self.frame = 0 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
156 self.state = STOPPING |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
157 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
158 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
159 def update(self): |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
160 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
|
161 self.anmrunner = None |
274
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 self.end_offset += self.speed |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
164 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
165 length = min(self.end_offset - self.start_offset, self.max_length) # TODO |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
166 if self.state == STARTING: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
167 if self.frame == self.start_duration: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
168 self.frame = 0 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
169 self.state = STARTED |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
170 else: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
171 width = self.width * float(self.frame) / self.start_duration #TODO |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
172 if self.state == STARTED: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
173 width = self.width #TODO |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
174 if self.frame == self.duration: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
175 self.frame = 0 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
176 self.state = STOPPING |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
177 if self.state == STOPPING: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
178 if self.frame == self.stop_duration: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
179 width = 0. |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
180 self.removed = True |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
181 else: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
182 width = self.width * (1. - float(self.frame) / self.stop_duration) #TODO |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
183 |
279
3539520fff93
Fix sprite rotation/translation.
Thibaut Girka <thib@sitedethib.com>
parents:
278
diff
changeset
|
184 offset = self.end_offset - length / 2. |
3539520fff93
Fix sprite rotation/translation.
Thibaut Girka <thib@sitedethib.com>
parents:
278
diff
changeset
|
185 self.x, self.y = self.base_pos[0] + offset * cos(self.angle), self.base_pos[1] + offset * sin(self.angle) |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
186 self.sprite.width_override = width or 0.01 #TODO |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
187 self.sprite.height_override = length or 0.01 #TODO |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
188 |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
189 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
|
190 self.sprite.changed = True #TODO |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
191 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
192 self.frame += 1 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
193 |
294
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
194 |
440
b9d2db93972f
Add a base Element class for every object in pytouhou.game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
433
diff
changeset
|
195 class PlayerLaser(Element): |
294
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
196 def __init__(self, laser_type, sprite_idx_offset, hitbox, damage, |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
197 angle, offset, duration, origin): |
440
b9d2db93972f
Add a base Element class for every object in pytouhou.game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
433
diff
changeset
|
198 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
|
199 |
294
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
200 self._laser_type = laser_type |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
201 self.origin = origin |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
202 |
390
b11953cf1d3b
Use only half-size hitboxes for player.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
384
diff
changeset
|
203 self.hitbox = hitbox[0], hitbox[1] |
294
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
204 |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
205 self.frame = 0 |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
206 self.duration = duration |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
207 |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
208 self.sprite_idx_offset = sprite_idx_offset |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
209 self.angle = angle |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
210 self.offset = offset |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
211 self.damage = damage |
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.set_anim() |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
214 |
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 @property |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
217 def x(self): |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
218 return self.origin.x + self.offset * cos(self.angle) |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
219 |
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 @property |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
222 def y(self): |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
223 return self.origin.y / 2. + self.offset * sin(self.angle) |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
224 |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
225 |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
226 def set_anim(self, sprite_idx_offset=None): |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
227 if sprite_idx_offset is not None: |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
228 self.sprite_idx_offset = sprite_idx_offset |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
229 |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
230 lt = self._laser_type |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
231 self.sprite = Sprite() |
430
c9433188ffdb
Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
390
diff
changeset
|
232 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
|
233 self.sprite, self.sprite_idx_offset) |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
234 #self.sprite.blendfunc = 1 #XXX |
294
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
235 |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
236 |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
237 def cancel(self): |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
238 self.anmrunner.interrupt(1) |
294
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
239 |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
240 |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
241 def update(self): |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
242 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
|
243 self.anmrunner = None |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
244 self.removed = True |
294
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
245 |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
246 length = self.origin.y |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
247 if self.frame == self.duration: |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
248 self.cancel() |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
249 |
304
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
250 self.sprite.height_override = length or 0.01 #TODO |
f3099ebf4f61
Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents:
294
diff
changeset
|
251 self.sprite.changed = True #TODO |
294
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
252 |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
253 self.frame += 1 |
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
293
diff
changeset
|
254 |