From 5de95942011a26768c94d85641395a776ff12eb1 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Sun, 28 Apr 2024 20:13:19 +0200 Subject: [PATCH] marker util --- init.lua | 1 + markers.lua | 38 ++++++++++++++++++++++++++++++++++++++ preview.lua | 3 ++- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 markers.lua diff --git a/init.lua b/init.lua index 93e135f..e32d27f 100644 --- a/init.lua +++ b/init.lua @@ -13,6 +13,7 @@ dofile(MP .. "/entity.lua") dofile(MP .. "/preview.lua") dofile(MP .. "/api.lua") dofile(MP .. "/common.lua") +dofile(MP .. "/markers.lua") dofile(MP .. "/placements/mapblock_lib.lua") dofile(MP .. "/placements/dummy.lua") dofile(MP .. "/conditions.lua") diff --git a/markers.lua b/markers.lua new file mode 100644 index 0000000..e60997c --- /dev/null +++ b/markers.lua @@ -0,0 +1,38 @@ + +-- type -> texture-name +local textures = { + arrow = "building_lib_arrow.png" +} + +-- name -> vector +-- assuming texture points upwards originally +local rotations = { + ["z-"] = { x=math.pi/2, y=0, z=0 }, + ["z+"] = { x=math.pi/2, y=0, z=math.pi }, + ["x+"] = { x=math.pi/2, y=0, z=math.pi/2 }, + ["x-"] = { x=math.pi/2, y=0, z=-math.pi/2 } +} + +function building_lib.create_marker(type, opts) + -- apply sane defaults + if not textures[type] then + type = "arrow" + end + opts = opts or {} + opts.pos = opts.pos or {} + opts.pos.x = opts.pos.x or 0 + opts.pos.y = opts.pos.y or 0 + opts.pos.z = opts.pos.z or 0 + opts.size = opts.size or 10 + + if not rotations[opts.rotation] then + opts.rotation = "z-" + end + + return { + texture = textures[type], + position = opts.pos, + rotation = rotations[opts.rotation], + size = {x=opts.size, y=opts.size} + } +end \ No newline at end of file diff --git a/preview.lua b/preview.lua index 71b9fff..7fee811 100644 --- a/preview.lua +++ b/preview.lua @@ -48,7 +48,8 @@ function building_lib.show_preview(playername, texture, color, building_def, map local unrotated_size = building_lib.get_building_size(building_def, 360 - rotation) for _, marker in ipairs(building_def.markers) do - local rotated_position = mapblock_lib.rotate_pos(marker.position, unrotated_size, rotation) + local center_rel_pos = vector.add(marker.position, 0.5) + local rotated_position = mapblock_lib.rotate_pos(center_rel_pos, unrotated_size, rotation) local node_pos = vector.multiply(vector.add(mapblock_pos1, rotated_position), 16) node_pos = vector.subtract(node_pos, 0.5) local z_rotation = marker.rotation.z