Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --HEY YOU. If you're trying to change what's displayed on the left/right crosshair sides, scroll down to line 50.
- --this is a moderately advanced example of an Advanced Addon,
- --so i suggest that beginners looking to create new crosshairs use a different addon for reference/template purposes,
- --since i removed most of the comments here that don't pertain to the novel nature of this particular crosshair addon
- --edit: this version displays armor instead of health
- local size = 48
- local hl2_red = Color("bb2d12")
- local hl2_orange = Color("FFA000")
- 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
- local id = "crosshev"
- local addon_data = {
- name = "CrossHEV",
- 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.
- local player = managers.player:local_player()
- if alive(player) then
- local parts = data.parts
- local panel_w = data.panel_w
- local panel_h = data.panel_h
- local crosshair_data = data.crosshair_data
- local weapon = data.weapon_base
- local settings = data.settings
- local user_setting_color = Color(settings.color) --as you might have guessed, this is the user's selected color
- --settings.color is a string hexadecimal representation, not an actual color object, so we need to convert it with the Color() constructor first
- local IMAGE_SIZE = crosshair_data.CROSSHAIR_IMAGE_SIZE
- local SCALED_SIZE = crosshair_data.SCALED_SIZE
- local THRESHOLD_EMPTY = crosshair_data.THRESHOLD_EMPTY
- local THRESHOLD_LOW = crosshair_data.THRESHOLD_LOW
- local dmgext = player:character_damage()
- local health_ratio = dmgext:health_ratio()
- local armor_ratio = dmgext:armor_ratio()
- local ammo_ratio = 1
- local ammo_total = 1
- local ammo_left = 1
- if weapon.get_ammo_max_per_clip and weapon.get_ammo_remaining_in_clip then
- ammo_total = weapon:get_ammo_max_per_clip()
- ammo_left = weapon:get_ammo_remaining_in_clip()
- elseif weapon._ammo and weapon._ammo.get_ammo_max_per_clip and weapon._ammo.get_ammo_remaining_in_clip then
- ammo_total = weapon._ammo:get_ammo_max_per_clip()
- ammo_left = weapon._ammo:get_ammo_remaining_in_clip()
- end
- if ammo_total ~= 0 then
- ammo_ratio = ammo_left/ammo_total
- end
- --all you need to do is change the value assignment for right_ratio or left_ratio.
- --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.
- local left_ratio = armor_ratio
- local right_ratio = health_ratio
- for index,bitmap in ipairs(parts) do
- if index == 1 then
- --right outline;
- if left_ratio <= THRESHOLD_EMPTY then
- bitmap:set_visible(math.sin(crosshair_data.EMPTY_FLASH_SPEED * t) > 0)
- bitmap:set_color(crosshair_data.COLOR_RED)
- else
- bitmap:set_color(user_setting_color)
- bitmap:show()
- end
- elseif index == 2 then
- --left outline;
- if right_ratio <= THRESHOLD_EMPTY then
- bitmap:set_visible(math.sin(crosshair_data.EMPTY_FLASH_SPEED * t) > 0)
- bitmap:set_color(crosshair_data.COLOR_RED)
- else
- bitmap:set_color(user_setting_color)
- bitmap:show()
- end
- elseif index == 3 then
- --left fill
- bitmap:set_texture_rect(0,IMAGE_SIZE * (1 - left_ratio),IMAGE_SIZE,IMAGE_SIZE * left_ratio)
- bitmap:set_h(SCALED_SIZE * left_ratio)
- bitmap:set_y(((data.panel_h - SCALED_SIZE) / 2) + ((1 - left_ratio) * SCALED_SIZE))
- if left_ratio <= THRESHOLD_EMPTY then
- bitmap:set_color(crosshair_data.COLOR_RED)
- elseif left_ratio <= THRESHOLD_LOW then
- bitmap:set_color(crosshair_data.COLOR_ORANGE)
- else
- bitmap:set_color(crosshair_data.COLOR_YELLOW)
- end
- elseif index == 4 then
- --right fill
- bitmap:set_texture_rect(0,IMAGE_SIZE * (1 - right_ratio),IMAGE_SIZE,IMAGE_SIZE * right_ratio)
- bitmap:set_h(SCALED_SIZE * right_ratio)
- bitmap:set_y(((data.panel_h - SCALED_SIZE) / 2) + ((1 - right_ratio) * SCALED_SIZE))
- if right_ratio <= THRESHOLD_EMPTY then
- bitmap:set_color(crosshair_data.COLOR_RED)
- elseif right_ratio <= THRESHOLD_LOW then
- bitmap:set_color(crosshair_data.COLOR_ORANGE)
- else
- bitmap:set_color(crosshair_data.COLOR_YELLOW)
- end
- end
- end
- end
- end,
- CROSSHAIR_IMAGE_SIZE = 96, --textures are 96x96
- SCALED_SIZE = size,
- COLOR_YELLOW = hl2_yellow,
- COLOR_ORANGE = hl2_orange,
- COLOR_RED = hl2_red,
- EMPTY_FLASH_SPEED = 720, --720 / 360 = 2 blinks per second
- THRESHOLD_EMPTY = 0.1, --10%
- THRESHOLD_LOW = 0.5, --50%
- parts = {
- {
- texture_path = "freeman/hl2_empty_left",
- rotation = 0,
- angle = 270,
- distance = 32,
- w = size,
- h = size,
- UNRECOLORABLE = true,
- color = hl2_yellow,
- layer = 2
- },
- {
- texture_path = "freeman/hl2_empty_right",
- rotation = 0,
- angle = 90,
- distance = 32,
- w = size,
- h = size,
- UNRECOLORABLE = true,
- color = hl2_yellow,
- layer = 2
- },
- {
- texture_path = "freeman/hl2_fill_left",
- rotation = 0,
- angle = 270,
- distance = 32,
- w = size,
- h = size,
- UNRECOLORABLE = true,
- color = hl2_yellow,
- layer = 1
- },
- {
- texture_path = "freeman/hl2_fill_right",
- rotation = 0,
- angle = 90,
- distance = 32,
- w = size,
- h = size,
- UNRECOLORABLE = true,
- color = hl2_yellow,
- layer = 1
- },
- {
- texture_path = "freeman/hl2_fivedot",
- w = size,
- h = size,
- layer = 1
- }
- }
- }
- return id, addon_data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement