Skip to content

Commit

Permalink
Make base_glide_aircraft handle rotor model/spin
Browse files Browse the repository at this point in the history
  • Loading branch information
StyledStrike committed Dec 28, 2024
1 parent 94f239c commit 6de461a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 35 deletions.
35 changes: 35 additions & 0 deletions lua/entities/base_glide_aircraft/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ function ENT:OnPostInitialize()
self.inputRoll = 0
self.inputYaw = 0

self.rotors = {}
self.areRotorsSpinningFast = false

-- Setup damage variables
self.engineDamageCD = -1
self.engineDamageSoundCD = -1
Expand Down Expand Up @@ -221,6 +224,38 @@ function ENT:OnPostThink( dt, selfTbl )
if selfTbl.HasLandingGear then
self:LandingGearThink( dt )
end

-- Update rotors
self:RotorsThink()
end

function ENT:RotorsThink()
local power = self:GetPower()

-- Spin the rotors
for _, rotor in ipairs( self.rotors ) do
if IsValid( rotor ) then
rotor.spinMultiplier = power
end
end

-- Call `RotorStartSpinningFast` or `RotorStopSpinningFast`
-- when the result from `ShouldRotorsSpinFast` changes.
local areRotorsSpinningFast = self:ShouldRotorsSpinFast()

if self.areRotorsSpinningFast ~= areRotorsSpinningFast then
self.areRotorsSpinningFast = areRotorsSpinningFast

for _, rotor in ipairs( self.rotors ) do
if IsValid( rotor ) then
if areRotorsSpinningFast then
self:RotorStartSpinningFast( rotor )
else
self:RotorStopSpinningFast( rotor )
end
end
end
end
end

do
Expand Down
6 changes: 5 additions & 1 deletion lua/entities/base_glide_aircraft/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ if SERVER then
[3] = { "glide/aircraft/gear_down.wav", 0.65, 90 }
}

-- You can override this on your child classes.
-- You can override these on your child classes.
function ENT:OnLandingGearStateChange( _state ) end

function ENT:ShouldRotorsSpinFast()
return self:GetPower() > 0.65
end

function ENT:RotorStartSpinningFast( rotor )
rotor:SetModel( rotor.modelFast or rotor.modelSlow )
end
Expand Down
7 changes: 0 additions & 7 deletions lua/entities/base_glide_heli/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,6 @@ function ENT:OnPostThink( dt, selfTbl )
self:SetPower( power )
end

-- Spin the rotors
for _, rotor in ipairs( selfTbl.rotors ) do
if IsValid( rotor ) then
rotor.spinMultiplier = power
end
end

-- Handle out-of-control state
if self:IsEngineOn() then
local isOutOfControl = self:GetOutOfControl()
Expand Down
14 changes: 3 additions & 11 deletions lua/entities/base_glide_plane/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ function ENT:OnPostInitialize()
BaseClass.OnPostInitialize( self )

-- Setup variables used on all planes
self.propellers = {}
self.powerResponse = 0.15

self.isGrounded = false
Expand Down Expand Up @@ -39,7 +38,7 @@ function ENT:Repair()
local validPropellers = {}
local validCount = 0

for _, prop in ipairs( self.propellers ) do
for _, prop in ipairs( self.rotors ) do
if IsValid( prop ) then
prop:Repair()

Expand All @@ -48,7 +47,7 @@ function ENT:Repair()
end
end

self.propellers = validPropellers
self.rotors = validPropellers
end

--- Creates and stores a new propeller entity.
Expand All @@ -74,7 +73,7 @@ function ENT:CreatePropeller( offset, radius, slowModel, fastModel )
prop:SetSpinAxis( "Forward" )
prop.maxSpinSpeed = 5000

self.propellers[#self.propellers + 1] = prop
self.rotors[#self.rotors + 1] = prop

return prop
end
Expand Down Expand Up @@ -206,13 +205,6 @@ function ENT:OnPostThink( dt, selfTbl )
end
end

-- Spin the propellers
for _, prop in ipairs( selfTbl.propellers ) do
if IsValid( prop ) then
prop.spinMultiplier = power
end
end

if TriggerOutput then
TriggerOutput( self, "Power", power )
end
Expand Down
16 changes: 1 addition & 15 deletions lua/entities/glide_rotor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ function ENT:Initialize()
self.baseAngles = Angle()
self.spinAngle = 0
self.spinAxis = "Up"

self.spinMultiplier = 0
self.isSpinningFast = false

-- Prepare damage & trace variables
self.rotorHealth = 400
Expand All @@ -68,7 +66,7 @@ function ENT:SetupRotor( offset, radius, modelSlow, modelFast )
self.radius = radius
self.modelSlow = modelSlow
self.modelFast = modelFast
self:SetModel( self.isSpinningFast and modelFast or modelSlow )
self:SetModel( modelSlow )
end

function ENT:SetBaseAngles( angles )
Expand Down Expand Up @@ -131,18 +129,6 @@ function ENT:Think()
return
end

-- Switch between fast/slow models depending on the spinMultiplier
local isSpinningFast = self.spinMultiplier > 0.65

if self.isSpinningFast ~= isSpinningFast then
self.isSpinningFast = isSpinningFast
if isSpinningFast then
self:GetParent():RotorStartSpinningFast( self )
else
self:GetParent():RotorStopSpinningFast( self )
end
end

local dt = FrameTime()

self.spinAngle = ( self.spinAngle + self.maxSpinSpeed * self.spinMultiplier * dt ) % 360
Expand Down
2 changes: 1 addition & 1 deletion lua/weapons/glide_repair.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function SWEP:PrimaryAttack()
local chassisHealth = ent:GetChassisHealth()

if chassisHealth >= ent.MaxChassisHealth and engineHealth >= 1 then
local rotors = ent.rotors or ent.propellers
local rotors = ent.rotors
if not rotors then return end

for i = 1, #rotors do
Expand Down

0 comments on commit 6de461a

Please sign in to comment.