Advertisement
offyerrocker

addon.lua

Mar 16th, 2022
1,129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.85 KB | None | 0 0
  1. --HEY YOU. If you're trying to change what's displayed on the left/right crosshair sides, scroll down to line 50.
  2.  
  3.  
  4. --this is a moderately advanced example of an Advanced Addon,
  5. --so i suggest that beginners looking to create new crosshairs use a different addon for reference/template purposes,
  6. --since i removed most of the comments here that don't pertain to the novel nature of this particular crosshair addon
  7. --edit: this version displays armor instead of health
  8. local size = 48
  9. local hl2_red = Color("bb2d12")
  10. local hl2_orange = Color("FFA000")
  11. local hl2_yellow = Color("FFD040") --these don't really matter since i opted not to make the crosshair unrecolorable, but you could re-enable it
  12. local id = "crosshev"
  13. local addon_data = {
  14.     name = "CrossHEV",
  15.  
  16.     update_func = function(t,dt,data) --(optional) this is the update function. unless you want your crosshair to do something extra fancy like this one, you can safely leave this out, but i suspect you might in fact want such a thing since you're looking at this example.
  17.         local player = managers.player:local_player()
  18.         if alive(player) then
  19.             local parts = data.parts
  20.             local panel_w = data.panel_w
  21.             local panel_h = data.panel_h
  22.             local crosshair_data = data.crosshair_data
  23.             local weapon = data.weapon_base
  24.             local settings = data.settings
  25.             local user_setting_color = Color(settings.color) --as you might have guessed, this is the user's selected color
  26.             --settings.color is a string hexadecimal representation, not an actual color object, so we need to convert it with the Color() constructor first
  27.             local IMAGE_SIZE = crosshair_data.CROSSHAIR_IMAGE_SIZE
  28.             local SCALED_SIZE = crosshair_data.SCALED_SIZE
  29.             local THRESHOLD_EMPTY = crosshair_data.THRESHOLD_EMPTY
  30.             local THRESHOLD_LOW = crosshair_data.THRESHOLD_LOW
  31.             local dmgext = player:character_damage()
  32.             local health_ratio = dmgext:health_ratio()
  33.             local armor_ratio = dmgext:armor_ratio()
  34.             local ammo_ratio = 1
  35.             local ammo_total = 1
  36.             local ammo_left = 1
  37.             if weapon.get_ammo_max_per_clip and weapon.get_ammo_remaining_in_clip then
  38.                 ammo_total = weapon:get_ammo_max_per_clip()
  39.                 ammo_left = weapon:get_ammo_remaining_in_clip()
  40.             elseif weapon._ammo and weapon._ammo.get_ammo_max_per_clip and weapon._ammo.get_ammo_remaining_in_clip then
  41.                 ammo_total = weapon._ammo:get_ammo_max_per_clip()
  42.                 ammo_left = weapon._ammo:get_ammo_remaining_in_clip()
  43.             end
  44.             if ammo_total ~= 0 then
  45.                 ammo_ratio = ammo_left/ammo_total
  46.             end
  47.            
  48.            
  49.            
  50.             --all you need to do is change the value assignment for right_ratio or left_ratio.
  51.             --Change "health_ratio" or "armor_ratio" (no quotation marks) to "health_ratio", "armor_ratio", or "ammo_ratio" (also no quotation marks) depending on what you want on whichever side you want. It is really that easy.
  52.             local left_ratio = armor_ratio
  53.             local right_ratio = health_ratio
  54.            
  55.             for index,bitmap in ipairs(parts) do
  56.                 if index == 1 then
  57.                     --right outline;
  58.                    
  59.                     if left_ratio <= THRESHOLD_EMPTY then
  60.                         bitmap:set_visible(math.sin(crosshair_data.EMPTY_FLASH_SPEED * t) > 0)
  61.                         bitmap:set_color(crosshair_data.COLOR_RED)
  62.                     else
  63.                         bitmap:set_color(user_setting_color)
  64.                         bitmap:show()
  65.                     end
  66.                 elseif index == 2 then
  67.                     --left outline;
  68.                    
  69.                     if right_ratio <= THRESHOLD_EMPTY then
  70.                         bitmap:set_visible(math.sin(crosshair_data.EMPTY_FLASH_SPEED * t) > 0)
  71.                         bitmap:set_color(crosshair_data.COLOR_RED)
  72.                     else
  73.                         bitmap:set_color(user_setting_color)
  74.                         bitmap:show()
  75.                     end
  76.                 elseif index == 3 then
  77.                     --left fill
  78.                    
  79.                     bitmap:set_texture_rect(0,IMAGE_SIZE * (1 - left_ratio),IMAGE_SIZE,IMAGE_SIZE * left_ratio)
  80.                     bitmap:set_h(SCALED_SIZE * left_ratio)
  81.                     bitmap:set_y(((data.panel_h - SCALED_SIZE) / 2) + ((1 - left_ratio) * SCALED_SIZE))
  82.                    
  83.                     if left_ratio <= THRESHOLD_EMPTY then
  84.                         bitmap:set_color(crosshair_data.COLOR_RED)
  85.                     elseif left_ratio <= THRESHOLD_LOW then
  86.                         bitmap:set_color(crosshair_data.COLOR_ORANGE)
  87.                     else
  88.                         bitmap:set_color(crosshair_data.COLOR_YELLOW)
  89.                     end
  90.                    
  91.                 elseif index == 4 then
  92.                     --right fill
  93.                
  94.                     bitmap:set_texture_rect(0,IMAGE_SIZE * (1 - right_ratio),IMAGE_SIZE,IMAGE_SIZE * right_ratio)
  95.                     bitmap:set_h(SCALED_SIZE * right_ratio)
  96.                     bitmap:set_y(((data.panel_h - SCALED_SIZE) / 2) + ((1 - right_ratio) * SCALED_SIZE))
  97.                    
  98.                     if right_ratio <= THRESHOLD_EMPTY then
  99.                         bitmap:set_color(crosshair_data.COLOR_RED)
  100.                     elseif right_ratio <= THRESHOLD_LOW then
  101.                         bitmap:set_color(crosshair_data.COLOR_ORANGE)
  102.                     else
  103.                         bitmap:set_color(crosshair_data.COLOR_YELLOW)
  104.                     end
  105.                 end
  106.             end
  107.         end
  108.     end,
  109.     CROSSHAIR_IMAGE_SIZE = 96, --textures are 96x96
  110.     SCALED_SIZE = size,
  111.     COLOR_YELLOW = hl2_yellow,
  112.     COLOR_ORANGE = hl2_orange,
  113.     COLOR_RED = hl2_red,
  114.     EMPTY_FLASH_SPEED = 720, --720 / 360 = 2 blinks per second
  115.     THRESHOLD_EMPTY = 0.1, --10%
  116.     THRESHOLD_LOW = 0.5, --50%
  117.     parts = {
  118.         {
  119.             texture_path = "freeman/hl2_empty_left",
  120.             rotation = 0,
  121.             angle = 270,
  122.             distance = 32,
  123.             w = size,
  124.             h = size,
  125.             UNRECOLORABLE = true,
  126.             color = hl2_yellow,
  127.             layer = 2
  128.         },
  129.         {
  130.             texture_path = "freeman/hl2_empty_right",
  131.             rotation = 0,
  132.             angle = 90,
  133.             distance = 32,
  134.             w = size,
  135.             h = size,
  136.             UNRECOLORABLE = true,
  137.             color = hl2_yellow,
  138.             layer = 2
  139.         },
  140.         {
  141.             texture_path = "freeman/hl2_fill_left",
  142.             rotation = 0,
  143.             angle = 270,
  144.             distance = 32,
  145.             w = size,
  146.             h = size,
  147.             UNRECOLORABLE = true,
  148.             color = hl2_yellow,
  149.             layer = 1
  150.         },
  151.         {
  152.             texture_path = "freeman/hl2_fill_right",
  153.             rotation = 0,
  154.             angle = 90,
  155.             distance = 32,
  156.             w = size,
  157.             h = size,
  158.             UNRECOLORABLE = true,
  159.             color = hl2_yellow,
  160.             layer = 1
  161.         },
  162.         {
  163.             texture_path = "freeman/hl2_fivedot",
  164.             w = size,
  165.             h = size,
  166.             layer = 1
  167.         }
  168.     }
  169. }
  170. return id, addon_data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement