Display sailable border.
This commit is contained in:
parent
f30f27bfb1
commit
c6890d5ef5
19
cities.lua
19
cities.lua
|
@ -11,25 +11,34 @@ local cities
|
||||||
local points = {}
|
local points = {}
|
||||||
local caps = {}
|
local caps = {}
|
||||||
|
|
||||||
t.selectedCity = nil
|
t.selected = nil
|
||||||
|
t.selectionLocked = false
|
||||||
|
|
||||||
|
function t.lockSelection()
|
||||||
|
t.selectionLocked = true
|
||||||
|
end
|
||||||
|
|
||||||
|
function t.unlockSelection()
|
||||||
|
t.selectionLocked = false
|
||||||
|
end
|
||||||
|
|
||||||
function t.draw()
|
function t.draw()
|
||||||
if cities.visible then lg.points( points ) end
|
if cities.visible then lg.points( points ) end
|
||||||
end
|
end
|
||||||
|
|
||||||
function t.drawSelected( r )
|
function t.drawSelected( r )
|
||||||
if not t.visible then return end
|
if not cities.visible then return end
|
||||||
local c = t.selectedCity
|
local c = t.selected
|
||||||
if not c then return end
|
if not c then return end
|
||||||
lg.circle( "fill", c.x, c.y, r )
|
lg.circle( "fill", c.x, c.y, r )
|
||||||
end
|
end
|
||||||
|
|
||||||
function t.drawCapitals()
|
function t.drawCapitals()
|
||||||
if t.visible then lg.points( caps ) end
|
if cities.visible then lg.points( caps ) end
|
||||||
end
|
end
|
||||||
|
|
||||||
function t.selectNearestCity(x, y)
|
function t.selectNearestCity(x, y)
|
||||||
t.selectedCity = cities:getClosestPoint(x, y)
|
if not t.selectionLocked then t.selected = cities:getClosestPoint(x, y) end
|
||||||
end
|
end
|
||||||
|
|
||||||
function t.load( filename )
|
function t.load( filename )
|
||||||
|
|
20
main.lua
20
main.lua
|
@ -50,14 +50,30 @@ function love.draw()
|
||||||
|
|
||||||
--Edit box.
|
--Edit box.
|
||||||
love.graphics.rectangle( "line", love.graphics.getWidth() / 2, h, love.graphics.getWidth() / 2, 30 )
|
love.graphics.rectangle( "line", love.graphics.getWidth() / 2, h, love.graphics.getWidth() / 2, 30 )
|
||||||
if map.cities.selectedCity then
|
if map.cities.selected then
|
||||||
local c = map.cities.selectedCity
|
local c = map.cities.selected
|
||||||
love.graphics.setColor( 0.2, 0.1, 0.1, 0.5 )
|
love.graphics.setColor( 0.2, 0.1, 0.1, 0.5 )
|
||||||
love.graphics.rectangle( "fill", 0, 0, 150 ,100 )
|
love.graphics.rectangle( "fill", 0, 0, 150 ,100 )
|
||||||
love.graphics.setColor( 1, 1, 1, 1 )
|
love.graphics.setColor( 1, 1, 1, 1 )
|
||||||
love.graphics.rectangle( "line", 0, 0, 150 ,100 )
|
love.graphics.rectangle( "line", 0, 0, 150 ,100 )
|
||||||
love.graphics.setColor( 1.2, 1.1, 1.1, 1.5 )
|
love.graphics.setColor( 1.2, 1.1, 1.1, 1.5 )
|
||||||
love.graphics.print( ("NAME: %s\nX: %3.2f\nY: %3.2f\nPOP: %d\nCAPITAL: %s\nCOUNTRY: %s"):format(c.name, c.x, c.y, c.pop, tostring(c.capital), c.country), 0, 0 )
|
love.graphics.print( ("NAME: %s\nX: %3.2f\nY: %3.2f\nPOP: %d\nCAPITAL: %s\nCOUNTRY: %s"):format(c.name, c.x, c.y, c.pop, tostring(c.capital), c.country), 0, 0 )
|
||||||
|
elseif map.travelnodes.selected then
|
||||||
|
local c = map.travelnodes.selected
|
||||||
|
love.graphics.setColor( 0.2, 0.1, 0.1, 0.5 )
|
||||||
|
love.graphics.rectangle( "fill", 0, 0, 150 ,100 )
|
||||||
|
love.graphics.setColor( 1, 1, 1, 1 )
|
||||||
|
love.graphics.rectangle( "line", 0, 0, 150 ,100 )
|
||||||
|
love.graphics.setColor( 1.2, 1.1, 1.1, 1.5 )
|
||||||
|
love.graphics.print( ("Node: %d\nX: %3.2f\nY: %3.2f\n"):format(c.number, c.x, c.y) )
|
||||||
|
elseif map.ainodes.selectedNode then
|
||||||
|
local c = map.ainodes.selected
|
||||||
|
love.graphics.setColor( 0.2, 0.1, 0.1, 0.5 )
|
||||||
|
love.graphics.rectangle( "fill", 0, 0, 150 ,100 )
|
||||||
|
love.graphics.setColor( 1, 1, 1, 1 )
|
||||||
|
love.graphics.rectangle( "line", 0, 0, 150 ,100 )
|
||||||
|
love.graphics.setColor( 1.2, 1.1, 1.1, 1.5 )
|
||||||
|
love.graphics.print( ("Node: %d\nX: %3.2f\nY: %3.2f\noffensive: %s"):format(c.number, c.x, c.y, c.attack) )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
13
map.lua
13
map.lua
|
@ -49,6 +49,15 @@ function map.draw()
|
||||||
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" )
|
||||||
|
|
||||||
|
lg.setColor( 1, 1, 1, 1 )
|
||||||
|
end
|
||||||
|
|
||||||
|
do --borders
|
||||||
|
lg.setColor( 1,1,1,1)
|
||||||
|
lg.setLineJoin( "none" )
|
||||||
|
lg.setLineWidth( 1 / Camera.zoom )
|
||||||
|
map.sailable:drawBorder()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -66,8 +75,8 @@ function map.draw()
|
||||||
lg.setPointSize( 1.0 * Camera.zoom )
|
lg.setPointSize( 1.0 * Camera.zoom )
|
||||||
map.cities.drawCapitals()
|
map.cities.drawCapitals()
|
||||||
|
|
||||||
lg.setColor( 1, 0, 1, 0.5 )
|
lg.setColor( 1, 1, 1, 0.5 )
|
||||||
map.cities.drawSelected( 22.0 / Camera.zoom )
|
map.cities.drawSelected( 15.0 / Camera.zoom )
|
||||||
|
|
||||||
map.ainodes:draw()
|
map.ainodes:draw()
|
||||||
end
|
end
|
||||||
|
|
11
nodes.lua
11
nodes.lua
|
@ -25,6 +25,17 @@ local function isConnected( startNode, endNode )
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function t.getClosest( nodes, x, y )
|
||||||
|
local d = math.huge
|
||||||
|
local closestNode
|
||||||
|
for _, node in pairs( nodes.nodes ) do
|
||||||
|
local nx, ny = node.x, node.y
|
||||||
|
local nd = (nx - x) * (nx - x) + (ny - y) * (ny - y)
|
||||||
|
if nd < d then d = nd; closestNode = node end
|
||||||
|
end
|
||||||
|
return closestNode
|
||||||
|
end
|
||||||
|
|
||||||
function t.load( filename, sailable )
|
function t.load( filename, sailable )
|
||||||
|
|
||||||
isSailable = sailable
|
isSailable = sailable
|
||||||
|
|
|
@ -18,15 +18,28 @@ function t.load( filename, name )
|
||||||
visible = true,
|
visible = true,
|
||||||
name = name,
|
name = name,
|
||||||
colour = colours[name],
|
colour = colours[name],
|
||||||
|
border = {},
|
||||||
img = img,
|
img = img,
|
||||||
imgd = imgd
|
imgd = imgd
|
||||||
}
|
}
|
||||||
|
|
||||||
if name == "sailable" then t.sailable = territory end
|
if name == "sailable" then
|
||||||
|
t.sailable = territory
|
||||||
|
t.computeBorder( territory, 20 / 255 )
|
||||||
|
end
|
||||||
return setmetatable( territory, {__index = t } )
|
return setmetatable( territory, {__index = t } )
|
||||||
end
|
end
|
||||||
|
|
||||||
--World space coordinate.
|
--World space coordinate.
|
||||||
|
local function ToWorld( x, y )
|
||||||
|
return x * 360 / 512 - 180, y * 200 / 285 - 100
|
||||||
|
end
|
||||||
|
|
||||||
|
local function ToPixel( x, y )
|
||||||
|
return (x + 180) * 512 / 360, 285 - ( y + 100 ) * 285 / 200
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function t.isValid( x, y )
|
function t.isValid( x, y )
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -59,11 +72,17 @@ function t.isSailable( x, y )
|
||||||
end
|
end
|
||||||
|
|
||||||
function t.canPlaceShip( territory, x, y )
|
function t.canPlaceShip( territory, x, y )
|
||||||
|
local water = assert( t.sailable )
|
||||||
|
if water:getPixel( x, y ) > 60 / 255
|
||||||
|
and territory:getPixel( x, y ) > 60 / 255 then return true end
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function t.canPlaceLand( territory, x, y )
|
function t.canPlaceLand( territory, x, y )
|
||||||
|
local water = assert( t.sailable )
|
||||||
|
if water:getPixel( x, y ) < 60 / 255
|
||||||
|
and territory:getPixel( x, y ) > 130 / 255 then return true end
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function t.draw( territory )
|
function t.draw( territory )
|
||||||
|
@ -71,6 +90,53 @@ function t.draw( territory )
|
||||||
lg.draw( territory.img )
|
lg.draw( territory.img )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function t.drawBorder( territory )
|
||||||
|
--lg.setColor( territory.colour )
|
||||||
|
for _, poly in ipairs( territory.border ) do
|
||||||
|
lg.line( poly )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function t.computeBorder( territory, threshold )
|
||||||
|
territory.border = {}
|
||||||
|
local border = territory.border
|
||||||
|
local n = 1
|
||||||
|
|
||||||
|
for x = 0, 511 do
|
||||||
|
for y = 0, 284 do
|
||||||
|
--Bottom left, bottom right, and top right of pixel in image coordinates:
|
||||||
|
local blx, bly = x, y + 1
|
||||||
|
local brx, bry = x + 1, y + 1
|
||||||
|
local trx, try = x + 1, y
|
||||||
|
|
||||||
|
--Sample image and detect edge:
|
||||||
|
local curValue = territory.imgd:getPixel( x, y )
|
||||||
|
local leftValue = territory.imgd:getPixel( math.min( x + 1, 511 ), y )
|
||||||
|
local downValue = territory.imgd:getPixel( x, math.min( 284, y + 1 ) )
|
||||||
|
|
||||||
|
local isLeftEdge =
|
||||||
|
((curValue >= threshold) and (leftValue < threshold)) or
|
||||||
|
((curValue <= threshold) and (leftValue > threshold))
|
||||||
|
local isDownEdge =
|
||||||
|
((curValue >= threshold) and (downValue < threshold)) or
|
||||||
|
((curValue <= threshold) and (downValue > threshold))
|
||||||
|
|
||||||
|
if isLeftEdge then
|
||||||
|
--print( "Left edge:", brx, bry, trx, try )
|
||||||
|
border[n] = { brx, bry, trx, try }
|
||||||
|
n = n + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
if isDownEdge then
|
||||||
|
--print( "Down edge:", blx, bly, brx, bry )
|
||||||
|
border[n] = { blx, bly, brx, bry }
|
||||||
|
n = n + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function t.drawConnections( nodes )
|
function t.drawConnections( nodes )
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue