comparison pytouhou/game/laser.py @ 293:ab618c2bbce8

Implement laser collision.
author Thibaut Girka <thib@sitedethib.com>
date Mon, 20 Feb 2012 18:58:07 +0100
parents 18e4ed141dd8
children 94c636f8f863
comparison
equal deleted inserted replaced
292:3a81b607f974 293:ab618c2bbce8
98 self._anmrunner = ANMRunner(lt.anm_wrapper, lt.anim_index, 98 self._anmrunner = ANMRunner(lt.anm_wrapper, lt.anim_index,
99 self._sprite, self.sprite_idx_offset) 99 self._sprite, self.sprite_idx_offset)
100 self._anmrunner.run_frame() 100 self._anmrunner.run_frame()
101 101
102 102
103 def _check_collision(self, point, border_size):
104 x, y = point[0] - self.base_pos[0], point[1] - self.base_pos[1]
105 dx, dy = cos(self.angle), sin(self.angle)
106 dx2, dy2 = -dy, dx
107
108 length = min(self.end_offset - self.start_offset, self.max_length)
109 offset = self.end_offset - length - border_size / 2.
110 end_offset = self.end_offset + border_size / 2.
111 half_width = self.width / 4. + border_size / 2.
112
113 c1 = dx * offset - dx2 * half_width, dy * offset - dy2 * half_width
114 c2 = dx * offset + dx2 * half_width, dy * offset + dy2 * half_width
115 c3 = dx * end_offset + dx2 * half_width, dy * end_offset + dy2 * half_width
116 vx, vy = x - c2[0], y - c2[1]
117 v1x, v1y = c1[0] - c2[0], c1[1] - c2[1]
118 v2x, v2y = c3[0] - c2[0], c3[1] - c2[1]
119
120 return (0 <= vx * v1x + vy * v1y <= v1x * v1x + v1y * v1y
121 and 0 <= vx * v2x + vy * v2y <= v2x * v2x + v2y * v2y)
122
123
124 def check_collision(self, point):
125 if self.state != STARTED:
126 return False
127
128 return self._check_collision(point, 2.5)
129
130
131 def check_grazing(self, point):
132 #TODO: quadruple check!
133 if self.state == STOPPING and self.frame >= self.grazing_extra_duration:
134 return False
135 if self.state == STARTING and self.frame <= self.grazing_delay:
136 return False
137 if self.frame % 12 != 0:
138 return False
139
140 return self._check_collision(point, 96 + 2.5)
141
142
103 def get_bullets_pos(self): 143 def get_bullets_pos(self):
104 #TODO: check 144 #TODO: check
105 length = min(self.end_offset - self.start_offset, self.max_length) 145 length = min(self.end_offset - self.start_offset, self.max_length)
106 offset = self.end_offset - length 146 offset = self.end_offset - length
107 dx, dy = cos(self.angle), sin(self.angle) 147 dx, dy = cos(self.angle), sin(self.angle)
109 yield (self.base_pos[0] + offset * dx, self.base_pos[1] + offset * dy) 149 yield (self.base_pos[0] + offset * dx, self.base_pos[1] + offset * dy)
110 offset += 48. 150 offset += 48.
111 151
112 152
113 def cancel(self): 153 def cancel(self):
154 self.grazing_extra_duration = 0
114 if self.state != STOPPING: 155 if self.state != STOPPING:
115 self.frame = 0 156 self.frame = 0
116 self.state = STOPPING 157 self.state = STOPPING
117 158
118 159