1
0
Fork 0

Refactor playerphysics init

This commit is contained in:
Wuzzy 2019-02-11 17:45:42 +01:00
parent 49949e2f36
commit 1cbbe8f4b1
1 changed files with 16 additions and 16 deletions

View File

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