From e55f19f8b9704fed1e4c4ae1028ecb0b41e38277 Mon Sep 17 00:00:00 2001 From: StyledStrike Date: Mon, 30 Dec 2024 18:06:17 -0300 Subject: [PATCH] Allow overriding physics init logic --- lua/entities/base_glide/init.lua | 55 +++++++++++++++++--------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/lua/entities/base_glide/init.lua b/lua/entities/base_glide/init.lua index 21c69e6..228db2f 100644 --- a/lua/entities/base_glide/init.lua +++ b/lua/entities/base_glide/init.lua @@ -65,11 +65,31 @@ function ENT:Initialize() -- Setup the chassis model and physics self:SetModel( self.ChassisModel ) - self:SetSolid( SOLID_VPHYSICS ) - self:SetMoveType( MOVETYPE_VPHYSICS ) - self:PhysicsInit( SOLID_VPHYSICS ) + self:InitializePhysics() self:SetUseType( SIMPLE_USE ) + local phys = self:GetPhysicsObject() + + if not IsValid( phys ) then + self:Remove() + error( "Failed to setup physics! Vehicle removed!" ) + return + end + + phys:SetMaterial( "metalvehicle" ) + phys:SetMass( self.ChassisMass ) + phys:EnableDrag( false ) + phys:SetDamping( 0, 0 ) + phys:SetDragCoefficient( 0 ) + phys:SetAngleDragCoefficient( 0 ) + phys:SetBuoyancyRatio( 0.05 ) + phys:EnableMotion( true ) + phys:Wake() + + self:StartMotionController() + + debugoverlay.Cross( self:LocalToWorld( phys:GetMassCenter() ), 20, 5, Color( 0, 200, 0 ), true ) + -- Let NPCs see through this vehicle self:AddEFlags( EFL_DONTBLOCKLOS ) self:AddFlags( FL_OBJECT ) @@ -80,29 +100,6 @@ function ENT:Initialize() -- Setup wheel systems self:WheelInit() - local phys = self:GetPhysicsObject() - - if IsValid( phys ) then - phys:SetMaterial( "metalvehicle" ) - phys:SetMass( self.ChassisMass ) - phys:EnableDrag( false ) - phys:SetDamping( 0, 0 ) - phys:SetDragCoefficient( 0 ) - phys:SetAngleDragCoefficient( 0 ) - phys:SetBuoyancyRatio( 0.05 ) - - phys:EnableMotion( true ) - phys:Wake() - - self:StartMotionController() - - debugoverlay.Cross( self:LocalToWorld( phys:GetMassCenter() ), 20, 5, Color( 0, 200, 0 ), true ) - else - self:Remove() - error( "Failed to setup physics! Vehicle removed!" ) - return - end - local data = { Color = self:GetSpawnColor() } self:SetColor( data.Color ) @@ -147,6 +144,12 @@ function ENT:Initialize() self:CreateFeatures() end +function ENT:InitializePhysics() + self:SetSolid( SOLID_VPHYSICS ) + self:SetMoveType( MOVETYPE_VPHYSICS ) + self:PhysicsInit( SOLID_VPHYSICS ) +end + function ENT:OnRemove() -- Stop physics processing local phys = self:GetPhysicsObject()