Hi this is a pretty simple light/shadow program that can be used to make real time shadow/light effects in 2D top down games (or why not platformers or any other 2D game for the matter). Fairly simple to implement depending on how you have defined your world.
The code has some useless stuff that remained after i deleted most of the non relevant code. This was part of a larger thing.
if you run it you may notice low fps, the background boxes are very resource intensive and sloppily coded. On a solid background with some boxes the ldensity 4 makes about 90fps on my comp
sync on : sync rate 60
a2setblendmode 2,2,1 : hide mouse : a2setlineaa 0 : set text size 1
global density# = 10.0
ldensity = 8 : ldensity# = ldensity
a = screen width()/density# : b = screen height()/density#
la = a*ldensity : lb = b*ldensity
dim n(a,b) as UDTnode
dim l(la,lb) as UDTnode
gosub _chaos:
dd# = 1.5
lr = 70*ldensity
make image 1,la,lb
ambient = 110
do
mx# = mousex() : my# = mousey()
mi = (mx#+density#/2)/density# : mj =(my#+density#/2)/density#
mli = mi*ldensity : mlj = mj*ldensity ` light grid coords
draw to image 1
for i = 0 to la : for j = 0 to lb : l(i,j).l# = 0 : next : next
j1 = max(mlj-lr,0) : j2 = min(mlj+lr,lb)
for i = max(mli-lr,0) to min(mli+lr,la) : bline(mli,mlj,i,j1,lr) : bline(mli,mlj,i,j2,lr) : NEXT
i1 = max(mli-lr,0) : i2 = min(mli+lr,la)
for j = max(mlj-lr,0) to min(mlj+lr,lb) : bline(mli,mlj,i1,j,lr) : bline(mli,mlj,i2,j,lr) : NEXT
draw to image 1 : a2setblendmode 2,1,1 : a2fillbox 0,0,la,lb,rgb(255-ambient,255-ambient,255-ambient) : a2setblendmode 2,2,3
for i = 0 to la : for j = 0 to lb
if l(i,j).w = 0 and l(i,j).l# > 0 then a2dot i,j,rgb(255*l(i,j).l#,255*l(i,j).l#,225*l(i,j).l#)
next : next
draw to bitmap 0
a2setblendmode 2,2,1
for i = 0 to a : for j = 0 to b
a2line (i+0.5)*density#,(j+0.5)*density#,(i+0.5)*density#,(j-0.5)*density#,rgb(30,60,160)
a2line (i+0.5)*density#,(j+0.5)*density#,(i-0.5)*density#,(j+0.5)*density#,rgb(30,60,160)
if n(i,j).w = 0 then a2fillbox (i+0.5)*density#-1,(j+0.5)*density#-1,(i-0.5)*density#+2,(j-0.5)*density#+2,rgb(200,200,200)
if n(i,j).w = 1 then a2fillbox (i+0.5)*density#-1,(j+0.5)*density#-1,(i-0.5)*density#+2,(j-0.5)*density#+2,rgb(80,30,15)
if i = mi and j = mj
if mouseclick() = 1 then starti = i : startj = j : n(i,j).w = 0 : done = 0
if mouseclick() = 2 then targeti = i : targetj = j : n(i,j).w = 0 : done = 0
a2fillbox (i+0.5)*density#-1,(j+0.5)*density#-1,(i-0.5)*density#+2,(j-0.5)*density#+2,rgb(30,30,30)
endif
if i = starti and j = startj then a2fillbox (i+0.5)*density#-1,(j+0.5)*density#-1,(i-0.5)*density#+2,(j-0.5)*density#+2,rgb(100,200,30)
if i = targeti and j = targetj then a2fillbox (i+0.5)*density#-1,(j+0.5)*density#-1,(i-0.5)*density#+2,(j-0.5)*density#+2,rgb(200,100,30)
NEXT : next
print k
dd# = dd#+mousemovez()/1800.0
print dd#
a2setblendmode 2,2,3 : a2drawimage 1,-4.5,-4.5,0,0,0,density#/ldensity#,0,rgb(255,255,255)
a2fillcircle mi*density#,mj*density#,4,rgb(255,150,50)
sync
cls
LOOP
function bline(x1,y1,x2,y2,r#)
dx = x2-x1 : dy = y2-y1 : light# = 1
if abs(dx) > abs(dy)
m# = (0.0 + dy)/dx : b# = y1 - m#*x1
if dx < 0 then dx = -1 : else dx = 1
while x1 <> x2
x1 = x1 + dx : j = int(m#*x1+b#)
if l(x1,j).w = 1 then exitfunction
l(x1,j).l# = light# : light# = light#-1/r#
endwhile
else
if dy <> 0
m# = (0.0 + dx)/dy : b# = x1 - m#*y1
if dy < 0 then dy = -1 : else dy = 1
while y1 <> y2
y1 = y1 + dy : i = int(m#*y1+b#)
if l(i,y1).w = 1 then exitfunction
l(i,y1).l# = light# : light# = light#-1/r#
endwhile
endif
endif
endfunction
_chaos:
for i = 1 to a-1 : for j = 1 to b-1
if i = 1 or j = 1 or i = a-1 or j = b-1 then n(i,j).w = 1
if random() < 0.01
for m = i-1 to i+1 : for n = j-1 to j+1
n(m,n).w = 1
for k = m*(ldensity) to (m)*(ldensity)+ldensity-1 : for o = n*(ldensity) to (n)*(ldensity)+ldensity-1
if k > 0 and k < la and o > 0 and o < lb then l(k,o).w = 1
NEXT : next
next : next
endif
n(i,j).v# = (sin(i)*cos(j*i)+1)/2.0
next : next
return
type UDTnode
v# ` velocity percentage
w ` wall 1/0
p_i : p_j `parent
closed : open : n
l# : lit
endtype
Oh it needs Advanced2d plugin and Matrix plugin
THE setting "ldensity = 8" int the beginning of the code defines the detail of the lightmap. 8 is 64 times more detailed than the grid you see. (8x8 = 64 times more nodes). This can probably run slow on slower PCs. For mine 6-8 is fine but 2-4 would already look very crisp if a rough texture was used as the ground. ( is pretty much as high as there is any point of going with this.
the setting "density# = 10" defines the base grid detail. higher values make larger boxes -> better performance