MineClone5/mods/PLAYER/playerphysics/init.lua

44 lines
1.3 KiB
Lua
Raw Normal View History

playerphysics = {}
2018-05-07 19:40:30 +02:00
local function calculate_physic_product(player, physic)
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
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
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
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
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