AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

ENT.vGravity = true
ENT.vOffset = Vector(0,0,60) --How far up the player the invisible object is
ENT.vMass = 1

/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()
	self.Entity:SetModel( "models/Combine_Helicopter/helicopter_bomb01.mdl" )
	self.Entity:SetColor( 0, 0, 0, 0 )
	self.Entity:SetMaterial("Models/effects/splodearc_sheet")
	self.Entity:SetCollisionGroup(1)
	self.Entity:DrawShadow( false )
	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()
	end
	vMass = self.Entity:GetPhysicsObject():GetMass()
	self:Massify()
end

function ENT:Massify()
	if( self.Entity:GetPhysicsObject():GetMass() == self.Entity:GetOwner():GetPhysicsObject():GetMass() ) then
		self.Entity:GetPhysicsObject():SetMass( self.vMass )
	else
		self.Entity:GetPhysicsObject():SetMass( self.Entity:GetOwner():GetPhysicsObject():GetMass() )
	end
end	

function ENT:Think()
end

function ENT:FreePlayer()
		if(!SERVER) then return end
		if (self.vGravity ~= true) then
			self.Entity:GetOwner():GetPhysicsObject():EnableGravity(true)
			self.vGravity = true
		end
		
		self.Entity:SetPos(self.Entity:GetOwner():GetPos() + self.vOffset - self:GetAdjust() )
		local vVelocity = self.Entity:GetVelocity() * Vector(1,1,0)
		self.Entity:SetLocalVelocity( vVelocity )
		self.Entity:GetPhysicsObject():SetVelocity( self.Entity:GetOwner():GetVelocity() )
end

function ENT:OffSet()
	return self.vOffset
end

function ENT:FixPlayer()
	local trace = {}
	local vStart = self.Entity:GetPos()
	trace.start = vStart
	trace.endpos = vStart - self.vOffset + self:GetAdjust()
	trace.filter = self.Entity
	trace.filter = self.Entity:GetOwner()
	local tr = util.TraceLine( trace )
	if (tr.Hit) then
		self.Entity:SetPos( vStart + (self.vOffset + self:GetAdjust()) * Vector( 0, 0, 1-tr.Fraction ) )
		self:FreePlayer()
		return true
	end
	return false
end

function ENT:GetAdjust()
	if ( self.Entity:GetOwner():Crouching() ) then
		return Vector(0,0,32)
	else
		return Vector(0,0,0)
	end
end

function ENT:Constrain()
	if(self:FixPlayer()) then return end
	if (self.vGravity) then
		self.Entity:GetOwner():GetPhysicsObject():EnableGravity(false)
		self.vGravity = false
	end
	local vVelocity = self.Entity:GetOwner():GetVelocity() * Vector(1,1,0)
	self.Entity:GetOwner():SetLocalVelocity( vVelocity )
	self.Entity:GetOwner():SetVelocity( self.Entity:GetVelocity() )
	self.Entity:GetOwner():SetPos( self.Entity:GetPos() - self.vOffset + self:GetAdjust() )
end
