2018-10-23 18:51:19 +02:00
|
|
|
playerphysics = {}
|
2018-05-07 19:40:30 +02:00
|
|
|
|
|
|
|
local function calculate_physic_product(player, physic)
|
2018-10-23 18:51:19 +02:00
|
|
|
local a = minetest.deserialize(player:get_attribute("playerphysics:physics"))
|
2018-05-07 19:40:30 +02:00
|
|
|
local product = 1
|
|
|
|
if a == nil or a[physic] == nil then
|
|
|
|
return product
|
|
|
|
end
|
|
|
|
local factors = a[physic]
|
|
|
|
if type(factors) == "table" then
|
|
|
|
for id, factor in pairs(factors) do
|
|
|
|
product = product * factor
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return product
|
|
|
|
end
|
|
|
|
|
2018-10-23 18:51:19 +02:00
|
|
|
function playerphysics.add_physics_factor(player, physic, id, value)
|
|
|
|
local a = minetest.deserialize(player:get_attribute("playerphysics:physics"))
|
2018-05-07 19:40:30 +02:00
|
|
|
if a == nil then
|
|
|
|
a = { [physic] = { [id] = value } }
|
|
|
|
elseif a[physic] == nil then
|
|
|
|
a[physic] = { [id] = value }
|
|
|
|
else
|
|
|
|
a[physic][id] = value
|
|
|
|
end
|
2018-10-23 18:51:19 +02:00
|
|
|
player:set_attribute("playerphysics:physics", minetest.serialize(a))
|
2018-05-07 19:40:30 +02:00
|
|
|
local raw_value = calculate_physic_product(player, physic)
|
|
|
|
player:set_physics_override({[physic] = raw_value})
|
|
|
|
end
|
|
|
|
|
2018-10-23 18:51:19 +02:00
|
|
|
function playerphysics.remove_physics_factor(player, physic, id)
|
|
|
|
local a = minetest.deserialize(player:get_attribute("playerphysics:physics"))
|
2018-05-07 19:40:30 +02:00
|
|
|
if a == nil or a[physic] == nil then
|
|
|
|
-- Nothing to remove
|
|
|
|
return
|
|
|
|
else
|
|
|
|
a[physic][id] = nil
|
|
|
|
end
|
2018-10-23 18:51:19 +02:00
|
|
|
player:set_attribute("playerphysics:physics", minetest.serialize(a))
|
2018-05-07 19:40:30 +02:00
|
|
|
local raw_value = calculate_physic_product(player, physic)
|
|
|
|
player:set_physics_override({[physic] = raw_value})
|
|
|
|
end
|