edit | blame | history | raw
local CONST   = require 'config.const'
local Balance = require 'config.balance'

local M = {}

local spawn_points = {}

local ITEM_POOL = {
    { key = CONST.ITEM_POISON,   weight = 15 },
    { key = CONST.ITEM_KNIFE,    weight = 10 },
    { key = CONST.ITEM_ARMOR,    weight = 15 },
    { key = CONST.ITEM_SMOKE,    weight = 15 },
    { key = CONST.ITEM_MASK,     weight = 8  },
    { key = CONST.ITEM_TRACKER,  weight = 15 },
    { key = CONST.ITEM_SILENCER, weight = 10 },
    { key = CONST.ITEM_JAMMER,   weight = 12 },
}

-- ---------------------------------------------------------
-- 初始化
-- ---------------------------------------------------------
function M.init()
    local areas = clicli.area.get_circle_areas_by_tag('item_spawn')
    for _, area in ipairs(areas) do
        local sp = {
            area          = area,
            current_item  = nil,
            respawn_timer = nil,
        }
        M.spawn_item(sp)
        table.insert(spawn_points, sp)
    end

    clicli.game:subscribe_event('单位-获得物品', function(trg, data)
        local unit = data.unit
        if unit and unit:has_tag('player_controlled') then
            local Suspicion = require 'systems.suspicion'
            Suspicion.add(unit, 3)

            M.on_item_picked_by_unit(data.item)
        end
    end)
end

-- ---------------------------------------------------------
-- 在刷新点生成物品
-- ---------------------------------------------------------
function M.spawn_item(sp)
    local item_key = M.roll_item()
    local pos = sp.area:get_center_point()
    local item = clicli.item.create_item(pos, item_key)
    sp.current_item = item
end

-- ---------------------------------------------------------
-- 物品权重随机
-- ---------------------------------------------------------
function M.roll_item()
    local total = 0
    for _, e in ipairs(ITEM_POOL) do
        total = total + e.weight
    end
    local r = math.random(total)
    local acc = 0
    for _, e in ipairs(ITEM_POOL) do
        acc = acc + e.weight
        if r <= acc then return e.key end
    end
    return ITEM_POOL[1].key
end

-- ---------------------------------------------------------
-- 被拾取后,找到对应刷新点并开始刷新倒计时
-- ---------------------------------------------------------
function M.on_item_picked_by_unit(item)
    for _, sp in ipairs(spawn_points) do
        if sp.current_item == item then
            M.on_item_picked(sp)
            break
        end
    end
end

function M.on_item_picked(sp)
    sp.current_item = nil
    local delay = Balance.ITEM_RESPAWN_TIME

    local GameMgr = require 'core.game_manager'
    if GameMgr.get_state() == 'overtime' then
        delay = delay * 0.5
    end

    sp.respawn_timer = clicli.timer.wait(delay, function()
        M.spawn_item(sp)
    end)
end

return M