From 880811262b9e511a1e98f8b060a3e41f3de70279 Mon Sep 17 00:00:00 2001 From: Tangent Date: Wed, 5 Nov 2025 01:08:13 -0700 Subject: [PATCH] movement and stuff, more data --- ReadMe.md | 7 +-- map.json | 9 +++- src/main.lua | 115 ++++++++++++++++++++++++++++++++++++-------------- test map.json | 9 ++++ 4 files changed, 104 insertions(+), 36 deletions(-) create mode 100644 test map.json diff --git a/ReadMe.md b/ReadMe.md index 6f6ca34..ea5b63c 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -8,10 +8,11 @@ Each node is an `x`, `y`, and `z` position. The optional 4th item is `true` to display the node's name, a string to display *that* as the node name, or `false` to completely hide a node. `edges` is an array of arrays of two names of nodes. An optional 3rd item can be set to `false` to hide an edge, or a string for a -custom label. +custom label. `edges` can be nonexistent. -- TODO: Make drag and drop loading of a JSON file possible. :D -- TODO: Make it not error with no map file. +WASD panning and -/+ to zoom. + +You can drag and drop a JSON file to load it. ## Example JSON diff --git a/map.json b/map.json index b6fbde8..6b5e163 100644 --- a/map.json +++ b/map.json @@ -25,7 +25,9 @@ "Ruined Nether Portal": [853, 79, 805, true], "Trickster Waterfall": [439, 81, 743, true], "Trickster Waterfall (bottom)": [441, 62, 730, false], - "village crossroad": [130, 101, 296, false, {"notes": "insignificant"}] + "village crossroad": [130, 101, 296, false, {"notes": "insignificant"}], + "Second Farm original route corner": [-180, 69, -30, false], + "Second Farm original route corner 2": [-202, 79, -169, false] }, "edges": [ ["tree farm north", "Second Farm"], @@ -45,6 +47,9 @@ ["Warehouse/Mine", "skeleton spawner (1st mine)", false], ["Great Rock Waterfall", "Great Rock Waterfall (bottom)"], ["waterfall 2 top", "waterfall 2 bottom"], - ["Trickster Waterfall", "Trickster Waterfall (bottom)", ""] + ["Trickster Waterfall", "Trickster Waterfall (bottom)", ""], + ["farm paths diverge point", "Second Farm original route corner"], + ["Second Farm original route corner", "Second Farm original route corner 2"], + ["Second Farm original route corner 2", "Second Farm"] ] } diff --git a/src/main.lua b/src/main.lua index fe6118d..ab9a75a 100644 --- a/src/main.lua +++ b/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) diff --git a/test map.json b/test map.json new file mode 100644 index 0000000..5d7361d --- /dev/null +++ b/test map.json @@ -0,0 +1,9 @@ +{ + "nodes": { + "TopLeft": [0, 0, 0, true], + "TopRight": [1600, 0, 0, true], + "BottomLeft": [0, 0, 900, true], + "BottomRight": [1600, 0, 900, true] + }, + "edges": [] +}