import pymunk
import pymunk.pygame_util
+import pygame.display
import pygame.draw
+import pygame.locals as pgl
+import pygame.rect
+
+from .constants import LIGHT_CATEGORY, FITTINGS_CATEGORY
+from .rays import RayPolyManager
+from .utils import DetailedTimer
+from .loader import loader
+from .transforms import Multiply
+
+LIGHT_FILTER = pymunk.ShapeFilter(
+ 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)
+
+# Just match lights, nothing else
+LIT_BY_FILTER = pymunk.ShapeFilter(mask=LIGHT_CATEGORY)
+
+
+class LightManager(object):
+ """ Manages a set of lights. """
+
+ def __init__(self, space, gamestate):
+ self._space = space
+ self._lights = [
+ BaseLight.load(cfg) for cfg in gamestate.station["lights"]]
+ for light in self._lights:
+ light.add(self._space)
+
+ def add_light(self, cfg):
+ light = BaseLight.load(cfg)
+ self._lights.append(light)
+ light.add(self._space)
+
+ def toggle_nearest(self, *args, **kw):
+ light = self.nearest(*args, **kw)
+ if light:
+ light.toggle()
+
+ def nearest(self, pos, surfpos=False, max_distance=1.0):
+ if surfpos:
+ surface = pygame.display.get_surface()
+ pos = pymunk.pygame_util.from_pygame(pos, surface)
+ point_info = self._space.point_query_nearest(
+ pos, max_distance, pymunk.ShapeFilter(mask=FITTINGS_CATEGORY))
+ if point_info is not None:
+ return point_info.shape.body.light
+ return None
+
+ def lit_by(self, pos, surfpos=False, max_distance=0.0):
+ if surfpos:
+ surface = pygame.display.get_surface()
+ pos = pymunk.pygame_util.from_pygame(pos, surface)
+ point_info_list = self._space.point_query(
+ pos, max_distance, pymunk.ShapeFilter(mask=LIGHT_CATEGORY))
+ lights = [p.shape.body.light for p in point_info_list]
+ return [light for light in lights if light.on]
+
+ def light_query(self, shape):
+ """Query the lights by shape"""
+ old_filter = shape.filter
+ # We need to restrict matches to only the lights
+ shape.filter = LIT_BY_FILTER
+ shape_info_list = self._space.shape_query(shape)
+ shape.filter = old_filter
+ lights = [p.shape.body.light for p in shape_info_list]
+ return [light for light in lights if light.on]
+
+ def render_light(self, surface):
+ for light in self._lights:
+ light.render_light(surface)
+
+ def render_fittings(self, surface):
+ for light in self._lights:
+ light.render_fitting(surface)
+
+ def tick(self):
+ for light in self._lights:
+ light.tick()
class BaseLight(object):
""" Common light functionality. """
- def __init__(self, colour, position):
- self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
- self.colour = colour
- self.position = position
+ COLOURS = {
+ "red": (255, 0, 0),
+ "green": (0, 255, 0),
+ "blue": (0, 0, 255),
+ "cyan": (0, 255, 255),
+ "yellow": (255, 255, 0),
+ "white": (255, 255, 255),
+ }
- def add(self, space):
- if self.body.space is not None:
- space.remove(self.body, *self.body.shapes)
- shapes = self.determine_ray_polys(space)
- space.add(self.body, *shapes)
+ # defaults
+ RAY_MANAGER = RayPolyManager
+ FITTING_IMG = None
+ FITTING_RADIUS = 24.0
+
+ # cached surfaces
+ _surface_cache = {}
+
+ def __init__(
+ self, colour, position, intensity=1.0, radius_limits=None,
+ angle_limits=None):
+ self.colour = colour
+ self.on = True
+ self.intensity = intensity
+ self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
+ self.body.light = self
+ self.ray_manager = self.RAY_MANAGER(
+ self.body, position, ray_filter=LIGHT_FILTER,
+ radius_limits=radius_limits, angle_limits=angle_limits)
+ self.fitting = pymunk.Circle(
+ self.body, self.FITTING_RADIUS, self.ray_manager.position)
+ self.fitting.filter = FITTINGS_FILTER
+ self._image = None
- def determine_ray_polys(self, space):
- raise NotImplementedError(
- "Lights should implement .determine_ray_polys.")
+ @property
+ def position(self):
+ return self.ray_manager.position
@classmethod
def load(cls, config):
if c.__name__.lower() == light_type]
return light_class(**kw)
+ def add(self, space):
+ if self.body.space is not None:
+ space.remove(self.body, *self.body.shapes)
+ space.add(self.body, self.fitting)
+ self.ray_manager.set_space(space)
+ self.ray_manager.update_shapes()
-class SpotLight(BaseLight):
- def __init__(
- self, colour="white", position=None, direction=90.0, spread=45.0):
- super(SpotLight, self).__init__(colour, position)
- self.direction = direction
- self.spread = spread
+ def toggle(self):
+ self.on = not self.on
+
+ def _cached_surface(self, name, surface):
+ surf = self._surface_cache.get(name)
+ if surf is None:
+ surf = self._surface_cache[name] = pygame.surface.Surface(
+ surface.get_size(), pgl.SWSURFACE
+ ).convert_alpha()
+ return surf
+
+ def light_colour(self):
+ light_colour = self.COLOURS[self.colour]
+ intensity = int(255 * self.intensity)
+ return light_colour + (intensity,)
+
+ def render_light(self, surface):
+ if not self.on:
+ return
+
+ dt = DetailedTimer("render_light")
+ dt.start()
+
+ max_radius = self.ray_manager.max_radius
+ min_radius = self.ray_manager.min_radius
+ dest_rect = self.ray_manager.pygame_rect(surface)
+
+ white, black = (255, 255, 255, 255), (0, 0, 0, 0)
+ light_colour = self.light_colour()
- def determine_ray_polys(self, space):
- x, y = self.position
- return [pymunk.Poly(self.body, [
- self.position, [x + 50, y], [x, y + 50]])]
+ radius_mask = self._cached_surface('radius_mask', surface)
+ radius_mask.set_clip(dest_rect)
+ ray_mask = self._cached_surface('ray_mask', surface)
+ ray_mask.set_clip(dest_rect)
- def render(self, surface):
- for shape in self.body.shapes:
- pygame_poly = [
- pymunk.pygame_util.to_pygame(v, surface) for v in
- shape.get_vertices()]
- pygame.draw.polygon(surface, (255, 255, 255), pygame_poly)
+ ray_mask.fill(black)
+ for pygame_poly in self.ray_manager.pygame_polys(surface):
+ pygame.draw.polygon(ray_mask, white, pygame_poly, 0)
+ pygame.draw.polygon(ray_mask, white, pygame_poly, 1)
+ dt.lap("ray mask rendered")
+
+ radius_mask.fill(black)
+ centre = self.ray_manager.pygame_position(surface)
+ pygame.draw.circle(
+ radius_mask, light_colour, centre, int(max_radius), 0)
+ pygame.draw.circle(
+ radius_mask, black, centre, int(min_radius), 0)
+ dt.lap("radius mask rendered")
+
+ ray_mask.blit(radius_mask, dest_rect, dest_rect, pgl.BLEND_RGBA_MULT)
+ dt.lap("blitted radius mask to ray mask")
+
+ surface.blit(ray_mask, dest_rect, dest_rect)
+ dt.lap("blitted surface")
+ dt.end()
+
+ def get_image(self):
+ if self._image is None:
+ fitting_colour = self.COLOURS[self.colour]
+ self._image = loader.load_image(
+ "48", self.FITTING_IMG,
+ transform=Multiply(colour=fitting_colour))
+ return self._image
+
+ def render_fitting(self, surface):
+ rx, ry = self.ray_manager.pygame_position(surface)
+ surface.blit(self.get_image(), (rx - 24, ry - 24), None, 0)
+
+ def tick(self):
+ pass
class Lamp(BaseLight):
- def __init__(self, colour="white", position=None, radius=100.0):
- super(Lamp, self).__init__(colour, position)
- self.radius = radius
+ FITTING_IMG = "lamp.png"
+
+ def __init__(self, **kw):
+ kw.pop("direction", None)
+ kw.pop("spread", None)
+ super(Lamp, self).__init__(**kw)
+
+
+class SpotLight(BaseLight):
+ FITTING_IMG = "spotlight.png"
+
+ def __init__(self, **kw):
+ kw.pop("direction", None)
+ kw.pop("spread", None)
+ self.angular_velocity = kw.pop("angular_velocity", None)
+ super(SpotLight, self).__init__(**kw)
+
+ def tick(self):
+ if self.angular_velocity:
+ self.ray_manager.rotate_degrees(self.angular_velocity)
+ self.ray_manager.update_shapes()