import pygame.locals as pgl
import pygame.rect
-from .constants import LIGHT_CATEGORY, FITTINGS_CATEGORY
+from .constants import LIGHT_CATEGORY, FITTINGS_CATEGORY, COLOURS
from .rays import RayPolyManager
from .utils import DetailedTimer
from .loader import loader
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]
+ return [
+ light for light in lights
+ if light.on and light.ray_manager.reaches(pos)
+ ]
def light_query(self, shape):
"""Query the lights by shape"""
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]
+ return [
+ light for light in lights
+ if light.on and light.ray_manager.reaches(shape.body.position)
+ ]
def render_light(self, surface):
for light in self._lights:
class BaseLight(object):
""" Common light functionality. """
- COLOURS = {
- "red": (255, 0, 0),
- "green": (0, 255, 0),
- "blue": (0, 255, 255),
- "yellow": (255, 255, 0),
- "white": (255, 255, 255),
- }
-
# defaults
RAY_MANAGER = RayPolyManager
FITTING_IMG = None
self.fitting = pymunk.Circle(
self.body, self.FITTING_RADIUS, self.ray_manager.position)
self.fitting.filter = FITTINGS_FILTER
- self._image = None
+ self._fitting_image = None
@property
def position(self):
return surf
def light_colour(self):
- light_colour = self.COLOURS[self.colour]
+ light_colour = COLOURS[self.colour]
intensity = int(255 * self.intensity)
return light_colour + (intensity,)
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(
+ def fitting_image(self):
+ if self._fitting_image is None:
+ fitting_colour = COLOURS[self.colour]
+ self._fitting_image = loader.load_image(
"48", self.FITTING_IMG,
transform=Multiply(colour=fitting_colour))
- return self._image
+ return self._fitting_image
+
+ def invalidate_fitting_image(self):
+ self._fitting_image = None
def render_fitting(self, surface):
rx, ry = self.ray_manager.pygame_position(surface)
- surface.blit(self.get_image(), (rx - 24, ry - 24), None, 0)
+ surface.blit(self.fitting_image(), (rx - 24, ry - 24), None, 0)
def tick(self):
pass
class Lamp(BaseLight):
+
+ FITTING_IMG = "lamp.png"
+
+
+class MultiColourLamp(BaseLight):
+
+ FITTING_IMG = "lamp.png"
+ DEFAULT_COLOURS = sorted(COLOURS.keys())
+
+ def __init__(self, **kw):
+ self.colour_cycle = kw.pop("colours", None)
+ self.colour_pos = 0
+ kw["colour"] = self.colour_cycle[0]
+ super(MultiColourLamp, self).__init__(**kw)
+
+ def toggle(self):
+ self.colour_pos += 1
+ if self.colour_pos >= len(self.colour_cycle):
+ self.colour = self.colour_cycle[0]
+ self.colour_pos = -1
+ self.on = False
+ else:
+ self.colour = self.colour_cycle[self.colour_pos]
+ self.on = True
+ self.invalidate_fitting_image()
+
+
+class PulsatingLamp(BaseLight):
+
FITTING_IMG = "lamp.png"
+ DEFAULT_PULSE_RANGE = (20, 100)
+ DEFAULT_PULSE_VELOCITY = 2
+ DEFAULT_INTENSITY_RANGE = (0.0, 0.9)
+ DEFAULT_INTENSITY_VELOCITY = 0.1
def __init__(self, **kw):
- kw.pop("direction", None)
- kw.pop("spread", None)
- super(Lamp, self).__init__(**kw)
+ self.pulse_range = kw.pop("pulse_range", self.DEFAULT_PULSE_RANGE)
+ self.pulse_velocity = kw.pop(
+ "pulse_velocity", self.DEFAULT_PULSE_VELOCITY)
+ self.intensity_range = kw.pop(
+ "intensity_range", self.DEFAULT_INTENSITY_RANGE)
+ self.intensity_velocity = kw.pop(
+ "intensity_velocity", self.DEFAULT_INTENSITY_VELOCITY)
+ super(PulsatingLamp, self).__init__(**kw)
+
+ def _update_range(self, value, velocity, value_range):
+ value += velocity
+ if value < value_range[0]:
+ value = value_range[0]
+ velocity = -velocity
+ elif value > value_range[1]:
+ value = value_range[1]
+ velocity = -velocity
+ return value, velocity
+
+ def tick(self):
+ self.ray_manager.max_radius, self.pulse_velocity = self._update_range(
+ self.ray_manager.max_radius, self.pulse_velocity, self.pulse_range)
+ self.intensity, self.intensity_velocity = self._update_range(
+ self.intensity, self.intensity_velocity, self.intensity_range)
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)