Liniuss

Snowflakes

Jan 2nd, 2021
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.21 KB | None | 0 0
  1.  
  2.  
  3. local background_alpha = 0
  4. local snowflake_alpha = 0
  5.  
  6. local screen = engine.get_screen_size()
  7.  
  8. local new_checkbox = ui.add_check_box
  9.  
  10. local background_enabled = new_checkbox('background', 'snowflake_background', true)
  11. local in_game = new_checkbox('show in game', 'snowflake_ingame', false)
  12.  
  13. local function clamp(min, max, val)
  14.     if val > max then return max end
  15.     if val < min then return min end
  16.     return val
  17. end
  18.  
  19. local function draw_line(x, y, x1, y1, r, g, b, a)
  20.     renderer.line(vec2_t.new(x, y), vec2_t.new(x1, y1), color_t.new(r, g, b, a))
  21. end
  22.  
  23. local function draw_rect(x, y, w, h, r, g, b, a)
  24.     renderer.rect_filled(vec2_t.new(x, y), vec2_t.new(x + w, y + h), color_t.new(r, g, b, a))
  25. end
  26.  
  27. local function draw_snowflake(x, y, size)
  28.     local base = 4 + size
  29.  
  30.     draw_line(x - base, y - base, x + base + 1, y + base + 1, 255, 255, 255, snowflake_alpha - 75)
  31.     draw_line(x + base, y - base, x - base, y + base, 255, 255, 255, snowflake_alpha - 75)
  32.  
  33.     base = 5 + size
  34.  
  35.     draw_line(x - base, y, x + base + 1, y, 255, 255, 255, snowflake_alpha - 75)
  36.     draw_line(x, y - base, x, y + base + 1, 255, 255, 255, snowflake_alpha - 75)
  37. end
  38.  
  39. local snowflakes = {}
  40. local time = 0
  41. local stored_time = 0
  42.  
  43. local function on_render()
  44.     local show_in_game = in_game:get_value()
  45.  
  46.     if background_enabled:get_value() then
  47.         if ui.is_visible() and background_alpha ~= 255 then
  48.             background_alpha = clamp(0, 255, background_alpha + 10)
  49.             snowflake_alpha = clamp(0, 255, snowflake_alpha + 10)
  50.         end
  51.  
  52.         if not ui.is_visible() and background_alpha ~= 0 then
  53.             background_alpha = clamp(0, 255, background_alpha - 10)
  54.             snowflake_alpha = clamp(0, 255, snowflake_alpha - 10)
  55.         end
  56.  
  57.         if ui.is_visible() or background_alpha ~= 0 then
  58.             draw_rect(0, 0, screen.x, screen.y, 0, 0, 0, background_alpha - 90)
  59.         end
  60.     end
  61.  
  62.     if not show_in_game and not ui.is_visible() then
  63.         return
  64.     end
  65.  
  66.     if show_in_game then
  67.         snowflake_alpha = 255
  68.     end
  69.  
  70.     local frametime = globalvars.get_frame_time()
  71.  
  72.     time = time + frametime
  73.  
  74.     if #snowflakes < 128 then
  75.         if time > stored_time then
  76.             stored_time = time
  77.  
  78.             table.insert(snowflakes, {
  79.                 math.random(10, screen.x - 10),
  80.                 1,
  81.                 math.random(1, 3),
  82.                 math.random(-60, 60) / 100,
  83.                 math.random(-3, 0)
  84.             })
  85.         end
  86.     end
  87.  
  88.     local fps = 1 / frametime
  89.  
  90.     for i = 1, #snowflakes do
  91.         local snowflake = snowflakes[i]
  92.         local x, y, vspeed, hspeed, size = snowflake[1], snowflake[2], snowflake[3], snowflake[4], snowflake[5]
  93.  
  94.         if screen.y <= y then
  95.             snowflake[1] = math.random(10, screen.x - 10)
  96.             snowflake[2] = 1
  97.             snowflake[3] = math.random(1, 3)
  98.             snowflake[4] = math.random(-60, 60) / 100
  99.             snowflake[5] = math.random(-3, 0)
  100.         end
  101.  
  102.         draw_snowflake(x, y, size)
  103.  
  104.         snowflake[2] = snowflake[2] + vspeed / fps * 100
  105.         snowflake[1] = snowflake[1] + hspeed / fps * 100
  106.     end
  107. end
  108.  
  109. client.register_callback('paint', on_render)
  110.  
  111.  
Advertisement
Add Comment
Please, Sign In to add comment