All pastes #417874 Raw Edit

Nailgun example.

public lua v1 · immutable
#417874 ·published 2007-03-31 07:27 UTC
rendered paste body
TOOL.Category		= "Constraints"TOOL.Name			= "#Nail"TOOL.Command		= nilTOOL.ConfigName		= nilTOOL.ClientConVar["forcelimit"] = "0"TOOL.RightClickAutomatic = truecleanup.Register( "nails" )function TOOL:LeftClick( trace )	// Bail if we hit world or a player	if (  !trace.Entity:IsValid() || trace.Entity:IsPlayer() ) then return false end		// If there's no physics object then we can't constraint it!	if ( SERVER && !util.IsValidPhysicsObject( trace.Entity, trace.PhysicsBone ) ) then return false end		local tr = {}		tr.start = trace.HitPos		tr.endpos = trace.HitPos + (self:GetOwner():GetAimVector() * 16.0)		tr.filter = { self:GetOwner(), trace.Entity }	local trTwo = util.TraceLine( tr )		if ( trTwo.Hit && !trTwo.Entity:IsPlayer() ) then		// Client can bail now		if ( CLIENT ) then return true end		local Phys = trace.Entity:GetPhysicsObjectNum( trace.PhysicsBone )		local LPos = Phys:WorldToLocal( trace.HitPos - ( self:GetOwner():GetAimVector() * 8.0 ) )		local LAng = self:GetOwner():GetAimVector():Angle() - Phys:GetAngle()		local Data =		{			Type	= "Nail",			forcelimit	= self:GetClientNumber( "forcelimit" ),			Entity =			{				{					Entity	= trace.Entity,					Bone	= trace.PhysicsBone,					LPos	= LPos,					LAng	= LAng,				},				{					Entity	= trTwo.Entity,					Bone	= trTwo.PhysicsBone,				}			}		}		// Weld them!		local nail, weld = constraint.Create( Data )		undo.Create("Nail")		undo.AddEntity( nail )		undo.AddEntity( weld )		undo.SetPlayer( self:GetOwner() )		undo.Finish()		self:GetOwner():AddCleanup( "nails", nail )		self:GetOwner():AddCleanup( "nails", weld )		return true	endend/*----------------------------------------------------------------------	Nail( ... )	Creates a solid weld constraint with a nail between the two entities		local Data =	{		Type	= "Nail",		forcelimit	= int,		Entity =		{			{				Entity	= obj,				Bone	= int,				LPos	= vec,		// All vectors under an entity should be local to the entity				LAng	= angle,	// All angles under an entity should be local to the entity			},			{				Entity	= obj,				Bone	= int,			}		}	}----------------------------------------------------------------------*/function constraint.Nail( Data )	local Ent1,  Ent2	= Data.Entity[1].Entity, Data.Entity[2].Entity	local Bone1, Bone2	= Data.Entity[1].Bone,	 Data.Entity[2].Bone	if ( !constraint.CanConstrain( Ent1, Bone1 ) ) then return false end	if ( !constraint.CanConstrain( Ent2, Bone2 ) ) then return false end	local LPos = Data.Entity[1].LPos	local Phys = Ent1:GetPhysicsObjectNum( Bone1 )	local WPos = Phys:LocalToWorld( LPos )	local WAng = Data.Entity[1].LAng + Phys:GetAngle()	local weld, NewWeld	local Constraint =	constraint.Find( Ent1, Ent2, "Nail", Bone1, Bone2 ) or						constraint.Find( Ent1, Ent2, "Weld", Bone1, Bone2 )	// If a weld or nail already exists don't make another, just add a nail Entity.	if ( Constraint )  then weld = Constraint else		local Data2	= table.Copy( Data )		Data2.nocollide		= false		Data2.deleteonbreak	= false		Data2.Type			= "Weld"		weld = constraint.Create( Data2 )		weld.Data = Data		NewWeld = true	end	local nail = ents.Create( "gmod_nail" )		nail:SetPos( WPos )		nail:SetAngles( WAng )		nail:SetParentPhysNum( Bone1 )  // Why doesn't this fucking work!!		nail:SetParent( Ent1 )	nail:Spawn()	nail:Activate()	weld:DeleteOnRemove( nail )	if ( NewWeld ) then return nail, weld end	return nailendfunction TOOL:RightClick( trace )	self:GetWeapon():SetNextSecondaryFire( CurTime() + 0.2 )	return self:LeftClick( trace )	endfunction TOOL:Reload( trace )	if (!trace.Entity:IsValid() || trace.Entity:IsPlayer() ) then return false end	if ( CLIENT ) then return true end		local  bool = constraint.RemoveConstraints( trace.Entity, "Nail" )	return bool	end