def target(self, enemy_tank, grid): gz = constant.GRID_SIZE tank = grid.get_object(self._object_id) x,y = enemy_tank.position enemy_hitbox = enemy_tank.hitbox gx,gy = int(x/gz), int(y/gz) t_angle = tank.angle mna, mxa = math.inf,-math.inf a1, a2 = None, None for bullet_angle in self._2damap[gx][gy]: trajectory = int(bullet_angle/10) bounce = bullet_angle % 10 if not self._asafe[trajectory]: continue l0 = self._amap[trajectory][bounce-1] if bounce !=0 else tank.position l1 = self._amap[trajectory][bounce] line = [l0,l1] if (collision.line_square(line, enemy_hitbox)): bullet_dir = math.atan2(l1[1]-l0[1],l1[0]-l0[0]) a_diff = VMath.angle_diff(bullet_dir, enemy_tank.angle) a_diff = VMath.astc(a_diff) if a_diff < mna: a1 = trajectory mna = a_diff if a_diff > mxa: a2 = trajectory mxa = a_diff if a1 is None and a2 is None: return False if a1 == a2: return [a1] else: return [a1,a2]
def _signed_distance(p1,p2,angle): distance = round(VMath.distance(p1,p2), constant.PRECISION) l_angle = math.atan2(p2[1]-p1[1],p2[0]-p1[0]) diff_angle = VMath.angle_diff(angle,l_angle) if diff_angle > math.pi/2: distance *= -1 return distance
def render_entity(self, surface, sprite, entity, time_step, angle=None, pivot=[0, 0]): if isinstance(sprite, pygame.Surface): tile = sprite else: i, j = sprite tile = self._sprite_sheet[i][j].copy().convert_alpha() if not angle: a1, a2 = entity.old_angle, entity.angle else: a1, a2 = angle[0], angle[1] angle = a1 + VMath.angle_diff(a1, a2) * time_step rot_tile = pygame.transform.rotate(tile, -math.degrees(angle)) center = rot_tile.get_width() / 2, rot_tile.get_height() / 2 x1, y1 = entity.position x0, y0 = entity.old_position entity_pos = x0 + (x1 - x0) * time_step, y0 + (y1 - y0) * time_step pos = VMath.subtract(entity_pos, center) # Account for pivot pos = VMath.subtract(pos, VMath.rotate([pivot], angle)[0]) surface.blit(rot_tile, pos)