Author Topic: "End Credits" script (rolling credits)  (Read 131 times)

alexgleason

  • Jr. Member
  • **
  • Posts: 85
  • Vegan on a Desert Island
    • View Profile
    • Vegan on a Desert Island
"End Credits" script (rolling credits)
« on: November 15, 2018, 09:54:14 pm »
I didn't share this before because it'd likely need to be adapted and parts of the code aren't super nice, but I figure maybe this will benefit someone.

It requires a dialog called "end_credits", and it just rolls through it until it's done.
Code: Lua
  1. -- ♡ Copying is an act of love. Please copy and share.
  2.  
  3. -- Game credits. Plays at the end of the game.
  4.  
  5. local end_credits = {}
  6.  
  7.  
  8. -- https://stackoverflow.com/a/8316375/8811886
  9. function build_array(...)
  10.   local arr = {}
  11.   for v in ... do
  12.     arr[#arr + 1] = v
  13.   end
  14.   return arr
  15. end
  16.  
  17.  
  18. -- Called when the menu is started
  19. function end_credits:on_started()
  20.   local lh = 12 -- line height in pixels
  21.   local speed = 24 -- scroll speed in px/s
  22.  
  23.   -- Credits dialog
  24.   self.dialog = sol.language.get_dialog("end_credits")
  25.  
  26.   -- Break dialog text into a table of lines
  27.   local lines = self.dialog.text
  28.   lines = lines:gsub("\r\n", "\n"):gsub("\r", "\n")
  29.   lines = build_array(lines:gmatch("([^\n]*)\n"))
  30.  
  31.  
  32.   -- Box where the credits go
  33.   self.credits_surface = sol.surface.create(
  34.     160, -- center the surface on the screen
  35.     (#lines * lh) -- surface is large enough to hold all lines
  36.       + 144 -- surface scrolls in from the bottom, so has a padding top equal to the screen size
  37.       + 16 -- room for 8px top and bottom padding
  38.   )
  39.  
  40.   -- Loop through all dialog lines and draw them
  41.   for i, line in ipairs(lines) do
  42.     local line_surface =  sol.text_surface.create({font="Comicoro", font_size=16, text=line})
  43.     -- Draw the given line
  44.     line_surface:draw(
  45.       self.credits_surface,
  46.       8, -- left padding
  47.       i * lh -- bump it down by line number and line height
  48.         + 8 -- top padding for whole box
  49.     )
  50.   end
  51.  
  52.   -- Animate the text box upwards
  53.   local m = sol.movement.create("straight")
  54.   m:set_angle(math.pi / 2)
  55.   m:set_speed(speed)
  56.   m:start(self.credits_surface)
  57.  
  58.   function m:on_position_changed()
  59.     local credits_surface = end_credits.credits_surface
  60.     local x, y = credits_surface:get_xy()
  61.     local w, h = credits_surface:get_size()
  62.  
  63.     log(string.format("Credits text box: (%d, %d)  ;;  y+h = %d", x, y, y+h))
  64.  
  65.     if y + h < 0 then
  66.       -- Credits are out of view, end the menu
  67.       log("Credits animation finished.")
  68.       self:stop()
  69.       sol.menu.stop(end_credits)
  70.     end
  71.   end
  72.  
  73. end
  74.  
  75.  
  76. -- Called each frame
  77. function end_credits:on_draw(dst_surface)
  78.   dst_surface:clear()
  79.   self.credits_surface:draw(dst_surface, 48, 144)
  80. end
  81.  
  82.  
  83. return end_credits

It's a menu, so put it in a .lua file (eg scripts/menus/end_credits.lua) and use something like this:

Code: Lua
  1. local end_credits = require("scripts/menus/end_credits")
  2.  
  3. sol.menu.start(game, end_credits)
  4.  
« Last Edit: November 15, 2018, 09:56:44 pm by alexgleason »
RIP Aaron Swartz

llamazing

  • Full Member
  • ***
  • Posts: 149
    • View Profile
Re: "End Credits" script (rolling credits)
« Reply #1 on: November 15, 2018, 11:24:03 pm »
Cool!

You should have it start playing a music file and then synchronize the scroll speed so that it ends as the music ends.

hmm... I don't think there is any way to get the duration of a music track from the Solarus API, though.