mirror of
https://github.com/minetest-mods/hbsprint.git
synced 2024-08-19 20:44:10 +02:00
refactor
This commit is contained in:
parent
0893fd8bec
commit
49305c18d3
129
init.lua
129
init.lua
@ -43,7 +43,8 @@ end
|
|||||||
-- Functions
|
-- Functions
|
||||||
|
|
||||||
local function start_sprint(player)
|
local function start_sprint(player)
|
||||||
if not sprinting[player:get_player_name()] then
|
local name = player:get_player_name()
|
||||||
|
if not sprinting[name] then
|
||||||
if mod_player_monoids then
|
if mod_player_monoids then
|
||||||
player_monoids.speed:add_change(player, speed, "hbsprint:speed")
|
player_monoids.speed:add_change(player, speed, "hbsprint:speed")
|
||||||
player_monoids.jump:add_change(player, jump, "hbsprint:jump")
|
player_monoids.jump:add_change(player, jump, "hbsprint:jump")
|
||||||
@ -53,11 +54,13 @@ local function start_sprint(player)
|
|||||||
else
|
else
|
||||||
player:set_physics_override({speed = speed, jump = jump})
|
player:set_physics_override({speed = speed, jump = jump})
|
||||||
end
|
end
|
||||||
|
sprinting[name] = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function stop_sprint(player)
|
local function stop_sprint(player)
|
||||||
if sprinting[player:get_player_name()] then
|
local name = player:get_player_name()
|
||||||
|
if sprinting[name] then
|
||||||
if mod_player_monoids then
|
if mod_player_monoids then
|
||||||
player_monoids.speed:del_change(player, "hbsprint:speed")
|
player_monoids.speed:del_change(player, "hbsprint:speed")
|
||||||
player_monoids.jump:del_change(player, "hbsprint:jump")
|
player_monoids.jump:del_change(player, "hbsprint:jump")
|
||||||
@ -67,13 +70,15 @@ local function stop_sprint(player)
|
|||||||
else
|
else
|
||||||
player:set_physics_override({speed = 1, jump = 1})
|
player:set_physics_override({speed = 1, jump = 1})
|
||||||
end
|
end
|
||||||
|
sprinting[name] = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function drain_stamina(player)
|
local function drain_stamina(player)
|
||||||
local player_stamina = tonumber(player:get_meta():get("hbsprint:stamina"))
|
local player_stamina = player:get_meta():get_float("hbsprint:stamina")
|
||||||
if player_stamina > 0 then
|
if player_stamina > 0 then
|
||||||
player:get_meta():set_float("hbsprint:stamina", player_stamina - stamina_drain)
|
player_stamina = math.max(0, player_stamina - stamina_drain)
|
||||||
|
player:get_meta():set_float("hbsprint:stamina", player_stamina)
|
||||||
end
|
end
|
||||||
if mod_hudbars then
|
if mod_hudbars then
|
||||||
if autohide and player_stamina < 20 then hb.unhide_hudbar(player, "stamina") end
|
if autohide and player_stamina < 20 then hb.unhide_hudbar(player, "stamina") end
|
||||||
@ -82,46 +87,49 @@ local function drain_stamina(player)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function replenish_stamina(player)
|
local function replenish_stamina(player)
|
||||||
local player_stamina = tonumber(player:get_meta():get("hbsprint:stamina"))
|
local player_stamina = player:get_meta():get_float("hbsprint:stamina")
|
||||||
if player_stamina < 20 then
|
if player_stamina < 20 then
|
||||||
player:get_meta():set_float("hbsprint:stamina", player_stamina + stamina_drain)
|
player_stamina = math.min(20, player_stamina + stamina_drain)
|
||||||
|
player:get_meta():set_float("hbsprint:stamina", player_stamina)
|
||||||
end
|
end
|
||||||
if mod_hudbars then
|
if mod_hudbars then
|
||||||
hb.change_hudbar(player, "stamina", player_stamina)
|
hb.change_hudbar(player, "stamina", player_stamina)
|
||||||
if autohide and player_stamina == 20 then hb.hide_hudbar(player, "stamina") end
|
if autohide and player_stamina >= 20 then hb.hide_hudbar(player, "stamina") end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function drain_hunger(player, hunger, name)
|
local function drain_hunger(player, name)
|
||||||
if hunger > 0 then
|
|
||||||
local newhunger = hunger - starve_drain
|
|
||||||
if starve == "hbhunger" then
|
if starve == "hbhunger" then
|
||||||
hbhunger.hunger[name] = newhunger
|
local hunger = tonumber(hbhunger.hunger[name]) - starve_drain
|
||||||
|
hbhunger.hunger[name] = math.max(0, hunger)
|
||||||
hbhunger.set_hunger_raw(player)
|
hbhunger.set_hunger_raw(player)
|
||||||
elseif starve == "hunger_ng" then
|
elseif starve == "hunger_ng" then
|
||||||
hunger_ng.alter_hunger(name, - starve_drain, "Sprinting")
|
hunger_ng.alter_hunger(name, -starve_drain, "Sprinting")
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function drain_breath(player)
|
local function drain_breath(player)
|
||||||
local player_breath = player:get_breath()
|
local player_breath = player:get_breath()
|
||||||
if player_breath < 11 then
|
if player_breath < 11 then
|
||||||
player_breath = player_breath - breath_drain
|
player_breath = math.max(0, player_breath - breath_drain)
|
||||||
if player_breath > 0 then
|
|
||||||
player:set_breath(player_breath)
|
player:set_breath(player_breath)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function create_particles(player, name, pos, ground)
|
local function create_particles(player, name, pos, ground)
|
||||||
if ground and ground.name ~= "air" and ground.name ~= "ignore" then
|
if not ground or ground.name == "air" or ground.name == "ignore" then
|
||||||
|
return
|
||||||
|
end
|
||||||
local def = minetest.registered_nodes[ground.name]
|
local def = minetest.registered_nodes[ground.name]
|
||||||
local tile = def.tiles[1] or def.inventory_image or ""
|
local tile = def.tiles[1] or def.inventory_image
|
||||||
if type(tile) == "string" then
|
if type(tile) ~= "string" then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local rand = function() return math.random(-1,1) * math.random() / 2 end
|
||||||
for i = 1, particles do
|
for i = 1, particles do
|
||||||
minetest.add_particle({
|
minetest.add_particle({
|
||||||
pos = {x = pos.x + math.random(-1,1) * math.random() / 2, y = pos.y + 0.1, z = pos.z + math.random(-1,1) * math.random() / 2},
|
pos = {x = pos.x + rand(), y = pos.y + 0.1, z = pos.z + rand()},
|
||||||
velocity = {x = 0, y = 5, z = 0},
|
velocity = {x = 0, y = 5, z = 0},
|
||||||
acceleration = {x = 0, y = -13, z = 0},
|
acceleration = {x = 0, y = -13, z = 0},
|
||||||
expirationtime = math.random(),
|
expirationtime = math.random(),
|
||||||
@ -130,8 +138,6 @@ local function create_particles(player, name, pos, ground)
|
|||||||
texture = tile,
|
texture = tile,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Registrations
|
-- Registrations
|
||||||
@ -152,66 +158,81 @@ if mod_hudbars and stamina then
|
|||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_on_joinplayer(function(player)
|
minetest.register_on_joinplayer(function(player)
|
||||||
if mod_hudbars and stamina then hb.init_hudbar(player, "stamina", 20, 20, autohide) end
|
if stamina then
|
||||||
|
if mod_hudbars then
|
||||||
|
hb.init_hudbar(player, "stamina", 20, 20, autohide)
|
||||||
|
end
|
||||||
player:get_meta():set_float("hbsprint:stamina", 20)
|
player:get_meta():set_float("hbsprint:stamina", 20)
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
minetest.register_globalstep(function(dtime)
|
local function sprint_step(player, dtime)
|
||||||
sprint_timer = sprint_timer + dtime
|
local name = player:get_player_name()
|
||||||
if sprint_timer >= sprint_timer_step then
|
|
||||||
for _,player in ipairs(minetest.get_connected_players()) do
|
if stamina then
|
||||||
|
stamina_timer[name] = (stamina_timer[name] or 0) + dtime
|
||||||
|
end
|
||||||
|
if breath then
|
||||||
|
breath_timer[name] = (breath_timer[name] or 0) + dtime
|
||||||
|
end
|
||||||
|
|
||||||
local ctrl = player:get_player_control()
|
local ctrl = player:get_player_control()
|
||||||
local key_press = false
|
local key_press
|
||||||
stamina_timer[player:get_player_name()] = (stamina_timer[player:get_player_name()] or 0) + sprint_timer
|
|
||||||
breath_timer[player:get_player_name()] = (breath_timer[player:get_player_name()] or 0) + sprint_timer
|
|
||||||
if dir then
|
if dir then
|
||||||
key_press = ctrl.aux1 and ctrl.up and not ctrl.left and not ctrl.right
|
key_press = ctrl.aux1 and ctrl.up and not ctrl.left and not ctrl.right
|
||||||
else
|
else
|
||||||
key_press = ctrl.aux1
|
key_press = ctrl.aux1
|
||||||
end
|
end
|
||||||
|
|
||||||
if key_press then
|
if not key_press then
|
||||||
local name = player:get_player_name()
|
stop_sprint(player)
|
||||||
local hunger = 30
|
if stamina and stamina_timer[name] >= replenish then
|
||||||
|
replenish_stamina(player)
|
||||||
|
stamina_timer[name] = 0
|
||||||
|
end
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
local pos = player:get_pos()
|
local pos = player:get_pos()
|
||||||
local ground = minetest.get_node_or_nil({x=pos.x, y=pos.y-1, z=pos.z})
|
local ground = minetest.get_node_or_nil({x=pos.x, y=pos.y-1, z=pos.z})
|
||||||
local walkable = false
|
local walkable = false
|
||||||
local player_stamina = tonumber(player:get_meta():get("hbsprint:stamina"))
|
if ground ~= nil then
|
||||||
|
local ground_def = minetest.registered_nodes[ground.name]
|
||||||
|
walkable = ground_def and (ground_def.walkable or ground_def.liquidtype ~= "none")
|
||||||
|
end
|
||||||
|
|
||||||
|
local player_stamina = 1
|
||||||
|
if stamina then
|
||||||
|
player_stamina = player:get_meta():get_float("hbsprint:stamina")
|
||||||
|
end
|
||||||
|
local hunger = 30
|
||||||
if starve == "hbhunger" then
|
if starve == "hbhunger" then
|
||||||
hunger = tonumber(hbhunger.hunger[name])
|
hunger = tonumber(hbhunger.hunger[name])
|
||||||
elseif starve == "hunger_ng" then
|
elseif starve == "hunger_ng" then
|
||||||
hunger = hunger_ng.get_hunger_information(name).hunger.exact
|
hunger = hunger_ng.get_hunger_information(name).hunger.exact
|
||||||
end
|
end
|
||||||
if ground ~= nil then
|
|
||||||
local ground_def = minetest.registered_nodes[ground.name]
|
|
||||||
if ground_def and (minetest.registered_nodes[ground.name].walkable or minetest.registered_nodes[ground.name].liquidtype ~= "none") then
|
|
||||||
walkable = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if player_stamina > 0 and hunger > starve_limit and walkable then
|
if player_stamina > 0 and hunger > starve_limit and walkable then
|
||||||
start_sprint(player)
|
start_sprint(player)
|
||||||
sprinting[name] = true
|
|
||||||
if stamina then drain_stamina(player) end
|
if stamina then drain_stamina(player) end
|
||||||
if starve then drain_hunger(player, hunger, name) end
|
if starve then drain_hunger(player, name) end
|
||||||
if breath then
|
if breath and breath_timer[name] >= 2 then
|
||||||
if breath_timer[name] >= 2 then
|
|
||||||
drain_breath(player)
|
drain_breath(player)
|
||||||
breath_timer[name] = 0
|
breath_timer[name] = 0
|
||||||
end
|
end
|
||||||
end
|
if particles then
|
||||||
if particles then create_particles(player, name, pos, ground) end
|
create_particles(player, name, pos, ground)
|
||||||
else
|
|
||||||
stop_sprint(player)
|
|
||||||
sprinting[name] = false
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
stop_sprint(player)
|
stop_sprint(player)
|
||||||
sprinting[player:get_player_name()] = false
|
|
||||||
if stamina_timer[player:get_player_name()] >= replenish then
|
|
||||||
if stamina then replenish_stamina(player) end
|
|
||||||
stamina_timer[player:get_player_name()] = 0
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
minetest.register_globalstep(function(dtime)
|
||||||
|
sprint_timer = sprint_timer + dtime
|
||||||
|
if sprint_timer >= sprint_timer_step then
|
||||||
|
for _, player in ipairs(minetest.get_connected_players()) do
|
||||||
|
sprint_step(player, sprint_timer)
|
||||||
end
|
end
|
||||||
sprint_timer = 0
|
sprint_timer = 0
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user