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, COLOURS
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]
69 light for light in lights
70 if light.on and light.ray_manager.reaches(pos)
73 def light_query(self, shape):
74 """Query the lights by shape"""
75 old_filter = shape.filter
76 # We need to restrict matches to only the lights
77 shape.filter = LIT_BY_FILTER
78 shape_info_list = self._space.shape_query(shape)
79 shape.filter = old_filter
80 lights = [p.shape.body.light for p in shape_info_list]
82 light for light in lights
83 if light.on and light.ray_manager.reaches(shape.body.position)
86 def render_light(self, surface):
87 for light in self._lights:
88 light.render_light(surface)
90 def render_fittings(self, surface):
91 for light in self._lights:
92 light.render_fitting(surface)
95 for light in self._lights:
99 class BaseLight(object):
100 """ Common light functionality. """
103 RAY_MANAGER = RayPolyManager
105 FITTING_RADIUS = 24.0
111 self, colour, position, intensity=1.0, radius_limits=None,
112 direction=None, spread=None):
115 self.intensity = intensity
116 self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
117 self.body.light = self
118 self.ray_manager = self.RAY_MANAGER(
119 self.body, position, ray_filter=LIGHT_FILTER,
120 radius_limits=radius_limits, direction=direction, spread=spread)
121 self.fitting = pymunk.Circle(
122 self.body, self.FITTING_RADIUS, self.ray_manager.position)
123 self.fitting.filter = FITTINGS_FILTER
124 self._fitting_image = None
128 return self.ray_manager.position
131 def load(cls, config):
133 light_type = kw.pop("type")
135 c for c in cls.__subclasses__()
136 if c.__name__.lower() == light_type]
137 return light_class(**kw)
139 def add(self, space):
140 if self.body.space is not None:
141 space.remove(self.body, *self.body.shapes)
142 space.add(self.body, self.fitting)
143 self.ray_manager.set_space(space)
144 self.ray_manager.update_shapes()
147 self.on = not self.on
149 def _cached_surface(self, name, surface):
150 surf = self._surface_cache.get(name)
152 surf = self._surface_cache[name] = pygame.surface.Surface(
153 surface.get_size(), pgl.SWSURFACE
157 def light_colour(self):
158 light_colour = COLOURS[self.colour]
159 intensity = int(255 * self.intensity)
160 return light_colour + (intensity,)
162 def render_light(self, surface):
166 dt = DetailedTimer("render_light")
169 max_radius = self.ray_manager.max_radius
170 min_radius = self.ray_manager.min_radius
171 dest_rect = self.ray_manager.pygame_rect(surface)
173 white, black = (255, 255, 255, 255), (0, 0, 0, 0)
174 light_colour = self.light_colour()
176 radius_mask = self._cached_surface('radius_mask', surface)
177 radius_mask.set_clip(dest_rect)
178 ray_mask = self._cached_surface('ray_mask', surface)
179 ray_mask.set_clip(dest_rect)
182 for pygame_poly in self.ray_manager.pygame_polys(surface):
183 pygame.draw.polygon(ray_mask, white, pygame_poly, 0)
184 pygame.draw.polygon(ray_mask, white, pygame_poly, 1)
185 dt.lap("ray mask rendered")
187 radius_mask.fill(black)
188 centre = self.ray_manager.pygame_position(surface)
190 radius_mask, light_colour, centre, int(max_radius), 0)
192 radius_mask, black, centre, int(min_radius), 0)
193 dt.lap("radius mask rendered")
195 ray_mask.blit(radius_mask, dest_rect, dest_rect, pgl.BLEND_RGBA_MULT)
196 dt.lap("blitted radius mask to ray mask")
198 surface.blit(ray_mask, dest_rect, dest_rect)
199 dt.lap("blitted surface")
202 def fitting_image(self):
203 if self._fitting_image is None:
204 fitting_colour = COLOURS[self.colour]
205 self._fitting_image = loader.load_image(
206 "48", self.FITTING_IMG,
207 transform=Multiply(colour=fitting_colour))
208 return self._fitting_image
210 def invalidate_fitting_image(self):
211 self._fitting_image = None
213 def render_fitting(self, surface):
214 rx, ry = self.ray_manager.pygame_position(surface)
215 surface.blit(self.fitting_image(), (rx - 24, ry - 24), None, 0)
221 class Lamp(BaseLight):
223 FITTING_IMG = "lamp.png"
226 class MultiColourLamp(BaseLight):
228 FITTING_IMG = "lamp.png"
229 DEFAULT_COLOURS = sorted(COLOURS.keys())
231 def __init__(self, **kw):
232 self.colour_cycle = kw.pop("colours", None)
234 kw["colour"] = self.colour_cycle[0]
235 super(MultiColourLamp, self).__init__(**kw)
239 if self.colour_pos >= len(self.colour_cycle):
240 self.colour = self.colour_cycle[0]
244 self.colour = self.colour_cycle[self.colour_pos]
246 self.invalidate_fitting_image()
249 class PulsatingLamp(BaseLight):
251 FITTING_IMG = "lamp.png"
252 DEFAULT_PULSE_RANGE = (20, 100)
253 DEFAULT_PULSE_VELOCITY = 2
254 DEFAULT_INTENSITY_RANGE = (0.0, 0.9)
255 DEFAULT_INTENSITY_VELOCITY = 0.1
257 def __init__(self, **kw):
258 self.pulse_range = kw.pop("pulse_range", self.DEFAULT_PULSE_RANGE)
259 self.pulse_velocity = kw.pop(
260 "pulse_velocity", self.DEFAULT_PULSE_VELOCITY)
261 self.intensity_range = kw.pop(
262 "intensity_range", self.DEFAULT_INTENSITY_RANGE)
263 self.intensity_velocity = kw.pop(
264 "intensity_velocity", self.DEFAULT_INTENSITY_VELOCITY)
265 super(PulsatingLamp, self).__init__(**kw)
267 def _update_range(self, value, velocity, value_range):
269 if value < value_range[0]:
270 value = value_range[0]
272 elif value > value_range[1]:
273 value = value_range[1]
275 return value, velocity
278 self.ray_manager.max_radius, self.pulse_velocity = self._update_range(
279 self.ray_manager.max_radius, self.pulse_velocity, self.pulse_range)
280 self.intensity, self.intensity_velocity = self._update_range(
281 self.intensity, self.intensity_velocity, self.intensity_range)
284 class SpotLight(BaseLight):
286 FITTING_IMG = "spotlight.png"
288 def __init__(self, **kw):
289 self.angular_velocity = kw.pop("angular_velocity", None)
290 super(SpotLight, self).__init__(**kw)
293 if self.angular_velocity:
294 self.ray_manager.rotate_degrees(self.angular_velocity)
295 self.ray_manager.update_shapes()