Display kill score as a HUD element (#182)

This commit is contained in:
ClobberXD 2018-11-12 11:45:51 +05:30 committed by GitHub
parent d111d8673a
commit 77cdecc15e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 1 deletions

View file

@ -44,6 +44,9 @@ local function bounty_player(target)
end end
bounty_score = math.floor(bounty_score) bounty_score = math.floor(bounty_score)
ctf_stats.current_bounty["name"] = bountied_player
ctf_stats.current_bounty["score"] = bounty_score
minetest.after(0.1, announce_all) minetest.after(0.1, announce_all)
end end
@ -70,6 +73,7 @@ minetest.after(math.random(500, 1000), bounty_find_new_target)
minetest.register_on_leaveplayer(function(player) minetest.register_on_leaveplayer(function(player)
if bountied_player == player:get_player_name() then if bountied_player == player:get_player_name() then
bountied_player = nil bountied_player = nil
ctf_stats.current_bounty = {}
end end
end) end)
@ -92,6 +96,7 @@ ctf.register_on_killedplayer(function(victim, killer)
ctf.needs_save = true ctf.needs_save = true
end end
bountied_player = nil bountied_player = nil
ctf_stats.current_bounty = {}
local msg = killer .. " has killed " .. victim .. " and received the prize!" local msg = killer .. " has killed " .. victim .. " and received the prize!"
minetest.chat_send_all(msg) minetest.chat_send_all(msg)

View file

@ -268,10 +268,87 @@ local function calculateKillReward(victim, killer)
return reward return reward
end end
ctf_stats.current_bounty = {}
local hud_timer = {}
local hud_main = {}
local hud_bounty = {}
local hud_duration = 2
-- Called on every player kill
-- Creates new HUD element or updates existing one
local function showKillPoints(killer, bounty_kill, points)
if not killer then
return
end
local name = killer:get_player_name()
local bounty = ctf_stats.current_bounty["score"]
-- If HUD element already exists, update
if hud_main[name] then
killer:hud_change(hud_main[name], "text", points)
if bounty_kill and hud_bounty[name] then
killer:hud_change(hud_bounty[name], "text", "+ " .. bounty)
end
-- Else create a new one
else
hud_main[name] = killer:hud_add({
hud_elem_type = "text",
position = {x = 0.5, y = 1},
alignment = {x = 0, y = 0},
offset = {x = 0, y = -150},
text = points,
number = 0x00DD00
})
-- If victim was a bounty target, show bounty points too
if bounty_kill then
hud_bounty[name] = killer:hud_add({
hud_elem_type = "text",
position = {x = 0.5, y = 1},
alignment = {x = 0, y = 0},
offset = {x = 0, y = -120},
text = "+ " .. bounty,
number = 0xFF4444
})
end
end
-- Set duration for which the HUD element would be visible
hud_timer[name] = os.time() + hud_duration
end
minetest.register_globalstep(function(dtime)
for _, player in pairs(minetest.get_connected_players()) do
local name = player:get_player_name()
-- If no timer exists for player, return
if not hud_timer[name] then
return
end
-- If HUD element's visibility duration expired, remove it
if hud_timer[name] <= os.time() then
player:hud_remove(hud_main[name])
hud_main[name] = nil
-- If bounty score is displayed, remove that too
if hud_bounty[name] then
player:hud_remove(hud_bounty[name])
hud_bounty[name] = nil
end
end
end
end)
ctf.register_on_killedplayer(function(victim, killer) ctf.register_on_killedplayer(function(victim, killer)
local main, match = ctf_stats.player(killer) local main, match = ctf_stats.player(killer)
if main and match then if main and match then
local reward = calculateKillReward(victim, killer) local reward = calculateKillReward(victim, killer)
local bounty_kill = victim:get_player_name() ==
ctf_stats.current_bounty["name"] and true or false
showKillPoints(killer, bounty_kill, reward)
main.kills = main.kills + 1 main.kills = main.kills + 1
main.score = main.score + reward main.score = main.score + reward
match.kills = match.kills + 1 match.kills = match.kills + 1