Added more borders. Fixed border render issue on edge cases with values equal to threshhold.

This commit is contained in:
wan-may 2023-09-03 22:15:01 -03:00
parent c6890d5ef5
commit ea91cc6bc9
2 changed files with 35 additions and 18 deletions

18
map.lua
View File

@ -42,10 +42,17 @@ function map.draw()
lg.clear( 0, 0, 0, 1 ) lg.clear( 0, 0, 0, 1 )
do --territory do --territory
lg.setColor( 1,1,1,1)
lg.setLineJoin( "none" )
lg.setLineWidth( 1 / Camera.zoom )
lg.replaceTransform( Camera.tfTerritory ) lg.replaceTransform( Camera.tfTerritory )
lg.setBlendMode( "add" ) lg.setBlendMode( "add" )
for k, v in pairs(map.territory) do for k, v in pairs(map.territory) do
if v.visible then v:draw() end if v.visible then
v:draw()
v:drawBorder( "land" )
v:drawBorder( "sea" )
end
end end
if map.sailable.visible then map.sailable:draw() end if map.sailable.visible then map.sailable:draw() end
lg.setBlendMode( "alpha" ) lg.setBlendMode( "alpha" )
@ -54,10 +61,15 @@ function map.draw()
end end
do --borders do --borders
lg.setColor( 1,1,1,1)
lg.setLineJoin( "none" ) lg.setLineJoin( "none" )
lg.setLineWidth( 1 / Camera.zoom ) lg.setLineWidth( 1 / Camera.zoom )
map.sailable:drawBorder()
lg.setColor( 1, 1, 1, 0.5)
map.sailable:drawBorder( "sailable" )
lg.setColor( 1, 0, 0, 0.5)
map.sailable:drawBorder( "placeable" )
end end

View File

@ -25,7 +25,11 @@ function t.load( filename, name )
if name == "sailable" then if name == "sailable" then
t.sailable = territory t.sailable = territory
t.computeBorder( territory, 20 / 255 ) t.computeBorder( territory, 20 / 255, "sailable" )
t.computeBorder( territory, 60 / 255, "placeable" )
else
t.computeBorder( territory, 60 / 255, "sea" )
t.computeBorder( territory, 130 / 255, "land" )
end end
return setmetatable( territory, {__index = t } ) return setmetatable( territory, {__index = t } )
end end
@ -90,17 +94,18 @@ function t.draw( territory )
lg.draw( territory.img ) lg.draw( territory.img )
end end
function t.drawBorder( territory ) function t.drawBorder( territory, key )
--lg.setColor( territory.colour ) key = key or "border"
for _, poly in ipairs( territory.border ) do for _, poly in ipairs( territory[key] ) do
lg.line( poly ) lg.line( poly )
end end
end end
function t.computeBorder( territory, threshold ) function t.computeBorder( territory, threshold, key )
territory.border = {} key = key or "border"
local border = territory.border territory[key] = {}
local border = territory[key]
local n = 1 local n = 1
for x = 0, 511 do for x = 0, 511 do
@ -112,25 +117,25 @@ function t.computeBorder( territory, threshold )
--Sample image and detect edge: --Sample image and detect edge:
local curValue = territory.imgd:getPixel( x, y ) local curValue = territory.imgd:getPixel( x, y )
local leftValue = territory.imgd:getPixel( math.min( x + 1, 511 ), y ) local rightValue = territory.imgd:getPixel( math.min( x + 1, 511 ), y )
local downValue = territory.imgd:getPixel( x, math.min( 284, y + 1 ) ) local downValue = territory.imgd:getPixel( x, math.min( 284, y + 1 ) )
local isLeftEdge = local isRightEdge =
((curValue >= threshold) and (leftValue < threshold)) or ((curValue >= threshold) and (rightValue < threshold)) or
((curValue <= threshold) and (leftValue > threshold)) ((curValue < threshold) and (rightValue >= threshold))
local isDownEdge = local isDownEdge =
((curValue >= threshold) and (downValue < threshold)) or ((curValue >= threshold) and (downValue < threshold)) or
((curValue <= threshold) and (downValue > threshold)) ((curValue < threshold) and (downValue >= threshold))
if isLeftEdge then if isRightEdge then
--print( "Left edge:", brx, bry, trx, try ) --print( "Left edge:", brx, bry, trx, try )
border[n] = { brx, bry, trx, try } border[n] = { x + 1, y, x + 1, y + 1 }
n = n + 1 n = n + 1
end end
if isDownEdge then if isDownEdge then
--print( "Down edge:", blx, bly, brx, bry ) --print( "Down edge:", blx, bly, brx, bry )
border[n] = { blx, bly, brx, bry } border[n] = { x, y + 1, x + 1, y + 1 }
n = n + 1 n = n + 1
end end
end end