dcearth/main.lua

98 lines
3.1 KiB
Lua
Raw Normal View History

2023-04-28 17:35:52 +00:00
local love = assert( love, "This tool requires LOVE: love2d.org" )
local map = require 'map'
local SAVEDIRECTORY = "out/"
local Camera = require 'camera'
local wasKeyPressed
2023-04-28 17:35:52 +00:00
function love.load()
2023-04-28 17:35:52 +00:00
local lfs = assert( love.filesystem )
lfs.setIdentity( "dcearth", false )
assert( lfs.createDirectory( SAVEDIRECTORY.."data/earth" ))
assert( lfs.createDirectory( SAVEDIRECTORY.."data/graphics" ))
map.load()
love.graphics.setNewFont( 12, "mono" )
2023-04-28 17:35:52 +00:00
end
function love.update( dt )
local tx, ty = 0, 0
local moveCamera = false
if love.keyboard.isScancodeDown( "w" ) then moveCamera = true; ty = ty + 1 end
if love.keyboard.isScancodeDown( "a" ) then moveCamera = true; tx = tx - 1 end
if love.keyboard.isScancodeDown( "s" ) then moveCamera = true; ty = ty - 1 end
if love.keyboard.isScancodeDown( "d" ) then moveCamera = true; tx = tx + 1 end
if moveCamera then Camera.Translate( tx, ty ) end
2023-04-28 17:35:52 +00:00
end
2023-04-28 17:35:52 +00:00
function love.draw()
love.graphics.push( "all" )
map.draw()
love.graphics.pop()
--Status bar.
local x, y = love.mouse.getPosition()
local wx, wy = Camera.GetWorldCoordinate( x, y )
local h = love.graphics.getHeight() - 30
love.graphics.setColor( 0.2, 0.1, 0.1, 0.5 )
love.graphics.rectangle( "fill", 0, h, love.graphics.getWidth() / 2, 30 )
love.graphics.setColor( 1, 1, 1, 1 )
love.graphics.rectangle( "line", 0, h, love.graphics.getWidth() / 2, 30 )
love.graphics.print(("SCREEN\t%d\t%d\nWORLD \t%5.2f\t%5.2f"):format(x, y, wx, wy), 0, h)
--Edit box.
love.graphics.rectangle( "line", love.graphics.getWidth() / 2, h, love.graphics.getWidth() / 2, 30 )
if map.cities.selectedCity then
local c = map.cities.selectedCity
2023-07-28 05:42:16 +00:00
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( ("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 )
end
end
function love.resize(w, h)
Camera.Resize( w, h )
end
function love.wheelmoved(x, y)
Camera.Zoom( (y > 0) and true or false )
end
function love.mousepressed( x, y, button, istouch, presses )
local wx, wy = Camera.GetWorldCoordinate( x, y )
2023-07-28 05:42:16 +00:00
if button == 1 then
map.cities.lockSelection()
else
map.cities.unlockSelection()
end
print( ("MOUSE\tx %f\ty %f\twx %f\twy %f"):format(x, y, wx, wy) )
end
function love.mousemoved( x, y, dx, dy, istouch )
map.cities.selectNearestCity( Camera.GetWorldCoordinate( x, y ) )
2023-04-28 17:35:52 +00:00
end
function love.keypressed(key)
if key == "l" then
2023-04-28 17:35:52 +00:00
-- To open a file or folder, "file://" must be prepended to the path.
love.system.openURL("file://"..love.filesystem.getSaveDirectory())
end
2023-07-28 05:42:16 +00:00
if key == "1" then
map.coastlines.visible = not map.coastlines.visible
end
if key == "2" then
map.coastlinesLow.visible = not map.coastlinesLow.visible
end
if key == "3" then
map.international.visible = not map.international.visible
end
wasKeyPressed = true
2023-04-28 17:35:52 +00:00
end