Move sitting functions in handlers folder (turn it globals)

master
Jean-Patrick Guerrero 2016-02-12 15:00:44 +01:00
parent 711d9fe88d
commit 84358fc489
7 changed files with 112 additions and 120 deletions

52
handlers/animations.lua Normal file
View File

@ -0,0 +1,52 @@
local function top_face(pointed_thing)
return pointed_thing.above.y > pointed_thing.under.y
end
function xdecor.sit(pos, node, clicker, pointed_thing)
if not top_face(pointed_thing) then return end
local player = clicker:get_player_name()
local objs = minetest.get_objects_inside_radius(pos, 0.1)
local vel = clicker:get_player_velocity()
local ctrl = clicker:get_player_control()
for _, p in pairs(objs) do
if p:get_player_name() ~= player then return end
end
if default.player_attached[player] then
pos.y = pos.y - 0.5
clicker:setpos(pos)
clicker:set_eye_offset({x=0, y=0, z=0}, {x=0, y=0, z=0})
clicker:set_physics_override(1, 1, 1)
default.player_attached[player] = false
default.player_set_animation(clicker, "stand", 30)
elseif not default.player_attached[player] and node.param2 <= 3 and not
ctrl.sneak and vel.x == 0 and vel.y == 0 and vel.z == 0 then
clicker:set_eye_offset({x=0, y=-7, z=2}, {x=0, y=0, z=0})
clicker:set_physics_override(0, 0, 0)
clicker:setpos(pos)
default.player_attached[player] = true
default.player_set_animation(clicker, "sit", 30)
if node.param2 == 0 then clicker:set_look_yaw(3.15)
elseif node.param2 == 1 then clicker:set_look_yaw(7.9)
elseif node.param2 == 2 then clicker:set_look_yaw(6.28)
elseif node.param2 == 3 then clicker:set_look_yaw(4.75) end
end
end
function xdecor.sit_dig(pos, player)
local pname = player:get_player_name()
local objs = minetest.get_objects_inside_radius(pos, 0.1)
for _, p in pairs(objs) do
if not player or not player:is_player() or p:get_player_name() or
default.player_attached[pname] then
return false
end
end
return true
end

View File

