physics: Add sanity checks and minor optimizations (#650)
- Throw error if an unsupported attribute is encountered. - Throw errors for invalid function arguments. - Optimize out attributes with a value of 1.
This commit is contained in:
parent
ec06b8edde
commit
7793e76890
1 changed files with 34 additions and 13 deletions
|
@ -1,6 +1,11 @@
|
||||||
physics = {}
|
physics = {}
|
||||||
|
|
||||||
local players = {}
|
local players = {}
|
||||||
|
local default_overrides = {
|
||||||
|
speed = 1,
|
||||||
|
jump = 1,
|
||||||
|
gravity = 1
|
||||||
|
}
|
||||||
|
|
||||||
minetest.register_on_joinplayer(function(player)
|
minetest.register_on_joinplayer(function(player)
|
||||||
players[player:get_player_name()] = {}
|
players[player:get_player_name()] = {}
|
||||||
|
@ -13,11 +18,7 @@ end)
|
||||||
local function update(name)
|
local function update(name)
|
||||||
assert(players[name])
|
assert(players[name])
|
||||||
local player = minetest.get_player_by_name(name)
|
local player = minetest.get_player_by_name(name)
|
||||||
local override = {
|
local override = table.copy(default_overrides)
|
||||||
speed = 1,
|
|
||||||
jump = 1,
|
|
||||||
gravity = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, layer in pairs(players[name]) do
|
for _, layer in pairs(players[name]) do
|
||||||
for attr, val in pairs(layer) do
|
for attr, val in pairs(layer) do
|
||||||
|
@ -28,20 +29,40 @@ local function update(name)
|
||||||
player:set_physics_override(override)
|
player:set_physics_override(override)
|
||||||
end
|
end
|
||||||
|
|
||||||
function physics.set(pname, name, modifiers)
|
function physics.set(name, layer, modifiers)
|
||||||
if not players[pname] then
|
-- 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
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
players[pname][name] = modifiers
|
for attr, val in pairs(modifiers) do
|
||||||
update(pname)
|
-- 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)
|
||||||
end
|
end
|
||||||
|
|
||||||
function physics.remove(pname, name)
|
function physics.remove(name, layer)
|
||||||
if not players[pname] then
|
-- Basic sanity checks
|
||||||
|
assert(type(name) == "string" and type(layer) == "string",
|
||||||
|
"physics.remove: Invalid function arguments!")
|
||||||
|
|
||||||
|
if not players[name] then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
players[pname][name] = nil
|
players[name][layer] = nil
|
||||||
update(pname)
|
update(name)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue