X-Git-Url: https://git.ctpug.org.za/?p=tabakrolletjie.git;a=blobdiff_plain;f=tabakrolletjie%2Frays.py;h=a475d8fb27bb6c680a90555cad7dc2a4f6043954;hp=6ff8a6ea526033de129d82aafe735a35ef71a213;hb=3341713feb17c2469631de0ef6078e5a4473b28c;hpb=314608904d58bd77b0c76c24c0a5135c8117a479 diff --git a/tabakrolletjie/rays.py b/tabakrolletjie/rays.py index 6ff8a6e..a475d8f 100644 --- a/tabakrolletjie/rays.py +++ b/tabakrolletjie/rays.py @@ -12,7 +12,7 @@ from .constants import SCREEN_SIZE from .utils import debug_timer -def screen_rays(pos): +def screen_rays(): """ An iterable that returns ordered rays from pos to the edge of the screen, starting with the edge point (0, 0) and continuing clockwise in pymunk coordinates. @@ -40,7 +40,7 @@ def calculate_ray_polys(space, position, light_filter): vertices = [position] start, end = None, None ray_polys = [] - for ray in screen_rays(position): + for ray in screen_rays(): info = space.segment_query_first(position, ray, 1, light_filter) point = ray if info is None else info.point vertices.append(point) @@ -78,14 +78,18 @@ def to_pymunk_radians(deg): class RayPolyManager(object): def __init__( - self, body, position, ray_filter, radius_limits, angle_limits): + self, body, position, ray_filter, radius_limits, direction, + spread): self._body = body # light's body self._position = pymunk.Vec2d(position) # light's position self._ray_filter = ray_filter # light filter self._rays = [] # list of RayPolys + self._direction = None # normal vector for direction self._start = None # normal vector in direction of start angle limit self._end = None # normal vector in direction of end angle limit - self._set_angle_limits(angle_limits) + self._set_angle_limits(direction, spread) + if direction: + self.direction = direction # Update direction self._max_radius = None # maximum radius in pixels self._min_radius = None # minimum radius in pixels self._set_radius_limits(radius_limits) @@ -114,7 +118,7 @@ class RayPolyManager(object): return self._max_radius @max_radius.setter - def max_radius_setter(self, value): + def max_radius(self, value): self._max_radius = value or 0.0 @property @@ -122,11 +126,11 @@ class RayPolyManager(object): return self._min_radius @min_radius.setter - def min_radius_setter(self, value): + def min_radius(self, value): self._min_radius = value or 0.0 def reaches(self, position): - distance = self.position.get_distance(self.position) + distance = self.position.get_distance(position) return (self._min_radius <= distance <= self._max_radius) def _set_radius_limits(self, radius_limits): @@ -139,20 +143,38 @@ class RayPolyManager(object): else: self._max_radius = radius_limits[1] - def rotate_degrees(self, degrees): - self._start.rotate_degrees(degrees) - self._end.rotate_degrees(degrees) + def rotatable(self): + return self._direction is not None + + @property + def direction(self): + if self._direction is None: + return 0 + return self._direction.angle_degrees + + @direction.setter + def direction(self, degrees): + spread = self._direction.get_angle_between(self._start) + self._direction.angle_degrees = degrees + self._start = self._direction.rotated(spread) + self._end = self._direction.rotated(-spread) self._poly_cache = None - def _set_angle_limits(self, angle_limits): - if angle_limits is None: + @property + def spread(self): + if not self._direction: + return 2 * math.pi + return math.fabs(self._start.get_angle_between(self._end)) + + def _set_angle_limits(self, direction, spread): + if direction is None or spread is None: + self._direction = None self._start = None self._end = None else: - self._start = pymunk.Vec2d(1, 0).rotated( - to_pymunk_radians(angle_limits[0])) - self._end = pymunk.Vec2d(1, 0).rotated( - to_pymunk_radians(angle_limits[1])) + self._direction = pymunk.Vec2d(1, 0) + self._start = self._direction.rotated_degrees(-spread/2.) + self._end = self._direction.rotated_degrees(spread/2.) self._poly_cache = None def polys(self):