Jump to content

Health bar moving backwards


JeViCo

Recommended Posts

Hello again :D I'm making some kind of HUD and i want to know, how to make health bar move backwards?

local sx,sy = guiGetScreenSize()
local px,py = 1280,720
local x,y =  (sx/px), (sy/py)        

dxDrawLine(x*447 - 1, y*615 - 1, x*447 - 1, y*636, tocolor(0, 0, 0, 255), 1, false)
dxDrawLine(x*682, y*615 - 1, x*447 - 1, y*615 - 1, tocolor(0, 0, 0, 255), 1, false)
dxDrawLine(x*447 - 1, y*636, x*682, y*636, tocolor(0, 0, 0, 255), 1, false)
dxDrawLine(x*682, y*636, x*682, y*615 - 1, tocolor(0, 0, 0, 255), 1, false)
dxDrawRectangle(x*447, y*615, x*235/100*health, y*21, tocolor(255, 0, 0, 255), false) --health

y4CvLeb.png

i think i'm close but still can't figure it out :/

Edited by JeViCo
Link to comment

use this

dxDrawRectangleBox(x*447, y*615, x*235/100, y*21, math.floor(getElementHealth(localPlayer)), tocolor(0, 0, 0, 150))

function dxDrawRectangleBox(startX, startY, sizeX, sizeY, playerStat, color)
    dxDrawRectangle(startX, startY, sizeX, sizeY, tocolor(107, 107, 107, 180)) --box
    dxDrawRectangle(startX-2, startY-2, 2, sizeY+4, tocolor(0, 0, 0, 220)) --left
    dxDrawRectangle(startX+sizeX, startY-2, 2, sizeY+4, tocolor(0, 0, 0, 220)) --right
    dxDrawRectangle(startX, startY-2, sizeX, 2, tocolor(0, 0, 0, 220)) --up
    dxDrawRectangle(startX, startY+sizeY, sizeX, 2, tocolor(0, 0, 0, 220)) --down    
    dxDrawRectangle(startX, startY, sizeX/100*playerStat, sizeY, color) --stat
end

 

Edited by TheMOG
Link to comment
12 minutes ago, TheMOG said:

use this


dxDrawRectangleBox(x*447, y*615, x*235/100, y*21, math.floor(getElementHealth(localPlayer)), tocolor(0, 0, 0, 150))

function dxDrawRectangleBox(startX, startY, sizeX, sizeY, playerStat, color)
    dxDrawRectangle(startX, startY, sizeX, sizeY, tocolor(107, 107, 107, 180)) --box
    dxDrawRectangle(startX-2, startY-2, 2, sizeY+4, tocolor(0, 0, 0, 220)) --left
    dxDrawRectangle(startX+sizeX, startY-2, 2, sizeY+4, tocolor(0, 0, 0, 220)) --right
    dxDrawRectangle(startX, startY-2, sizeX, 2, tocolor(0, 0, 0, 220)) --up
    dxDrawRectangle(startX, startY+sizeY, sizeX, 2, tocolor(0, 0, 0, 220)) --down    
    dxDrawRectangle(startX, startY, sizeX/100*playerStat, sizeY, color) --stat
end

 

My code for his other thread that I posted above has everything you'll ever need. If he wants to draw lines around it then he can. My code is Auto relative, he puts in his resolution then positions it and sizes it, it will resize and reposition for other resolutions. He can also change the colors of it. Mainly the progress of the progress bar, which is controlled when player loses health.

styles = {}
styles['progressBar'] = {
	['default'] = {
		bgColor = tocolor(0,0,0,180),
		barColor = tocolor(255,255,255,200),
		textColor = tocolor(255,0,0,255)
	}
}

local sx,sy = guiGetScreenSize()
local sw,sh = 1360,768-- Change this to your resolution and use absolute values when creating elements.
local draw = {}
local validTypes = {'progressBar'}

function dxRect(...)
	arg[1],arg[2],arg[3],arg[4] = arg[1]/sw*sx,arg[2]/sh*sy,arg[3]/sw*sx,arg[4]/sh*sy
	return dxDrawRectangle(unpack(arg))
end

function dxText(...)
	arg[2],arg[3],arg[4],arg[5],arg[7] = arg[2]/sw*sx,arg[3]/sh*sy,(arg[4]+arg[2])/sw*sx,(arg[5]+arg[3])/sh*sy,(arg[7] or 1)/sw*sx
	return dxDrawText(unpack(arg))
end

function dx(type,theme,x,y,w,h,visible)
	for i=1,#validTypes do
		if type == validTypes[i] then
			local self = {}
			self.type = type
			self.x = x or 0
			self.y = y or 0
			self.w = w or 0
			self.h = h or 0
			self.style = styles[self.type][theme] or styles[self.type]['default'] 
			self.visible = visible or true
			
			self.destroy = function()
				for i=1,#draw do
					if draw[i] == self then
						table.remove(draw,i)
					end
				end
			end
			
			return self
		end
	end
	return false
