Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  *
  3.  * Title: Old spectators list
  4.  * Author: april#0001
  5.  * Description: Recreates the V1's spectators list
  6.  *
  7. */
  8.  
  9. //region menu
  10.  
  11. // Backups our positions
  12. const window_x = UI.AddSliderInt("window_x", 0, Global.GetScreenSize()[0])
  13. const window_y = UI.AddSliderInt("window_y", 0, Global.GetScreenSize()[1])
  14.  
  15. //endregion
  16.  
  17. //region functions
  18.  
  19. /**
  20.  * Updates the visibility of our menu elements
  21.  */
  22. function update_menu()
  23. {
  24.     UI.SetEnabled("Misc", "JAVASCRIPT", "Script items", "window_x", false)
  25.     UI.SetEnabled("Misc", "JAVASCRIPT", "Script items", "window_y", false)
  26. }
  27.  
  28. // Update it whenever the script is activated.
  29. update_menu();
  30.  
  31. /**
  32.  * Gets the names of the players spectating you
  33.  *
  34.  * @returns {[]}
  35.  */
  36. function get_spectators()
  37. {
  38.     var specs = [];
  39.     const players = Entity.GetPlayers();
  40.  
  41.     for (i = 0; i < players.length; i++)
  42.     {
  43.         const cur = players[i];
  44.  
  45.         if (Entity.GetProp(cur, "CBasePlayer", "m_hObserverTarget") != "m_hObserverTarget") {
  46.             const obs = Entity.GetProp(cur, "CBasePlayer", "m_hObserverTarget")
  47.  
  48.             if (obs === Entity.GetLocalPlayer())
  49.             {
  50.                 const name = Entity.GetName(cur);
  51.                 specs.push(name);
  52.             }
  53.         }
  54.     }
  55.  
  56.     return specs;
  57. }
  58.  
  59. /**
  60.  * Checks if a point is inside a perimeter
  61.  *
  62.  * @param vec
  63.  * @param x
  64.  * @param y
  65.  * @param x2
  66.  * @param y2
  67.  * @returns {boolean}
  68.  */
  69. function in_bounds(vec, x, y, x2, y2)
  70. {
  71.     return (vec[0] > x) && (vec[1] > y) && (vec[0] < x2) && (vec[1] < y2)
  72. }
  73.  
  74. /**
  75.  * Where the magic happens
  76.  */
  77. function main()
  78. {
  79.     // Get our drawing properties
  80.     const names = get_spectators();
  81.     const x = UI.GetValue("Misc", "JAVASCRIPT", "Script items", "window_x"),
  82.             y = UI.GetValue("Misc", "JAVASCRIPT", "Script items", "window_y");
  83.  
  84.     // Rainbow color for our bar
  85.     const rainbow = [
  86.         Math.floor(Math.sin(Global.Realtime() * 2) * 127 + 128),
  87.         Math.floor(Math.sin(Global.Realtime() * 2 + 2) * 127 + 128),
  88.         Math.floor(Math.sin(Global.Realtime() * 2 + 4) * 127 + 128),
  89.         255
  90.     ];
  91.  
  92.     // Draw the spectators list
  93.     Render.Rect(x - 1, y - 1, 202, 61 + 15 * (names.length - 1), [2, 2, 2, 100]);
  94.     Render.FilledRect(x, y, 200, 60 + 15 * (names.length - 1), [55, 55, 55, 200]);
  95.     Render.Rect(x + 5, y + 5, 190, 50 + 15 * (names.length - 1), [2, 2, 2, 100]);
  96.     Render.FilledRect(x + 5, y + 5, 190, 50 + 15 * (names.length - 1), [25, 25, 25, 200]);
  97.     Render.FilledRect(x + 9, y + 25, 181, 3, rainbow);
  98.     Render.String(x + 100, y + 10, 1, "spectators (" + names.length + ")", [200, 200, 200, 200], 3);
  99.  
  100.     // For each player who's spectating us, draw their names
  101.     for (i = 0; i < names.length; i++)
  102.     {
  103.         Render.String(x + 100, y + 35 + 15 * i, 1, names[i], [200, 200, 200, 200], 3);
  104.     }
  105.  
  106.     // Handles the drag function
  107.     if (Global.IsKeyPressed(1)) {
  108.         // Getting our mouse pos
  109.         const mouse_pos = Global.GetCursorPosition();
  110.  
  111.         // Check if we're clicking and if we're in bounds of the drag area
  112.         if (in_bounds(mouse_pos, x, y, x + 200, y + 30)) {
  113.  
  114.             // Update values (not the most efficient way to do it but wtvr)
  115.             UI.SetValue("Misc", "JAVASCRIPT", "Script items", "window_x", mouse_pos[0] - 100);
  116.             UI.SetValue("Misc", "JAVASCRIPT", "Script items", "window_y", mouse_pos[1] - 20);
  117.         }
  118.     }
  119.  
  120. }
  121. //endregion
  122.  
  123. //region callbacks
  124.  
  125. // Callback our main function
  126. Global.RegisterCallback("Draw", "main")
  127.  
  128. //endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement