techage/ta2_energy_storage/ta2_rope.lua

86 lines
2.0 KiB
Lua
Raw Permalink Normal View History

2021-06-14 22:36:58 +03:00
--[[
TechAge
=======
Copyright (C) 2019-2021 Joachim Stolberg
GPL v3
See LICENSE.txt for more information
2021-06-16 23:51:30 +03:00
Rope for TA2 gravity-based energy storage
2021-06-14 22:36:58 +03:00
]]--
local Entities = {}
-- Return first pos after start pos and the destination pos
2021-06-16 23:51:30 +03:00
local function get_positions(pos, length, force)
2021-06-14 22:36:58 +03:00
local pos1 = {x = pos.x, y = pos.y - 1, z = pos.z} -- start pos
2021-06-16 23:51:30 +03:00
local pos2 = {x = pos.x, y = pos.y - 1 - length, z = pos.z} -- end pos
if force then
return pos1, pos2 -- force given length
end
2021-06-14 22:36:58 +03:00
local _, pos3 = minetest.line_of_sight(pos1, pos2)
return pos1, pos3 or pos2 -- new values
end
local function del_rope(pos)
local key = minetest.hash_node_position(pos)
local rope = Entities[key]
if rope then
rope:remove()
Entities[key] = nil
end
return key
end
local function add_rope(pos, pos1, pos2)
local key = del_rope(pos)
pos1.y = pos1.y + 0.5 -- from
2021-06-16 23:51:30 +03:00
pos2.y = pos2.y - 0.5 -- to
2021-06-14 22:36:58 +03:00
local pos3 = {x = pos1.x, y = (pos1.y + pos2.y) / 2, z = pos1.z} -- mid-pos
local length = math.abs(pos1.y - pos2.y)
2022-01-03 23:40:31 +03:00
2021-06-14 22:36:58 +03:00
local rope = minetest.add_entity(pos3, "techage:ta2_rope")
if rope then
2021-06-16 23:51:30 +03:00
rope:set_properties({visual_size = {x = 0.06, y = length}, collisionbox = {x = 0.06, y = length}})
2021-06-14 22:36:58 +03:00
end
Entities[key] = rope
end
minetest.register_entity("techage:ta2_rope", {
initial_properties = {
visual = "cube",
textures = {
"techage_rope.png",
"techage_rope.png",
"techage_rope.png",
"techage_rope.png",
"techage_rope.png",
"techage_rope.png",
},
use_texture_alpha = false,
2021-06-16 23:51:30 +03:00
physical = true,
collide_with_objects = true,
pointable = true,
2021-06-14 22:36:58 +03:00
static_save = false,
2021-06-16 23:51:30 +03:00
visual_size = {x = 0.06, y = 10, z = 0.06},
2021-06-14 22:36:58 +03:00
shaded = true,
},
})
-------------------------------------------------------------------------------
-- API functions
-------------------------------------------------------------------------------
2021-06-16 23:51:30 +03:00
function techage.renew_rope(pos, length, force)
local pos1, pos2 = get_positions(pos, length, force)
2021-06-14 22:36:58 +03:00
if pos1 then
add_rope(pos, pos1, pos2)
return pos1, pos2
end
end
techage.del_rope = del_rope