MineClone5-FlamingRCCars/mods/PLAYER/playerphysics/init.lua

46 lines
1.4 KiB
Lua
Raw Normal View History

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