forked from mtcontrib/minetest_hudbars
Add experimental exhaustion to control hunger (based on player actions)
This commit is contained in:
parent
36a1b23490
commit
26d3f38a9a
35
hunger.lua
35
hunger.lua
@ -11,6 +11,8 @@ local function poisenp(tick, time, time_left, player)
|
|||||||
time_left = time_left + tick
|
time_left = time_left + tick
|
||||||
if time_left < time then
|
if time_left < time then
|
||||||
minetest.after(tick, poisenp, tick, time, time_left, player)
|
minetest.after(tick, poisenp, tick, time, time_left, player)
|
||||||
|
else
|
||||||
|
--reset hud image
|
||||||
end
|
end
|
||||||
if player:get_hp()-1 > 0 then
|
if player:get_hp()-1 > 0 then
|
||||||
player:set_hp(player:get_hp()-1)
|
player:set_hp(player:get_hp()-1)
|
||||||
@ -40,6 +42,7 @@ function hud.item_eat(hunger_change, replace_with_item, poisen, heal)
|
|||||||
end
|
end
|
||||||
-- Poison
|
-- Poison
|
||||||
if poisen then
|
if poisen then
|
||||||
|
--set hud-img
|
||||||
poisenp(1.0, poisen, 0, user)
|
poisenp(1.0, poisen, 0, user)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -259,3 +262,35 @@ if minetest.get_modpath("ethereal") then
|
|||||||
overwrite("ethereal:yellowleaves", 1, "", nil, 1)
|
overwrite("ethereal:yellowleaves", 1, "", nil, 1)
|
||||||
overwrite("ethereal:sashimi", 4)
|
overwrite("ethereal:sashimi", 4)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- player-action based hunger changes
|
||||||
|
function hud.handle_node_actions(pos, oldnode, player, ext)
|
||||||
|
if not player or not player:is_player() then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local name = player:get_player_name()
|
||||||
|
local exhaus = hud.exhaustion[name]
|
||||||
|
local new = HUD_HUNGER_EXHAUST_PLACE
|
||||||
|
-- placenode event
|
||||||
|
if not ext then
|
||||||
|
new = HUD_HUNGER_EXHAUST_DIG
|
||||||
|
end
|
||||||
|
-- assume its send by main timer when movement detected
|
||||||
|
if not pos and not oldnode then
|
||||||
|
new = HUD_HUNGER_EXHAUST_MOVE
|
||||||
|
end
|
||||||
|
exhaus = exhaus + new
|
||||||
|
minetest.chat_send_all(exhaus)
|
||||||
|
if exhaus > HUD_HUNGER_EXHAUST_LVL then
|
||||||
|
exhaus = 0
|
||||||
|
local h = tonumber(hud.hunger[name])
|
||||||
|
h = h - 1
|
||||||
|
if h < 0 then h = 0 end
|
||||||
|
hud.hunger[name] = h
|
||||||
|
hud.set_hunger(player)
|
||||||
|
end
|
||||||
|
hud.exhaustion[name] = exhaus
|
||||||
|
end
|
||||||
|
|
||||||
|
minetest.register_on_placenode(hud.handle_node_actions)
|
||||||
|
minetest.register_on_dignode(hud.handle_node_actions)
|
||||||
|
25
init.lua
25
init.lua
@ -45,7 +45,17 @@ if dump(minetest.hud_replace_builtin) ~= "nil" then
|
|||||||
end
|
end
|
||||||
|
|
||||||
HUD_TICK = 0.1
|
HUD_TICK = 0.1
|
||||||
HUD_HUNGER_TICK = 300
|
|
||||||
|
--Some hunger settings
|
||||||
|
hud.exhaustion = {} -- Exhaustion is experimental!
|
||||||
|
|
||||||
|
HUD_HUNGER_TICK = 800 -- time in seconds after that 1 hunger point is taken
|
||||||
|
HUD_HUNGER_EXHAUST_DIG = 3 -- exhaustion increased this value after digged node
|
||||||
|
HUD_HUNGER_EXHAUST_PLACE = 1 -- exhaustion increased this value after placed
|
||||||
|
HUD_HUNGER_EXHAUST_MOVE = 0.3 -- exhaustion increased this value if player movement detected
|
||||||
|
HUD_HUNGER_EXHAUST_LVL = 160 -- at what exhaustion player saturation gets lowerd
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
HUD_ENABLE_HUNGER = minetest.setting_getbool("hud_hunger_enable")
|
HUD_ENABLE_HUNGER = minetest.setting_getbool("hud_hunger_enable")
|
||||||
if HUD_ENABLE_HUNGER == nil then
|
if HUD_ENABLE_HUNGER == nil then
|
||||||
@ -247,6 +257,7 @@ minetest.register_on_joinplayer(function(player)
|
|||||||
if HUD_ENABLE_HUNGER then
|
if HUD_ENABLE_HUNGER then
|
||||||
hud.hunger[name] = hud.get_hunger(player)
|
hud.hunger[name] = hud.get_hunger(player)
|
||||||
hud.hunger_out[name] = hud.hunger[name]
|
hud.hunger_out[name] = hud.hunger[name]
|
||||||
|
hud.exhaustion[name] = 0
|
||||||
end
|
end
|
||||||
hud.armor[name] = 0
|
hud.armor[name] = 0
|
||||||
hud.armor_out[name] = 0
|
hud.armor_out[name] = 0
|
||||||
@ -263,9 +274,11 @@ minetest.register_on_respawnplayer(function(player)
|
|||||||
-- reset player breath since the engine doesnt
|
-- reset player breath since the engine doesnt
|
||||||
player:set_breath(11)
|
player:set_breath(11)
|
||||||
-- reset hunger (and save)
|
-- reset hunger (and save)
|
||||||
hud.hunger[player:get_player_name()] = 20
|
local name = player:get_player_name()
|
||||||
|
hud.hunger[name] = 20
|
||||||
if HUD_ENABLE_HUNGER then
|
if HUD_ENABLE_HUNGER then
|
||||||
minetest.after(0.5, hud.set_hunger, player)
|
minetest.after(0.5, hud.set_hunger, player)
|
||||||
|
hud.exhaustion[name] = 0
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@ -308,6 +321,14 @@ minetest.after(2.5, function()
|
|||||||
|
|
||||||
-- update all hud elements
|
-- update all hud elements
|
||||||
update_hud(player)
|
update_hud(player)
|
||||||
|
|
||||||
|
if HUD_ENABLE_HUNGER then
|
||||||
|
local controls = player:get_player_control()
|
||||||
|
-- Determine if the player is walking
|
||||||
|
if controls.up or controls.down or controls.left or controls.right then
|
||||||
|
hud.handle_node_actions(nil, nil, player)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user