Add physics modifiers wrapper mod for player physics overrides (#289)

Closes #288
This commit is contained in:
ANAND 2019-03-17 07:20:27 +05:30 committed by rubenwardy
parent 3e2b6c083b
commit 721290085d
3 changed files with 56 additions and 5 deletions

View file

@ -1,2 +1,3 @@
medkits medkits
physics
hudbars? hudbars?

View file

@ -7,10 +7,6 @@ local MOD_JUMP = tonumber(minetest.settings:get("sprint_jump") or 1.1)
local STAMINA_MAX = tonumber(minetest.settings:get("sprint_stamina") or 20) local STAMINA_MAX = tonumber(minetest.settings:get("sprint_stamina") or 20)
local HEAL_RATE = tonumber(minetest.settings:get("sprint_heal_rate") or 0.5) local HEAL_RATE = tonumber(minetest.settings:get("sprint_heal_rate") or 0.5)
local MIN_SPRINT = tonumber(minetest.settings:get("sprint_min") or 0.5) local MIN_SPRINT = tonumber(minetest.settings:get("sprint_min") or 0.5)
local SPRINT_MODIFIERS = {
[true] = { speed = MOD_WALK, jump = MOD_JUMP },
[false] = { speed = 1.0, jump = 1.0 },
}
if minetest.get_modpath("hudbars") ~= nil then if minetest.get_modpath("hudbars") ~= nil then
hb.register_hudbar("sprint", 0xFFFFFF, "Stamina", hb.register_hudbar("sprint", 0xFFFFFF, "Stamina",
@ -27,7 +23,14 @@ local players = {}
local function setSprinting(player, info, sprinting) local function setSprinting(player, info, sprinting)
if info.sprinting ~= sprinting then if info.sprinting ~= sprinting then
player:set_physics_override(SPRINT_MODIFIERS[sprinting]) if sprinting then
physics.set(player:get_player_name(), "sprint:sprint", {
speed = MOD_WALK,
jump = MOD_JUMP
})
else
physics.remove(player:get_player_name(), "sprint:sprint")
end
info.sprinting = sprinting info.sprinting = sprinting
end end
end end

47
mods/physics/init.lua Normal file
View file

@ -0,0 +1,47 @@
physics = {}
local players = {}
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)
local override = {
speed = 1,
jump = 1,
gravity = 1
}
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
function physics.set(pname, name, modifiers)
assert(players[pname] and not players[pname][name])
players[pname][name] = modifiers
update(pname)
end
function physics.change(pname, name, modifier)
assert(players[pname] and players[pname][name])
players[pname][name] = modifier
update(pname)
end
function physics.remove(pname, name)
assert(players[pname] and players[pname][name])
players[pname][name] = nil
update(pname)
end