Lighting System - mganzarcik/fabulae GitHub Wiki
The engine uses three different lighting systems to create the illusion of dynamic lighting without the need to use anything more complex than the default box2dlights implementation.
All the lights in these systems are managed by the Sun. Sun is an object that modifies the ambient light in the game based on the time of day. It also modifies the light intensity of the lights it manages to make sure no light, when combined with the ambient light, can exceed the maximum light intensity allowed.
For example, if the current ambient light is 0.4, the maximum light intensity is 0.6 and a specific light has an intensity of 0.3, the sun will lower 0.3 to 0.2 according to this formula:
(currentAmbientLight / maxLightIntensity) * lightIntensity
`[toggle gif]`
Night
Dawn
Day
Static lights belong to the majority of lights in the game. These lights are limited in their movement to a single, very small area, or are completely stationary. An example would be the light of a fireplace, which moves around an area few pixels wide to simulate the movement of the flames, the light from a lamppost that is swinging slightly in the wind, or a completely static light coming out of a window.
These lights cast shadows. The shadow casting geometry is defined in the map file and specifically constructed to fit the placement of the light. This means that all shadow-casting geometry belongs to a specific light. This is done to avoid the problem of calculating 3D shadows on the fly in a 2D isometric or orthogonal environment without real 3D information.
Here, the light source is the blue dot. The red lines represent wall geometry as seen by the player, while the green lines represent the actual shadow casting geometry baked into the map itself. Obviously, if the light source moved to a different location (i.e. above the wall), the shadows cast would not make sense. Therefore the light needs to remain static, or only move very small distances to create the illusion of correctly cast shadows.
These are, in short, moving lights. There will only be a minimum of these in the game, usually tied to the PC characters (for example when carrying torches). These lights do not cast shadows.
These lights become activated when the Stealth mode is turned on. These lights do not represent actual light conceptually; instead they show the player what is visible to individual NPCs.
These lights cast shadows and are moving. The shadows cast do not take into account the 3D nature of the world. Instead, the geometry is baked into the map in a simple way – everything that should obstruct the FOV is created as a polygon in the map without considering the height of the object. So for example for a wall, the light obstructing polygon would correspond to the base of the wall – the view cone light would keep the wall in the shadow when looking from below:
In the example, the blue dot represents an NPC. The visible (non-shaded) part of the view cone is the grey area, while the not visible (shaded) part is the black area. The wall geometry is ignored by the shadow casting logic – parts of it are shaded, parts are not. The actual geometry used to determine by the shadow casting logic is displayed in green – this is the part defined in the map file.