All pastes #265561 Raw Edit

Mine

public text v1 · immutable
·published 2006-12-03 11:36 UTC
rendered paste body
function GM:DrawSelectedUnitBoxes()
	local player = LocalPlayer()
	local playerpos = player:GetPos()					-- Get players position

	for entID,entTable in pairs(LocalPlayer():GetTable().SelectedUnits) do	-- Do the following for all the currently selected ents
		ent = Entity(entID)
		if player:IsValid() and ent:IsValid() and entTable.Owner == player then 	-- Check the ent is valid, and is owned by the player
		
			local entpos = ent:GetPos()				-- Get the ents current position
			local dist = playerpos - entpos
			local dist = dist:Length()				-- get the distance from the ent to the player
			
			if dist < 4096 then					-- If the ent is close enough....

				local alpha = 512 - (dist * 0.125)		-- Make an alpha value based on the distance
				if alpha > 255 then alpha = 255 end		-- Clip it to 255 if over (this causes the ents to only start fading at 1024 distance)

				local bc, tc = ent:WorldSpaceAABB()					-- Get the ents bounding box
				local Ta =	Vector(tc.x, tc.y, tc.z):ToScreen()			-- Get the screen coords of one of the corners

				if alpha > 0 and Ta.visible then					-- If alpha is more than zero, and the corner we just got is visible...

					local Tb,Tc,Td =	Vector(tc.x, bc.y, tc.z):ToScreen(),	-- ...get the rest of the bounding box corners
								Vector(bc.x, tc.y, tc.z):ToScreen(),	-- ( these first ones are the at the top of the ent)
								Vector(bc.x, bc.y, tc.z):ToScreen()

					local Ba,Bb,Bc,Bd =	Vector(tc.x, tc.y, bc.z):ToScreen(),	-- ( these are the ones at the bottom)
								Vector(tc.x, bc.y, bc.z):ToScreen(),
								Vector(bc.x, tc.y, bc.z):ToScreen(),
								Vector(bc.x, bc.y, bc.z):ToScreen()

					local x,y =	math.min(Ta.x, Tb.x, Tc.x, Td.x,   Ba.x, Bb.x, Bc.x, Bd.x),	-- Get the lowest x,y values (the top left of the 2d bounding box)
							math.min(Ta.y, Tb.y, Tc.y, Td.y,   Ba.y, Bb.y, Bc.y, Bd.y)

					local w,h =	math.max(Ta.x, Tb.x, Tc.x, Td.x,   Ba.x, Bb.x, Bc.x, Bd.x) - x, -- Get the highest x,y values, and subtract the lowest (Giving us the height & Width)
							math.max(Ta.y, Tb.y, Tc.y, Td.y,   Ba.y, Bb.y, Bc.y, Bd.y) - y

					surface.SetDrawColor( 30, 200, 30, alpha )
					surface.DrawOutlinedRect( x, y, w, h )				-- Draw the box
					surface.DrawOutlinedRect( x-1, y, w+2, h+1 )			-- Make it thicker
					
					local hBar = 0							-- Set healthbar height (default zero)
					local health, maxhealth = entTable.Health, entTable.MaxHealth	-- Get the ents health & max health

					if dist < 512 then	hBar = 5				-- Scale the health bar size based on distance from the player
					elseif dist < 1024 then	hBar = 4
					elseif dist < 2048 then	hBar = 3
					elseif dist < 3072 then	hBar = 2
					elseif dist < 4096 then	hBar = 1
					end

					surface.DrawRect( x-1, y - hBar - 1, w + 2, hBar + 2 )		-- Draw the green outline (the draw colour is still green from earlier)
					surface.SetDrawColor( 0, 0, 0, 255 )
					surface.DrawRect( x, y - hBar, w, hBar)				-- Draw the black bit
					surface.SetDrawColor( 255, 20, 30, 255 )
					surface.DrawRect( x, y - hBar, w * (health/maxhealth), hBar)	-- Draw the red bit (the actual health)
					
				end
			end
		else
		LocalPlayer():GetTable().SelectedUnits[entID]	= nil					-- If ent is not valid, remove it from the selected list so we don't call it again.
		LocalPlayer():GetTable().Units[entID]		= nil
		end
	end
end