Initial commit.
This commit is contained in:
commit
7a71923caa
5 changed files with 250 additions and 0 deletions
127
init.lua
Normal file
127
init.lua
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
local minetest = core
|
||||
|
||||
local game = (function()
|
||||
if minetest.get_modpath("rgt_player") or minetest.get_modpath("artifact_player") then
|
||||
return "rgt"
|
||||
else
|
||||
return "mtg"
|
||||
end
|
||||
end)()
|
||||
|
||||
local disguises = {}
|
||||
|
||||
local function box_to_points(box)
|
||||
return vector.sort(vector.new(box[1], box[2], box[3]), vector.new(box[4], box[5], box[6]))
|
||||
end
|
||||
|
||||
local function get_disguise_transform(props, base_scale)
|
||||
local min, max = box_to_points(props.selectionbox)
|
||||
if max.x -min.x < 0.02 then
|
||||
print(max.x -min.x)
|
||||
min, max = box_to_points(props.collisionbox)
|
||||
if max.x -min.x < 0.02 then
|
||||
min = vector.new(-0.5, -0.5, -0.5)
|
||||
max = vector.new(0.5, 0.5, 0.5)
|
||||
end
|
||||
end
|
||||
max = max *8
|
||||
min = min *8
|
||||
local scale = base_scale *math.min((max.x -min.x) /6, (max.y -min.y) /6)
|
||||
return vector.new(min.x +(max.x -min.x) /2, max.y *base_scale.y -(1 *scale.y), max.z *base_scale.z +(1 *scale.z)), scale
|
||||
end
|
||||
|
||||
local add_entity = minetest.add_entity
|
||||
|
||||
minetest.register_entity(":disguises:disguise", {
|
||||
initial_properties = {
|
||||
visual = "mesh",
|
||||
mesh = "disguises.gltf",
|
||||
textures = {"disguises.png"},
|
||||
pointable = false,
|
||||
static_save = false
|
||||
},
|
||||
on_activate = function(e, data)
|
||||
|
||||
end,
|
||||
get_staticdata = function(e)
|
||||
return minetest.serialize{player = e.player}
|
||||
end,
|
||||
on_deactivate = function(e, force)
|
||||
if e.player and not force then
|
||||
local player = e.player
|
||||
minetest.after(0, function()
|
||||
local p = minetest.get_player_by_name(player)
|
||||
if p then
|
||||
local disguise = add_entity(p:get_pos(), "disguises:disguise")
|
||||
disguise:get_luaentity():attach_to(p)
|
||||
disguises[p:get_player_name()] = disguise
|
||||
end
|
||||
end)
|
||||
end
|
||||
end,
|
||||
attach_to = function(e, obj)
|
||||
if not obj then return e.object:remove() end
|
||||
e.obj = obj
|
||||
if minetest.is_player(obj) then
|
||||
e.player = obj:get_player_name()
|
||||
if game == "rgt" or game == "mtg" then
|
||||
e.object:set_attach(obj, "Head", vector.new(0, 1, -2.25), vector.new(0, 180, 0))
|
||||
else
|
||||
e:attach_to_obj(obj)
|
||||
end
|
||||
else
|
||||
e:attach_to_obj(obj)
|
||||
end
|
||||
end,
|
||||
attach_to_obj = function(e, obj)
|
||||
local le = obj:get_luaentity()
|
||||
local props = obj:get_properties()
|
||||
local size = props.visual_size:apply(function(a) return a == 0 and a or 1 /a end)
|
||||
local pos, scale = get_disguise_transform(props, size)
|
||||
e.object:set_properties {
|
||||
visual_size = scale
|
||||
}
|
||||
if not le then return end
|
||||
local on_deactivate = le.on_deactivate
|
||||
le.on_deactivate = function(...)
|
||||
e.object:remove()
|
||||
if on_deactivate then on_deactivate(...) end
|
||||
end
|
||||
e.object:set_attach(obj, nil, pos)
|
||||
end
|
||||
})
|
||||
|
||||
minetest.add_entity = function(pos, name, data)
|
||||
local out = add_entity(pos, name, data)
|
||||
local disguise = add_entity(pos, "disguises:disguise")
|
||||
disguise:get_luaentity():attach_to(out)
|
||||
return out
|
||||
end
|
||||
|
||||
minetest.register_on_joinplayer(function(p)
|
||||
minetest.after(0, function()
|
||||
local disguise = add_entity(p:get_pos(), "disguises:disguise")
|
||||
disguise:get_luaentity():attach_to(p)
|
||||
disguises[p:get_player_name()] = disguise
|
||||
end)
|
||||
end)
|
||||
|
||||
minetest.register_on_leaveplayer(function(p)
|
||||
disguises[p:get_player_name()]:remove()
|
||||
disguises[p:get_player_name()] = nil
|
||||
end)
|
||||
|
||||
minetest.register_on_mods_loaded(function()
|
||||
for name, def in pairs(minetest.registered_entities) do
|
||||
if name ~= "disguises:disguise" then
|
||||
local on_activate = def.on_activate
|
||||
def.on_activate = function(e, ...)
|
||||
if on_activate then on_activate(e, ...) end
|
||||
minetest.after(0, function()
|
||||
local disguise = add_entity(e.object:get_pos() or vector.zero(), "disguises:disguise")
|
||||
disguise:get_luaentity():attach_to(e.object)
|
||||
end)
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
Loading…
Add table
Add a link
Reference in a new issue