Hav rushed thru the code now janbo and here is it as a sample to run in agk right away...
Going ti implement it in to the Engine so will we see wath happens
I made the code mark walls so i could see that it would be usefull for a level detail system.
remstart
Tile based line of sight by
janbo ( germany) and cliff mellangård ( Sweden ) / play8bitars ) ( 3degs )
find original code at
https://forum.thegamecreators.com/thread/221376#msg2616287
remend
// Project: Tile line of sight
// Created: 2018-01-27
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Tile line of sight" )
SetWindowSize( 768, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 528, 528 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 0, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
// This tile based line off sight wll see all border pixels as solids
type sTlos
Solid as integer
ID as integer
endtype
Global Dim Tlos[32,32] as sTlos
#Constant TLScalus = 16
#Constant TLrange = 4
sprite=1
// Lets draw up our tiles
// skip border tiles as we see them as solids //
for x=1 to 31
for y=1 to 31
Createsprite(sprite,0)
setspritesize(sprite,TLScalus,TLScalus)
Setspriteposition(sprite,x*TLScalus,y*TLScalus)
Setspritecoloralpha(sprite,50)
Tlos[x,y].ID =sprite
if random(1,4) = 2 // create random solids
Tlos[x,y].Solid = 1
Setspritecoloralpha(sprite,255)
endif
inc sprite
next y
next x
global player = 2000
createsprite(player,0)
setspritesize(player,TLScalus,TLScalus)
Setspritecolor(player,0,0,255,255)
while exits = 0
x = random(14,22)
y = random(14,22)
setspriteposition(player,x*TLScalus,y*TLScalus)
if Tlos[x,y].Solid = 0 then exits=1
endwhile
// mainloop
do
// only used in this demostration to reset sprites
for x =1 to 31
for y =1 to 31
if Tlos[x,y].Solid =0 then Setspritecoloralpha(Tlos[x,y].ID,50) // resets visuals
if Tlos[x,y].Solid =1 then Setspritecoloralpha(Tlos[x,y].ID,255)
next y
next x
// change player position
if getpointerreleased()
x=(getpointerx()-(TLScalus/2))/(32-TLScalus) //-(TLScalus/2)
y=(getpointery()-(TLScalus/2))/(32-TLScalus)
setspriteposition(player,x*TLScalus,y*TLScalus)
endif
// nead to convert our players position to grid
plX=(getspritex(player)/TLScalus)
plY=(getspritey(player)/TLScalus)
for x =(plX-TLrange)-1 to (plX+TLrange)+1
for y =(plY-TLrange)-1 to (plY+TLrange)+1
if y=>1 and y<=31 // or make the array start at 1 to end+1...to save the if then every iteration
if x=>1 and x<=31
DistX#=plX-x+0.5*TLScalus
DistY#=plY-y+0.5*TLScalus
Dist#=sqrt(DistX#*DistX#+DistY#*DistY#) // take Manhattan distance here
if Dist#<=TLrange*TLScalus
if TLos_Calculation(plX,plY,x,y)=1
if Tlos[x,y].Solid =0 then Setspritecoloralpha(Tlos[x,y].ID,170) //Tile[0,x,y].Visible=1
if Tlos[x,y].Solid =1 then Setspritecoloralpha(Tlos[x,y].ID,230)
else
if Tlos[x,y].Solid =0 then Setspritecoloralpha(Tlos[x,y].ID,50) //Tile[0,x,y].Visible=0
endif
else
if Tlos[x,y].Solid =0 then Setspritecoloralpha(Tlos[x,y].ID,50) //Tile[0,x,y].Visible=0
endif
endif
endif
next y
next x
Print( ScreenFPS() )
Sync()
loop
// s = start e = end t = temp
function TLos_Calculation( sX as integer, sY as integer, eX as integer, eY as integer)
// if at start tile skip
if sX = eX and sY = eY then exitfunction 1
DistX as integer
DistY as integer
// calculate how many steps exist between start- and end-position on X and Y-axis of LOS
DistX = sX - eX
DistY = sY - eY
// convert the differences to absolute numbers and multiply them
AbsX = ABS(DistX)*2
AbsY = ABS(DistY)*2
// set the sign of the axis to be positive by default...
SgnX=1
SgnY=1
// ...but set it to be negative if the difference on the X-axis is less then Zero
if DistX < 0 then SgnX = - 1
if DistY < 0 then SgnY = - 1
tX as integer
tY as integer
tX = eX
tY = eY
// check to see if either the x- or Y-axis is dominant and loops accordingly
if AbsX > AbsY
Point = AbsY -( AbsX/2)
repeat
if Point >= 0
tY = tY + SgnY
Point = Point - AbsX
endif
tX = tX + SgnX
Point = Point + AbsY
// exit from function if we are at the player's position
if (tX=sX) and (tY=sY) then exitfunction 1
// keep checking until specified location is blocked by an non-transparent object
until Tlos[ tX , tY ].Solid = 1
exitfunction 0
else
Point = AbsX - ( AbsY/2 )
repeat
if Point >= 0
tX = tX + SgnX
Point = Point - AbsY
endif
tY = tY + SgnY
Point = Point + AbsX
if ( tX = sX ) and ( tY = sY ) then exitfunction 1
until Tlos[ tX , tY ].Solid = 1
exitfunction 0
endif
endfunction 0