Mercurial > touhou
annotate pytouhou/game/laser.py @ 279:3539520fff93
Fix sprite rotation/translation.
ANM translations are done *after* rotation, lasers are handled differently.
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Wed, 08 Feb 2012 18:43:39 +0100 |
parents | 615c0bb6064b |
children | 13dcde917083 |
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 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
17 from pytouhou.vm.anmrunner import ANMRunner |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
18 from pytouhou.game.sprite import Sprite |
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 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
21 STARTING, STARTED, STOPPING = range(3) |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
22 |
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 class Laser(object): |
279
3539520fff93
Fix sprite rotation/translation.
Thibaut Girka <thib@sitedethib.com>
parents:
278
diff
changeset
|
25 def __init__(self, base_pos, laser_type, sprite_idx_offset, |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
26 angle, speed, start_offset, end_offset, max_length, width, |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
27 start_duration, duration, stop_duration, |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
28 grazing_delay, grazing_extra_duration, |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
29 game): |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
30 self._game = game |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
31 #TODO: aux sprite |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
32 self._sprite = None |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
33 self._anmrunner = None |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
34 self._removed = False |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
35 self._laser_type = laser_type |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
36 self.state = STARTING |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
37 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
38 #TODO: hitbox |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
39 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
40 self.frame = 0 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
41 self.start_duration = start_duration |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
42 self.duration = duration |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
43 self.stop_duration = stop_duration |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
44 self.grazing_delay = grazing_delay |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
45 self.grazing_extra_duration = grazing_extra_duration |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
46 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
47 self.sprite_idx_offset = sprite_idx_offset |
279
3539520fff93
Fix sprite rotation/translation.
Thibaut Girka <thib@sitedethib.com>
parents:
278
diff
changeset
|
48 self.base_pos = base_pos |
3539520fff93
Fix sprite rotation/translation.
Thibaut Girka <thib@sitedethib.com>
parents:
278
diff
changeset
|
49 self.x, self.y = 0, 0 |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
50 self.angle = angle |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
51 self.speed = speed |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
52 self.start_offset = start_offset |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
53 self.end_offset = end_offset |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
54 self.max_length = max_length |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
55 self.width = width |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
56 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
57 self.set_anim() |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
58 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
59 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
60 def set_anim(self, sprite_idx_offset=None): |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
61 if sprite_idx_offset is not None: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
62 self.sprite_idx_offset = sprite_idx_offset |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
63 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
64 lt = self._laser_type |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
65 self._sprite = Sprite() |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
66 self._sprite.angle = self.angle |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
67 self._anmrunner = ANMRunner(lt.anm_wrapper, lt.anim_index, |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
68 self._sprite, self.sprite_idx_offset) |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
69 self._anmrunner.run_frame() |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
70 |
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 def get_bullets_pos(self): |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
73 #TODO: check |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
74 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
|
75 offset = self.end_offset - length |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
76 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
|
77 while self.start_offset <= offset < self.end_offset: |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
78 yield (self.x + offset * dx, self.y + offset * dy) |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
79 offset += 48. |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
80 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
81 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
82 def cancel(self): |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
83 if self.state != STOPPING: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
84 self.frame = 0 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
85 self.state = STOPPING |
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 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
88 def update(self): |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
89 if self._anmrunner is not None and not self._anmrunner.run_frame(): |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
90 self._anmrunner = None |
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 self.end_offset += self.speed |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
93 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
94 length = min(self.end_offset - self.start_offset, self.max_length) # TODO |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
95 if self.state == STARTING: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
96 if self.frame == self.start_duration: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
97 self.frame = 0 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
98 self.state = STARTED |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
99 else: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
100 width = self.width * float(self.frame) / self.start_duration #TODO |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
101 if self.state == STARTED: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
102 width = self.width #TODO |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
103 if self.frame == self.duration: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
104 self.frame = 0 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
105 self.state = STOPPING |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
106 if self.state == STOPPING: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
107 if self.frame == self.stop_duration: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
108 width = 0. |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
109 self._removed = True |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
110 else: |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
111 width = self.width * (1. - float(self.frame) / self.stop_duration) #TODO |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
112 |
279
3539520fff93
Fix sprite rotation/translation.
Thibaut Girka <thib@sitedethib.com>
parents:
278
diff
changeset
|
113 offset = self.end_offset - length / 2. |
3539520fff93
Fix sprite rotation/translation.
Thibaut Girka <thib@sitedethib.com>
parents:
278
diff
changeset
|
114 self.x, self.y = self.base_pos[0] + offset * cos(self.angle), self.base_pos[1] + offset * sin(self.angle) |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
115 self._sprite.width_override = width or 0.01 #TODO |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
116 self._sprite.height_override = length or 0.01 #TODO |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
117 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
118 self._sprite.update_orientation(pi/2. - self.angle, True) |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
119 self._sprite._changed = True #TODO |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
120 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
121 self.frame += 1 |
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
122 |