From 0bc2bc6919a4a263bd836c9c9f1822043e40eccb Mon Sep 17 00:00:00 2001 From: tacotexmex Date: Mon, 24 Jul 2017 21:36:47 +0200 Subject: [PATCH] Drain air while sprinting under water --- README.md | 1 + init.lua | 27 ++++++++++++++++++++++++--- settingtypes.txt | 6 ++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6cf6699..86d0433 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ It has no dependencies, but it supports on [hudbars](http://repo.or.cz/w/minetes - Displays and drains stamina (by default, if hudbars is present). Hides stamina bar if full. - Displays and drains satiation (by default, if hbhunger is present) +- Drains air faster while sprinting under water (by default) - Requires only forward key to be pressed, not left and right (by default) - Requires walkable ground (no water sprinting) - Particle spawning based on ground type (Thanks to [octacian](https://github.com/octacian/sprint/)) diff --git a/init.lua b/init.lua index 81217a5..8f1c9ba 100644 --- a/init.lua +++ b/init.lua @@ -9,17 +9,21 @@ local stamina = minetest.settings:get_bool("sprint_stamina") or true local stamina_drain = tonumber(minetest.settings:get("sprint_stamina_drain")) or 2 local replenish = tonumber(minetest.settings:get("sprint_stamina_replenish")) or 2 local starve = minetest.settings:get_bool("sprint_starve") or true -local starve_drain = minetest.settings:get("sprint_starve_drain") or 0.5 +local starve_drain = tonumber(minetest.settings:get("sprint_starve_drain")) or 0.5 +local breath = minetest.settings:get_bool("sprint_breath") or true +local breath_drain = tonumber(minetest.settings:get("sprint_breath_drain")) or 1 local sprint_timer_step = 0.5 local sprint_timer = 0 local player_stamina = 20 local stamina_timer = 0 +local breath_timer = 0 local sprinting = false -if dir == nil then dir = true end +if dir ~= false then dir = true end if stamina ~= false then stamina = true end -if starve == nil then starve = true end +if starve ~= false then starve = true end +if breath ~= false then breath = true end if minetest.get_modpath("hudbars") ~= nil then hudbars = true end if minetest.get_modpath("hbhunger") ~= nil then starve = true end if minetest.get_modpath("player_monoids") ~= nil then monoids = true end @@ -77,6 +81,16 @@ local function drain_hunger(player, hunger, name) end end +local function drain_breath(player) + local player_breath = player:get_breath() + if player_breath < 11 then + player_breath = player_breath - breath_drain + if player_breath > 0 then + player:set_breath(player_breath) + end + end +end + local function create_particles(player, name, pos, ground) if ground and ground.name ~= "air" and ground.name ~= "ignore" then local def = minetest.registered_nodes[ground.name] @@ -118,6 +132,7 @@ end) minetest.register_globalstep(function(dtime) sprint_timer = sprint_timer + dtime stamina_timer = stamina_timer + dtime + breath_timer = breath_timer + dtime if sprint_timer >= sprint_timer_step then for _,player in ipairs(minetest.get_connected_players()) do local ctrl = player:get_player_control() @@ -150,6 +165,12 @@ minetest.register_globalstep(function(dtime) start_sprint(player) if stamina then drain_stamina(player) end if starve then drain_hunger(player, hunger, name) end + if breath then + if breath_timer >= 2 then + drain_breath(player) + breath_timer = 0 + end + end if particles then create_particles(player, name, pos, ground) end end sprinting = true diff --git a/settingtypes.txt b/settingtypes.txt index 1eaf0ad..8478182 100644 --- a/settingtypes.txt +++ b/settingtypes.txt @@ -27,3 +27,9 @@ sprint_starve (Starve) bool true #The amount of satiation to drain while sprinting sprint_starve_drain (Starve drain) float 0.5 + +#Drain air while sprinting under water +sprint_breath (Breath) bool true + +#The amount of air to drain while sprinting under water +sprint_breath_drain (Breath drain) float 1