2019-03-17 01:50:27 +00:00
|
|
|
physics = {}
|
|
|
|
|
|
|
|
local players = {}
|
2020-09-22 15:50:27 +00:00
|
|
|
local default_overrides = {
|
|
|
|
speed = 1,
|
|
|
|
jump = 1,
|
|
|
|
gravity = 1
|
|
|
|
}
|
2019-03-17 01:50:27 +00:00
|
|
|
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
players[player:get_player_name()] = {}
|
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
players[player:get_player_name()] = nil
|
|
|
|
end)
|
|
|
|
|
|
|
|
local function update(name)
|
|
|
|
assert(players[name])
|
|
|
|
local player = minetest.get_player_by_name(name)
|
2020-09-22 15:50:27 +00:00
|
|
|
local override = table.copy(default_overrides)
|
2019-03-17 01:50:27 +00:00
|
|
|
|
|
|
|
for _, layer in pairs(players[name]) do
|
|
|
|
for attr, val in pairs(layer) do
|
|
|
|
override[attr] = override[attr] * val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
player:set_physics_override(override)
|
|
|
|
end
|
|
|
|
|
2020-09-22 15:50:27 +00:00
|
|
|
function physics.set(name, layer, modifiers)
|
|
|
|
-- Basic sanity checks
|
|
|
|
assert(
|
|
|
|
type(name) == "string" and type(layer) == "string" and type(modifiers) == "table",
|
|
|
|
"physics.set: Invalid function arguments!"
|
|
|
|
)
|
|
|
|
|
|
|
|
if not players[name] then
|
2019-03-26 01:58:58 +00:00
|
|
|
return
|
|
|
|
end
|
2019-03-17 01:50:27 +00:00
|
|
|
|
2020-09-22 15:50:27 +00:00
|
|
|
for attr, val in pairs(modifiers) do
|
|
|
|
-- Throw error if an unsupported attribute is encountered
|
|
|
|
assert(default_overrides[attr], "physics: Unsupported attribute!")
|
|
|
|
|
|
|
|
-- Remove an attribute if its value is 1
|
|
|
|
if val == 1 then
|
|
|
|
modifiers[attr] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
players[name][layer] = modifiers
|
|
|
|
update(name)
|
2019-03-17 01:50:27 +00:00
|
|
|
end
|
|
|
|
|
2020-09-22 15:50:27 +00:00
|
|
|
function physics.remove(name, layer)
|
|
|
|
-- Basic sanity checks
|
|
|
|
assert(type(name) == "string" and type(layer) == "string",
|
|
|
|
"physics.remove: Invalid function arguments!")
|
|
|
|
|
|
|
|
if not players[name] then
|
2019-03-26 01:58:58 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-09-22 15:50:27 +00:00
|
|
|
players[name][layer] = nil
|
|
|
|
update(name)
|
2019-03-17 01:50:27 +00:00
|
|
|
end
|