wiremod-svn-archive/wire/lua/entities/gmod_wire_explosive/init.lua

326 lines
9.1 KiB
Lua
Raw Normal View History

2007-02-04 01:16:06 +00:00
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')
ENT.WireDebugName = "Explosive"
ENT.OverlayDelay = 0
2007-02-04 01:16:06 +00:00
/*---------------------------------------------------------
Name: Initialize
Desc: First function called. Use to set up your entity
---------------------------------------------------------*/
function ENT:Initialize()
self.Entity:PhysicsInit( SOLID_VPHYSICS )
self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
self.Entity:SetSolid( SOLID_VPHYSICS )
local phys = self.Entity:GetPhysicsObject()
if (phys:IsValid()) then
phys:Wake()
2007-02-04 01:16:06 +00:00
end
self.exploding = false
self.reloading = false
self.NormInfo = ""
2007-02-04 01:16:06 +00:00
self.count = 0
self.ExplodeTime = 0
self.ReloadTime = 0
self.CountTime = 0
2007-02-04 01:16:06 +00:00
self.Inputs = Wire_CreateInputs(self.Entity, { "Detonate", "ResetHealth" })
self.Entity:SetMaxHealth(100)
self.Entity:SetHealth(100)
self.Outputs = Wire_CreateOutputs(self.Entity, { "Health" })
end
/*---------------------------------------------------------
Name: TriggerInput
Desc: the inputs
---------------------------------------------------------*/
function ENT:TriggerInput(iname, value)
if (iname == "Detonate") then
if ( !self.exploding && !self.reloading ) then
if ( math.abs(value) == self.key ) then
self:Trigger()
end
end
elseif (iname == "ResetHealth") then
self:ResetHealth()
end
2007-02-04 01:16:06 +00:00
end
/*---------------------------------------------------------
Name: Setup
Desc: does a whole lot of setting up
---------------------------------------------------------*/
function ENT:Setup( damage, delaytime, removeafter, doblastdamage, radius, affectother, notaffected, delayreloadtime, maxhealth, bulletproof, explosionproof, fallproof, explodeatzero, resetatexplode, fireeffect, coloreffect, invisibleatzero, nocollide )
2007-02-04 01:16:06 +00:00
self.Damage = damage
self.Delaytime = delaytime
2007-02-04 01:16:06 +00:00
self.Removeafter = removeafter
self.DoBlastDamage = doblastdamage
self.Radius = math.max(radius, 1)
2007-02-04 01:16:06 +00:00
self.Affectother = affectother
self.Notaffected = notaffected
self.Delayreloadtime = delayreloadtime
self.BulletProof = bulletproof
self.ExplosionProof = explosionproof
self.FallProof = fallproof
self.ExplodeAtZero = explodeatzero
self.ResetAtExplode = resetatexplode
self.FireEffect = fireeffect
self.ColorEffect = coloreffect
self.NoCollide = nocollide
self.InvisibleAtZero = invisibleatzero
2007-02-04 01:16:06 +00:00
self.Entity:SetMaxHealth(maxhealth)
self:ResetHealth()
//self.Entity:SetHealth(maxhealth)
//Wire_TriggerOutput(self.Entity, "Health", maxhealth)
2007-02-04 01:16:06 +00:00
//reset everthing back and try to stop exploding
//self.exploding = false
//self.reloading = false
//self.count = 0
//self.Entity:Extinguish()
//if (self.ColorEffect) then self.Entity:SetColor(255, 255, 255, 255) end
2007-02-04 01:16:06 +00:00
self.NormInfo = "Explosive"
if (self.DoBlastDamage) then self.NormInfo = self.NormInfo.." (Damage: "..self.Damage..")" end
if (self.Radius > 0 || self.Delaytime > 0) then self.NormInfo = self.NormInfo.."\n" end
2007-02-04 01:16:06 +00:00
if (self.Radius > 0 ) then self.NormInfo = self.NormInfo.." Rad: "..self.Radius end
if (self.Delaytime > 0) then self.NormInfo = self.NormInfo.." Delay: "..self.Delaytime end
2007-02-04 01:16:06 +00:00
self:ShowOutput()
end
function ENT:ResetHealth( )
self.Entity:SetHealth( self.Entity:GetMaxHealth() )
Wire_TriggerOutput(self.Entity, "Health", self.Entity:GetMaxHealth())
//put the fires out and try to stop exploding
self.exploding = false
self.reloading = false
self.count = 0
self.Entity:Extinguish()
2007-02-04 01:16:06 +00:00
if (self.ColorEffect) then self.Entity:SetColor(255, 255, 255, 255) end
self.Entity:SetNoDraw( false )
if (self.NoCollide) then
self.Entity:SetCollisionGroup(COLLISION_GROUP_WORLD)
else
self.Entity:SetCollisionGroup(COLLISION_GROUP_NONE)
end
2007-02-04 01:16:06 +00:00
self:ShowOutput()
end
/*---------------------------------------------------------
Name: OnTakeDamage
Desc: Entity takes damage
---------------------------------------------------------*/
function ENT:OnTakeDamage( dmginfo )
if ( dmginfo:GetInflictor():GetClass() == "gmod_wire_explosive" && !self.Affectother ) then return end
if ( !self.Notaffected ) then self.Entity:TakePhysicsDamage( dmginfo ) end
if (dmginfo:IsBulletDamage() && self.BulletProof) ||
(dmginfo:IsExplosionDamage() && self.ExplosionProof) ||
(dmginfo:IsFallDamage() && self.FallProof) then return end //fix fall damage, it doesn't happen
if (self.Entity:Health() > 0) then //don't need to beat a dead horse
2007-05-26 07:23:42 +00:00
local dammage = dmginfo:GetDamage()
local h = self.Entity:Health() - dammage
2007-02-04 01:16:06 +00:00
if (h < 0) then h = 0 end
self.Entity:SetHealth(h)
Wire_TriggerOutput(self.Entity, "Health", h)
self:ShowOutput()
if (self.ColorEffect) then
if (h == 0) then c = 0 else c = 255 * (h / self.Entity:GetMaxHealth()) end
self.Entity:SetColor(255, c, c, 255)
end
if (h == 0) then
if (self.ExplodeAtZero) then self:Trigger() end //oh shi--
end
2007-02-04 01:16:06 +00:00
end
end
/*---------------------------------------------------------
Name: Trigger
Desc: Start exploding
---------------------------------------------------------*/
function ENT:Trigger()
if ( self.Delaytime > 0 ) then
//self.exploding = true
self.ExplodeTime = CurTime() + self.Delaytime
if (self.FireEffect) then self.Entity:Ignite((self.Delaytime + 3),0) end
/* timer.Simple( self.Delaytime, self.Explode, self )
//self.count = self.Delaytime
//self:Countdown()
//else
//self.exploding = true
//self:Explode()
*/
end
self.exploding = true
// Force reset of counter
self.CountTime = 0
end
/*---------------------------------------------------------
Name: Think
Desc: Thinks :P
---------------------------------------------------------*/
function ENT:Think()
self.BaseClass.Think(self)
if (self.exploding) then
if (self.ExplodeTime < CurTime()) then
self:Explode()
end
elseif (self.reloading) then
if (self.ReloadTime < CurTime()) then
self.reloading = false
if (self.ResetAtExplode) then
self:ResetHealth()
else
self:ShowOutput()
end
end
2007-02-04 01:16:06 +00:00
end
// Do count check to ensure that
// ShowOutput() is called every second
// when exploding or reloading
2007-05-26 07:23:42 +00:00
if ((self.CountTime or 0) < CurTime()) then
local temptime = 0
if (self.exploding) then
temptime = self.ExplodeTime
elseif (self.reloading) then
temptime = self.ReloadTime
end
if (temptime > 0) then
self.count = math.ceil(temptime - CurTime())
self:ShowOutput()
end
self.CountTime = CurTime() + 1
end
self.Entity:NextThink(CurTime() + 0.05)
return true
2007-02-04 01:16:06 +00:00
end
/*---------------------------------------------------------
Name: Explode
Desc: is one needed?
---------------------------------------------------------*/
function ENT:Explode( )
if ( !self.Entity:IsValid() ) then return end
self.Entity:Extinguish()
if (!self.exploding) then return end //why are we exploding if we shouldn't be
ply = self:GetPlayer() or self.Entity
2007-02-04 01:16:06 +00:00
if (self.InvisibleAtZero) then
ply:SetCollisionGroup(COLLISION_GROUP_DEBRIS)
ply:SetNoDraw( true )
ply:SetColor(255, 255, 255, 0)
end
2007-02-04 01:16:06 +00:00
if ( self.DoBlastDamage ) then
util.BlastDamage( self.Entity, ply, self.Entity:GetPos(), self.Radius, self.Damage )
end
local effectdata = EffectData()
effectdata:SetOrigin( self.Entity:GetPos() )
util.Effect( "Explosion", effectdata, true, true )
if ( self.Removeafter ) then
self.Entity:Remove()
return
end
self.exploding = false
self.reloading = true
self.ReloadTime = CurTime() + math.max(1, self.Delayreloadtime)
// Force reset of counter
self.CountTime = 0
self:ShowOutput()
2007-02-04 01:16:06 +00:00
/*if ( self.Delayreloadtime > 0 ) then
2007-02-04 01:16:06 +00:00
//t = self.Delayreloadtime + 1
self.reloading = true
//timer.Simple( self.Delayreloadtime, self.Reloaded, self )
//self.count = self.Delayreloadtime
//self:Countdown()
2007-02-04 01:16:06 +00:00
else //keep it from going off again for at least another second
self.reloading = true
timer.Simple( 1, self.Reloaded, self )
self:ShowOutput(0)
end*/
2007-02-04 01:16:06 +00:00
end
/* Don't need these anymore
2007-02-04 01:16:06 +00:00
function ENT:Reloaded( )
self.reloading = false
if (self.ResetAtExplode) then self:ResetHealth() end
end
function ENT:Countdown( )
self:ShowOutput()
self.count = self.count - 1
if ( self.count > 0 ) then //theres still time left
timer.Simple( 1, self.Countdown, self )
else //will be done after this second
timer.Simple( 1, self.ShowOutput, self )
end
end
*/
2007-02-04 01:16:06 +00:00
/*---------------------------------------------------------
Name: ShowOutput
Desc: don't foreget to call this when changes happen
---------------------------------------------------------*/
function ENT:ShowOutput( )
local txt = ""
if (self.reloading && self.Delayreloadtime > 0) then
2007-02-04 01:16:06 +00:00
txt = "Rearming... "..self.count
if (self.ColorEffect && !self.InvisibleAtZero) then
2007-02-04 01:16:06 +00:00
if (self.count == 0) then c = 255 else c = 255 * ((self.Delayreloadtime - self.count) / self.Delayreloadtime) end
self.Entity:SetColor(255, c, c, 255)
end
if (self.InvisibleAtZero) then
ply:SetNoDraw( false )
if (self.count == 0) then c = 255 else c = 255 * ((self.Delayreloadtime - self.count) / self.Delayreloadtime) end
self.Entity:SetColor(255, 255, 255, c)
end
2007-02-04 01:16:06 +00:00
elseif (self.exploding) then
txt = "Triggered... "..self.count
else
txt = self.NormInfo.."\nHealth: "..self.Entity:Health().."/"..self.Entity:GetMaxHealth()
end
self:SetOverlayText(txt)
2007-02-04 01:16:06 +00:00
end