end

function dxCreateProgressBar(text,x,y,w,h)
	local self = dx('progressBar','default',x,y,w,h,true)
	if self then
		self.progress = 0
		
		local pbText = text
		self.draw = function()
			dxRect(self.x,self.y,self.w,self.h,self.style.bgColor)
			if string.find(pbText,'%progress%',1,true) then pbText = string.gsub(pbText,'%progress%',tostring(math.floor(self.progress))..'%') end
			dxText(pbText,self.x,self.y,self.w,self.h,self.style.textColor)
			dxRect(self.x,self.y,(self.w/100)*self.progress,self.h,self.style.barColor)
		end
		
		table.insert(draw,self)
		return self
	end
end

addEventHandler('onClientRender',root,function()
	for i=1,#draw do
		if draw[i].visible then
			draw[i].draw()
		end
	end
end)

local progressBar = dxCreateProgressBar('Health %progress%',500,500,600,200)-- create a progress bar like this
setTimer(function()
	local health = getPlayerHealth(localPlayer)-- if its 100 then use it, if its 200 then divide it by 2
	if getPlayerMaxHealth(localPlayer) > 100 then
		health = health/2
	end
	progressBar.progress = health -- then set the progress
end,50,0)
-- you can also set colors like this
progressBar.style.barColor = tocolor(255,0,0,200)
progressBar.style.bgColor = tocolor(255,255,255,160)

 

Edited by ShayF
Link to comment

Try this:

local sx,sy = guiGetScreenSize()
local px,py = 1280,720
local x,y =  (sx/px), (sy/py)
local width = 235
local healthBarW = ( health / getPedMaxHealth() ) * width

dxDrawLine(x*447 - 1, y*615 - 1, x*447 - 1, y*636, tocolor(0, 0, 0, 255), 1, false)
dxDrawLine(x*682, y*615 - 1, x*447 - 1, y*615 - 1, tocolor(0, 0, 0, 255), 1, false)
dxDrawLine(x*447 - 1, y*636, x*682, y*636, tocolor(0, 0, 0, 255), 1, false)
dxDrawLine(x*682, y*636, x*682, y*615 - 1, tocolor(0, 0, 0, 255), 1, false)
dxDrawRectangle(x*447, y*615, healthBarW, y*21, tocolor(255, 0, 0, 255), false) --health


function getPedMaxHealth()
    assert(isElement(localPlayer) and (getElementType(localPlayer) == "ped" or getElementType(localPlayer) == "player"), "Bad argument @ 'getPedMaxHealth' [Expected ped/player at argument 1, got " .. tostring(localPlayer) .. "]")
    local stat = getPedStat(localPlayer, 24)
 
    local maxhealth = 100 + (stat - 569) / 4.31
    return math.max(1, maxhealth)
end

 

Edited by DNL291
added getPedMaxHealth
  • Thanks 1
Link to comment
15 hours ago, DNL291 said:

Try this:


local sx,sy = guiGetScreenSize()
local px,py = 1280,720
local x,y =  (sx/px), (sy/py)
local width = 235
local healthBarW = ( health / getPedMaxHealth() ) * width

dxDrawLine(x*447 - 1, y*615 - 1, x*447 - 1, y*636, tocolor(0, 0, 0, 255), 1, false)
dxDrawLine(x*682, y*615 - 1, x*447 - 1, y*615 - 1, tocolor(0, 0, 0, 255), 1, false)
dxDrawLine(x*447 - 1, y*636, x*682, y*636, tocolor(0, 0, 0, 255), 1, false)
dxDrawLine(x*682, y*636, x*682, y*615 - 1, tocolor(0, 0, 0, 255), 1, false)
dxDrawRectangle(x*447, y*615, healthBarW, y*21, tocolor(255, 0, 0, 255), false) --health


function getPedMaxHealth()
    assert(isElement(localPlayer) and (getElementType(localPlayer) == "ped" or getElementType(localPlayer) == "player"), "Bad argument @ 'getPedMaxHealth' [Expected ped/player at argument 1, got " .. tostring(localPlayer) .. "]")
    local stat = getPedStat(localPlayer, 24)
 
    local maxhealth = 100 + (stat - 569) / 4.31
    return math.max(1, maxhealth)
end

 

I tried to remake it and it worked. I copied health bar more to the right, removed old one and made (-healh) on coordinates. Thank you)

17 hours ago, #RooTs said:

x*235/+100*health 
or
x*235/-100*health

 

i was trying to manage with @DNL291's script until i opened wiki of dxDrawRectangle. It said that i use at least first coordinate + width + height. @#RooTs suggested to draw health bar backwards. Why not? Thanks everyone!

Edited by JeViCo
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...