Initial commit
[minetest_playerphysics.git] / init.lua
blob2b7d7df025cc4daaaf636f0e391029703c36bf29
1 playerphysics = {}
3 local function calculate_attribute_product(player, attribute)
4 local a = minetest.deserialize(player:get_attribute("playerphysics:physics"))
5 local product = 1
6 if a == nil or a[attribute] == nil then
7 return product
8 end
9 local factors = a[attribute]
10 if type(factors) == "table" then
11 for _, factor in pairs(factors) do
12 product = product * factor
13 end
14 end
15 return product
16 end
18 function playerphysics.add_physics_factor(player, attribute, id, value)
19 local a = minetest.deserialize(player:get_attribute("playerphysics:physics"))
20 if a == nil then
21 a = { [attribute] = { [id] = value } }
22 elseif a[attribute] == nil then
23 a[attribute] = { [id] = value }
24 else
25 a[attribute][id] = value
26 end
27 player:set_attribute("playerphysics:physics", minetest.serialize(a))
28 local raw_value = calculate_attribute_product(player, attribute)
29 player:set_physics_override({[attribute] = raw_value})
30 end
32 function playerphysics.remove_physics_factor(player, attribute, id)
33 local a = minetest.deserialize(player:get_attribute("playerphysics:physics"))
34 if a == nil or a[attribute] == nil then
35 -- Nothing to remove
36 return
37 else
38 a[attribute][id] = nil
39 end
40 player:set_attribute("playerphysics:physics", minetest.serialize(a))
41 local raw_value = calculate_attribute_product(player, attribute)
42 player:set_physics_override({[attribute] = raw_value})
43 end