Skip to content

Custom Gun Lua: Projectile explodes after attack key is released

Vuthakral edited this page Mar 15, 2022 · 1 revision

About

Author: Vuthakral
Originally From: Draconic Halo UNSC SWEPs
Description: This is a bit of custom code which mimics how the Halo Reach grenade launcher works -- mostly. This will make it so when you fire a projectile, you can hold it down for its entire lifetime and it will explode when you release your primary attack key.

This code also has the EMP effect of Halo Reach's grenade launcher implemented into it, but can be easily removed by removing the line which holds the code of self.PTable[1].EMP = true.

Code:

SWEP.VElements = {
	["Screen"] = { type = "Quad", bone = "b_gun", rel = "", pos = Vector(5.41, -1.85, 5.32), angle = Angle(0, -90, 48.902), size = 0.0025, draw_func = nil},
	["Screen2"] = { type = "Quad", bone = "b_gun", rel = "", pos = Vector(5.787, -1.8, 5.7), angle = Angle(0, -90, 48.902), size = 0.0058, draw_func = nil},
	["Screen3"] = { type = "Quad", bone = "b_gun", rel = "", pos = Vector(5.9, -1.74, 5.8), angle = Angle(0, -90, 48.902), size = 0.0028, draw_func = nil}
}

function SWEP:DoCustomInitialize()
	local ply = self:GetOwner()
end

function SWEP:DoCustomPrimaryAttackEvents()
	local ply = self:GetOwner()
	if !ply:IsPlayer() then return end

	timer.Simple(0.1, function()
		if !self.PTable then return end
		if !self.PTable[1] then return end
		if ply:KeyDown(IN_ATTACK) == false then 
			self.PTable[1].ManualDetonation = false
		else
			self.PTable[1].ManualDetonation = true
			self.PTable[1].EMP = true
		end
	end)
end

function SWEP:DoCustomThink()
	local ply = self:GetOwner()
	local m1 = ply:KeyDown(IN_ATTACK)
	local projectiles = self.PTable
	local emp = self:GetNWBool("EMP")
	local dist = self:GetNWFloat("Distance")
	
	if projectiles && ply:IsPlayer() then
		local nade = nil
		if projectiles[1] then
			nade = projectiles[1]
			if !m1 && nade.ManualDetonation == true then nade:Detonate() end
			if nade.ManualDetonation == true && emp == false then self:SetNWBool("EMP", true) end
			self:SetNWFloat("Distance", self:GetPos():Distance(projectiles[1]:GetPos()))
		elseif !IsValid(projectiles[1]) && emp == true then
			self:SetNWBool("EMP", false)
			self:SetNWFloat("Distance", 0)
		end
	end
	
	if !game.IsDedicated() then
		self.VElements["Screen"].draw_func = function( weapon )
			if emp == true then
				draw.SimpleTextOutlined("LNKNG", "reach_ammocounter", 0, 12.5, Color(37,141,170,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM, 0.7, Color(16, 60, 80))
			elseif self:Clip1() == 1 then
				draw.SimpleTextOutlined("READY", "reach_ammocounter", 0, 12.5, Color(37,141,170,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM, 0.7, Color(16, 60, 80))
			else
				draw.SimpleTextOutlined("EMPTY", "reach_ammocounter", 0, 12.5, Color(37,141,170,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM, 0.7, Color(16, 60, 80))
			end
		end
		
		self.VElements["Screen2"].draw_func = function( weapon )
		local distance = math.Round(dist / 16 * 0.3048)
			if distance < 10 then distance = ("00".. distance .."")
			elseif distance >= 10 && distance < 100 then distance = ("0".. distance .."") end
		
			if dist == 0 then
				draw.SimpleTextOutlined("000", "343_ammocounter", 0, 12.5, Color(37,141,170,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM, 0.7, Color(16, 60, 80))
			else
				draw.SimpleTextOutlined(distance, "343_ammocounter", 0, 12.5, Color(37,141,170,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM, 0.7, Color(16, 60, 80))
			end
		end
		
		self.VElements["Screen3"].draw_func = function( weapon )
			if emp == true then
				draw.SimpleTextOutlined("▅▅", "reach_ammocounter", 0, 12.5, Color(255,255,0,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM, 0.7, Color(100, 100, 0))
			else
				draw.SimpleTextOutlined("▅▅", "reach_ammocounter", 0, 12.5, Color(30,30,30,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM, 0.7, Color(10, 10, 10))
			end
		end
	end
end

function SWEP:DoCustomReloadStartEvents()
	self:SetNWFloat("Distance", 0)
end
Clone this wiki locally