Example #1
0
    def _add_wall(self):
        if self.player_hide.walls_counter < self.player_hide.walls_max and not self.player_hide.wall_timer:
            wall_pos = copy.deepcopy(self.player_hide.pos)
            wall_size = (max(int(self.player_hide.width / 10), 2),
                         max(int(self.player_hide.height / 2), 2))  # minimum 2x2 Wall
            vision_arc_range = np.sqrt((self.player_hide.vision_top.x - self.player_hide.pos.x) * (self.player_hide.vision_top.x - self.player_hide.pos.x) + (
                self.player_hide.vision_top.y - self.player_hide.pos.y) * (self.player_hide.vision_top.y - self.player_hide.pos.y))
            # vision arc range - 1.5 wall width, so the wall is always created inside PoV.
            wall_pos.x = wall_pos.x + vision_arc_range - \
                (1.5 * wall_size[0])
            wall_pos = Point.triangle_unit_circle_relative(
                self.player_hide.direction, self.player_hide.pos, wall_pos)

            wall = Wall(self.player_hide, wall_pos.x,
                        wall_pos.y, wall_size, self.cfg['graphics_path_wall_owner'])
            wall._rotate(self.player_hide.direction, wall_pos)
            if self._can_create_wall(wall, self.agent_env['p_hide']['enemy']):
                self.player_hide.walls_counter += 1
                self.walls_group.add(wall)
                self.player_hide.wall_timer = copy.deepcopy(
                    self.player_hide.wall_timer_init)
                return True
            else:
                del wall

        return False
Example #2
0
    def _rotate(self, angle, position):
        """
        Rotates the sprite by creating new Rectangle and updates its polygon points

        Parameters
        ----------
            angle : float
                player direction in radians
            position : Point
                center of the wall

        Returns
        -------
            None
        """
        self.direction = angle
        # Copy and then rotate the original image.
        copied_image = self.image.copy()
        self.image = pygame.transform.rotozoom(copied_image,
                                               -angle * 180 / math.pi, 1)
        self.image.set_colorkey((0, 0, 0))

        # Create a new rect with the center of the sprite.
        self.rect = self.image.get_rect()
        self.rect.center = (position.x, position.y)
        self.width = self.rect.width
        self.height = self.rect.height

        # Update the polygon points for collisions
        self.polygon_points = [
            Point.triangle_unit_circle_relative(angle, self.pos, polygon_point)
            for polygon_point in self.polygon_points
        ]
Example #3
0
    def _determine_new_ray_points(self, wall_edges):
        """
        Algorithm which calculates new possible ray points based on the current Agent Position and Agent Direction

        Parameters
        ----------
            None

        Returns
        -------
            ray_points : list of Point
                list of new, standard Ray Points
        """

        ray_points = []
        dir_t = self.direction - 2*math.pi if self.direction > math.pi else self.direction
        point_angle_0 = self.pos + Point((self.vision_radius, 0))
        angles = np.linspace(dir_t - self.vision_rad / 2, dir_t + self.vision_rad / 2, num=11,
                             endpoint=True)  # counter-clockwise
        angles_min, angles_max = min(angles), max(angles)

        for p in [pnt for wall_edge in wall_edges for pnt in wall_edge]:
            delta = p - self.pos
            theta_radians = math.atan2(delta.y, delta.x)

            # 3rd quartet (2nd in math, 3rd in Y being from top to bottom), fixes 2nd; from [-PI; PI] to [-2PI; 0]
            if angles_min < -math.pi and theta_radians > 0:
                theta_radians = theta_radians - 2*math.pi
            # 2nd quartet (3rd in math, 2nd in Y being from top to bottom), fixes 3rd; from [-PI; PI] to [0; 2PI]
            elif angles_max > math.pi and theta_radians < 0:
                theta_radians = theta_radians + 2*math.pi

            if theta_radians not in angles and theta_radians > angles_min and theta_radians < angles_max:
                angles = np.append(angles, theta_radians)
        angles = np.sort(angles)

        for angle in angles:
            ray_point = Point.triangle_unit_circle_relative(
                angle, self.pos, point_angle_0)
            ray_points.append(ray_point)

        return ray_points