aboutsummaryrefslogtreecommitdiff
path: root/gamemode/client/healthbar.lua
blob: d9a050c7febd34fed2a06ed6d05f20291fc9a180 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
hook.Add( "HUDShouldDraw", "HideHUD", function( name )
	if name == "CHudHealth" then return false end
end )

--A function that rotates a point around another point
local function rotatepoint(x,y,cx,cy,rot)
	rot = math.rad(rot)
	local cs,sn = math.cos(rot),math.sin(rot)
	local px = x * cs - y * sn;
	local py = x * sn + y * cs;
	return px,py
end

--Add a function to the draw library to draw elipses for blood
local segments = 20
local fade = 0.5
function draw.DrawElipse(x,y,radius,elong,rotation)
	rotation = math.rad(rotation)
	local cir = {}
	table.insert( cir, { x = x, y = y, u = fade, v = fade } )
	for i = 0, segments do
		local a = math.rad( ( i / segments ) * -360 )
		local xu,yu = math.sin(a),math.cos(a)
		local xpos = xu * radius * elong
		local ypos = yu * radius
		local cs,sn = math.cos(rotation),math.sin(rotation)
		xpos,ypos = xpos * cs - ypos * sn, xpos * sn + ypos * cs
		table.insert( cir, {
			x = x + xpos,
			y = y + ypos,
			u = 1,
			v = 1 } )
	end
	surface.DrawPoly( cir )
end

local blobs = {}
local lastpos = 0
local delpoint = ScrH()
hook.Add( "Tick", "Hudpaintbloodtick",function()
	for k,v in pairs(blobs) do
		if v.y > delpoint then
			blobs[k] = nil
		else
			blobs[k] = {
				["x"] = v.x + v.xv,
				["y"] = v.y + v.yv,
				["xv"] = v.xv,
				["yv"] = v.yv + 1,
			}
		end
	end
end)

local lasthealth
local width,height = ScrW(),ScrH()
local padding = height / 32
local barheight = height / 16
local obarlength = width / 4
local barlength = obarlength
local xs,ys = padding,height - padding - barheight
hook.Add( "HUDPaint", "HUD_DrawHealth", function()

	--Spawn a bunch of blobs if our health has changed
	local health = LocalPlayer():Health()
	if lasthealth == nil then
		lasthealth = health
	end
	if health ~= lasthealth then
		local difference = lasthealth - health
		barlength = obarlength * (health/100)
		for i=0,difference*3 do
			local rtime = math.random()
			timer.Simple(rtime,function()
				local yvel = -20 + math.random(10)
				local xvel = (math.random()*5)
				local xpos = padding + xs + barlength - difference - 5
				local ypos = ys + (math.random() * barheight)
				table.insert(blobs,{
					x = xpos,
					y = ypos,
					xv = xvel,
					yv = yvel,
				})
			end)
		end
		lasthealth = health
	end

	--Draw the current health thing
	surface.SetDrawColor(100,100,100,100)
	surface.DrawRect( xs, ys, obarlength, barheight)
	surface.SetDrawColor( 150, 0, 0, 255 )
	surface.DrawRect( xs, ys, barlength, barheight )

	--Draw the animation for blobs
	for k,v in pairs(blobs) do
		--Elongation is based on velocity
		local elong = (v.yv^2 + v.xv^2)^0.5
		elong = elong/5
		elong = math.Clamp(elong,1,3)
		--Rotation is also based on velcotiy
		local rot = math.deg(math.atan(v.yv/v.xv))
		draw.DrawElipse(v.x,v.y,10/elong,elong,rot)
	end
end )