movement and stuff, more data
This commit is contained in:
115
src/main.lua
115
src/main.lua
@@ -5,43 +5,63 @@ local screen_width, screen_height = love.graphics.getDimensions()
|
||||
local node_radius = 2
|
||||
local safe_zone = 12
|
||||
|
||||
-- panning / scaling control tuning
|
||||
local scale_adjust_speed = 2
|
||||
local translation_adjust_speed = 600
|
||||
local scale_fix_translation_adjustment = 300 -- this is wrong, and the wrong way to do this, but I don't know what the correct version is
|
||||
|
||||
|
||||
local map = (function()
|
||||
local file = io.open("map.json", "r")
|
||||
local text = file:read("*all")
|
||||
|
||||
local nodes, edges, minimums, maximums, scale, translation
|
||||
local function load_map(map_file_name)
|
||||
local file, text
|
||||
if type(map_file_name) == "string" then
|
||||
file = io.open(map_file_name, "r")
|
||||
if not file then
|
||||
nodes = {}
|
||||
edges = {}
|
||||
minimums = {0, 0}
|
||||
maximums = {100, 100}
|
||||
scale = 1
|
||||
translation = {0, 0}
|
||||
return
|
||||
end
|
||||
text = file:read("*all")
|
||||
else
|
||||
file = map_file_name
|
||||
file:open("r")
|
||||
text = file:read("string")
|
||||
end
|
||||
|
||||
local map = json.decode(text)
|
||||
return map
|
||||
end)()
|
||||
nodes = map.nodes
|
||||
edges = map.edges or {}
|
||||
|
||||
local nodes = map.nodes
|
||||
local edges = map.edges
|
||||
-- find map size
|
||||
minimums = {math.huge, math.huge} -- x, z
|
||||
maximums = {-math.huge, -math.huge} -- x, z
|
||||
for _, node in pairs(nodes) do
|
||||
if node[1] > maximums[1] then
|
||||
maximums[1] = node[1]
|
||||
end
|
||||
if node[1] < minimums[1] then
|
||||
minimums[1] = node[1]
|
||||
end
|
||||
if node[3] > maximums[2] then
|
||||
maximums[2] = node[3]
|
||||
end
|
||||
if node[3] < minimums[2] then
|
||||
minimums[2] = node[3]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
-- find map size
|
||||
local minimums = {math.huge, math.huge} -- x, z
|
||||
local maximums = {-math.huge, -math.huge} -- x, z
|
||||
for _, node in pairs(nodes) do
|
||||
if node[1] > maximums[1] then
|
||||
maximums[1] = node[1]
|
||||
end
|
||||
if node[1] < minimums[1] then
|
||||
minimums[1] = node[1]
|
||||
end
|
||||
if node[3] > maximums[2] then
|
||||
maximums[2] = node[3]
|
||||
end
|
||||
if node[3] < minimums[2] then
|
||||
minimums[2] = node[3]
|
||||
end
|
||||
-- find scale factor, translate
|
||||
local distance_x = maximums[1] - minimums[1]
|
||||
local distance_z = maximums[2] - minimums[2]
|
||||
scale = math.min((screen_width - safe_zone * 2) / distance_x, (screen_height - safe_zone * 2) / distance_z)
|
||||
translation = {-minimums[1] + safe_zone / scale, -minimums[2] + safe_zone / scale}
|
||||
end
|
||||
|
||||
-- find scale factor, translate
|
||||
local distance_x = maximums[1] - minimums[1]
|
||||
local distance_z = maximums[2] - minimums[2]
|
||||
local scale = math.min((screen_width - safe_zone * 2) / distance_x, (screen_height - safe_zone * 2) / distance_z)
|
||||
local translation = {-minimums[1] + safe_zone / scale, -minimums[2] + safe_zone / scale}
|
||||
load_map("map.json")
|
||||
|
||||
|
||||
|
||||
@@ -101,6 +121,39 @@ function love.draw()
|
||||
love.graphics.print(name, text_x, text_y, 0, 1 / scale, 1 / scale)
|
||||
end
|
||||
end
|
||||
|
||||
-- TEMP center of map
|
||||
-- love.graphics.setColor(1, 1, 1, 1)
|
||||
-- love.graphics.circle("fill", minimums[1] + (maximums[1] - minimums[1]) / 2, minimums[2] + (maximums[2] - minimums[2]) / 2, node_radius / scale)
|
||||
end
|
||||
|
||||
function love.update(dt)
|
||||
if love.keyboard.isDown("w") then
|
||||
translation[2] = translation[2] + translation_adjust_speed / scale * dt
|
||||
end
|
||||
if love.keyboard.isDown("s") then
|
||||
translation[2] = translation[2] - translation_adjust_speed / scale * dt
|
||||
end
|
||||
if love.keyboard.isDown("a") then
|
||||
translation[1] = translation[1] + translation_adjust_speed / scale * dt
|
||||
end
|
||||
if love.keyboard.isDown("d") then
|
||||
translation[1] = translation[1] - translation_adjust_speed / scale * dt
|
||||
end
|
||||
if love.keyboard.isDown("=") then
|
||||
scale = scale + scale * scale_adjust_speed * dt
|
||||
translation[1] = translation[1] - scale_fix_translation_adjustment * scale_adjust_speed * dt
|
||||
translation[2] = translation[2] - scale_fix_translation_adjustment * scale_adjust_speed * dt
|
||||
end
|
||||
if love.keyboard.isDown("-") then
|
||||
scale = scale - scale * scale_adjust_speed * dt
|
||||
translation[1] = translation[1] + scale_fix_translation_adjustment * scale_adjust_speed * dt
|
||||
translation[2] = translation[2] + scale_fix_translation_adjustment * scale_adjust_speed * dt
|
||||
end
|
||||
end
|
||||
|
||||
function love.filedropped(file)
|
||||
load_map(file)
|
||||
end
|
||||
|
||||
function love.keypressed(key)
|
||||
|
||||
Reference in New Issue
Block a user