Class LightingManagerRgb

java.lang.Object
com.github.yellowstonegames.grid.LightingManager
com.github.yellowstonegames.grid.LightingManagerRgb
All Implemented Interfaces:
com.github.yellowstonegames.core.ISerializersNeeded

public class LightingManagerRgb extends LightingManager implements com.github.yellowstonegames.core.ISerializersNeeded
A convenience class that makes dealing with multiple colored light sources easier. All fields are public and documented to encourage their use alongside the API methods. The typical usage case for this class is when a game has complex lighting needs that should be consolidated into one LightingManager per level, where a level corresponds to a char[][]. After constructing a LightingManager with the resistances for a level, you should add all light sources with their positions, either using LightingManager.addLight(int, int, Radiance) or by directly putting keys and values into LightingManager.lights. Then you can calculate the visible cells once lighting is considered (which may include distant lit cells with unseen but unobstructing cells between the viewer and the light) using LightingManager.calculateFOV(Coord), which should be called every time the viewer moves. You can update the flicker and strobe effects on all Radiance objects, which is typically done every frame, using LightingManager.update() or LightingManager.updateAll() (updateAll() is for when there is no viewer), and once that update() call has been made you can call draw(int[][]) to change a 2D int array that holds packed int colors (such as the backgrounds in a GlyphMap). To place user-interface lighting effects that don't affect the in-game-world lights, you can use LightingManager.updateUI(Coord, Radiance), which is called after LightingManager.update() but before draw(int[][]).
This class uses the normal RGBA color space, like DescriptiveColorRgb, for almost every place it deals with color. The exception here is drawOklab(int[][]), which uses RGBA internally but converts the colors to OKlab when it outputs them.
Honestly, this class is quite complex, and you should really take a look at a demo that uses it to see how the different parts fit together. If you have the SquidSquad test sources, LightingTest in squidglyph provides a relatively simple example using many colors of light. VisionFramework also can be used to handle much of the boilerplate associated with vision and lighting.
  • Constructor Details

    • LightingManagerRgb

      public LightingManagerRgb()
      Unlikely to be used except during serialization; makes a LightingManager for a 20x20 fully visible level. The viewer vision range will be 4.0f, and lights will use a circular shape.
    • LightingManagerRgb

      public LightingManagerRgb(float[][] resistance)
      Given a resistance array as produced by FOV.generateResistances(char[][]) or FOV.generateSimpleResistances(char[][]), makes a LightingManager that can have Radiance objects added to it in various locations. This will use a solid black background when it casts light on cells without existing lighting. The viewer vision range will be 4.0f, and lights will use a circular shape.
      Parameters:
      resistance - a resistance array as produced by DungeonUtility
    • LightingManagerRgb

      public LightingManagerRgb(float[][] resistance, String backgroundColor, Radius radiusStrategy, float viewerVisionRange)
      Given a resistance array as produced by FOV.generateResistances(char[][]) or FOV.generateSimpleResistances(char[][]), makes a LightingManager that can have Radiance objects added to it in various locations.
      Parameters:
      resistance - a resistance array as produced by DungeonUtility
      backgroundColor - the background color to use, as a color description
      radiusStrategy - the shape lights should take, typically Radius.CIRCLE for "realistic" lights or one of Radius.DIAMOND or Radius.SQUARE to match game rules for distance
      viewerVisionRange - how far the player can see without light, in cells
    • LightingManagerRgb

      public LightingManagerRgb(float[][] resistance, int backgroundColor, Radius radiusStrategy, float viewerVisionRange)
      Given a resistance array as produced by FOV.generateResistances(char[][]) or FOV.generateSimpleResistances(char[][]), makes a LightingManager that can have Radiance objects added to it in various locations.
      Parameters:
      resistance - a resistance array as produced by DungeonUtility
      backgroundColor - the background color to use, as an RGBA8888 int
      radiusStrategy - the shape lights should take, typically Radius.CIRCLE for "realistic" lights or one of Radius.DIAMOND or Radius.SQUARE to match game rules for distance
      viewerVisionRange - how far the player can see without light, in cells
    • LightingManagerRgb

      public LightingManagerRgb(float[][] resistance, int backgroundColor, Radius radiusStrategy, float viewerVisionRange, LightingManager.SymmetryMode symmetry)
      Given a resistance array as produced by FOV.generateResistances(char[][]) or FOV.generateSimpleResistances(char[][]), makes a LightingManager that can have Radiance objects added to it in various locations.
      Parameters:
      resistance - a resistance array as produced by DungeonUtility
      backgroundColor - the background color to use, as an RGBA8888 int
      radiusStrategy - the shape lights should take, typically Radius.CIRCLE for "realistic" lights or one of Radius.DIAMOND or Radius.SQUARE to match game rules for distance
      viewerVisionRange - how far the player can see without light, in cells
  • Method Details

    • getNeutralColor

      public int getNeutralColor()
      An extension point for subclasses that don't use the Oklab color space; this subclass returns DescriptiveColorRgb.WHITE. There is no setter or field for the neutral color.
      Overrides:
      getNeutralColor in class LightingManager
      Returns:
      in this subclass, DescriptiveColorRgb.WHITE
    • mixColoredLighting

      public void mixColoredLighting(float flare)
      Edits LightingManager.colorLighting by adding in and mixing the colors in LightingManager.fovLightColors, with the strength of light in fovLightColors boosted by flare (which can be any finite float greater than -1f, but is usually from 0f to 1f when increasing strength). The strengths of each colored light is determined by LightingManager.lightFromFOV and the colors of lights are determined by LightingManager.fovLightColors. If a color of light is fully transparent, this skips that light.
      This is very limited-use; the related method mixColoredLighting(float, int) is used as part of LightingManager.update(), but this method is meant for when multiple colors of FOV light need to be mixed at once.
      Overrides:
      mixColoredLighting in class LightingManager
      Parameters:
      flare - boosts the effective strength of lighting in LightingManager.fovLightColors; usually from 0 to 1
    • mixColoredLighting

      public void mixColoredLighting(float flare, int color)
      Edits LightingManager.colorLighting by adding in and mixing the given color where the light strength in LightingManager.lightFromFOV is greater than 0, with that strength boosted by flare (which can be any finite float greater than -1f, but is usually from 0f to 1f when increasing strength). This draws its existing lighting strength from LightingManager.lightingStrength and its existing light colors from LightingManager.colorLighting; it modifies both of these.
      This has limited use outside this class, unless you are reimplementing part of LightingManager.update() or something like it.
      Overrides:
      mixColoredLighting in class LightingManager
      Parameters:
      flare - boosts the effective strength of lighting in LightingManager.lightFromFOV; usually from 0 to 1
      color - the RGBA8888 int color to mix in where the light strength in LightingManager.lightFromFOV is greater than 0
    • draw

      public void draw(int[][] backgrounds)
      Given a 2D array that should hold RGBA int colors, fills the 2D array with different RGBA colors based on what lights are present in line of sight of the viewer and the various flicker or strobe effects that Radiance light sources can do. You should usually call LightingManager.update() before each call to draw(), but you may want to make custom changes to the lighting in between those two calls (that is the only place those changes will be noticed). A common use for this in text-based games uses a GlyphMap's backgrounds field as the parameter. This always mixes the calculated lights in LightingManager.colorLighting with the LightingManager.backgroundColor, using LightingManager.lightingStrength to determine how much the lights should affect the background color.
      If this class is extended, this method should be considered as one to override.
      Overrides:
      draw in class LightingManager
      Parameters:
      backgrounds - a 2D int array, which will be modified in-place; visible cells will receive RGBA8888 colors
    • drawOklab

      public void drawOklab(int[][] backgrounds)
      Given a 2D array that will hold Oklab int colors, fills the 2D array with different Oklab colors based on what lights are present in line of sight of the viewer and the various flicker or strobe effects that Radiance light sources can do. You should usually call LightingManager.update() before each call to drawOklab(), but you may want to make custom changes to the lighting in between those two calls (that is the only place those changes will be noticed). A common use for this in text-based games uses a GlyphMap's backgrounds field as the parameter. This always mixes the calculated lights in LightingManager.colorLighting with the LightingManager.backgroundColor, using LightingManager.lightingStrength to determine how much the lights should affect the background color.
      If this class is extended, this method should be considered as one to override.
      Overrides:
      drawOklab in class LightingManager
      Parameters:
      backgrounds - a 2D int array, which will be modified in-place; visible cells will receive Oklab colors
    • toString

      public String toString()
      Overrides:
      toString in class LightingManager
    • getSerializersNeeded

      public List<Class<?>> getSerializersNeeded()
      Specified by:
      getSerializersNeeded in interface com.github.yellowstonegames.core.ISerializersNeeded
      Overrides:
      getSerializersNeeded in class LightingManager