More Minecraft-like hunger system (partially)

This commit is contained in:
Wuzzy 2017-01-21 15:10:08 +01:00
parent 15d569b769
commit 3278085904
2 changed files with 28 additions and 14 deletions

View file

@ -41,7 +41,7 @@ function hbhunger.eat(hp_change, replace_with_item, itemstack, user, pointed_thi
hp_change = 1 hp_change = 1
core.log("error", "Wrong on_use() definition for item '" .. item .. "'") core.log("error", "Wrong on_use() definition for item '" .. item .. "'")
end end
def.saturation = hp_change * 1.3 def.saturation = hp_change
def.replace = replace_with_item def.replace = replace_with_item
end end
local func = hbhunger.item_eat(def.saturation, def.replace, def.poisen, def.healing, def.sound) local func = hbhunger.item_eat(def.saturation, def.replace, def.poisen, def.healing, def.sound)
@ -79,9 +79,9 @@ function hbhunger.item_eat(hunger_change, replace_with_item, poisen, heal, sound
minetest.sound_play({name = sound or "hbhunger_eat_generic", gain = 1}, {pos=user:getpos(), max_hear_distance = 16}) minetest.sound_play({name = sound or "hbhunger_eat_generic", gain = 1}, {pos=user:getpos(), max_hear_distance = 16})
-- Saturation -- Saturation
if h < 30 and hunger_change then if h < 20 and hunger_change then
h = h + hunger_change h = h + hunger_change
if h > 30 then h = 30 end if h > 20 then h = 20 end
hbhunger.hunger[name] = h hbhunger.hunger[name] = h
hbhunger.set_hunger_raw(user) hbhunger.set_hunger_raw(user)
end end

View file

@ -46,7 +46,7 @@ end
dofile(minetest.get_modpath("hbhunger").."/hunger.lua") dofile(minetest.get_modpath("hbhunger").."/hunger.lua")
-- register satiation hudbar -- register satiation hudbar
hb.register_hudbar("satiation", 0xFFFFFF, S("Satiation"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 20, 30, false) hb.register_hudbar("satiation", 0xFFFFFF, S("Satiation"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 20, 20, false)
-- update hud elemtens if value has changed -- update hud elemtens if value has changed
local function update_hud(player) local function update_hud(player)
@ -78,7 +78,7 @@ hbhunger.set_hunger_raw = function(player)
local name = player:get_player_name() local name = player:get_player_name()
local value = hbhunger.hunger[name] local value = hbhunger.hunger[name]
if not inv or not value then return nil end if not inv or not value then return nil end
if value > 30 then value = 30 end if value > 20 then value = 20 end
if value < 0 then value = 0 end if value < 0 then value = 0 end
inv:set_stack("hunger", 1, ItemStack({name=":", count=value+1})) inv:set_stack("hunger", 1, ItemStack({name=":", count=value+1}))
@ -107,26 +107,33 @@ minetest.register_on_respawnplayer(function(player)
end) end)
local main_timer = 0 local main_timer = 0
local timer = 0 local timer = 0 -- Half second timer
local timer2 = 0 local timer2 = 0
local timerMult = 1 -- Cycles from 0 to 7, each time when timer hits half a second
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
main_timer = main_timer + dtime main_timer = main_timer + dtime
timer = timer + dtime timer = timer + dtime
timer2 = timer2 + dtime timer2 = timer2 + dtime
if main_timer > hbhunger.HUD_TICK or timer > 4 or timer2 > hbhunger.HUNGER_TICK then if main_timer > hbhunger.HUD_TICK or timer > 0.5 or timer2 > hbhunger.HUNGER_TICK then
if main_timer > hbhunger.HUD_TICK then main_timer = 0 end if main_timer > hbhunger.HUD_TICK then main_timer = 0 end
for _,player in ipairs(minetest.get_connected_players()) do for _,player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name() local name = player:get_player_name()
local h = tonumber(hbhunger.hunger[name]) local h = tonumber(hbhunger.hunger[name])
local hp = player:get_hp() local hp = player:get_hp()
if timer > 4 then if timer > 0.5 then
-- heal player by 1 hp if not dead and satiation is > 15 (of 30) -- Quick heal (every 0.5s)
if h > 15 and hp > 0 and player:get_breath() > 0 then if h >= 20 and hp > 0 then
player:set_hp(hp+1) player:set_hp(hp+1)
-- or damage player by 1 hp if satiation is < 2 (of 30) -- Slow heal, and hunger damage (every 4s)
elseif h <= 1 then elseif timerMult == 0 then
if hp-1 >= 0 then player:set_hp(hp-1) end if h >= 18 and hp > 0 then
player:set_hp(hp+1)
elseif h == 0 then
-- Damage hungry player down to 1 HP
if hp-1 >= 0 then
player:set_hp(hp-1)
end
end end
end end
-- lower satiation by 1 point after xx seconds -- lower satiation by 1 point after xx seconds
@ -147,8 +154,15 @@ minetest.register_globalstep(function(dtime)
hbhunger.handle_node_actions(nil, nil, player) hbhunger.handle_node_actions(nil, nil, player)
end end
end end
end
end
if timer > 0.5 then
timer = 0
timerMult = timerMult + 1
if timerMult > 7 then
timerMult = 0
end
end end
if timer > 4 then timer = 0 end
if timer2 > hbhunger.HUNGER_TICK then timer2 = 0 end if timer2 > hbhunger.HUNGER_TICK then timer2 = 0 end
end) end)