Package squidpony.squidgrid.gui.gdx
Class LightingHandler
java.lang.Object
squidpony.squidgrid.gui.gdx.LightingHandler
- All Implemented Interfaces:
Serializable
public class LightingHandler extends Object implements Serializable
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 LightingHandler per level,
where a level corresponds to a
Created by Tommy Ettinger on 11/2/2018.
char[][]
. After constructing a LightingHandler with the resistances for a
level, you should add all light sources with their positions, either using addLight(int, int, Radiance)
or
by directly putting keys and values into 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 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 update()
or
updateAll()
(updateAll() is for when there is no viewer), and once that update() call has been made you can
call draw(SparseLayers)
to change the background colors of a SparseLayers, draw(SquidPanel)
to
change the colors of a SquidPanel (typically the background layer of a SquidLayers, as from
SquidLayers.getBackgroundLayer()
), or draw(float[][])
to change a 2D float array that holds packed
float colors (which may be used in some custom setup). To place user-interface lighting effects that don't affect the
actual FOV of creatures in the game, you can use updateUI(Coord, Radiance)
, which is called after
update()
but before draw(float[][])
.
Created by Tommy Ettinger on 11/2/2018.
- See Also:
- Serialized Form
-
Field Summary
Fields Modifier and Type Field Description float
backgroundColor
The packed float color to mix background cells with when a cell has lighting and is within line-of-sight, but has no background color to start with (its color is 0f as a packed float, orSColor.TRANSPARENT
).float[][][]
colorLighting
A pair of 2D float arrays with different usages;colorLighting[0]
is a 2D array that stores the strength of light in each cell, andcolorLighting[1]
is a 2D array that stores the color of light in each cell, as a packed float color.double[][]
fovResult
What the "viewer" (as passed tocalculateFOV(Coord)
) can see either nearby without light or because an area in line-of-sight has light in it.int
height
Height of the 2D arrays used in this, as obtained fromresistances
.OrderedMap<Coord,Radiance>
lights
double[][]
losResult
A 2D array of doubles that are either 0.0 if a cell has an obstruction between it and the viewer, or greater than 0.0 otherwise.GreasedRegion
noticeable
A GreasedRegion that stores any cells that are in line-of-sight or are close enough to a cell in line-of-sight to potentially cast light into such a cell.Radius
radiusStrategy
How light should spread; usuallyRadius.CIRCLE
unless gameplay reasons need it to be SQUARE or DIAMOND.double[][]
resistances
The 2D array of light-resistance values from 0.0 to 1.0 for each cell on the map, as produced byDungeonUtility.generateResistances(char[][])
.float[][][]
tempColorLighting
Temporary storage array used for calculations involvingcolorLighting
; it sometimes may make sense for other code to use this as temporary storage as well.double[][]
tempFOV
Temporary storage array used for calculations involvingfovResult
; it sometimes may make sense for other code to use this as temporary storage as well.double
viewerRange
How far the viewer can see without light; defaults to 4.0 cells, and you are encouraged to change this member field if the vision range changes after construction.int
width
Width of the 2D arrays used in this, as obtained fromresistances
. -
Constructor Summary
Constructors Constructor Description LightingHandler()
Unlikely to be used except during serialization; makes a LightingHandler for a 20x20 fully visible level.LightingHandler(double[][] resistance)
Given a resistance array as produced byDungeonUtility.generateResistances(char[][])
orDungeonUtility.generateSimpleResistances(char[][])
, makes a LightingHandler that can haveRadiance
objects added to it in various locations.LightingHandler(double[][] resistance, float backgroundColor, Radius radiusStrategy, double viewerVisionRange)
Given a resistance array as produced byDungeonUtility.generateResistances(char[][])
orDungeonUtility.generateSimpleResistances(char[][])
, makes a LightingHandler that can haveRadiance
objects added to it in various locations.LightingHandler(double[][] resistance, com.badlogic.gdx.graphics.Color backgroundColor, Radius radiusStrategy, double viewerVisionRange)
Given a resistance array as produced byDungeonUtility.generateResistances(char[][])
orDungeonUtility.generateSimpleResistances(char[][])
, makes a LightingHandler that can haveRadiance
objects added to it in various locations. -
Method Summary
Modifier and Type Method Description LightingHandler
addLight(int x, int y, Radiance light)
Adds a Radiance as a light source at the given position.LightingHandler
addLight(Coord position, Radiance light)
Adds a Radiance as a light source at the given position.double[][]
calculateFOV(int viewerX, int viewerY)
Used to calculate what cells are visible as if any flicker or strobe effects were simply constant light sources.double[][]
calculateFOV(int viewerX, int viewerY, int minX, int minY, int maxX, int maxY)
Used to calculate what cells are visible as if any flicker or strobe effects were simply constant light sources.double[][]
calculateFOV(Coord viewer)
Used to calculate what cells are visible as if any flicker or strobe effects were simply constant light sources.void
draw(float[][] backgrounds)
Given a 2D array of packed float colors, fills the 2D array with different 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.void
draw(SparseLayers layers)
Given a SparseLayers, fills the SparseLayers with different 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.void
draw(SquidPanel background)
Given a SquidPanel that should be only solid blocks (such as the background of a SquidLayers) and a position for the viewer (typically the player), fills the SquidPanel with different colors based on what lights are present in line of sight of the viewer and the various flickering or pulsing effects that Radiance light sources can do.Radiance
get(int x, int y)
Gets the Radiance at the given position, if present, or null if there is no light source there.Radiance
get(Coord position)
Gets the Radiance at the given position, if present, or null if there is no light source there.void
mixColoredLighting(float flare)
EditscolorLighting
by adding in and mixing the colors intempColorLighting
, with the strength of light in tempColorLighting boosted by flare (which can be any finite float greater than -1f, but is usually from 0f to 1f when increasing strength).void
mixColoredLighting(float flare, float color)
EditscolorLighting
by adding in and mixing the given color where the light strength intempFOV
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).LightingHandler
moveLight(int oldX, int oldY, int newX, int newY)
If a Radiance is present at oldX,oldY, this will move it to newX,newY and overwrite any existing Radiance at newX,newY.LightingHandler
moveLight(Coord oldPosition, Coord newPosition)
If a Radiance is present at oldPosition, this will move it to newPosition and overwrite any existing Radiance at newPosition.LightingHandler
removeLight(int x, int y)
Removes a Radiance as a light source from the given position, if any is present.LightingHandler
removeLight(Coord position)
Removes a Radiance as a light source from the given position, if any is present.void
update()
Typically called every frame, this updates the flicker and strobe effects of Radiance objects and applies those changes in lighting color and strength to the various fields of this LightingHandler.void
updateAll()
Typically called every frame when there isn't a single viewer, this updates the flicker and strobe effects of Radiance objects and applies those changes in lighting color and strength to the various fields of this LightingHandler.void
updateUI(int lightX, int lightY, Radiance radiance)
Updates the flicker and strobe effects of a Radiance object and applies the lighting from just that Radiance to just thecolorLighting
field, without changing FOV.void
updateUI(Coord pos, Radiance radiance)
Updates the flicker and strobe effects of a Radiance object and applies the lighting from just that Radiance to just thecolorLighting
field, without changing FOV.
-
Field Details
-
radiusStrategy
How light should spread; usuallyRadius.CIRCLE
unless gameplay reasons need it to be SQUARE or DIAMOND. -
resistances
The 2D array of light-resistance values from 0.0 to 1.0 for each cell on the map, as produced byDungeonUtility.generateResistances(char[][])
. -
fovResult
What the "viewer" (as passed tocalculateFOV(Coord)
) can see either nearby without light or because an area in line-of-sight has light in it. Edited bycalculateFOV(Coord)
andupdate()
, but notupdateUI(Coord, Radiance)
(which is meant for effects that are purely user-interface). -
losResult
A 2D array of doubles that are either 0.0 if a cell has an obstruction between it and the viewer, or greater than 0.0 otherwise. -
tempFOV
Temporary storage array used for calculations involvingfovResult
; it sometimes may make sense for other code to use this as temporary storage as well. -
colorLighting
A pair of 2D float arrays with different usages;colorLighting[0]
is a 2D array that stores the strength of light in each cell, andcolorLighting[1]
is a 2D array that stores the color of light in each cell, as a packed float color. Both 2D arrays are the size of the map, as defined byresistances
initially and later available inwidth
andheight
. -
tempColorLighting
Temporary storage array used for calculations involvingcolorLighting
; it sometimes may make sense for other code to use this as temporary storage as well. -
width
Width of the 2D arrays used in this, as obtained fromresistances
. -
height
Height of the 2D arrays used in this, as obtained fromresistances
. -
backgroundColor
The packed float color to mix background cells with when a cell has lighting and is within line-of-sight, but has no background color to start with (its color is 0f as a packed float, orSColor.TRANSPARENT
). -
viewerRange
How far the viewer can see without light; defaults to 4.0 cells, and you are encouraged to change this member field if the vision range changes after construction. -
lights
A mapping from positions asCoord
objects toRadiance
objects that describe the color, lighting radius, and changes over time of any in-game lights that should be shown on the map and change FOV. You can edit this manually or by usingmoveLight(int, int, int, int)
,addLight(int, int, Radiance)
, andremoveLight(int, int)
. -
noticeable
A GreasedRegion that stores any cells that are in line-of-sight or are close enough to a cell in line-of-sight to potentially cast light into such a cell. Depends on the highestRadiance.range
inlights
.
-
-
Constructor Details
-
LightingHandler
public LightingHandler()Unlikely to be used except during serialization; makes a LightingHandler for a 20x20 fully visible level. The viewer vision range will be 4.0, and lights will use a circular shape. -
LightingHandler
Given a resistance array as produced byDungeonUtility.generateResistances(char[][])
orDungeonUtility.generateSimpleResistances(char[][])
, makes a LightingHandler that can haveRadiance
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.0, and lights will use a circular shape.- Parameters:
resistance
- a resistance array as produced by DungeonUtility
-
LightingHandler
public LightingHandler(double[][] resistance, com.badlogic.gdx.graphics.Color backgroundColor, Radius radiusStrategy, double viewerVisionRange)Given a resistance array as produced byDungeonUtility.generateResistances(char[][])
orDungeonUtility.generateSimpleResistances(char[][])
, makes a LightingHandler that can haveRadiance
objects added to it in various locations.- Parameters:
resistance
- a resistance array as produced by DungeonUtilitybackgroundColor
- the background color to use, as a libGDX colorradiusStrategy
- the shape lights should take, typicallyRadius.CIRCLE
for "realistic" lights or one ofRadius.DIAMOND
orRadius.SQUARE
to match game rules for distanceviewerVisionRange
- how far the player can see without light, in cells
-
LightingHandler
public LightingHandler(double[][] resistance, float backgroundColor, Radius radiusStrategy, double viewerVisionRange)Given a resistance array as produced byDungeonUtility.generateResistances(char[][])
orDungeonUtility.generateSimpleResistances(char[][])
, makes a LightingHandler that can haveRadiance
objects added to it in various locations.- Parameters:
resistance
- a resistance array as produced by DungeonUtilitybackgroundColor
- the background color to use, as a packed float (produced byColor.toFloatBits()
)radiusStrategy
- the shape lights should take, typicallyRadius.CIRCLE
for "realistic" lights or one ofRadius.DIAMOND
orRadius.SQUARE
to match game rules for distanceviewerVisionRange
- how far the player can see without light, in cells
-
-
Method Details
-
addLight
Adds a Radiance as a light source at the given position. Overwrites any existing Radiance at the same position.- Parameters:
x
- the x-position to add the Radiance aty
- the y-position to add the Radiance atlight
- a Radiance object that can have a changing radius, color, and various other effects on lighting- Returns:
- this for chaining
-
addLight
Adds a Radiance as a light source at the given position. Overwrites any existing Radiance at the same position.- Parameters:
position
- the position to add the Radiance atlight
- a Radiance object that can have a changing radius, color, and various other effects on lighting- Returns:
- this for chaining
-
removeLight
Removes a Radiance as a light source from the given position, if any is present.- Parameters:
x
- the x-position to remove the Radiance fromy
- the y-position to remove the Radiance from- Returns:
- this for chaining
-
removeLight
Removes a Radiance as a light source from the given position, if any is present.- Parameters:
position
- the position to remove the Radiance from- Returns:
- this for chaining
-
moveLight
If a Radiance is present at oldX,oldY, this will move it to newX,newY and overwrite any existing Radiance at newX,newY. If no Radiance is present at oldX,oldY, this does nothing.- Parameters:
oldX
- the x-position to move a Radiance fromoldY
- the y-position to move a Radiance fromnewX
- the x-position to move a Radiance tonewY
- the y-position to move a Radiance to- Returns:
- this for chaining
-
moveLight
If a Radiance is present at oldPosition, this will move it to newPosition and overwrite any existing Radiance at newPosition. If no Radiance is present at oldPosition, this does nothing.- Parameters:
oldPosition
- the Coord to move a Radiance fromnewPosition
- the Coord to move a Radiance to- Returns:
- this for chaining
-
get
Gets the Radiance at the given position, if present, or null if there is no light source there.- Parameters:
x
- the x-position to look upy
- the y-position to look up- Returns:
- the Radiance at the given position, or null if none is present there
-
get
Gets the Radiance at the given position, if present, or null if there is no light source there.- Parameters:
position
- the position to look up- Returns:
- the Radiance at the given position, or null if none is present there
-
mixColoredLighting
EditscolorLighting
by adding in and mixing the colors intempColorLighting
, with the strength of light in tempColorLighting boosted by flare (which can be any finite float greater than -1f, but is usually from 0f to 1f when increasing strength). Primarily used internally, but exposed so outside code can do the same things this class can.- Parameters:
flare
- boosts the effective strength of lighting intempColorLighting
; usually from 0 to 1
-
mixColoredLighting
EditscolorLighting
by adding in and mixing the given color where the light strength intempFOV
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). Primarily used internally, but exposed so outside code can do the same things this class can.- Parameters:
flare
- boosts the effective strength of lighting intempColorLighting
; usually from 0 to 1
-
update
Typically called every frame, this updates the flicker and strobe effects of Radiance objects and applies those changes in lighting color and strength to the various fields of this LightingHandler. This will only have an effect ifcalculateFOV(Coord)
orcalculateFOV(int, int)
was called during the last time the viewer position changed; typically calculateFOV() only needs to be called once per move, while update() needs to be called once per frame. This method is usually called before each call todraw(float[][])
, but other code may be between the calls and may affect the lighting in customized ways. -
updateAll
Typically called every frame when there isn't a single viewer, this updates the flicker and strobe effects of Radiance objects and applies those changes in lighting color and strength to the various fields of this LightingHandler. This method is usually called before each call todraw(float[][])
, but other code may be between the calls and may affect the lighting in customized ways. This overload has no viewer, so all cells are considered visible unless they are fully obstructed (solid cells behind walls, for example). Unlike update(), this method does not needcalculateFOV(Coord)
to be called for it to work properly. -
updateUI
Updates the flicker and strobe effects of a Radiance object and applies the lighting from just that Radiance to just thecolorLighting
field, without changing FOV. This method is meant to be used for GUI effects that aren't representative of something a character in the game could interact with. It is usually called afterupdate()
and before each call todraw(float[][])
, but other code may be between the calls and may affect the lighting in customized ways.- Parameters:
pos
- the position of the light effectradiance
- the Radiance to update standalone, which does not need to be already added to this
-
updateUI
Updates the flicker and strobe effects of a Radiance object and applies the lighting from just that Radiance to just thecolorLighting
field, without changing FOV. This method is meant to be used for GUI effects that aren't representative of something a character in the game could interact with. It is usually called afterupdate()
and before each call todraw(float[][])
, but other code may be between the calls and may affect the lighting in customized ways.- Parameters:
lightX
- the x-position of the light effectlightY
- the y-position of the light effectradiance
- the Radiance to update standalone, which does not need to be already added to this
-
draw
Given a SparseLayers, fills the SparseLayers with different 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 callupdate()
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).- Parameters:
layers
- a SparseLayers that may have existing background colors (these will be mixed in)
-
draw
Given a SquidPanel that should be only solid blocks (such as the background of a SquidLayers) and a position for the viewer (typically the player), fills the SquidPanel with different colors based on what lights are present in line of sight of the viewer and the various flickering or pulsing effects that Radiance light sources can do. Given a SquidPanel that should be only solid blocks (such as the background of a SquidLayers), fills the SquidPanel with different 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 callupdate()
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).- Parameters:
background
- a SquidPanel used as a background, such as the back Panel of a SquidLayers
-
draw
Given a 2D array of packed float colors, fills the 2D array with different 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 callupdate()
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).- Parameters:
backgrounds
- a 2D float array, typically obtained fromSquidPanel.colors
orSparseLayers.backgrounds
-
calculateFOV
Used to calculate what cells are visible as if any flicker or strobe effects were simply constant light sources. Runs part of the calculations to draw lighting as if all radii are at their widest, but does no actual drawing. This should be called any time the viewer moves to a different cell, and it is critical that this is called (at least) once after a move but beforeupdate()
gets called to change lighting at the new cell. This sets important information on what lights might need to be calculated during each update(Coord) call; it does not need to be called beforeupdateAll()
(with no arguments) because that doesn't need a viewer. SetsfovResult
,losResult
, andnoticeable
based on the given viewer position and any lights inlights
.- Parameters:
viewer
- the position of the player or other viewer- Returns:
- the calculated FOV 2D array, which is also stored in
fovResult
-
calculateFOV
Used to calculate what cells are visible as if any flicker or strobe effects were simply constant light sources. Runs part of the calculations to draw lighting as if all radii are at their widest, but does no actual drawing. This should be called any time the viewer moves to a different cell, and it is critical that this is called (at least) once after a move but beforeupdate()
gets called to change lighting at the new cell. This sets important information on what lights might need to be calculated during each update(Coord) call; it does not need to be called beforeupdateAll()
(with no arguments) because that doesn't need a viewer. SetsfovResult
,losResult
, andnoticeable
based on the given viewer position and any lights inlights
.- Parameters:
viewerX
- the x-position of the player or other viewerviewerY
- the y-position of the player or other viewer- Returns:
- the calculated FOV 2D array, which is also stored in
fovResult
-
calculateFOV
Used to calculate what cells are visible as if any flicker or strobe effects were simply constant light sources. Runs part of the calculations to draw lighting as if all radii are at their widest, but does no actual drawing. This should be called any time the viewer moves to a different cell, and it is critical that this is called (at least) once after a move but beforeupdate()
gets called to change lighting at the new cell. This sets important information on what lights might need to be calculated during each update(Coord) call; it does not need to be called beforeupdateAll()
(with no arguments) because that doesn't need a viewer. This overload allows the area this processes to be restricted to a rectangle betweenminX
andmaxX
and betweenminY
andmaxY
, ignoring any lights outside that area (typically because they are a long way out from the map's shown area). SetsfovResult
,losResult
, andnoticeable
based on the given viewer position and any lights inlights
.- Parameters:
viewerX
- the x-position of the player or other viewerviewerY
- the y-position of the player or other viewerminX
- inclusive lower bound on x to calculateminY
- inclusive lower bound on y to calculatemaxX
- exclusive upper bound on x to calculatemaxY
- exclusive upper bound on y to calculate- Returns:
- the calculated FOV 2D array, which is also stored in
fovResult
-