Improving lighting

Hi,

Looking at revamping lighting in my tiled game. When a block is placed, if block above is null (i.e. no block above/outside) then light is set to max for that block, if there is a block below, the light for this block would get set to block above light value -1 to a minimum of 1. My question is - how do you go about doing light value if a block is placed at the left of an existing block or right of an existing block as surely the light value then needs to adjusted on these?

Any advice is much appreciated as always.

Thanks

The easiest way to make sure they are all correct is to first set all tiles that are known to be bright to their correct values. Then do multiple passes where you check all neighboring tiles to “expand” the light to neighbors.

Example: You have a single bright block with a light value of 8 surrounded by dark (light value 0) blocks. You do a pass to spread the light to nearby tiles, where each tile checks its neighboring tile and sets its own light to the maximum of the neighbors minus one (or itself if it’s already brighter than the neighbors). This pass will cause the blocks next to the bright block to gain a light value of 7. Then you can run the exact same pass again, causing the next set of neighbors to gain a light value of 6. You can repeat this until no tiles were updated, at which point you stop.

Obviously you’d not want to do this each game update, but to update the lighting every time a block changes. If your world is big, you can split up the world into chunks of, say, 16x16 tiles and only update the tiles that are affected by a given block change.

Lighting does work when adding blocks, block picks up its colour from the neighbouring ones as you suggest.
What I need is, if I was to remove a block above a block, want the block then to become lighter…

This is what I have:

So, for instance, far right where there is a big hole/cave blocks go darker as they go further down, if I was to cover this cave up at the top, then the blocks
making up the cave should go darker.

Thanks

What you could do is when you destroy a block, you update the light of its neighbours, and for each neighbours if the light wwas updated you update the light of its neighbours. Something like this ( of course it’s just to get the idea ) :


public void onRemoveBlock(Block block){
    Block[] neighbours = block.getNeighbours();
    for(Block neighbour : neighbours ){
        updateLight(neighbour);
    }
}
public void updateLight(Block block){
    //Do your stuff
     if(block.lightWasUpdated){
          block.lightWasUpdated = false;
          Block[] neighbours = block.getNeighbours();
          for(Block neighbour : neighbours ){
             updateLight(neighbour);
          }
    }
}