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
11 from .constants import LIGHT_CATEGORY, FITTINGS_CATEGORY
12 from .rays import RayPolyManager
13 from .utils import DetailedTimer
14 from .loader import loader
15 from .transforms import Multiply
17 LIGHT_FILTER = pymunk.ShapeFilter(
18 mask=pymunk.ShapeFilter.ALL_MASKS ^ (
19 LIGHT_CATEGORY | FITTINGS_CATEGORY),
20 categories=LIGHT_CATEGORY)
22 FITTINGS_FILTER = pymunk.ShapeFilter(
23 mask=pymunk.ShapeFilter.ALL_MASKS ^ (
24 LIGHT_CATEGORY | FITTINGS_CATEGORY),
25 categories=FITTINGS_CATEGORY)
27 # Just match lights, nothing else
28 LIT_BY_FILTER = pymunk.ShapeFilter(mask=LIGHT_CATEGORY)
31 class LightManager(object):
32 """ Manages a set of lights. """
34 def __init__(self, space, gamestate):
37 BaseLight.load(cfg) for cfg in gamestate.station["lights"]]
38 for light in self._lights:
39 light.add(self._space)
41 def add_light(self, cfg):
42 light = BaseLight.load(cfg)
43 self._lights.append(light)
44 light.add(self._space)
46 def toggle_nearest(self, *args, **kw):
47 light = self.nearest(*args, **kw)
51 def nearest(self, pos, surfpos=False, max_distance=1.0):
53 surface = pygame.display.get_surface()
54 pos = pymunk.pygame_util.from_pygame(pos, surface)
55 point_info = self._space.point_query_nearest(
56 pos, max_distance, pymunk.ShapeFilter(mask=FITTINGS_CATEGORY))
57 if point_info is not None:
58 return point_info.shape.body.light
61 def lit_by(self, pos, surfpos=False, max_distance=0.0):
63 surface = pygame.display.get_surface()
64 pos = pymunk.pygame_util.from_pygame(pos, surface)
65 point_info_list = self._space.point_query(
66 pos, max_distance, pymunk.ShapeFilter(mask=LIGHT_CATEGORY))
67 lights = [p.shape.body.light for p in point_info_list]
68 return [light for light in lights if light.on]
70 def light_query(self, shape):
71 """Query the lights by shape"""
72 old_filter = shape.filter
73 # We need to restrict matches to only the lights
74 shape.filter = LIT_BY_FILTER
75 shape_info_list = self._space.shape_query(shape)
76 shape.filter = old_filter
77 lights = [p.shape.body.light for p in shape_info_list]
78 return [light for light in lights if light.on]
80 def render_light(self, surface):
81 for light in self._lights:
82 light.render_light(surface)
84 def render_fittings(self, surface):
85 for light in self._lights:
86 light.render_fitting(surface)
89 for light in self._lights:
93 class BaseLight(object):
94 """ Common light functionality. """
99 "blue": (0, 255, 255),
100 "yellow": (255, 255, 0),
101 "white": (255, 255, 255),
105 RAY_MANAGER = RayPolyManager
107 FITTING_RADIUS = 24.0
113 self, colour, position, intensity=1.0, radius_limits=None,
117 self.intensity = intensity
118 self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
119 self.body.light = self
120 self.ray_manager = self.RAY_MANAGER(
121 self.body, position, ray_filter=LIGHT_FILTER,
122 radius_limits=radius_limits, angle_limits=angle_limits)
123 self.fitting = pymunk.Circle(
124 self.body, self.FITTING_RADIUS, self.ray_manager.position)
125 self.fitting.filter = FITTINGS_FILTER
130 return self.ray_manager.position
133 def load(cls, config):
135 light_type = kw.pop("type")
137 c for c in cls.__subclasses__()
138 if c.__name__.lower() == light_type]
139 return light_class(**kw)
141 def add(self, space):
142 if self.body.space is not None:
143 space.remove(self.body, *self.body.shapes)
144 space.add(self.body, self.fitting)
145 self.ray_manager.set_space(space)
146 self.ray_manager.update_shapes()
149 self.on = not self.on
151 def _cached_surface(self, name, surface):
152 surf = self._surface_cache.get(name)
154 surf = self._surface_cache[name] = pygame.surface.Surface(
155 surface.get_size(), pgl.SWSURFACE
159 def light_colour(self):
160 light_colour = self.COLOURS[self.colour]
161 intensity = int(255 * self.intensity)
162 return light_colour + (intensity,)
164 def render_light(self, surface):
168 dt = DetailedTimer("render_light")
171 max_radius = self.ray_manager.max_radius
172 min_radius = self.ray_manager.min_radius
173 dest_rect = self.ray_manager.pygame_rect(surface)
175 white, black = (255, 255, 255, 255), (0, 0, 0, 0)
176 light_colour = self.light_colour()
178 radius_mask = self._cached_surface('radius_mask', surface)
179 radius_mask.set_clip(dest_rect)
180 ray_mask = self._cached_surface('ray_mask', surface)
181 ray_mask.set_clip(dest_rect)
184 for pygame_poly in self.ray_manager.pygame_polys(surface):
185 pygame.draw.polygon(ray_mask, white, pygame_poly, 0)
186 pygame.draw.polygon(ray_mask, white, pygame_poly, 1)
187 dt.lap("ray mask rendered")
189 radius_mask.fill(black)
190 centre = self.ray_manager.pygame_position(surface)
192 radius_mask, light_colour, centre, int(max_radius), 0)
194 radius_mask, black, centre, int(min_radius), 0)
195 dt.lap("radius mask rendered")
197 ray_mask.blit(radius_mask, dest_rect, dest_rect, pgl.BLEND_RGBA_MULT)
198 dt.lap("blitted radius mask to ray mask")
200 surface.blit(ray_mask, dest_rect, dest_rect)
201 dt.lap("blitted surface")
205 if self._image is None:
206 fitting_colour = self.COLOURS[self.colour]
207 self._image = loader.load_image(
208 "48", self.FITTING_IMG,
209 transform=Multiply(colour=fitting_colour))
212 def render_fitting(self, surface):
213 rx, ry = self.ray_manager.pygame_position(surface)
214 surface.blit(self.get_image(), (rx - 24, ry - 24), None, 0)
220 class Lamp(BaseLight):
221 FITTING_IMG = "lamp.png"
223 def __init__(self, **kw):
224 kw.pop("direction", None)
225 kw.pop("spread", None)
226 super(Lamp, self).__init__(**kw)
229 class SpotLight(BaseLight):
230 FITTING_IMG = "spotlight.png"
232 def __init__(self, **kw):
233 kw.pop("direction", None)
234 kw.pop("spread", None)
235 self.angular_velocity = kw.pop("angular_velocity", None)
236 super(SpotLight, self).__init__(**kw)
239 if self.angular_velocity:
240 self.ray_manager.rotate_degrees(self.angular_velocity)
241 self.ray_manager.update_shapes()