Skip to content

Commit

Permalink
Rework entity attachment and character model
Browse files Browse the repository at this point in the history
  • Loading branch information
stujones11 committed Nov 6, 2013
1 parent d2e49ec commit 9cf2cb9
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 881 deletions.
135 changes: 82 additions & 53 deletions init.lua
Original file line number Diff line number Diff line change
@@ -1,77 +1,106 @@
dofile(minetest.get_modpath(minetest.get_current_modname()).."/location.lua")

local update_time = 2 -- number of seconds between wielditem updates
local bone = "Armature_Wield_Item"
local location = {
"Armature_Arm_Right", -- default bone
{x=0.2, y=5.5, z=3}, -- default position
{x=-100, y=225, z=90}, -- default rotation
}
local player_wielding = {}
local timer = 0

dofile(minetest.get_modpath(minetest.get_current_modname()).."/rotation.lua")
local function add_wield_entity(player)
player:set_properties({
visual = "mesh",
mesh = "wield3d_character.x",
visual_size = {x=1, y=1},
})
local pos = player:getpos()
if pos then
local object = minetest.add_entity(pos, "wield3d:wield_entity")
if object then
object:set_attach(player, location[1], location[2], location[3])
local luaentity = object:get_luaentity()
if luaentity then
luaentity.player = player
return 1
end
end
end
end

minetest.register_item("wield3d:hand", {
type = "none",
wield_image = "wield3d_trans.png",
})

minetest.register_entity("wield3d:wield_entity", {
initial_properties = {
physical = false,
collisionbox = {x=0, y=0, z=0},
visual = "wielditem",
visual_size = {x=0.25, y=0.25},
},
wield_item = nil,
physical = false,
collisionbox = {x=0, y=0, z=0},
visual = "wielditem",
visual_size = {x=0.25, y=0.25},
textures = {"wield3d:hand"},
player = nil,
item = nil,
timer = 0,
rotation = 0,
location = location,
on_step = function(self, dtime)
local player = self.player
if player == nil then
self.object:remove()
return
end
self.timer = self.timer + dtime
if self.timer < update_time then
if self.timer > update_time then
return
end
self.timer = 0
if minetest.env:get_player_by_name(player:get_player_name()) == nil then
self.object:remove()
return
end
local stack = player:get_wielded_item()
local item = stack:get_name()
if item == self.wield_item then
return
end
self.wield_item = item
if item == "" then
item = "wield3d:hand"
end
local rotation = wield3d_rotation[item] or 0
if rotation ~= self.rotation then
self.object:setpos(player:getpos())
self.object:set_detach()
self.object:set_attach(player, bone, {x=0, y=0, z=0}, {x=0, y=0, z=rotation})
self.rotation = rotation
local player = self.player
if player then
local pos = player:getpos()
if pos then
local stack = player:get_wielded_item()
local item = stack:get_name()
if item == self.item then
return
end
self.item = item
if item == "" then
item = "wield3d:hand"
end
local loc = wield3d_location[item] or location
if vector.equals(loc[1], self.location[1]) == false
or vector.equals(loc[2], self.location[2]) == false
or vector.equals(loc[3], self.location[3]) == false then
self.object:setpos(pos)
self.object:set_detach()
self.object:set_attach(player, loc[1], loc[2], loc[3])
self.location = loc
end
self.object:set_properties({textures={item}})
return
end
end
self.object:set_properties({textures={item}})
self.object:remove()
end,
})

minetest.register_on_joinplayer(function(player)
minetest.after(1, function(player)
player:set_properties({
visual = "mesh",
mesh = "wield3d_character.x",
visual_size = {x=1, y=1},
})
local pos = player:getpos()
if pos then
local entity = minetest.env:add_entity(pos, "wield3d:wield_entity")
if entity then
entity:set_attach(player, bone, {x=0, y=0, z=0}, {x=0, y=0, z=0})
entity = entity:get_luaentity()
entity.player = player
return
minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer > update_time then
for _,player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
if name then
if not player_wielding[name] then
if add_wield_entity(player) then
player_wielding[name] = 1
end
end
end
end
minetest.log("error", "wield3d failed to attach wielditem")
end, player)
timer = 0
end
end)

minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
if name then
player_wielding[name] = nil
end
end)

32 changes: 32 additions & 0 deletions location.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- Wielded Item Location Overrides - [item_name] = {bone, position, rotation}

local bone = "Armature_Arm_Right"

wield3d_location = {
["default:torch"] = {bone, {x=0.2, y=5.5, z=3}, {x=-100, y=182, z=83}},
["default:sapling"] = {bone, {x=0.2, y=5.5, z=3}, {x=-100, y=182, z=83}},
["flowers:dandelion_white"] = {bone, {x=0.2, y=5.5, z=3}, {x=-100, y=182, z=83}},
["flowers:dandelion_yellow"] = {bone, {x=0.2, y=5.5, z=3}, {x=-100, y=182, z=83}},
["flowers:geranium"] = {bone, {x=0.2, y=5.5, z=3}, {x=-100, y=182, z=83}},
["flowers:rose"] = {bone, {x=0.2, y=5.5, z=3}, {x=-100, y=182, z=83}},
["flowers:tulip"] = {bone, {x=0.2, y=5.5, z=3}, {x=-100, y=182, z=83}},
["flowers:viola"] = {bone, {x=0.2, y=5.5, z=3}, {x=-100, y=182, z=83}},
["default:shovel_wood"] = {bone, {x=0, y=5.5, z=3}, {x=-90, y=137, z=83}},
["default:shovel_stone"] = {bone, {x=0, y=5.5, z=3}, {x=-90, y=137, z=83}},
["default:shovel_steel"] = {bone, {x=0, y=5.5, z=3}, {x=-90, y=137, z=83}},
["default:shovel_bronze"] = {bone, {x=0, y=5.5, z=3}, {x=-90, y=137, z=83}},
["default:shovel_mese"] = {bone, {x=0, y=5.5, z=3}, {x=-90, y=137, z=83}},
["default:shovel_diamond"] = {bone, {x=0, y=5.5, z=3}, {x=-90, y=137, z=83}},
["bucket:bucket_empty"] = {bone, {x=0, y=5.5, z=3}, {x=-90, y=137, z=83}},
["bucket:bucket_water"] = {bone, {x=0, y=5.5, z=3}, {x=-90, y=137, z=83}},
["bucket:bucket_lava"] = {bone, {x=0, y=5.5, z=3}, {x=-90, y=137, z=83}},
["screwdriver:screwdriver"] = {bone, {x=0, y=5.5, z=3}, {x=-90, y=137, z=83}},
["screwdriver:screwdriver1"] = {bone, {x=0, y=5.5, z=3}, {x=-90, y=137, z=83}},
["screwdriver:screwdriver2"] = {bone, {x=0, y=5.5, z=3}, {x=-90, y=137, z=83}},
["screwdriver:screwdriver3"] = {bone, {x=0, y=5.5, z=3}, {x=-90, y=137, z=83}},
["screwdriver:screwdriver4"] = {bone, {x=0, y=5.5, z=3}, {x=-90, y=137, z=83}},
["vessels:glass_bottle"] = {bone, {x=0, y=5.5, z=3}, {x=-90, y=137, z=83}},
["vessels:drinking_glass"] = {bone, {x=0, y=5.5, z=3}, {x=-90, y=137, z=83}},
["vessels:steel_bottle"] = {bone, {x=0, y=5.5, z=3}, {x=-90, y=137, z=83}},
}

Loading

0 comments on commit 9cf2cb9

Please sign in to comment.