diff --git a/.luacheckrc b/.luacheckrc index 6d21f31..3dcbf46 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -51,7 +51,8 @@ read_globals = { fields = { "hypot", "sign", - "factorial" + "factorial", + "round", } }, diff --git a/README.md b/README.md index aa3f7a4..cdc3f2a 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,16 @@ Modified by SaKeL: - x_bows_quiver_empty_mesh.png - x_bows_quiver_blank_mesh.png - x_bows_quiver_slot.png +- x_bows_dmg_0.png +- x_bows_dmg_1.png +- x_bows_dmg_2.png +- x_bows_dmg_3.png +- x_bows_dmg_4.png +- x_bows_dmg_5.png +- x_bows_dmg_6.png +- x_bows_dmg_7.png +- x_bows_dmg_8.png +- x_bows_dmg_9.png ### Sounds diff --git a/api.lua b/api.lua index cc14ce4..0f0d52e 100644 --- a/api.lua +++ b/api.lua @@ -44,7 +44,8 @@ XBows = { registered_entities = {}, player_bow_sneak = {}, settings = { - x_bows_attach_arrows_to_entities = minetest.settings:get_bool('x_bows_attach_arrows_to_entities', false) + x_bows_attach_arrows_to_entities = minetest.settings:get_bool('x_bows_attach_arrows_to_entities', false), + x_bows_show_damage_numbers = minetest.settings:get_bool('x_bows_show_damage_numbers', false) }, charge_sound_after_job = {}, fallback_quiver = not minetest.global_exists('sfinv') and not minetest.global_exists('unified_inventory') and not minetest.global_exists('i3') @@ -1119,6 +1120,8 @@ function XBowsEntityDef.on_step(self, selfObj, dtime) selfObj._caused_damage = _damage selfObj._caused_knockback = knockback + XBows:show_damage_numbers(selfObj.object:get_pos(), _damage, selfObj._is_critical_hit) + -- already dead (entity) if not pointed_thing.ref:get_luaentity() and not pointed_thing.ref:is_player() then selfObj.object:remove() @@ -2375,3 +2378,64 @@ function XBowsQuiver.hide_3d_quiver(self, player) player_api.set_textures(player, player_textures) end end + +---string split to characters +---@param str string +---@return string[] | nil +local function split(str) + if #str > 0 then + return str:sub(1,1), split(str:sub(2)) + end +end + +function XBows.show_damage_numbers(self, pos, damage, is_crit) + if not pos or not self.settings.x_bows_show_damage_numbers then + return + end + + ---get damage texture + local dmgstr = tostring(math.round(damage)) + local results = {split(dmgstr)} + local texture = '' + local dmg_nr_offset = 0 + + for i, value in ipairs(results) do + if i == 1 then + texture = texture .. '[combine:' .. 7 * #results .. 'x' .. 9 * #results .. ':0,0=x_bows_dmg_' .. value .. '.png' + else + texture = texture .. ':' .. dmg_nr_offset .. ',0=x_bows_dmg_' .. value .. '.png' + end + + dmg_nr_offset = dmg_nr_offset + 7 + end + + if texture and texture ~= '' then + local size = 7 + + if is_crit then + size = 14 + texture = texture .. '^[colorize:#FF0000:255' + else + texture = texture .. '^[colorize:#FFFF00:127' + end + + ---show damage texture + minetest.add_particlespawner({ + amount = 1, + time = 0.01, + minpos = {x = pos.x, y = pos.y + 1, z = pos.z}, + maxpos = {x = pos.x, y = pos.y + 2, z = pos.z}, + minvel = {x = math.random(-1, 1), y = 5, z = math.random(-1, 1)}, + maxvel = {x = math.random(-1, 1), y = 5, z = math.random(-1, 1)}, + minacc = {x = math.random(-1, 1), y = -7, z = math.random(-1, 1)}, + maxacc = {x = math.random(-1, 1), y = -7, z = math.random(-1, 1)}, + minexptime = 2, + maxexptime = 2, + minsize = size, + maxsize = size, + texture = texture, + collisiondetection = true, + glow = 10 + }) + end +end diff --git a/settingtypes.txt b/settingtypes.txt index f4842e4..29aabaf 100644 --- a/settingtypes.txt +++ b/settingtypes.txt @@ -1,2 +1,5 @@ # Disabled per default due to inconsistent mob models scaling (visual_size). This will scale the arrows unproportionally and looks bad. If you have mobs with no scaling you can enable this setting. x_bows_attach_arrows_to_entities (Attach arrows to entities) bool false + +# Shows caused damage number flying out from the hit object/player. +x_bows_show_damage_numbers (Show damage caused) bool false diff --git a/textures/x_bows_dmg_0.png b/textures/x_bows_dmg_0.png new file mode 100644 index 0000000..a19cd00 Binary files /dev/null and b/textures/x_bows_dmg_0.png differ diff --git a/textures/x_bows_dmg_1.png b/textures/x_bows_dmg_1.png new file mode 100644 index 0000000..6702c8d Binary files /dev/null and b/textures/x_bows_dmg_1.png differ diff --git a/textures/x_bows_dmg_2.png b/textures/x_bows_dmg_2.png new file mode 100644 index 0000000..8b35f55 Binary files /dev/null and b/textures/x_bows_dmg_2.png differ diff --git a/textures/x_bows_dmg_3.png b/textures/x_bows_dmg_3.png new file mode 100644 index 0000000..dc24c16 Binary files /dev/null and b/textures/x_bows_dmg_3.png differ diff --git a/textures/x_bows_dmg_4.png b/textures/x_bows_dmg_4.png new file mode 100644 index 0000000..f603d7c Binary files /dev/null and b/textures/x_bows_dmg_4.png differ diff --git a/textures/x_bows_dmg_5.png b/textures/x_bows_dmg_5.png new file mode 100644 index 0000000..3300e98 Binary files /dev/null and b/textures/x_bows_dmg_5.png differ diff --git a/textures/x_bows_dmg_6.png b/textures/x_bows_dmg_6.png new file mode 100644 index 0000000..8da46e5 Binary files /dev/null and b/textures/x_bows_dmg_6.png differ diff --git a/textures/x_bows_dmg_7.png b/textures/x_bows_dmg_7.png new file mode 100644 index 0000000..d01881f Binary files /dev/null and b/textures/x_bows_dmg_7.png differ diff --git a/textures/x_bows_dmg_8.png b/textures/x_bows_dmg_8.png new file mode 100644 index 0000000..6cd88e5 Binary files /dev/null and b/textures/x_bows_dmg_8.png differ diff --git a/textures/x_bows_dmg_9.png b/textures/x_bows_dmg_9.png new file mode 100644 index 0000000..72feaa1 Binary files /dev/null and b/textures/x_bows_dmg_9.png differ diff --git a/types/math.type.lua b/types/math.type.lua new file mode 100644 index 0000000..4d62d97 --- /dev/null +++ b/types/math.type.lua @@ -0,0 +1,7 @@ +---https://github.com/sumneko/lua-language-server/wiki + +---@alias mathlib mathlib|MathAbstract + +---Math helpers +---@class MathAbstract +---@field round fun(x: number): number Returns `x` rounded to the nearest integer. At a multiple of 0.5, rounds away from zero. diff --git a/types/xbows.type.lua b/types/xbows.type.lua index 81ecbdc..d89e2b5 100644 --- a/types/xbows.type.lua +++ b/types/xbows.type.lua @@ -32,6 +32,7 @@ ---@field open_quiver fun(self: XBowsQuiver, itemstack: ItemStack, user: ObjectRef): ItemStack Open quiver ---@field uuid fun(): string Creates UUID ---@field fallback_quiver boolean If no invenotory mod is detected then fallback solution will be used +---@field show_damage_numbers fun(self: XBows, pos: Vector, damaga: number, is_crit?: boolean): nil Builds textures and shows textures in particle spawner ---XBowsQuiver class extended from XBows