Add bossbar support

This commit is contained in:
cora 2022-10-23 03:44:15 +02:00
parent 627ce0dc3b
commit f17a9220bb
2 changed files with 50 additions and 8 deletions

View File

@ -5,6 +5,8 @@ local active_events = {}
local tpl_eventdef = { local tpl_eventdef = {
stage = 1, stage = 1,
max_stage = 1, max_stage = 1,
percent = 100,
bossbars = {},
--pos = vector.zero(), --pos = vector.zero(),
--time_start = 0, --time_start = 0,
completed = false, completed = false,
@ -23,6 +25,21 @@ function mcl_events.register_event(name,def)
mcl_events.registered_events[name].name = name mcl_events.registered_events[name].name = name
end end
local function addbars(self)
for _,player in pairs(minetest.get_connected_players()) do
if vector.distance(self.pos,player:get_pos()) < 75 then
local bar = mcl_bossbars.add_bar(player, {color = "red", text = self.name .. " stage "..self.stage.." / "..self.max_stage, percentage = self.percent }, true,1)
table.insert(self.bars,bar)
end
end
end
local function update_bars(self)
for _,b in pairs(self.bars) do
mcl_bossbars.update_bar(b,{text = self.name .. " stage "..self.stage,percentage=self.percent})
end
end
local function start_event(p,e) local function start_event(p,e)
minetest.log("event started: "..e.name.." at "..minetest.pos_to_string(p)) minetest.log("event started: "..e.name.." at "..minetest.pos_to_string(p))
local idx = #active_events + 1 local idx = #active_events + 1
@ -32,11 +49,17 @@ local function start_event(p,e)
active_events[idx].stage = 1 active_events[idx].stage = 1
active_events[idx].time_start = os.time() active_events[idx].time_start = os.time()
active_events[idx]:on_start(p) active_events[idx]:on_start(p)
active_events[idx].bars = {}
active_events[idx].percent = 0
addbars(active_events[idx])
end end
local function finish_event(self,idx) local function finish_event(self,idx)
minetest.log("event finished: "..self.name.." at "..minetest.pos_to_string(self.pos)) minetest.log("event finished: "..self.name.." at "..minetest.pos_to_string(self.pos))
if self.on_complete then self:on_complete() end if self.on_complete then self:on_complete() end
for _,b in pairs(self.bars) do
mcl_bossbars.remove_bar(b)
end
table.remove(active_events,idx) table.remove(active_events,idx)
end end
@ -53,16 +76,16 @@ function check_events(dtime)
local p = ae:cond_progress() local p = ae:cond_progress()
if p == true then if p == true then
ae.stage = ae.stage + 1 ae.stage = ae.stage + 1
minetest.log("event progressed to stage "..ae.stage)
ae:on_stage_begin() ae:on_stage_begin()
elseif tonumber(p) then elseif tonumber(p) then
ae.stage = tonumber(p) or ae.stage + 1 ae.stage = tonumber(p) or ae.stage + 1
minetest.log("event progressed to stage "..ae.stage)
ae:on_stage_begin() ae:on_stage_begin()
end end
elseif not ae.finished and ae.on_step then elseif not ae.finished and ae.on_step then
ae:on_step() ae:on_step()
end end
addbars(ae)
--update_bars(ae)
end end
etime = etime - dtime etime = etime - dtime
if etime > 0 then return end if etime > 0 then return end
@ -81,6 +104,8 @@ minetest.register_globalstep(check_events)
mcl_events.register_event("infestation",{ mcl_events.register_event("infestation",{
max_stage = 5, max_stage = 5,
health = 1,
health_max = 1,
cond_start = function(self) cond_start = function(self)
local r = {} local r = {}
for _,p in pairs(minetest.get_connected_players()) do for _,p in pairs(minetest.get_connected_players()) do
@ -93,28 +118,45 @@ mcl_events.register_event("infestation",{
end, end,
on_start = function(self) on_start = function(self)
self.mobs = {} self.mobs = {}
self.health_max = 1
self.health = 0
end, end,
cond_progress = function(self) cond_progress = function(self)
local m = {} local m = {}
local h = 0
for k,o in pairs(self.mobs) do for k,o in pairs(self.mobs) do
if o and o:get_pos() then table.insert(m,o) end if o and o:get_pos() then
local l = o:get_luaentity()
h = h + l.health
table.insert(m,o)
end end
end
self.health = h
self.percent = math.max(0,(self.health / self.health_max ) * 100)
if #m < 1 then if #m < 1 then
minetest.log("INFESTATION stage "..self.stage.." completed")
return true end return true end
self.mobs = m self.mobs = m
end, end,
on_stage_begin = function(self) on_stage_begin = function(self)
minetest.log("event "..self.name.." stage "..self.stage.." begin...") self.health_max = 1
for i=1,5 * self.stage do for i=1,5 * self.stage do
local m = mcl_mobs.spawn(vector.add(self.pos,vector.new(math.random(20)-10,0,math.random(20)-10)),"mobs_mc:silverfish") local m = mcl_mobs.spawn(vector.add(self.pos,vector.new(math.random(20)-10,0,math.random(20)-10)),"mobs_mc:silverfish")
if m then local l = m:get_luaentity()
if l then
self.health_max = self.health_max + l.health
table.insert(self.mobs,m) table.insert(self.mobs,m)
end end
end end
end, end,
cond_complete = function(self) cond_complete = function(self)
return self.stage >= self.max_stage local m = {}
for k,o in pairs(self.mobs) do
if o and o:get_pos() then
local l = o:get_luaentity()
table.insert(m,o)
end
end
return self.stage >= self.max_stage and #m < 1
end, end,
on_complete = function(self) on_complete = function(self)
minetest.log("INFESTATION complete") minetest.log("INFESTATION complete")

View File

@ -1,3 +1,3 @@
name = mcl_events name = mcl_events
author = cora author = cora
depends = mcl_mobs depends = mcl_mobs,mcl_bossbars