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

local CAMERA_MODEL = "models/dav0r/camera.mdl"


local I_KEY 			= 0

local E_PLAYER 			= 0
local E_TRACK			= 1

local V_TRACK			= 0

/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()

	self.Entity:SetModel( CAMERA_MODEL )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	self.Entity:DrawShadow( false )
	
	// Don't collide with the player
	self.Entity:SetCollisionGroup( COLLISION_GROUP_WEAPON )
	
	local phys = self.Entity:GetPhysicsObject()
	
	if (phys:IsValid()) then
		phys:Sleep()
	end
	
end

function ENT:SetKey( key )

	self.Entity:SetNetworkedInt( I_KEY, key )

end

function ENT:SetTracking( Ent, LPos )

	if ( Ent:IsValid() ) then
	
		self.Entity:SetMoveType( MOVETYPE_NONE )
		self.Entity:SetSolid( SOLID_BBOX )
	
	else
	
		self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
		self.Entity:SetSolid( SOLID_VPHYSICS )
	
	end
	
	Msg("SetTracking: "..Ent.."\n")

	self.Entity:SetNetworkedVector( V_TRACK, LPos )
	self.Entity:SetNetworkedEntity( E_TRACK, Ent )
	self.Entity:NextThink( CurTime() )
	
	self.TrackEnt = Ent

end


function ENT:Think()

	self.TrackEnt = self.Entity:GetNetworkedEntity( E_TRACK )	
	self:TrackEntity( self.TrackEnt, self.Entity:GetNetworkedVector( V_TRACK ) )
	
	self.Entity:NextThink( CurTime() )
	
end

function ENT:SetPlayer( pl )

	self.Entity:SetNetworkedEntity( E_PLAYER, pl )

end

function ENT:SetLocked( locked )

	if (locked == 1) then
	
		self.PhysgunDisabled = true
		
		local phys = self.Entity:GetPhysicsObject()
		if ( phys:IsValid() ) then
			phys:EnableMotion( false )
		end
	
	else
	
		self.PhysgunDisabled = false
	
	end
	
	self.locked = locked

end

/*---------------------------------------------------------
   Name: OnTakeDamage
---------------------------------------------------------*/
function ENT:OnTakeDamage( dmginfo )
	self.Entity:TakePhysicsDamage( dmginfo )
end

/*---------------------------------------------------------
   Name: OnTakeDamage
---------------------------------------------------------*/
function ENT:Toggle( player )
	
	local ptab = player:GetTable()
	if ( ptab.UsingCamera && ptab.UsingCamera == self.Entity ) then
	
		player:SetViewEntity( player )
		player:GetTable().UsingCamera = nil
		self.UsingPlayer = nil
		
	else
	
		player:SetViewEntity( self.Entity )
		ptab.UsingCamera = self.Entity
		self.UsingPlayer = player
		
	end
	
end

function ENT:OnRemove()

	if (self.UsingPlayer && self.UsingPlayer != NULL) then
	
		self.UsingPlayer:SetViewEntity( self.UsingPlayer )
	
	end

end

/*---------------------------------------------------------
   Numpad control functions
   These are layed out like this so it'll all get saved properly
---------------------------------------------------------*/
local function Camera_Toggle( pl, ent )

	if (!ent:IsValid()) then
		 pl:SetViewEntity( pl )
		return false 
	end
	
	ent:GetTable():Toggle( pl )
	
	return true
	
end

// register numpad functions
numpad.Register( "Camera_Toggle", Camera_Toggle )

include('shared.lua')