export default class Timeline {
    constructor() {
        this.actions = []
        this.time = 0
    }

    add (unit, action, delay) {
        this.actions.push({
            unit,
            action,
            time: this.time + delay
        })

        this.actions.sort((a, b) => +(a.time > b.time) || +(a.time === b.time) - 1)
    }

    next () {
        const action = this.actions.shift()
        const timeDif = action.time - this.time
        this.time += timeDif
        return action
    }

    unitNextAction (unit) {
        return this.actions.find(item => item.unit === unit)
    }
}

