Skip to content

Instantly share code, notes, and snippets.

@hakusaro
Created May 2, 2012 06:59
Show Gist options
  • Select an option

  • Save hakusaro/2574650 to your computer and use it in GitHub Desktop.

Select an option

Save hakusaro/2574650 to your computer and use it in GitHub Desktop.
Lua cluster grenade (Tribes: Ascend style)
if SERVER then
AddCSLuaFile ("shared.lua")
SWEP.Weight = 5
SWEP.AutoSwitchTo = true
SWEP.AutoSwitchFrom = true
elseif CLIENT then
SWEP.PrintName = "Cluster Grenade"
SWEP.Slot = 5
SWEP.SlotPos = 1
SWEP.DrawAmmo = false
SWEP.DrawCrosshair = true
end
-- SWEP base:
SWEP.Author = "Lucas Nicodemus"
SWEP.Contact = "[email protected]"
SWEP.Purpose = "Fun?"
SWEP.Instructions = "Left click to chuck into the nearest group of baddies.\nRight click to chuck into face."
SWEP.Category = "Far too underused stuff"
SWEP.Spawnable = false
SWEP.AdminSpawnable = true
SWEP.ViewModel = "models/weapons/v_grenade.mdl"
SWEP.WorldModel = "models/weapons/w_grenade.mdl"
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "Grenade"
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"
-- Locals:
local throwSnd = Sound("WeaponFrag.Throw")
local rollSnd = Sound("WeaponFrag.Roll")
local explodeSnd = Sound("explode_7")
local blipSnd = Sound("Grenade.Blip")
local failSnd = Sound("Buttons.snd2")
local grenadeTable = nil
-- Used hooks:
function SWEP:Initialize()
self:SetWeaponHoldType("grenade")
end
function SWEP:PrimaryAttack()
if (!self:CanPrimaryAttack()) then return end
self:EmitSound(throwSnd)
self:DecrementAmmo(1)
animationSequence = self.Owner:LookupSequence(ACT_HL2MP_GESTURE_RANGE_ATTACK_GRENADE)
self.Owner:SetSequence(animationSequence)
if (!SERVER) then return end
self:LaunchInitialGrenade(2, false)
end
function SWEP:SecondaryAttack()
if (!self:CanPrimaryAttack()) then return end
self:EmitSound(rollSnd)
self:DecrementAmmo(1)
if (!SERVER) then return end
self:LaunchInitialGrenade(2, true)
end
function SWEP:CanPrimaryAttack()
if (self:Ammo1() <= 0) then
self:EmitSound(failSnd)
return false
else
return true
end
end
function OnGrenadeExplode(ent)
if (!SERVER) then return end
if (grenadeTable == nil) then return end
for k,v in pairs(grenadeTable) do
if (ent == v) then
ent:EmitSound(explodeSnd)
LaunchMoreGrenades(ent)
end
end
end
hook.Add("EntityRemoved", "grenadeExploded", OnGrenadeExplode)
-- Misc functions:
function LaunchMoreGrenades(ent)
for i=1,6 do
local grenade = ents.Create("npc_grenade_frag")
grenade:Fire("SetTimer", .5, 0)
grenade:SetPos(ent:GetPos())
grenade:SetAngles(Angle(math.random(0, 180), math.random(0, 180), math.random(0, 180)))
grenade:Spawn()
local physObj = grenade:GetPhysicsObject()
physObj:ApplyForceCenter(Vector(math.random(0, 10), math.random(0, 10), 13))
end
end
function SWEP:LaunchInitialGrenade(forcePow, toss)
local eyeTrace = self.Owner:GetEyeTrace()
local grenade = ents.Create("npc_grenade_frag")
grenade:Fire("SetTimer", 1, 0)
if (toss) then
grenade:SetPos(self.Owner:EyePos() + (self.Owner:GetAimVector() * 2))
end
grenade:SetPos(self.Owner:EyePos() + (self.Owner:GetAimVector() * 6))
grenade:SetAngles(self.Owner:EyeAngles() + Angle(math.random(-120,120), math.random(-120,120), math.random(-120,120)))
grenade:Spawn()
if (grenadeTable == nil) then
grenadeTable = {grenade}
else
grenadeTable[table.getn(grenadeTable) + 1] = grenade
end
local physObj = grenade:GetPhysicsObject()
if (toss) then
forcePow = 1
end
physObj:ApplyForceCenter(self.Owner:GetAimVector():GetNormalized() * math.pow(eyeTrace.HitPos:Length() + 2, forcePow))
end
function SWEP:DecrementAmmo(amount)
if (self:Ammo1() <= 0) then return end
if (self:Ammo1() >= 1) then
self.Owner:RemoveAmmo(amount, self:GetPrimaryAmmoType())
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment