1
0
Fork 0
MineClone2/mods/PLAYER/playerphysics/init.lua

46 lines
1.4 KiB
Lua
Raw Permalink Normal View History

playerphysics = {}
2018-05-07 17:40:30 +00:00
2019-02-11 16:45:42 +00:00
local function calculate_attribute_product(player, attribute)
local a = minetest.deserialize(player:get_meta():get_string("playerphysics:physics"))
2018-05-07 17:40:30 +00:00
local product = 1
2019-02-11 16:45:42 +00:00
if a == nil or a[attribute] == nil then
2018-05-07 17:40:30 +00:00
return product
end
2019-02-11 16:45:42 +00:00
local factors = a[attribute]
2018-05-07 17:40:30 +00:00
if type(factors) == "table" then
2019-02-11 16:45:42 +00:00
for _, factor in pairs(factors) do
2018-05-07 17:40:30 +00:00
product = product * factor
end
end
return product
end
2019-02-11 16:45:42 +00:00
function playerphysics.add_physics_factor(player, attribute, id, value)
local meta = player:get_meta()
local a = minetest.deserialize(meta:get_string("playerphysics:physics"))
2018-05-07 17:40:30 +00:00
if a == nil then
2019-02-11 16:45:42 +00:00
a = { [attribute] = { [id] = value } }
elseif a[attribute] == nil then
a[attribute] = { [id] = value }
2018-05-07 17:40:30 +00:00
else
2019-02-11 16:45:42 +00:00
a[attribute][id] = value
2018-05-07 17:40:30 +00:00
end
meta:set_string("playerphysics:physics", minetest.serialize(a))
2019-02-11 16:45:42 +00:00
local raw_value = calculate_attribute_product(player, attribute)
player:set_physics_override({[attribute] = raw_value})
2018-05-07 17:40:30 +00:00
end
2019-02-11 16:45:42 +00:00
function playerphysics.remove_physics_factor(player, attribute, id)
local meta = player:get_meta()
local a = minetest.deserialize(meta:get_string("playerphysics:physics"))
2019-02-11 16:45:42 +00:00
if a == nil or a[attribute] == nil then
2018-05-07 17:40:30 +00:00
-- Nothing to remove
return
else
2019-02-11 16:45:42 +00:00
a[attribute][id] = nil
2018-05-07 17:40:30 +00:00
end
meta:set_string("playerphysics:physics", minetest.serialize(a))
2019-02-11 16:45:42 +00:00
local raw_value = calculate_attribute_product(player, attribute)
player:set_physics_override({[attribute] = raw_value})
2018-05-07 17:40:30 +00:00
end