; SPDX-FileCopyrightText: 2023 Jummit
;
; SPDX-License-Identifier: GPL-3.0-or-later

(local {: new-world} (require :world))
(local {: draw-portraits} (require :portraits))
(local {: opponents} (require :opponents))
(local {: draw-text} (require :text))
(local {: slide} (require :slide))

(fn timers [world]
  (each [id entity (ipairs world)]
    (case entity
      {:timer {: time-left : timeout &as timer}}
      (do
        (set timer.time-left (- time-left 1))
        (when (<= timer.time-left 0)
          (world.remove id)
          (timeout world))))))

(fn new-credits [previous]
  (_G.music 0)
  (local entities
         [{:text "Thanks for playing!" :x 69 :y 50}
          {:portrait (. opponents 2) :x 12 :y 83}
          {:text "Code and graphics by Jummit" :x 68 :y 100}
          {:text "Music by Max" :x 20 :y 165}
          {:portrait (. opponents 1) :x 173 :y 149}
          {:portrait (. opponents 4) :x 15 :y 212}
          {:text "Original game \"Ghosts\"\nby Alex Randolph" :x 78 :y 222}
          {:text "Names and design of\ncharacters and the game\nby 5pb."
           :x 10
           :y 298}
          {:portrait (. opponents 3) :x 173 :y 289}
          {:text "Made for the\nAutumn Lisp  Game Jam 2023" :x 44 :y 369}
          {:text "El. Psy. Kongroo." :x 74 :y 430}
          {:timer {:time-left 3480
                   :timeout (fn [world]
                              (each [_ entity (ipairs world)]
                                (when entity.sliding
                                  (set entity.sliding nil))))}}
          {:timer {:time-left 3800
                   :timeout (fn [world]
                              (_G.music 1)
                              (set world.next previous))}}])
  (each [_ entity (ipairs entities)]
    (when entity.y
      (set entity.sliding {:y -0.1})))
  (local world (new-world [(fn [] (_G.cls 0))
                           draw-portraits
                           slide
                           draw-text
                           timers] entities))
  world)

{: new-credits}