Mercurial > touhou
annotate pytouhou/game/bullet.py @ 88:2cc60137d368
Ugly implementation of three new attack flags.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 03 Sep 2011 17:44:48 -0700 |
parents | a87a3c080585 |
children | 1513f5626656 |
rev | line source |
---|---|
84 | 1 # -*- encoding: utf-8 -*- |
2 ## | |
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> | |
4 ## | |
5 ## This program is free software; you can redistribute it and/or modify | |
6 ## it under the terms of the GNU General Public License as published | |
7 ## by the Free Software Foundation; version 3 only. | |
8 ## | |
9 ## This program is distributed in the hope that it will be useful, | |
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 ## GNU General Public License for more details. | |
13 ## | |
14 | |
15 from math import cos, sin, atan2, pi | |
16 | |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
17 from pytouhou.utils.interpolator import Interpolator |
84 | 18 from pytouhou.vm.anmrunner import ANMRunner |
19 from pytouhou.game.sprite import Sprite | |
20 | |
21 | |
22 class Bullet(object): | |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
23 __slots__ = ('x', 'y', 'angle', 'speed', 'frame', 'grazed', |
84 | 24 'flags', 'attributes', 'anim_idx', 'sprite_idx_offset', 'player', |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
25 'speed_interpolator', |
84 | 26 '_game_state', '_sprite', '_anmrunner', |
27 '_removed', '_launched') | |
28 | |
29 def __init__(self, pos, anim_idx, sprite_idx_offset, | |
30 angle, speed, attributes, flags, player, game_state): | |
31 self._game_state = game_state | |
32 self._sprite = None | |
33 self._anmrunner = None | |
34 self._removed = False | |
35 self._launched = False | |
36 | |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
37 self.speed_interpolator = None |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
38 self.frame = 0 |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
39 self.grazed = False |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
40 |
84 | 41 self.player = player |
42 | |
43 self.anim_idx = anim_idx | |
44 self.sprite_idx_offset = sprite_idx_offset | |
45 | |
46 #TODO | |
47 #if flags & (2|4|8): | |
48 # index = {2: 11, 4: 12, 8: 13}[flags & (2|4|8)] | |
49 # self._sprite = Sprite() | |
50 # self._anmrunner = ANMRunner(self._game_state.resources.etama_anm_wrappers[0], | |
51 # index, self._sprite, sprite_idx_offset) | |
52 | |
53 self.flags = flags | |
54 self.attributes = list(attributes) | |
55 | |
56 self.x, self.y = pos | |
57 self.angle = angle | |
58 self.speed = speed | |
59 | |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
60 if flags & 1: |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
61 self.speed_interpolator = Interpolator((speed + 5.,)) |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
62 self.speed_interpolator.set_interpolation_start(0, (speed + 5.,)) |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
63 self.speed_interpolator.set_interpolation_end(16, (speed,)) |
88
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
64 if flags & 448: |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
65 self.speed_interpolator = Interpolator((speed,)) |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
66 self.speed_interpolator.set_interpolation_start(0, (speed,)) |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
67 self.speed_interpolator.set_interpolation_end(attributes[0] - 1, (0,)) # TODO: really -1? Without, the new speed isn’t set. |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
68 |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
69 |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
70 def get_player_angle(self): |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
71 return atan2(self.player.y - self.y, self.player.x - self.x) |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
72 |
84 | 73 |
74 def is_visible(self, screen_width, screen_height): | |
75 if self._sprite: | |
76 tx, ty, tw, th = self._sprite.texcoords | |
77 if self._sprite.corner_relative_placement: | |
78 raise Exception #TODO | |
79 else: | |
80 tx, ty, tw, th = 0., 0., 0., 0. | |
81 | |
82 max_x = tw / 2. | |
83 max_y = th / 2. | |
84 min_x = -max_x | |
85 min_y = -max_y | |
86 | |
87 if any((min_x > screen_width - self.x, | |
88 max_x < -self.x, | |
89 min_y > screen_height - self.y, | |
90 max_y < -self.y)): | |
91 return False | |
92 return True | |
93 | |
94 | |
85 | 95 def get_objects_by_texture(self, objects_by_texture): |
96 sprite = self._sprite | |
97 key = sprite.anm.first_name, sprite.anm.secondary_name | |
98 key = (key, sprite.blendfunc) | |
84 | 99 if not key in objects_by_texture: |
100 objects_by_texture[key] = (0, [], [], []) | |
85 | 101 vertices = ((x + self.x, y + self.y, z) for x, y, z in sprite._vertices) |
84 | 102 objects_by_texture[key][1].extend(vertices) |
85 | 103 objects_by_texture[key][2].extend(sprite._uvs) |
104 objects_by_texture[key][3].extend(sprite._colors) | |
84 | 105 |
106 | |
107 def update(self): | |
108 if not self._sprite or self._sprite._removed: | |
109 self._launched = True | |
110 self._sprite = Sprite() | |
111 self._anmrunner = ANMRunner(self._game_state.resources.etama_anm_wrappers[0], #TODO | |
112 self.anim_idx, self._sprite, self.sprite_idx_offset) | |
113 | |
114 if self._anmrunner and not self._anmrunner.run_frame(): | |
115 self._anmrunner = None | |
116 | |
85 | 117 self._sprite.update() |
118 if self._sprite._changed: #TODO | |
119 angle = pi/2.-self.angle if self._sprite.automatic_orientation else 0. | |
120 self._sprite.update_vertices_uvs_colors(angle_base=angle) | |
84 | 121 |
122 #TODO: flags | |
123 x, y = self.x, self.y | |
124 | |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
125 if self.flags & 16: |
88
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
126 duration = self.attributes[0] |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
127 length, angle = self.attributes[4:6] |
88
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
128 if self.frame < duration: |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
129 angle = self.angle if angle < -900.0 else angle #TODO: is that right? |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
130 dx = cos(self.angle) * self.speed + cos(angle) * length |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
131 dy = sin(self.angle) * self.speed + sin(angle) * length |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
132 self.speed = (dx ** 2 + dy ** 2) ** 0.5 |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
133 self.angle = atan2(dy, dx) |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
134 elif self.flags & 32: |
88
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
135 #TODO: count |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
136 #TODO: check |
88
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
137 duration, count = self.attributes[0:2] |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
138 acceleration, angular_speed = self.attributes[4:6] |
88
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
139 if self.frame < duration: |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
140 self.speed += acceleration |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
141 self.angle += angular_speed |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
142 elif self.flags & 448: |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
143 #TODO: check |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
144 frame, count = self.attributes[0:2] |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
145 if count > 0: |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
146 angle, speed = self.attributes[4:6] |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
147 if frame == self.frame: |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
148 self._sprite._removed = True |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
149 count = count - 1 |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
150 |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
151 if self.flags & 64: |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
152 self.angle = self.angle + angle |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
153 elif self.flags & 128: |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
154 self.angle = self.get_player_angle() + angle |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
155 elif self.flags & 256: |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
156 self.angle = angle |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
157 |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
158 if count: |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
159 self.speed_interpolator.set_interpolation_start(self.frame, (speed,)) |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
160 self.speed_interpolator.set_interpolation_end(self.frame + frame - 1, (0,)) # TODO: really -1? Without, the new speed isn’t set. |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
161 else: |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
162 self.speed = speed |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
163 |
2cc60137d368
Ugly implementation of three new attack flags.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
86
diff
changeset
|
164 self.attributes[1] = count |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
165 #TODO: other flags |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
166 |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
167 if self.speed_interpolator: |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
168 self.speed_interpolator.update(self.frame) |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
169 self.speed, = self.speed_interpolator.values |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
170 |
84 | 171 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed |
172 self.x, self.y = x + dx, y + dy | |
173 | |
86
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
174 self.frame += 1 |
a87a3c080585
Handle a few attack flags
Thibaut Girka <thib@sitedethib.com>
parents:
85
diff
changeset
|
175 |