MineClone2/mods/PLAYER/playerphysics/init.lua

44 lines
1.3 KiB
Lua
Raw Normal View History

playerphysics = {}
2018-05-07 20:40:30 +03:00
2019-02-11 18:45:42 +02:00
local function calculate_attribute_product(player, attribute)
local a = minetest.deserialize(player:get_attribute("playerphysics:physics"))
2018-05-07 20:40:30 +03:00
local product = 1
2019-02-11 18:45:42 +02:00
if a == nil or a[attribute] == nil then
2018-05-07 20:40:30 +03:00
return product
end
2019-02-11 18:45:42 +02:00
local factors = a[attribute]
2018-05-07 20:40:30 +03:00
if type(factors) == "table" then
2019-02-11 18:45:42 +02:00
for _, factor in pairs(factors) do
2018-05-07 20:40:30 +03:00
product = product * factor
end
end
return product
end
2019-02-11 18:45:42 +02:00
function playerphysics.add_physics_factor(player, attribute, id, value)
local a = minetest.deserialize(player:get_attribute("playerphysics:physics"))
2018-05-07 20:40:30 +03:00
if a == nil then
2019-02-11 18:45:42 +02:00
a = { [attribute] = { [id] = value } }
elseif a[attribute] == nil then
a[attribute] = { [id] = value }
2018-05-07 20:40:30 +03:00
else
2019-02-11 18:45:42 +02:00
a[attribute][id] = value
2018-05-07 20:40:30 +03:00
end
player:set_attribute("playerphysics:physics", minetest.serialize(a))
2019-02-11 18:45:42 +02:00
local raw_value = calculate_attribute_product(player, attribute)
player:set_physics_override({[attribute] = raw_value})
2018-05-07 20:40:30 +03:00
end
2019-02-11 18:45:42 +02:00
function playerphysics.remove_physics_factor(player, attribute, id)
local a = minetest.deserialize(player:get_attribute("playerphysics:physics"))
2019-02-11 18:45:42 +02:00
if a == nil or a[attribute] == nil then
2018-05-07 20:40:30 +03:00
-- Nothing to remove
return
else
2019-02-11 18:45:42 +02:00
a[attribute][id] = nil
2018-05-07 20:40:30 +03:00
end
player:set_attribute("playerphysics:physics", minetest.serialize(a))
2019-02-11 18:45:42 +02:00
local raw_value = calculate_attribute_product(player, attribute)
player:set_physics_override({[attribute] = raw_value})
2018-05-07 20:40:30 +03:00
end