X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=tabakrolletjie%2Flights.py;h=c5c72af0df43cb289f4528ea94fabb65c41b52ee;hb=0ec83ab8cb7e31363fb994a282911156579dc514;hp=6f2c6e7a1082a5eba81d57e636fd9f1c42f34ff9;hpb=5e5685068049bfe0c06508bd4c71437b3c1675fd;p=tabakrolletjie.git diff --git a/tabakrolletjie/lights.py b/tabakrolletjie/lights.py index 6f2c6e7..c5c72af 100644 --- a/tabakrolletjie/lights.py +++ b/tabakrolletjie/lights.py @@ -1,18 +1,24 @@ """ May it be a light for you in dark places, when all other lights go out. """ -import time - import pymunk import pymunk.pygame_util import pygame.draw -from .constants import SCREEN_SIZE, LIGHT_CATEGORY, DEBUG +from .constants import ( + SCREEN_SIZE, LIGHT_CATEGORY, FITTINGS_CATEGORY) +from .utils import debug_timer LIGHT_FILTER = pymunk.ShapeFilter( - mask=pymunk.ShapeFilter.ALL_MASKS ^ LIGHT_CATEGORY, + mask=pymunk.ShapeFilter.ALL_MASKS ^ ( + LIGHT_CATEGORY | FITTINGS_CATEGORY), categories=LIGHT_CATEGORY) +FITTINGS_FILTER = pymunk.ShapeFilter( + mask=pymunk.ShapeFilter.ALL_MASKS ^ ( + LIGHT_CATEGORY | FITTINGS_CATEGORY), + categories=FITTINGS_CATEGORY) + def screen_rays(pos): """ An iterable that returns ordered rays from pos to the edge of the @@ -32,8 +38,8 @@ def screen_rays(pos): yield pymunk.Vec2d(x, bottom) +@debug_timer("lights.calculate_ray_polys") def calculate_ray_polys(space, body, position): - start_time = time.time() position = pymunk.Vec2d(position) vertices = [position] ray_polys = [] @@ -54,11 +60,6 @@ def calculate_ray_polys(space, body, position): vertices = trial_poly.get_vertices() + [point] if len(vertices) > 2: ray_polys.append(pymunk.Poly(body, vertices)) - end_time = time.time() - if DEBUG: - print( - "calculate_ray_polys: %d polys, %g seconds" % - (len(ray_polys), end_time - start_time)) return ray_polys @@ -76,8 +77,9 @@ class BaseLight(object): def __init__(self, colour, position): self.on = True self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC) + self.fitting = pymunk.Circle(self.body, 5.0) self.colour = colour - self.position = position + self.position = pymunk.Vec2d(position) @classmethod def load(cls, config): @@ -95,7 +97,8 @@ class BaseLight(object): calculate_ray_polys(space, self.body, self.position)) for shape in shapes: shape.filter = LIGHT_FILTER - space.add(self.body, *shapes) + self.fitting.filter = FITTINGS_FILTER + space.add(self.body, self.fitting, *shapes) def shapes_for_ray_polys(self, ray_polys): return ray_polys @@ -109,6 +112,8 @@ class BaseLight(object): subsurface = surface.copy() light_colour = self.COLOURS[self.colour] for shape in self.body.shapes: + if shape is self.fitting: + continue pygame_poly = [ pymunk.pygame_util.to_pygame(v, surface) for v in shape.get_vertices()] @@ -120,9 +125,11 @@ class BaseLight(object): surface.blit(subsurface, (0, 0), None) def render_fittings(self, surface): + centre = self.position + self.fitting.offset pygame.draw.circle( surface, (255, 255, 0), - pymunk.pygame_util.to_pygame(self.position, surface), 5) + pymunk.pygame_util.to_pygame(centre, surface), + int(self.fitting.radius)) class SpotLight(BaseLight):