Skip to content

Commit

Permalink
Expanded refill effects
Browse files Browse the repository at this point in the history
Adds refill effects to fuel tanks, adds the ability to customize ammo/fuel refill colors, and marks the beginning of a new effects library (to be worked on later)
  • Loading branch information
thecraftianman committed Apr 25, 2024
1 parent e0c0210 commit 22d540d
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 87 deletions.
2 changes: 2 additions & 0 deletions lua/acf/core/globals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ do -- ACF global vars
ACF.AmmoCaseScale = 1 -- How much larger the diameter of the case is versus the projectile (necked cartridges, M829 is 1.4, .50 BMG is 1.6)
ACF.AmmoMinSize = 6 -- Defines the shortest possible length of ammo crates for all their axises, in gmu
ACF.AmmoMaxSize = 96 -- Defines the highest possible length of ammo crates for all their axises, in gmu
ACF.AmmoRefillColor = Color(255, 255, 0, 10) -- The color to use for the ammo refill effect
ACF.PropImpetus = 1075 -- Energy in KJ produced by 1kg of propellant, based off M30A1 propellant
ACF.PDensity = 0.95 -- Propellant loading density (Density of propellant + volume lost due to packing density)

Expand Down Expand Up @@ -133,6 +134,7 @@ do -- ACF global vars
ACF.FuelMinSize = 6 -- Defines the shortest possible length of fuel tanks for all their axises, in gmu
ACF.FuelMaxSize = 96 -- Defines the highest possible length of fuel tanks for all their axises, in gmu
ACF.FuelArmor = 5 -- How many millimeters of armor fuel tanks have
ACF.FuelRefillColor = Color(76, 201, 250, 10) -- The color to use for the fuel refill effect
ACF.TankVolumeMul = 1 -- Multiplier for fuel tank capacity, 1.0 is approx real world
ACF.LiIonED = 0.458 -- li-ion energy density: kw hours / liter
ACF.RefillDistance = 300 -- Distance in which ammo crate starts refilling.
Expand Down
64 changes: 64 additions & 0 deletions lua/acf/core/utilities/effects/effects_cl.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
local ACF = ACF
local Refills = {}

ACF.Utilities.Effects.Refills = Refills

do -- Resupply effect (applies to ammo and fuel)
local render = render
local Distance = ACF.RefillDistance

local function DrawSpheres(bDrawingDepth, _, isDraw3DSkybox)
if bDrawingDepth or isDraw3DSkybox then return end
render.SetColorMaterial()

for Entity in pairs(Refills) do
local Pos = Entity:GetPos()
local RefillColor

if Entity:GetClass() == "acf_fueltank" then
RefillColor = ACF.FuelRefillColor
else
RefillColor = ACF.AmmoRefillColor
end

render.DrawSphere(Pos, Distance, 50, 50, RefillColor)
render.DrawSphere(Pos, -Distance, 50, 50, RefillColor)
end
end

local function Remove(Entity)
if not IsValid(Entity) then return end

Refills[Entity] = nil

Entity:RemoveCallOnRemove("ACF_Refill")

if not next(Refills) then
hook.Remove("PostDrawOpaqueRenderables", "ACF_Refill")
end
end

local function Add(Entity)
if not IsValid(Entity) then return end

if not next(Refills) then
hook.Add("PostDrawOpaqueRenderables", "ACF_Refill", DrawSpheres)
end

Refills[Entity] = true

Entity:CallOnRemove("ACF_Refill", Remove)
end

net.Receive("ACF_RefillEffect", function()
local Entity = net.ReadEntity()

Add(Entity)
end)

net.Receive("ACF_StopRefillEffect", function()
local Entity = net.ReadEntity()

Remove(Entity)
end)
end
20 changes: 20 additions & 0 deletions lua/acf/menu/items_cl/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ do -- Clientside settings
Particles:SetConVar("acf_cl_particlemul")

