Make it possible to place maps

This commit is contained in:
Nils Dagsson Moskopp 2022-05-21 21:42:46 +02:00
parent b7a5f69dc0
commit a4261aa369
Signed by untrusted user who does not match committer: erlehmann
GPG Key ID: A3BC671C35191080
1 changed files with 133 additions and 0 deletions

133
init.lua
View File

@ -741,12 +741,145 @@ minetest.register_globalstep(
end
)
minetest.register_entity(
"xmaps:map",
{
visual = "upright_sprite",
visual_size = { x = 1, y = 1 },
physical = false,
collide_with_objects = false,
textures = { "xmaps_map.tga" },
on_activate = function(self, staticdata)
if (
staticdata and
"" ~= staticdata
) then
local data = minetest.deserialize(staticdata)
if not data then
return
end
self._wallmounted = data._wallmounted
assert( self._wallmounted )
self._itemstring = data._itemstring
assert( self._itemstring )
local itemstack = ItemStack(self._itemstring)
local texture, itemstack = xmaps.load_map_item(itemstack)
self._itemstring = itemstack:to_string()
local min, max = -8/16, 8/16
local len = 1/64
local sbox
if 2 == self._wallmounted then
sbox = { -len, min, min, len, max, max }
elseif 3 == self._wallmounted then
sbox = { -len, min, min, len, max, max }
elseif 4 == self._wallmounted then
sbox = { min, min, -len, max, max, len }
elseif 5 == self._wallmounted then
sbox = { min, min, -len, max, max, len }
end
assert( sbox )
self.object:set_properties({
selectionbox = sbox,
textures = { texture },
})
local yaw = minetest.dir_to_yaw(
minetest.wallmounted_to_dir(
self._wallmounted
)
)
self.object:set_yaw(yaw)
end
end,
get_staticdata = function(self)
return minetest.serialize(
{
_wallmounted = self._wallmounted,
_itemstring = self._itemstring,
}
)
end,
on_punch = function(self)
-- TODO: implement protection
local pos = self.object:get_pos()
local itemstring = self._itemstring
if pos and itemstring then
minetest.add_item(
pos,
itemstring
)
self.object:remove()
end
end
}
)
minetest.register_craftitem(
"xmaps:map",
{
description = "Map",
inventory_image = "xmaps_map.tga",
groups = { not_in_creative_inventory = 1 },
on_place = function(itemstack, player, pointed_thing)
if "node" ~= pointed_thing.type then
return
end
local player_pos = player:get_pos()
if not player_pos then
return
end
local node_pos = pointed_thing.under
local direction = vector.normalize(
vector.subtract(
node_pos,
player_pos
)
)
local wallmounted = minetest.dir_to_wallmounted(
direction
)
-- TODO: implement maps on floor or ceiling
if wallmounted < 2 then
return
end
direction = minetest.wallmounted_to_dir(
wallmounted
)
local pos = vector.subtract(
node_pos,
vector.multiply(
direction,
1/2 + 1/256 -- avoid z-fighting
)
)
local itemstring = itemstack:to_string()
if pos and "" ~= itemstring then
local staticdata = {
_wallmounted = wallmounted,
_itemstring = itemstring,
}
local obj = minetest.add_entity(
pos,
"xmaps:map",
minetest.serialize(staticdata)
)
if obj then
-- TODO: creative mode
itemstack:take_item()
end
end
return itemstack
end
}
)