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