local class = require("lib_class")
include("deathRollManager.lua")

DeathRollScript = class()

function DeathRollScript:init()
end

function DeathRollScript:missionStart()
    self.managers = {}
end

function DeathRollScript:checkShips()
    for i = 1, #mn.Ships do
        local ship = mn.Ships[i]

        if (ship:getTimeUntilExplosion() >= 0) then
            if (self.managers[ship:getSignature()] == nil) then
                self.managers[ship:getSignature()] = DeathRollManager(self, ship)
            end
        end
    end
end

function DeathRollScript:updateManagers(time)
    for i, v in pairs(self.managers) do
        if (v:isValid()) then
            v:update(time)
        end
    end
end

function DeathRollScript:cleanupManagers()
    for i, v in pairs(self.managers) do
        if (not v:isValid()) then
            self.managers[i] = nil
        end
    end
end

function DeathRollScript:onFrame(time)
    if (self.managers == nil) then
        return
    end

    self:checkShips()

    self:updateManagers(time)

    self:cleanupManagers()
end

function DeathRollScript:missionEnd()
    self.managers = nil
end

Globals.deathRollScript = DeathRollScript()
