V1.16 New Light System
I finally made a lighting system that, to my knowledge, works bug-free. The V1.16 New Lighting System uses a tile-based system similar to that of the cubic function family of coding, using wave function collapse and 2d tile scrolling, all similar and good waypoints for learning how to code.
The lighting system runs on a 0 to 6 light scale that uses the equation (alpha = lightLevel * (1/6)) to set the world.lightImage's alpha value before calling a fillRect using a "xor" and "source" blend mode to draw the light level, the light map itself is just an image displayed after the mobs and world layers.
to go in depth about the lighting system, it uses a diamond pattern to find light sources by first finding all the ones in a 7x7 diamond area and setting all the tiles light level within a 6x6 diamond area to 0. the next step is to run a altherigium to calculate the light levels from here.
the calculations happen by using a while loop that runs if there is a tile id in the updateList, every tile in the updateList is updated to check its surrounding cells if there light ale is less than its own light value -1 and if so than it sets the light value to its own -1 and adds the new tile's id to a newList which at the end of the loop function is used to replace the updateList.
The world generator uses a different method to calculate light as to keep the load time short for making worlds, it simply finds the highest light level of itself and surrounding tiles and sets its light level to it, -1 if the light level isn't its own. It also updates from the top of the world to the bottom, as the top has lots of empty space for light.
worldUpdateLight = function(x, y) local updateList = [] // Find sources for x2=-7 to 7 for y2 = -7+abs(x2) to 7-abs(x2) // Clear light if abs(x2)+abs(y2) <= 6 then world.mapLight[x+x2][y+y2].lightLevel = 0 end if ( blockTypes.phaze.contains(world.mapWall[x+x2][y+y2].name) and blockTypes.phaze.contains(world.mapFloor[x+x2][y+y2].name) ) or world.mapLight[x+x2][y+y2].lightLevel > 0 or world.mapFloor[x+x2][y+y2].name == "lava" or world.mapFloor[x+x2][y+y2].name == "torch" then data = object x = x+x2 y = y+y2 end updateList.push(data) if world.mapLight[x+x2][y+y2].lightLevel == 0 then world.mapLight[x+x2][y+y2].lightLevel = 6 end end end end // Calculate light while updateList.length != 0 local newList = [] for i=0 to updateList.length-1 local light = world.mapLight[updateList[i].x][updateList[i].y].lightLevel // UP if world.mapLight[updateList[i].x][updateList[i].y+1].lightLevel < light-1 then world.mapLight[updateList[i].x][updateList[i].y+1].lightLevel = light-1 local data = object x = updateList[i].x y = updateList[i].y+1 end if newList.contains(data) == false then newList.push(data) end end // left if world.mapLight[updateList[i].x-1][updateList[i].y].lightLevel < light-1 then world.mapLight[updateList[i].x-1][updateList[i].y].lightLevel = light-1 local data = object x = updateList[i].x-1 y = updateList[i].y end if newList.contains(data) == false then newList.push(data) end end // right if world.mapLight[updateList[i].x+1][updateList[i].y].lightLevel < light-1 then world.mapLight[updateList[i].x+1][updateList[i].y].lightLevel = light-1 local data = object x = updateList[i].x+1 y = updateList[i].y end if newList.contains(data) == false then newList.push(data) end end // Down if world.mapLight[updateList[i].x][updateList[i].y-1].lightLevel < light-1 then world.mapLight[updateList[i].x][updateList[i].y-1].lightLevel = light-1 local data = object x = updateList[i].x y = updateList[i].y-1 end if newList.contains(data) == false then newList.push(data) end end end updateList = newList end for x2=-6 to 6 for y2 = -6+abs(x2) to 6-abs(x2) drawLight(x+x2, y+y2) end end end drawLight = function(x, y) drawTile("wall", x, y) drawTile("floor", x, y) drawTile("light", x, y) end
Get Faith
Faith
Gods lost will
Status | In development |
Author | Waharocket |
Genre | Survival |
Leave a comment
Log in with itch.io to leave a comment.