@ -21,7 +21,8 @@ xdecor.nodebox = {
xdecor.pixelbox = function(size, boxes)
local fixed = {}
for _, box in pairs(boxes) do
local x, y, z, w, h, l = unpack(box) -- `unpack` has been changed to `table.unpack` in newest Lua versions.
-- `unpack` has been changed to `table.unpack` in newest Lua versions.
local x, y, z, w, h, l = unpack(box)
fixed[#fixed+1] = {
(x / size) - 0.5,
(y / size) - 0.5,

View File

@ -87,9 +87,7 @@ function xdecor.register(name, def)
if inventory then
def.on_construct = def.on_construct or function(pos)
local meta = minetest.get_meta(pos)
if infotext then
meta:set_string("infotext", infotext)
end
if infotext then meta:set_string("infotext", infotext) end
local size = inventory.size or default_inventory_size
local inv = meta:get_inventory()

View File

@ -2,9 +2,13 @@
xdecor = {}
local modpath = minetest.get_modpath("xdecor")
-- Handlers.
dofile(modpath.."/handlers/animations.lua")
dofile(modpath.."/handlers/helpers.lua")
dofile(modpath.."/handlers/nodeboxes.lua")
dofile(modpath.."/handlers/registration.lua")
-- Item files.
dofile(modpath.."/chess.lua")
dofile(modpath.."/cooking.lua")
dofile(modpath.."/craftitems.lua")
@ -15,7 +19,6 @@ dofile(modpath.."/mailbox.lua")
dofile(modpath.."/nodes.lua")
dofile(modpath.."/recipes.lua")
dofile(modpath.."/rope.lua")
dofile(modpath.."/sitting.lua")
dofile(modpath.."/worktable.lua")
dofile(modpath.."/xwall.lua")
--print(string.format("[xdecor] loaded in %.2f ms", (os.clock()-t)*1000))

View File

@ -90,6 +90,27 @@ xdecor.register("candle", {
}
})
xdecor.register("chair", {
description = "Chair",
tiles = {"xdecor_wood.png"},
sounds = default.node_sound_wood_defaults(),
groups = {choppy=3, oddly_breakable_by_hand=2, flammable=3},
on_rotate = screwdriver.rotate_simple,
node_box = xdecor.pixelbox(16, {
{3, 0, 11, 2, 16, 2},
{11, 0, 11, 2, 16, 2},
{5, 9, 11.5, 6, 6, 1},
{3, 0, 3, 2, 6, 2},
{11, 0, 3, 2, 6, 2},
{3, 6, 3, 10, 2, 8}
}),
can_dig = xdecor.sit_dig,
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
pos.y = pos.y + 0 -- Sitting position.
xdecor.sit(pos, node, clicker, pointed_thing)
end
})
xpanes.register_pane("chainlink", {
description = "Chain Link",
tiles = {"xdecor_chainlink.png"},
@ -172,6 +193,34 @@ xdecor.register("crate", {
sounds = default.node_sound_wood_defaults()
})
xdecor.register("cushion", {
description = "Cushion",
tiles = {"xdecor_cushion.png"},
groups = {snappy=3, flammable=3, fall_damage_add_percent=-50},
on_place = minetest.rotate_node,
node_box = xdecor.nodebox.slab_y(0.5),
can_dig = xdecor.sit_dig,
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
pos.y = pos.y + 0
xdecor.sit(pos, node, clicker, pointed_thing)
local wield_item = clicker:get_wielded_item():get_name()
if wield_item == "xdecor:cushion" and clicker:get_player_control().sneak then
local player_name = clicker:get_player_name()
if minetest.is_protected(pos, player_name) then
minetest.record_protection_violation(pos, player_name) return
end
minetest.set_node(pos, {name="xdecor:cushion_block", param2=node.param2})
if not minetest.setting_getbool("creative_mode") then
itemstack:take_item()
end
return itemstack
end
end
})
xdecor.register("cushion_block", {
tiles = {"xdecor_cushion.png"},
groups = {snappy=3, flammable=3, fall_damage_add_percent=-75, not_in_creative_inventory=1},

View File

@ -1,113 +0,0 @@
local function pointed_face(pointed_thing)
return pointed_thing.above.y > pointed_thing.under.y -- Top face pointed.
end
local function sit(pos, node, clicker, pointed_thing)
if not pointed_face(pointed_thing) then return end
local player = clicker:get_player_name()
local objs = minetest.get_objects_inside_radius(pos, 0.1)
for _, p in pairs(objs) do
if p:get_player_name() ~= clicker:get_player_name() then
return
end
end
if default.player_attached[player] == true then
pos.y = pos.y - 0.5
clicker:setpos(pos)
clicker:set_eye_offset({x=0, y=0, z=0}, {x=0, y=0, z=0})
clicker:set_physics_override(1, 1, 1)
default.player_attached[player] = false
default.player_set_animation(clicker, "stand", 30)
elseif default.player_attached[player] ~= true and node.param2 <= 3 and
clicker:get_player_control().sneak == false and
clicker:get_player_velocity().x == 0 and
clicker:get_player_velocity().y == 0 and
clicker:get_player_velocity().z == 0 then
clicker:set_eye_offset({x=0, y=-7, z=2}, {x=0, y=0, z=0})
clicker:set_physics_override(0, 0, 0)
clicker:setpos(pos)
default.player_attached[player] = true
default.player_set_animation(clicker, "sit", 30)
if node.param2 == 0 then
clicker:set_look_yaw(3.15)
elseif node.param2 == 1 then
clicker:set_look_yaw(7.9)
elseif node.param2 == 2 then
clicker:set_look_yaw(6.28)
elseif node.param2 == 3 then
clicker:set_look_yaw(4.75)
end
end
end
local function dig(pos, player)
local pname = player:get_player_name()
local objs = minetest.get_objects_inside_radius(pos, 0.1)
for _, p in pairs(objs) do
if not player or not player:is_player() or p:get_player_name() ~= nil or
default.player_attached[pname] == true then
return false
end
end
return true
end
xdecor.register("chair", {
description = "Chair",
tiles = {"xdecor_wood.png"},
sounds = default.node_sound_wood_defaults(),
groups = {choppy=3, oddly_breakable_by_hand=2, flammable=3},
on_rotate = screwdriver.rotate_simple,
node_box = xdecor.pixelbox(16, {
{3, 0, 11, 2, 16, 2},
{11, 0, 11, 2, 16, 2},
{5, 9, 11.5, 6, 6, 1},
{3, 0, 3, 2, 6, 2},
{11, 0, 3, 2, 6, 2},
{3, 6, 3, 10, 2, 8}
}),
can_dig = dig,
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
pos.y = pos.y + 0 -- Sitting position.
sit(pos, node, clicker, pointed_thing)
end
})
xdecor.register("cushion", {
description = "Cushion",
tiles = {"xdecor_cushion.png"},
groups = {snappy=3, flammable=3, fall_damage_add_percent=-50},
on_place = minetest.rotate_node,
node_box = xdecor.nodebox.slab_y(0.5),
can_dig = dig,
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
pos.y = pos.y + 0
sit(pos, node, clicker, pointed_thing)
local wield_item = clicker:get_wielded_item():get_name()
if wield_item == "xdecor:cushion" and clicker:get_player_control().sneak then
local player_name = clicker:get_player_name()
if minetest.is_protected(pos, player_name) then
minetest.record_protection_violation(pos, player_name)
return
end
if pointed_face(pointed_thing) then
minetest.set_node(pos, {name="xdecor:cushion_block", param2=node.param2})
end
if not minetest.setting_getbool("creative_mode") then
itemstack:take_item()
end
return itemstack
end
end
})

View File

@ -122,7 +122,8 @@ function worktable:craftguide_formspec(meta, pagenum, item, recipe_num, filter)
local items = minetest.get_all_craft_recipes(item)[recipe_num].items
local width = minetest.get_all_craft_recipes(item)[recipe_num].width
if width == 0 then width = math.min(3, #items) end
local rows = math.ceil(table.maxn(items) / width) -- Lua 5.3 removed `table.maxn`, use `xdecor.maxn` in case of failure.
-- Lua 5.3 removed `table.maxn`, use `xdecor.maxn` in case of breakage.
local rows = math.ceil(table.maxn(items) / width)
local function is_group(item)
if item:sub(1,6) == "group:" then return "\nG" end
@ -414,7 +415,8 @@ for node in pairs(minetest.registered_nodes) do
sounds = def.sounds,
tiles = tiles,
groups = groups,
node_box = xdecor.pixelbox(16, {unpack(d, 3)}), -- `unpack` has been changed to `table.unpack` in newest Lua versions.
-- `unpack` has been changed to `table.unpack` in newest Lua versions.
node_box = xdecor.pixelbox(16, {unpack(d, 3)}),
sunlight_propagates = true,
on_place = minetest.rotate_node,
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)