Add basic 2x2 Drawers

They're still missing some functionality as pipeworks and drops after dug.
This will be done in the next commit(s).
master
LNJ 2017-04-02 21:29:32 +02:00
parent 06beee4652
commit d3d29fb497
No known key found for this signature in database
GPG Key ID: 69268DBD835B6B0B
7 changed files with 158 additions and 48 deletions

View File

@ -32,7 +32,8 @@ License of media:
-----------------
Copyright (C) 2014 Justin Aquadro (MIT):
textures/drawers_wood.png
textures/drawers_wood_front.png
textures/drawers_wood_front_1.png
textures/drawers_wood_front_4.png
Everything not listed in here:
Copyright (C) 2017 LNJ <git@lnj.li> (CC0 1.0)

View File

@ -59,8 +59,10 @@ dofile(modpath .. "/lua/api.lua")
drawers.register_drawer("drawers:wood", {
description = "Wooden Drawer",
tiles = {"drawers_wood.png", "drawers_wood.png", "drawers_wood.png",
"drawers_wood.png", "drawers_wood.png", "drawers_wood_front.png"},
tiles1 = {"drawers_wood.png", "drawers_wood.png", "drawers_wood.png",
"drawers_wood.png", "drawers_wood.png", "drawers_wood_front_1.png"},
tiles4 = {"drawers_wood.png", "drawers_wood.png", "drawers_wood.png",
"drawers_wood.png", "drawers_wood.png", "drawers_wood_front_4.png"},
groups = {choppy = 3, oddly_breakable_by_hand = 2},
sounds = drawers.WOOD_SOUNDS,
drawer_stack_max_factor = 3 * 8, -- normal chest size

View File

@ -37,33 +37,40 @@ drawers.node_box_simple = {
function drawers.drawer_on_construct(pos)
local node = core.get_node(pos)
local ndef = core.registered_nodes[node.name]
local drawerType = ndef.groups.drawer
local base_stack_max = core.nodedef_default.stack_max or 99
local stack_max_factor = ndef.drawer_stack_max_factor or 24 -- 3x8
stack_max_factor = math.floor(stack_max_factor / drawerType) -- drawerType => number of drawers in node
-- meta
local meta = core.get_meta(pos)
meta:set_string("name", "")
meta:set_int("count", 0)
meta:set_int("max_count", base_stack_max * stack_max_factor)
meta:set_int("stack_max_factor", stack_max_factor)
meta:set_int("base_stack_max", base_stack_max)
meta:set_string("entity_infotext", drawers.gen_info_text("Empty", 0,
stack_max_factor, base_stack_max))
i = 1
while i <= drawerType do
meta:set_string("name"..i, "")
meta:set_int("count"..i, 0)
meta:set_int("max_count"..i, base_stack_max * stack_max_factor)
meta:set_int("base_stack_max"..i, base_stack_max)
meta:set_string("entity_infotext"..i, drawers.gen_info_text("Empty", 0,
stack_max_factor, base_stack_max))
meta:set_int("stack_max_factor"..i, stack_max_factor)
i = i + 1
end
drawers.spawn_visual(pos)
end
-- destruct drawer
function drawers.drawer_on_destruct(pos)
local objs = core.get_objects_inside_radius(pos, 0.5)
if objs then
for _, obj in pairs(objs) do
if obj and obj:get_luaentity() and
obj:get_luaentity().name == "drawers:visual" then
obj:remove()
return
end
local objs = core.get_objects_inside_radius(pos, 0.537)
if not objs then return end
for _, obj in pairs(objs) do
if obj and obj:get_luaentity() and
obj:get_luaentity().name == "drawers:visual" then
obj:remove()
end
end
end
@ -110,7 +117,6 @@ function drawers.register_drawer(name, def)
def.paramtype2 = "facedir"
def.legacy_facedir_simple = true
def.groups = def.groups or {}
def.groups.drawer = def.groups.drawer or 1
def.drawer_stack_max_factor = def.drawer_stack_max_factor or 24
-- events
@ -134,7 +140,23 @@ function drawers.register_drawer(name, def)
def.after_dig_node = pipeworks.after_dig
end
core.register_node(name, def)
-- normal drawer 1x1 = 1
def1 = table.copy(def)
def1.tiles = def.tiles or def.tiles1
def1.tiles1 = nil
def1.tiles4 = nil
def1.groups.drawer = 1
core.register_node(name .. "1", def1)
core.register_alias(name, name .. "1") -- 1x1 drawer is the default one
-- 2x2 = 4
def4 = table.copy(def)
def4.description = def4.description .. " (2x2)"
def4.tiles = def.tiles4
def4.tiles1 = nil
def4.tiles4 = nil
def4.groups.drawer = 4
core.register_node(name .. "4", def4)
if (not def.no_craft) and def.material then
core.register_craft({

View File

@ -68,20 +68,84 @@ end
function drawers.spawn_visual(pos)
local node = core.get_node(pos)
local ndef = core.registered_nodes[node.name]
local drawerType = ndef.groups.drawer
-- data for the new visual
drawers.last_drawer_pos = pos
drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name"))
drawers.last_drawer_type = drawerType
local bdir = core.facedir_to_dir(node.param2)
local fdir = vector.new(-bdir.x, 0, -bdir.z)
local pos2 = vector.add(pos, vector.multiply(fdir, 0.438))
if drawerType == 1 then -- 1x1 drawer
drawers.last_visual_id = ""
drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name"))
obj = core.add_entity(pos2, "drawers:visual")
local bdir = core.facedir_to_dir(node.param2)
local fdir = vector.new(-bdir.x, 0, -bdir.z)
local pos2 = vector.add(pos, vector.multiply(fdir, 0.438))
if bdir.x < 0 then obj:setyaw(0.5 * math.pi) end
if bdir.z < 0 then obj:setyaw(math.pi) end
if bdir.x > 0 then obj:setyaw(1.5 * math.pi) end
obj = core.add_entity(pos2, "drawers:visual")
drawers.last_texture = nil
if bdir.x < 0 then obj:setyaw(0.5 * math.pi) end
if bdir.z < 0 then obj:setyaw(math.pi) end
if bdir.x > 0 then obj:setyaw(1.5 * math.pi) end
drawers.last_texture = nil
else -- 2x2 drawer
local bdir = core.facedir_to_dir(node.param2)
local fdir1
local fdir2
local fdir3
local fdir4
if node.param2 == 2 then
fdir1 = vector.new(-bdir.x + 0.5, 0.5, -bdir.z)
fdir2 = vector.new(-bdir.x - 0.5, 0.5, -bdir.z)
fdir3 = vector.new(-bdir.x + 0.5, -0.5, -bdir.z)
fdir4 = vector.new(-bdir.x - 0.5, -0.5, -bdir.z)
elseif node.param2 == 0 then
fdir1 = vector.new(-bdir.x - 0.5, 0.5, -bdir.z)
fdir2 = vector.new(-bdir.x + 0.5, 0.5, -bdir.z)
fdir3 = vector.new(-bdir.x - 0.5, -0.5, -bdir.z)
fdir4 = vector.new(-bdir.x + 0.5, -0.5, -bdir.z)
elseif node.param2 == 1 then
fdir1 = vector.new(-bdir.x, 0.5, -bdir.z + 0.5)
fdir2 = vector.new(-bdir.x, 0.5, -bdir.z - 0.5)
fdir3 = vector.new(-bdir.x, -0.5, -bdir.z + 0.5)
fdir4 = vector.new(-bdir.x, -0.5, -bdir.z - 0.5)
else
fdir1 = vector.new(-bdir.x, 0.5, -bdir.z - 0.5)
fdir2 = vector.new(-bdir.x, 0.5, -bdir.z + 0.5)
fdir3 = vector.new(-bdir.x, -0.5, -bdir.z - 0.5)
fdir4 = vector.new(-bdir.x, -0.5, -bdir.z + 0.5)
end
objs = {}
drawers.last_visual_id = 1
drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name1"))
local pos1 = vector.add(pos, vector.multiply(fdir1, 0.438))
objs[1] = core.add_entity(pos1, "drawers:visual")
drawers.last_visual_id = 2
drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name2"))
local pos2 = vector.add(pos, vector.multiply(fdir2, 0.438))
objs[2] = core.add_entity(pos2, "drawers:visual")
drawers.last_visual_id = 3
drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name3"))
local pos3 = vector.add(pos, vector.multiply(fdir3, 0.438))
objs[3] = core.add_entity(pos3, "drawers:visual")
drawers.last_visual_id = 4
drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name4"))
local pos4 = vector.add(pos, vector.multiply(fdir4, 0.438))
objs[4] = core.add_entity(pos4, "drawers:visual")
for i,obj in pairs(objs) do
if bdir.x < 0 then obj:setyaw(0.5 * math.pi) end
if bdir.z < 0 then obj:setyaw(math.pi) end
if bdir.x > 0 then obj:setyaw(1.5 * math.pi) end
end
end
end

View File

@ -43,7 +43,9 @@ core.register_entity("drawers:visual", {
drawer_posx = self.drawer_pos.x,
drawer_posy = self.drawer_pos.y,
drawer_posz = self.drawer_pos.z,
texture = self.texture
texture = self.texture,
drawerType = self.drawerType,
visualId = self.visualId
})
end,
@ -57,9 +59,13 @@ core.register_entity("drawers:visual", {
z = data.drawer_posz,
}
self.texture = data.texture
self.drawerType = data.drawerType or 1
self.visualId = data.visualId or ""
else
self.drawer_pos = drawers.last_drawer_pos
self.texture = drawers.last_texture or "drawers_empty.png"
self.visualId = drawers.last_visual_id
self.drawerType = drawers.last_drawer_type
end
-- add self to public drawer visuals
@ -68,7 +74,6 @@ core.register_entity("drawers:visual", {
-- PLEASE contact me, if this is wrong
drawers.drawer_visuals[core.serialize(self.drawer_pos)] = self
local node = core.get_node(self.drawer_pos)
-- collisionbox
@ -76,23 +81,39 @@ core.register_entity("drawers:visual", {
if node.param2 == 1 or node.param2 == 3 then
colbox = {0, -0.4374, -0.4374, 0, 0.4374, 0.4374}
end
-- only half the size if it's a small drawer
if self.drawerType >= 2 then
for i,j in pairs(colbox) do
colbox[i] = j * 0.5
end
end
-- visual size
local visual_size = {x = 0.6, y = 0.6}
if self.drawerType >= 2 then
core.chat_send_all("small")
visual_size = {x = 0.3, y = 0.3}
end
-- drawer values
local meta = core.get_meta(self.drawer_pos)
self.count = meta:get_int("count")
self.itemName = meta:get_string("name")
self.maxCount = meta:get_int("max_count")
self.itemStackMax = meta:get_int("base_stack_max")
self.stackMaxFactor = meta:get_int("stack_max_factor")
local vid = self.visualId
self.count = meta:get_int("count"..vid)
self.itemName = meta:get_string("name"..vid)
self.maxCount = meta:get_int("max_count"..vid)
self.itemStackMax = meta:get_int("base_stack_max"..vid)
self.stackMaxFactor = meta:get_int("stack_max_factor"..vid)
-- infotext
local infotext = meta:get_string("entity_infotext") .. "\n\n\n\n\n"
local infotext = meta:get_string("entity_infotext"..vid) .. "\n\n\n\n\n"
self.object:set_properties({
collisionbox = colbox,
infotext = infotext,
textures = {self.texture}
textures = {self.texture},
visual_size = visual_size
})
-- make entity undestroyable
@ -129,7 +150,7 @@ core.register_entity("drawers:visual", {
inv:add_item("main", stack)
self.count = self.count - removeCount
meta:set_int("count", self.count)
meta:set_int("count"..self.visualId, self.count)
-- update infotext
local itemDescription = ""
@ -139,14 +160,14 @@ core.register_entity("drawers:visual", {
if self.count <= 0 then
self.itemName = ""
meta:set_string("name", self.itemName)
meta:set_string("name"..self.visualId, self.itemName)
self.texture = "drawers_empty.png"
itemDescription = "Empty"
end
local infotext = drawers.gen_info_text(itemDescription,
self.count, self.stackMaxFactor, self.itemStackMax)
meta:set_string("entity_infotext", infotext)
meta:set_string("entity_infotext"..self.visualId, infotext)
self.object:set_properties({
infotext = infotext .. "\n\n\n\n\n",
@ -209,7 +230,7 @@ core.register_entity("drawers:visual", {
end
local infotext = drawers.gen_info_text(itemDescription,
self.count, self.stackMaxFactor, self.itemStackMax)
meta:set_string("entity_infotext", infotext)
meta:set_string("entity_infotext"..self.visualId, infotext)
-- texture
self.texture = drawers.get_inv_image(self.itemName)
@ -226,11 +247,11 @@ core.register_entity("drawers:visual", {
end,
saveMetaData = function(self, meta)
meta:set_int("count", self.count)
meta:set_string("name", self.itemName)
meta:set_int("max_count", self.maxCount)
meta:set_int("base_stack_max", self.itemStackMax)
meta:set_int("stack_max_factor", self.stackMaxFactor)
meta:set_int("count"..self.visualId, self.count)
meta:set_string("name"..self.visualId, self.itemName)
meta:set_int("max_count"..self.visualId, self.maxCount)
meta:set_int("base_stack_max"..self.visualId, self.itemStackMax)
meta:set_int("stack_max_factor"..self.visualId, self.stackMaxFactor)
end
})
@ -239,7 +260,7 @@ core.register_lbm({
nodenames = {"group:drawer"},
run_at_every_load = true,
action = function(pos, node)
local objs = core.get_objects_inside_radius(pos, 0.5)
local objs = core.get_objects_inside_radius(pos, 0.537)
if objs then
for _, obj in pairs(objs) do
if obj and obj:get_luaentity() and

View File

Before

Width:  |  Height:  |  Size: 229 B

After

Width:  |  Height:  |  Size: 229 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 587 B