Base:AddHelp("Defines the clientside particle multiplier, reduce it if you're experiencing lag when ACF effects are created.")

Base:AddLabel("Ammo Refill Color")
local AmmoRefillColor = Base:AddPanel("DColorMixer")
AmmoRefillColor:SetColor(ACF.AmmoRefillColor)
AmmoRefillColor:SetClientData("AmmoRefillColor", "ValueChanged")
AmmoRefillColor:DefineSetter(function(_, _, _, Value)
ACF.AmmoRefillColor = Value

return Value
end)

Base:AddLabel("Fuel Refill Color")
local FuelRefillColor = Base:AddPanel("DColorMixer")
FuelRefillColor:SetColor(ACF.FuelRefillColor)
FuelRefillColor:SetClientData("FuelRefillColor", "ValueChanged")
FuelRefillColor:DefineSetter(function(_, _, _, Value)
ACF.FuelRefillColor = Value

return Value
end)
end)

ACF.AddClientSettings(201, "Legal Checks", function(Base)
Expand Down
2 changes: 2 additions & 0 deletions lua/acf/persisted_vars/vars_cl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

-- Settings
ACF.PersistClientData("Volume", 0.5)
ACF.PersistClientData("AmmoRefillColor", Color(255, 255, 0, 10))
ACF.PersistClientData("FuelRefillColor", Color(76, 201, 250, 10))

-- Crate size
ACF.PersistClientData("CrateSizeX", 24)
Expand Down
57 changes: 1 addition & 56 deletions lua/entities/acf_ammo/cl_init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local ACF = ACF
local MaxRounds = GetConVar("acf_maxroundsdisplay")
local Refills = {}
local Refills = ACF.Utilities.Effects.Refills
local Queued = {}

DEFINE_BASECLASS("acf_base_scalable") -- Required to get the local BaseClass
Expand Down Expand Up @@ -103,61 +103,6 @@ function ENT:OnRemove()
cvars.RemoveChangeCallback("acf_maxroundsdisplay", "Ammo Crate " .. self:EntIndex())
end

-- TODO: Resupply effect library, should apply for both ammo and fuel
do -- Resupply effect
local render = render
local Yellow = Color(255, 255, 0, 10)
local Distance = ACF.RefillDistance

local function DrawSpheres(bDrawingDepth, _, isDraw3DSkybox)
if bDrawingDepth or isDraw3DSkybox then return end
render.SetColorMaterial()

for Entity in pairs(Refills) do
local Pos = Entity:GetPos()

render.DrawSphere(Pos, Distance, 50, 50, Yellow)
render.DrawSphere(Pos, -Distance, 50, 50, Yellow)
end
end

local function Remove(Entity)
if not IsValid(Entity) then return end

Refills[Entity] = nil

Entity:RemoveCallOnRemove("ACF_Refill")

if not next(Refills) then
hook.Remove("PostDrawOpaqueRenderables", "ACF_Refill")
end
end

local function Add(Entity)
if not IsValid(Entity) then return end

if not next(Refills) then
hook.Add("PostDrawOpaqueRenderables", "ACF_Refill", DrawSpheres)
end

Refills[Entity] = true

Entity:CallOnRemove("ACF_Refill", Remove)
end

net.Receive("ACF_RefillEffect", function()
local Entity = net.ReadEntity()

Add(Entity)
end)

net.Receive("ACF_StopRefillEffect", function()
local Entity = net.ReadEntity()

Remove(Entity)
end)
end

do -- Ammo overlay
local DrawBoxes = GetConVar("acf_drawboxes")

Expand Down
83 changes: 52 additions & 31 deletions lua/entities/acf_fueltank/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -519,53 +519,74 @@ function ENT:Consume(Amount, SelfTbl)
end
end

function ENT:Think()
self:NextThink(Clock.CurTime + 1)
do
local function RefillEffect(Entity)
net.Start("ACF_RefillEffect")
net.WriteEntity(Entity)
net.Broadcast()
end

local Leaking = self.Leaking
local function StopRefillEffect(Entity)
net.Start("ACF_StopRefillEffect")
net.WriteEntity(Entity)
net.Broadcast()
end

if Leaking > 0 then
self:Consume(Leaking)
function ENT:Think()
self:NextThink(Clock.CurTime + 1)

local Fuel = self.Fuel
Leaking = Clamp(Leaking - (1 / math.max(Fuel, 1)) ^ 0.5, 0, Fuel) -- Fuel tanks are self healing
self.Leaking = Leaking
local Leaking = self.Leaking

WireLib.TriggerOutput(self, "Leaking", Leaking > 0 and 1 or 0)
if Leaking > 0 then
self:Consume(Leaking)

self:NextThink(Clock.CurTime + 0.25)
end
local Fuel = self.Fuel
Leaking = Clamp(Leaking - (1 / math.max(Fuel, 1)) ^ 0.5, 0, Fuel) -- Fuel tanks are self healing
self.Leaking = Leaking

-- Refuelling
if self.SupplyFuel and self:CanConsume() then
local DeltaTime = Clock.CurTime - self.LastThink
local Position = self:GetPos()
WireLib.TriggerOutput(self, "Leaking", Leaking > 0 and 1 or 0)

for Tank in pairs(ACF.FuelTanks) do
if CanRefuel(self, Tank, Position:DistToSqr(Tank:GetPos())) then
local Exchange = math.min(DeltaTime * ACF.RefuelSpeed * ACF.FuelRate, self.Fuel, Tank.Capacity - Tank.Fuel)
self:NextThink(Clock.CurTime + 0.25)
end

if HookRun("ACF_CanRefuel", self, Tank, Exchange) == false then continue end
if self.Refilling then
StopRefillEffect(self)
self.Refilling = false
end

self:Consume(Exchange)
Tank:Consume(-Exchange)
-- Refuelling
if self.SupplyFuel and self:CanConsume() then
local DeltaTime = Clock.CurTime - self.LastThink
local Position = self:GetPos()

if self.FuelType == "Electric" then
Sounds.SendSound(self, "ambient/energy/newspark04.wav", 70, 100, 0.5)
Sounds.SendSound(Tank, "ambient/energy/newspark04.wav", 70, 100, 0.5)
else
Sounds.SendSound(self, "vehicles/jetski/jetski_no_gas_start.wav", 70, 120, 0.5)
Sounds.SendSound(Tank, "vehicles/jetski/jetski_no_gas_start.wav", 70, 120, 0.5)
for Tank in pairs(ACF.FuelTanks) do
if CanRefuel(self, Tank, Position:DistToSqr(Tank:GetPos())) then
local Exchange = math.min(DeltaTime * ACF.RefuelSpeed * ACF.FuelRate, self.Fuel, Tank.Capacity - Tank.Fuel)

if HookRun("ACF_CanRefuel", self, Tank, Exchange) == false then continue end

self:Consume(Exchange)
Tank:Consume(-Exchange)

if self.FuelType == "Electric" then
Sounds.SendSound(self, "ambient/energy/newspark04.wav", 70, 100, 0.5)
Sounds.SendSound(Tank, "ambient/energy/newspark04.wav", 70, 100, 0.5)
else
Sounds.SendSound(self, "vehicles/jetski/jetski_no_gas_start.wav", 70, 120, 0.5)
Sounds.SendSound(Tank, "vehicles/jetski/jetski_no_gas_start.wav", 70, 120, 0.5)
end

RefillEffect(self)
self.Refilling = true
end
end
end
end

self.LastThink = Clock.CurTime
self.LastThink = Clock.CurTime

return true
return true
end
end

function ENT:OnRemove()
local Class = self.ClassData

Expand Down

0 comments on commit 22d540d

Please sign in to comment.