Refactor playerphysics init

This commit is contained in:
Wuzzy 2019-02-11 17:45:42 +01:00
parent 49949e2f36
commit 1cbbe8f4b1

View File

@ -1,43 +1,43 @@
playerphysics = {}
local function calculate_physic_product(player, physic)
local function calculate_attribute_product(player, attribute)
local a = minetest.deserialize(player:get_attribute("playerphysics:physics"))
local product = 1
if a == nil or a[physic] == nil then
if a == nil or a[attribute] == nil then
return product
end
local factors = a[physic]
local factors = a[attribute]
if type(factors) == "table" then
for id, factor in pairs(factors) do
for _, factor in pairs(factors) do
product = product * factor
end
end
return product
end
function playerphysics.add_physics_factor(player, physic, id, value)
function playerphysics.add_physics_factor(player, attribute, id, value)
local a = minetest.deserialize(player:get_attribute("playerphysics:physics"))
if a == nil then
a = { [physic] = { [id] = value } }
elseif a[physic] == nil then
a[physic] = { [id] = value }
a = { [attribute] = { [id] = value } }
elseif a[attribute] == nil then
a[attribute] = { [id] = value }
else
a[physic][id] = value
a[attribute][id] = value
end
player:set_attribute("playerphysics:physics", minetest.serialize(a))
local raw_value = calculate_physic_product(player, physic)
player:set_physics_override({[physic] = raw_value})
local raw_value = calculate_attribute_product(player, attribute)
player:set_physics_override({[attribute] = raw_value})
end
function playerphysics.remove_physics_factor(player, physic, id)
function playerphysics.remove_physics_factor(player, attribute, id)
local a = minetest.deserialize(player:get_attribute("playerphysics:physics"))
if a == nil or a[physic] == nil then
if a == nil or a[attribute] == nil then
-- Nothing to remove
return
else
a[physic][id] = nil
a[attribute][id] = nil
end
player:set_attribute("playerphysics:physics", minetest.serialize(a))
local raw_value = calculate_physic_product(player, physic)
player:set_physics_override({[physic] = raw_value})
local raw_value = calculate_attribute_product(player, attribute)
player:set_physics_override({[attribute] = raw_value})
end