stage1_ending ={
    time = 0,
    batvio_vx = 1,
    update = function(self)
        stage1_ending:update_scene()
    end,
    ended = function(self)
        stage1_ending:end_scene()
    end
}

function stage1_ending:update_scene()
    -- print("UPDATING "..self.time)

    if self.time==0 then
        start_scene(scenes.stage1_ending, nil, false)
    -- segona escena - batman escapa
    elseif self.time<70 then
        batvio.x =  batvio.x - self.batvio_vx
        if self.time % 6 == 0 then 
            self.batvio_vx= self.batvio_vx+1
        end
        batvio.update()
    elseif self.time==70 then
        batvio.enabled = false
        premiere.stage1_ending_init()
        elalien.stage1_ending_init()
    elseif self.time<200 then
    -- tercera escena - Aparició de elalien i premiere
        -- elalien 9,1,3 -> 8,6,3
        -- premiere 7,6,3 -> 8,1,3
        premiere.update()
        elalien.update()
    elseif self.time==200 then
        music.stop()
    -- quarta escena - dialeg entre els 3
        start_scene(scenes.stage1_ending_2, nil, false)
    elseif self.time==201 then
        premiere.stage1_ending_init2()
    elseif self.time<400 then
    -- quinta escena - premiere se'n va (vore dialegs draw)
        premiere.update()
    elseif self.time==400 then
        premiere.stage1_ending_init3()
        copter.hab=premiere.hab
        copter.x=premiere.x
        copter.y=premiere.y
        -- copter.init()
        copter.y=copter.y-16
        table.insert( actors, copter)
        music.play(audio_song_copter)
    elseif self.time<720 then
        -- sisena escena - premiere copter
        premiere.update()
        copter.x = premiere.x-32
        copter.y = premiere.y-48
        copter.update()
    elseif self.time==720 then
        start_scene(scenes.stage1_ending_3, nil, false)
    elseif self.time==721 then
        fade.fadeoutin()
    end
    
    surf.target(0)
    surf.cls(16)  
    world_draw()
    -- dialegs draw
    if self.time>201 and self.time<300 then
    -- bocadillo quinta escena (1)
        dialeg.show(premiere.x, premiere.y,
                   premiere.w, false,
                   "Volare uoh oh...", true)
    elseif self.time>=300 and self.time<400 then
    -- bocadillo quinta escena (2)
        dialeg.show(premiere.x, premiere.y,
                   premiere.w, false,
                   "Cantare uoh oh oh oh...", true)
    end
    score.draw()
    
    self.time = self.time + 1
    
    -- Acabar ending
    if self.time>=730 then
        self.time = 0
        states:next()
    end
end

function stage1_ending:end_scene()
    -- print("END")
    if self.time==0 then fade.fadein() end
    surf.target(0)
    surf.cls(16)
    font.current(font_sf)
    draw.text("Gracies per jugar!",20,100,2)
    self.time = self.time + 1
    if self.time == 200 then
        -- states:finish()
        states:executar("title",false)
    end
end

states:registrar("stage1_ending",{
    stage1_ending.update,
    stage1_ending.ended
})