1 """ May it be a light for you in dark places, when all other lights go out.
5 import pymunk.pygame_util
8 import pygame.locals as pgl
10 from .constants import LIGHT_CATEGORY, FITTINGS_CATEGORY
11 from .rays import calculate_ray_polys
13 LIGHT_FILTER = pymunk.ShapeFilter(
14 mask=pymunk.ShapeFilter.ALL_MASKS ^ (
15 LIGHT_CATEGORY | FITTINGS_CATEGORY),
16 categories=LIGHT_CATEGORY)
18 FITTINGS_FILTER = pymunk.ShapeFilter(
19 mask=pymunk.ShapeFilter.ALL_MASKS ^ (
20 LIGHT_CATEGORY | FITTINGS_CATEGORY),
21 categories=FITTINGS_CATEGORY)
23 # Just match lights, nothing else
24 LIT_BY_FILTER = pymunk.ShapeFilter(mask=LIGHT_CATEGORY)
27 class LightManager(object):
28 """ Manages a set of lights. """
30 def __init__(self, space, gamestate):
33 BaseLight.load(cfg) for cfg in gamestate.station["lights"]]
34 for light in self._lights:
35 light.add(self._space)
37 def toggle_nearest(self, *args, **kw):
38 light = self.nearest(*args, **kw)
42 def nearest(self, pos, surfpos=False, max_distance=1.0):
44 surface = pygame.display.get_surface()
45 pos = pymunk.pygame_util.from_pygame(pos, surface)
46 point_info = self._space.point_query_nearest(
47 pos, max_distance, pymunk.ShapeFilter(mask=FITTINGS_CATEGORY))
48 if point_info is not None:
49 return point_info.shape.body.light
52 def lit_by(self, pos, surfpos=False, max_distance=0.0):
54 surface = pygame.display.get_surface()
55 pos = pymunk.pygame_util.from_pygame(pos, surface)
56 point_info_list = self._space.point_query(
57 pos, max_distance, pymunk.ShapeFilter(mask=LIGHT_CATEGORY))
58 lights = [p.shape.body.light for p in point_info_list]
59 return [light for light in lights if light.on]
61 def light_query(self, shape):
62 """Query the lights by shape"""
63 old_filter = shape.filter
64 # We need to restrict matches to only the lights
65 shape.filter = LIT_BY_FILTER
66 shape_info_list = self._space.shape_query(shape)
67 shape.filter = old_filter
68 lights = [p.shape.body.light for p in shape_info_list]
69 return [light for light in lights if light.on]
71 def render_light(self, surface):
72 for light in self._lights:
73 light.render_light(surface)
75 def render_fittings(self, surface):
76 for light in self._lights:
77 light.render_fitting(surface)
80 class BaseLight(object):
81 """ Common light functionality. """
86 "blue": (0, 255, 255),
87 "yellow": (255, 255, 0),
88 "white": (255, 255, 255),
95 self, colour, position, intensity=1.0,
96 radius_limits=(None, None), angle_limits=(None, None)):
98 self.position = pymunk.Vec2d(position)
100 self.intensity = intensity
101 self.radius_limits = radius_limits
102 self.angle_limits = angle_limits
103 self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
104 self.fitting = pymunk.Circle(self.body, 10.0, self.position)
105 self.body.light = self
108 def load(cls, config):
110 light_type = kw.pop("type")
112 c for c in cls.__subclasses__()
113 if c.__name__.lower() == light_type]
114 return light_class(**kw)
116 def add(self, space):
117 if self.body.space is not None:
118 space.remove(self.body, *self.body.shapes)
119 shapes = self.shapes_for_ray_polys(
120 calculate_ray_polys(space, self.body, self.position, LIGHT_FILTER))
122 shape.filter = LIGHT_FILTER
123 self.fitting.filter = FITTINGS_FILTER
124 space.add(self.body, self.fitting, *shapes)
126 def shapes_for_ray_polys(self, ray_polys):
130 self.on = not self.on
132 def _cached_surfaces(self, surface):
133 radius_mask = self._surface_cache.get('radius_mask')
134 if radius_mask is None:
135 radius_mask = self._surface_cache['radius_mask'] = (
136 pygame.surface.Surface(surface.get_size(), pgl.SWSURFACE))
138 ray_mask = self._surface_cache.get('ray_mask')
140 ray_mask = self._surface_cache['ray_mask'] = (
141 pygame.surface.Surface(surface.get_size(), pgl.SWSURFACE))
143 overlay_surf = self._surface_cache.get('overlay_surf')
144 if overlay_surf is None:
145 overlay_surf = self._surface_cache['overlay_surf'] = (
146 pygame.surface.Surface(
147 surface.get_size(), pgl.SWSURFACE | pgl.SRCALPHA))
149 return radius_mask, ray_mask, overlay_surf
151 def render_light(self, surface):
155 radius_mask, ray_mask, overlay_surf = self._cached_surfaces(surface)
156 white, black = (255, 255, 255), (0, 0, 0)
159 for shape in self.body.shapes:
160 if shape is self.fitting:
163 pymunk.pygame_util.to_pygame(v, surface) for v in
164 shape.get_vertices()]
165 pygame.draw.polygon(ray_mask, white, pygame_poly, 0)
166 pygame.draw.aalines(ray_mask, white, True, pygame_poly, 1)
168 radius_mask.fill(black)
169 centre = pymunk.pygame_util.to_pygame(self.position, surface)
170 max_radius = self.radius_limits[1] or 50.0
171 min_radius = self.radius_limits[0] or 0
172 width = max_radius - min_radius
174 radius_mask, white, centre, int(max_radius), int(width))
176 radius_mask, white, (centre[0] + 1, centre[1]),
177 int(max_radius), int(width))
179 ray_mask.blit(radius_mask, (0, 0), None, pgl.BLEND_RGB_MULT)
180 ray_mask.set_colorkey(black)
182 light_colour = self.COLOURS[self.colour]
183 intensity = int(255 * self.intensity)
184 light_colour = light_colour + (intensity,)
186 overlay_surf.blit(ray_mask, (0, 0), None)
187 overlay_surf.fill(light_colour, None, pgl.BLEND_RGBA_MULT)
189 surface.blit(overlay_surf, (0, 0), None)
191 def render_fitting(self, surface):
193 surface, (255, 255, 0),
194 pymunk.pygame_util.to_pygame(self.fitting.offset, surface),
195 int(self.fitting.radius))
198 class SpotLight(BaseLight):
199 def __init__(self, **kw):
200 kw.pop("direction", None)
201 kw.pop("spread", None)
202 super(SpotLight, self).__init__(**kw)