I am working on a top-down, 2d cell-based RPG game. I would like to implement a cell based lighting system, something like this and this.
I basically have several light sources and light deteriorates on every cell in every direction. Something like the below (numbers represent the level of light in each cell):
1 1 1 1 1 0 0 0 0 0 0
1 2 2 2 1 0 0 1 1 1 0
1 2 3 2 1 0 0 1 2 1 0
1 2 2 2 1 0 0 1 1 1 0
1 1 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 0 0 0 0
0 0 0 0 1 2 1 0 0 0 0
0 0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
So basically I insert a source of light of "strength" x
at a given spot. I then set the surrounding cells' light strength to x-1
and so on until I reach zero (or a set global minimum value). I am looking for an efficient algorithm to implement and generate such a lightning grid based on my light sources and any tips regarding such implementation.
I am currently not looking into field of view algorithms, just an efficient way of generating the above. Any help will be much appreciated.
EDIT: Just to clarify, this should handle movement of light sources (e.g. player walking with a torch).
EDIT2: Ok, I've solved my problem myself based on the simple distance suggestion by Jari (thus I'll accept his answer although I'm still looking for a more efficient way of generating such array). If anyone's interested here is a demo of what I came up with (multiple light sources with alpha compositing)
Answer
I'd probably just create a 256x256 (or some such) pre-calculated array and scaled that as needed; that way you can do all sorts of special lights if you want as well.
Going procedurally, the easiest way would probably be to just calculate the distance.
xd = posx-centerx
yd = posy-centery
dist = sqrt(xd*xd+yd*yd)
pow = maxpow - dist
if (pow < 0) pow = 0
and then just draw a (maxpow*2)-1 square around the light center..
Something like that. Doesn't give exactly the results you mentioned, because this one generates a more round light (yours are rather square).
No comments:
Post a Comment