Optimize the mod code

This also improves formatting to comply with Minetest's
Lua code style guidelines.
This commit is contained in:
Maksim 2020-01-01 13:42:09 +01:00 committed by Hugo Locurcio
parent b2bb39322a
commit f9471512bd
No known key found for this signature in database
GPG Key ID: 39E8F8BE30B0A49C
13 changed files with 40 additions and 50 deletions

View File

@ -3,62 +3,52 @@
-- Copyright © 2014-2020 4aiman, Hugo Locurcio and contributors - MIT License
-- See `LICENSE.md` included in the source distribution for details.
local hp_bar = {
physical = false,
collisionbox = {x = 0, y = 0, z = 0},
if minetest.settings:get_bool("health_bars") == false or
not minetest.settings:get_bool("enable_damage")
then return end
minetest.register_entity("gauges:hp_bar", {
visual = "sprite",
textures = {"20.png"}, -- The texture is changed later in the code
visual_size = {x = 1.5, y = 0.09375, z = 1.5}, -- Y value is (1 / 16) * 1.5
wielder = nil,
}
visual_size = {x=1, y=1/16, z = 1},
-- The texture is changed later in the code
textures = {"blank.png"},
collisionbox = {0},
physical = false,
function vector.sqdist(a, b)
local dx = a.x - b.x
local dy = a.y - b.y
local dz = a.z - b.z
return dx * dx + dy * dy + dz * dz
end
on_step = function(self)
local player = self.wielder
function hp_bar:on_step(dtime)
local wielder = self.wielder and minetest.get_player_by_name(self.wielder)
if not player or
not minetest.is_player(player) or
vector.distance(player:get_pos(), self.object:get_pos()) > 3
then
self.object:remove()
return
end
if
wielder == nil or
vector.sqdist(wielder:get_pos(), self.object:get_pos()) > 3
then
self.object:remove()
return
local hp = player:get_hp() <= 20 and player:get_hp() or 20
local breath = player:get_breath() <= 10 and player:get_breath() or 11
if self.hp ~= hp or self.breath ~= breath then
self.object:set_properties({
textures = {
"health_"..tostring(hp)..".png^"..
"breath_"..tostring(breath)..".png"
}
})
self.hp = hp
self.breath = breath
end
end
})
local hp = wielder:get_hp()
local breath = wielder:get_breath()
local function add_gauge(player)
local entity = minetest.add_entity(player:get_pos(), "gauges:hp_bar")
self.object:set_properties({
textures = {
"health_" .. tostring(hp) .. ".png^breath_" .. tostring(breath) .. ".png",
},
})
entity:set_attach(player, "", {x=0, y=19, z=0}, {x=0, y=0, z=0})
entity:get_luaentity().wielder = player
end
minetest.register_entity("gauges:hp_bar", hp_bar)
local function add_HP_gauge(name)
local player = minetest.get_player_by_name(name)
local pos = player:get_pos()
local ent = minetest.add_entity(pos, "gauges:hp_bar")
if ent ~= nil then
ent:set_attach(player, "", {x = 0, y = 19, z = 0}, {x = 0, y = 0, z = 0})
ent = ent:get_luaentity()
ent.wielder = player:get_player_name()
end
end
if
minetest.settings:get_bool("enable_damage") and
minetest.settings:get_bool("health_bars") ~= false
then
minetest.register_on_joinplayer(function(player)
minetest.after(1, add_HP_gauge, player:get_player_name())
end)
end
minetest.register_on_joinplayer(function(player)
minetest.after(1, add_gauge, player)
end)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 124 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 170 B

After

Width:  |  Height:  |  Size: 111 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 171 B

After

Width:  |  Height:  |  Size: 118 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 167 B

After

Width:  |  Height:  |  Size: 120 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 162 B

After

Width:  |  Height:  |  Size: 120 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 155 B

After

Width:  |  Height:  |  Size: 120 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 151 B

After

Width:  |  Height:  |  Size: 124 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 68 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 146 B

After

Width:  |  Height:  |  Size: 126 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 140 B

After

Width:  |  Height:  |  Size: 126 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 126 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 107 B

After

Width:  |  Height:  |  Size: 106 B