Skip to content

Commit

Permalink
Add physics modifiers wrapper mod for player physics overrides (#289)
Browse files Browse the repository at this point in the history
Closes #288
  • Loading branch information
ClobberXD authored and rubenwardy committed Mar 17, 2019
1 parent 3e2b6c0 commit 7212900
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions mods/other/sprint/depends.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
medkits
physics
hudbars?
13 changes: 8 additions & 5 deletions mods/other/sprint/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 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 SPRINT_MODIFIERS = {
[true] = { speed = MOD_WALK, jump = MOD_JUMP },
[false] = { speed = 1.0, jump = 1.0 },
}

if minetest.get_modpath("hudbars") ~= nil then
hb.register_hudbar("sprint", 0xFFFFFF, "Stamina",
Expand All @@ -27,7 +23,14 @@ local players = {}

local function setSprinting(player, info, sprinting)
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
end
end
Expand Down
47 changes: 47 additions & 0 deletions mods/physics/init.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7212900

Please sign in to comment.