1 """ May it be a light for you in dark places, when all other lights go out.
7 import pymunk.pygame_util
10 import pygame.locals as pgl
13 from .constants import LIGHT_CATEGORY, FITTINGS_CATEGORY, COLOURS
14 from .rays import RayPolyManager
15 from .utils import DetailedTimer
16 from .loader import loader
17 from .transforms import Multiply
19 LIGHT_FILTER = pymunk.ShapeFilter(
20 mask=pymunk.ShapeFilter.ALL_MASKS ^ (
21 LIGHT_CATEGORY | FITTINGS_CATEGORY),
22 categories=LIGHT_CATEGORY)
24 FITTINGS_FILTER = pymunk.ShapeFilter(
25 mask=pymunk.ShapeFilter.ALL_MASKS ^ (
26 LIGHT_CATEGORY | FITTINGS_CATEGORY),
27 categories=FITTINGS_CATEGORY)
29 # Just match lights, nothing else
30 LIT_BY_FILTER = pymunk.ShapeFilter(mask=LIGHT_CATEGORY)
33 class LightManager(object):
34 """ Manages a set of lights. """
36 def __init__(self, space, gamestate):
39 BaseLight.load(cfg) for cfg in gamestate.station["lights"]]
40 for light in self._lights:
41 light.add(self._space)
43 def add_light(self, cfg):
44 light = BaseLight.load(cfg)
45 self._lights.append(light)
46 light.add(self._space)
48 def toggle_nearest(self, *args, **kw):
49 light = self.nearest(*args, **kw)
53 def nearest(self, pos, surfpos=False, max_distance=1.0):
55 surface = pygame.display.get_surface()
56 pos = pymunk.pygame_util.from_pygame(pos, surface)
57 point_info = self._space.point_query_nearest(
58 pos, max_distance, pymunk.ShapeFilter(mask=FITTINGS_CATEGORY))
59 if point_info is not None:
60 return point_info.shape.body.light
63 def lit_by(self, pos, surfpos=False, max_distance=0.0):
65 surface = pygame.display.get_surface()
66 pos = pymunk.pygame_util.from_pygame(pos, surface)
67 point_info_list = self._space.point_query(
68 pos, max_distance, pymunk.ShapeFilter(mask=LIGHT_CATEGORY))
69 lights = [p.shape.body.light for p in point_info_list]
71 light for light in lights
72 if light.on and light.ray_manager.reaches(pos)
75 def light_query(self, shape):
76 """Query the lights by shape"""
77 old_filter = shape.filter
78 # We need to restrict matches to only the lights
79 shape.filter = LIT_BY_FILTER
80 shape_info_list = self._space.shape_query(shape)
81 shape.filter = old_filter
82 lights = [p.shape.body.light for p in shape_info_list]
84 light for light in lights
85 if light.on and light.ray_manager.reaches(shape.body.position)
88 def total_power_usage(self):
89 return sum(light.power_usage() for light in self._lights)
91 def render_light(self, surface):
92 for light in self._lights:
93 light.render_light(surface)
95 def render_fittings(self, surface):
96 for light in self._lights:
97 light.render_fitting(surface)
100 for light in self._lights:
104 class BaseLight(object):
105 """ Common light functionality. """
108 RAY_MANAGER = RayPolyManager
110 FITTING_RADIUS = 24.0
116 self, colours, position, intensity=1.0, radius_limits=None,
117 direction=None, spread=None):
118 self.colour_cycle = colours
120 self.colour = colours[0]
122 self.intensity = intensity
123 self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
124 self.body.light = self
125 self.ray_manager = self.RAY_MANAGER(
126 self.body, position, ray_filter=LIGHT_FILTER,
127 radius_limits=radius_limits, direction=direction, spread=spread)
128 self.fitting = pymunk.Circle(
129 self.body, self.FITTING_RADIUS, self.ray_manager.position)
130 self.fitting.filter = FITTINGS_FILTER
131 self._fitting_image = None
135 return self.ray_manager.position
138 def load(cls, config):
140 light_type = kw.pop("type")
142 c for c in cls.__subclasses__()
143 if c.__name__.lower() == light_type]
144 return light_class(**kw)
146 def add(self, space):
147 if self.body.space is not None:
148 space.remove(self.body, *self.body.shapes)
149 space.add(self.body, self.fitting)
150 self.ray_manager.set_space(space)
151 self.ray_manager.update_shapes()
153 def _cached_surface(self, name, surface):
154 surf = self._surface_cache.get(name)
156 surf = self._surface_cache[name] = pygame.surface.Surface(
157 surface.get_size(), pgl.SWSURFACE
161 def light_colour(self):
162 light_colour = COLOURS[self.colour]
163 intensity = int(255 * self.intensity)
164 return light_colour + (intensity,)
166 def render_light(self, surface):
170 dt = DetailedTimer("render_light")
173 max_radius = self.ray_manager.max_radius
174 min_radius = self.ray_manager.min_radius
175 dest_rect = self.ray_manager.pygame_rect(surface)
177 white, black = (255, 255, 255, 255), (0, 0, 0, 0)
178 light_colour = self.light_colour()
180 radius_mask = self._cached_surface('radius_mask', surface)
181 radius_mask.set_clip(dest_rect)
182 ray_mask = self._cached_surface('ray_mask', surface)
183 ray_mask.set_clip(dest_rect)
186 for pygame_poly in self.ray_manager.pygame_polys(surface):
187 pygame.draw.polygon(ray_mask, white, pygame_poly, 0)
188 pygame.draw.polygon(ray_mask, white, pygame_poly, 1)
189 dt.lap("ray mask rendered")
191 radius_mask.fill(black)
192 centre = self.ray_manager.pygame_position(surface)
194 radius_mask, light_colour, centre, int(max_radius), 0)
196 radius_mask, black, centre, int(min_radius), 0)
197 dt.lap("radius mask rendered")
199 ray_mask.blit(radius_mask, dest_rect, dest_rect, pgl.BLEND_RGBA_MULT)
200 dt.lap("blitted radius mask to ray mask")
202 surface.blit(ray_mask, dest_rect, dest_rect)
203 dt.lap("blitted surface")
206 def fitting_image(self):
207 if self._fitting_image is None:
208 fitting_colour = COLOURS[self.colour]
209 self._fitting_image = loader.load_image(
210 "48", self.FITTING_IMG,
211 transform=Multiply(colour=fitting_colour))
212 return self._fitting_image
214 def invalidate_fitting_image(self):
215 self._fitting_image = None
217 def render_fitting(self, surface):
218 rx, ry = self.ray_manager.pygame_position(surface)
219 surface.blit(self.fitting_image(), (rx - 24, ry - 24), None, 0)
221 def power_usage(self):
224 area = math.pi * (self.ray_manager.max_radius ** 2) # radius
225 area = area * (self.ray_manager.spread / (2 * math.pi)) # spread
226 return 5 * area * self.intensity
228 def base_damage(self):
229 return 5 * self.intensity
233 if self.colour_pos >= len(self.colour_cycle):
234 self.colour = self.colour_cycle[0]
238 self.colour = self.colour_cycle[self.colour_pos]
240 self.invalidate_fitting_image()
246 class Lamp(BaseLight):
248 FITTING_IMG = "lamp.png"
251 class MultiColourLamp(BaseLight):
253 FITTING_IMG = "lamp.png"
255 def __init__(self, **kw):
256 super(MultiColourLamp, self).__init__(**kw)
259 class PulsatingLamp(BaseLight):
261 FITTING_IMG = "lamp.png"
262 DEFAULT_PULSE_RANGE = (20, 100)
263 DEFAULT_PULSE_VELOCITY = 2
264 DEFAULT_INTENSITY_RANGE = (0.0, 0.9)
265 DEFAULT_INTENSITY_VELOCITY = 0.1
267 def __init__(self, **kw):
268 self.pulse_range = kw.pop("pulse_range", self.DEFAULT_PULSE_RANGE)
269 self.pulse_velocity = kw.pop(
270 "pulse_velocity", self.DEFAULT_PULSE_VELOCITY)
271 self.intensity_range = kw.pop(
272 "intensity_range", self.DEFAULT_INTENSITY_RANGE)
273 self.intensity_velocity = kw.pop(
274 "intensity_velocity", self.DEFAULT_INTENSITY_VELOCITY)
275 super(PulsatingLamp, self).__init__(**kw)
277 def _update_range(self, value, velocity, value_range):
279 if value < value_range[0]:
280 value = value_range[0]
282 elif value > value_range[1]:
283 value = value_range[1]
285 return value, velocity
288 self.ray_manager.max_radius, self.pulse_velocity = self._update_range(
289 self.ray_manager.max_radius, self.pulse_velocity, self.pulse_range)
290 self.intensity, self.intensity_velocity = self._update_range(
291 self.intensity, self.intensity_velocity, self.intensity_range)
294 class SpotLight(BaseLight):
296 FITTING_IMG = "spotlight.png"
298 def __init__(self, **kw):
299 self.angular_velocity = kw.pop("angular_velocity", None)
300 super(SpotLight, self).__init__(**kw)
303 if self.angular_velocity:
304 self.ray_manager.direction -= self.angular_velocity
305 self.ray_manager.update_shapes()