Advertisement
Guest User

Untitled

a guest
Apr 12th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.66 KB | None | 0 0
  1. local lc = require("engine")
  2. local g = require("global")
  3. local utf8 = require("utf8")
  4. local round
  5. round = lc.steelpan.utils.math.round
  6. local wait_after = 0.3
  7. local time_per_character
  8. time_per_character = function()
  9.   return lc.tr_text.glyph_duration or 0.1
  10. end
  11. local tmin = 1
  12. local box_width = 156
  13. local distance_from_character = 10
  14. local padding = 3
  15. local M = { }
  16. local current_character, current_text_lowres, current_text_hires, t, tmax, cursor_oldstatus
  17. local say
  18. say = function(character, text, time)
  19.   local cursor = require("ui.cursor")
  20.   setfenv(1, g.thread_registry:get_thread_environment())
  21.   t = 0
  22.   local len = utf8.len(text)
  23.   tmax = time or math.max(time_per_character() * len, tmin)
  24.   current_character = character
  25.   current_text_lowres = love.graphics.newText(g.font_lowres)
  26.   current_text_hires = love.graphics.newText(g.font_hires)
  27.   current_text_hires:setf(text, box_width / g.hires_scale, "center")
  28.   current_text_lowres:setf(text, box_width, "center")
  29.   if not (character.fake) then
  30.     character:stop_walking()
  31.     character._animation = character._talking_animations[character._direction]
  32.     character._animation:start()
  33.   end
  34.   wait_signal(character, "finished talking")
  35.   if not (character.fake) then
  36.     character._animation = character._standing_animations[character._direction]
  37.   end
  38.   return wait(wait_after)
  39. end
  40. lc.extend_global_thread_environment({
  41.   say = say
  42. })
  43. local finish
  44. finish = function()
  45.   lc.signals.emit(current_character, "finished talking")
  46.   current_character, current_text_lowres, current_text_hires = nil
  47.   local cursor = require("ui.cursor")
  48. end
  49. M.update = function(dt)
  50.   if not (current_text_lowres) then
  51.     return
  52.   end
  53.   t = t + dt
  54.   if t > tmax then
  55.     return finish()
  56.   end
  57. end
  58. M.skip = function()
  59.   if not (current_text_lowres) then
  60.     return
  61.   end
  62.   return finish()
  63. end
  64. local left_shift = box_width / 2
  65. local draw
  66. draw = function()
  67.   local current_text = g.use_hires_font and current_text_hires or current_text_lowres
  68.   local font_scale = g.use_hires_font and g.hires_scale or 1
  69.   local h = font_scale * current_text:getHeight()
  70.   local pos_x, pos_y
  71.   if current_character.height then
  72.     pos_x = current_character._position.x
  73.     pos_y = math.max(padding, current_character._position.y - current_character.height)
  74.   else
  75.     pos_x, pos_y = current_character._spk.point:unpack()
  76.   end
  77.   pos_y = math.max(padding, pos_y - distance_from_character - h)
  78.   local w = round(font_scale * current_text:getWidth() / 2)
  79.   pos_x = math.max(w + padding, pos_x)
  80.   pos_x = math.min(g.game_width - w - padding, pos_x)
  81.   local scale = g.use_hires_font and g.scale or 1
  82.   local xshift = g.use_hires_font and g.xshift or 0
  83.   local yshift = g.use_hires_font and g.yshift or 0
  84.   love.graphics.push("all")
  85.   love.graphics.setColor(current_character._spk.colour)
  86.   love.graphics.scale(scale)
  87.   love.graphics.translate(xshift, yshift)
  88.   love.graphics.setFont(g.use_hires_font and g.font_hires or g.font_lowres)
  89.   if g.use_hires_font then
  90.     love.graphics.setShader(g.hires_shader_outer)
  91.     love.graphics.draw(current_text, pos_x - left_shift + 1, pos_y, 0, font_scale)
  92.     love.graphics.setShader(g.hires_shader_inner)
  93.   end
  94.   love.graphics.draw(current_text, pos_x - left_shift + 1, pos_y, 0, font_scale)
  95.   return love.graphics.pop()
  96. end
  97. M.draw_canvas = function()
  98.   if not (current_text_lowres) then
  99.     return
  100.   end
  101.   if not g.use_hires_font then
  102.     return draw()
  103.   end
  104. end
  105. M.draw_screen = function()
  106.   if not (current_text_lowres) then
  107.     return
  108.   end
  109.   if g.use_hires_font then
  110.     return draw()
  111.   end
  112. end
  113. M.is_on = function(self)
  114.   return not not current_text_lowres
  115. end
  116. return M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement