--Tables. Don't change!
-------------------------------------------------------------------
G_Physgun = {}
G_Physgun.Attacking = {}
G_Physgun.Ent = {}
G_Physgun.Lenght = {}
for i=1,32 do 
	G_Physgun.Attacking[i] = false
end
-------------------------------------------------------------------
--Change these if you want
-------------------------------------------------------------------
G_Physgun.Types = {"npc_","prop_","func_","weapon_","player"}
G_Physgun.MoveSpeed = 3
G_Physgun.RotateSpeed = 2
-------------------------------------------------------------------
--Don't change below
-------------------------------------------------------------------
--Set pos/angle of entity
-------------------------------------------------------------------
function onThink()
	local vecpos = _PlayerGetShootPos(Owner)
	local plyang = _PlayerGetShootAng(Owner)
	_TraceLine(vecpos,plyang,8096,Owner)
	if _PlayerIsKeyDown(Owner,IN_ATTACK) then
		if G_Physgun.Attacking[Owner] then
			_EntSetPos(G_Physgun.Ent[Owner],vecAdd(vecpos,vecMul(plyang,G_Physgun.Lenght[Owner])))
			if _PlayerIsKeyDown(Owner,IN_SPEED) and G_Physgun.Lenght[Owner] < 8000 then
				G_Physgun.Lenght[Owner] = G_Physgun.Lenght[Owner] + G_Physgun.MoveSpeed 
			end
			if _PlayerIsKeyDown(Owner,IN_DUCK) and G_Physgun.Lenght[Owner] > 40 then
				_PlayerSetVecDuck(Owner,vector3(0,0,64)) --Prevent ducking
				G_Physgun.Lenght[Owner] = G_Physgun.Lenght[Owner] - G_Physgun.MoveSpeed
			else
				_PlayerSetVecDuck(Owner,vector3(0,0,24)) --Normal ducking again
			end
		end
	else
		G_Physgun.Attacking[Owner] = false
		_phys.ApplyForceCenter(G_Physgun.Ent[Owner],vector3(0,0,1)) --For some reason it won't be affected by grav before it has moved a bit
	end
	if _PlayerIsKeyDown(Owner,IN_ATTACK) and _PlayerIsKeyDown(Owner,IN_ATTACK2) then
		_PlayerLockInPlace(Owner,true)
		if _PlayerIsKeyDown(Owner,IN_MOVELEFT) then
			local Old_Ang = _EntGetAngAngle(G_Physgun.Ent[Owner])
			_EntSetAngAngle(G_Physgun.Ent[Owner],vecAdd(Old_Ang,vector3(0,-G_Physgun.RotateSpeed,0))) 
		end
		if _PlayerIsKeyDown(Owner,IN_MOVERIGHT) then
			local Old_Ang = _EntGetAngAngle(G_Physgun.Ent[Owner])
			_EntSetAngAngle(G_Physgun.Ent[Owner],vecAdd(Old_Ang,vector3(0,G_Physgun.RotateSpeed,0))) 
		end
		if _PlayerIsKeyDown(Owner,IN_FORWARD) then
			local Old_Ang = _EntGetAngAngle(G_Physgun.Ent[Owner])
			_EntSetAngAngle(G_Physgun.Ent[Owner],vecAdd(Old_Ang,vector3(G_Physgun.RotateSpeed,0,0))) 
		end
		if _PlayerIsKeyDown(Owner,IN_BACK) then
			local Old_Ang = _EntGetAngAngle(G_Physgun.Ent[Owner])
			_EntSetAngAngle(G_Physgun.Ent[Owner],vecAdd(Old_Ang,vector3(-G_Physgun.RotateSpeed,0,0))) 
		end
	else
		_PlayerLockInPlace(Owner,false)
	end
	if _PlayerIsKeyDown(Owner,IN_RELOAD) and G_Physgun.Attacking[Owner] then 
		_EntSetAng(G_Physgun.Ent[Owner],vector3(0,0,0)) 
	end 
	_GModText_Start("Default")
	_GModText_SetPos(-1,0)
	_GModText_SetTime(2,0,0.2)
	_GModText_SetText(" ")
	_GModText_SetColor(0,0,0,255)
	_GModText_Send(Owner,7353)
end
-------------------------------------------------------------------
--Get id and lenght
-------------------------------------------------------------------
function onPrimaryAttack()
	_PlayerViewModelSequence(Owner, ACT_RUN);
	_EntEmitSound(Owner,"wii/primary.mp3")
	_TraceLine(_PlayerGetShootPos(Owner),_PlayerGetShootAng(Owner),1024,Owner)
	for i = 1,table.getn(G_Physgun.Types) do
		if string.find(_EntGetType(_TraceGetEnt()),G_Physgun.Types[i]) then
			G_Physgun.Attacking[Owner] = true
			G_Physgun.Ent[Owner] = _TraceGetEnt()
			G_Physgun.Lenght[Owner] = vecLength(vecSub(_EntGetPos(_TraceGetEnt()),_PlayerGetShootPos(Owner)))
			break
		end
	end
end

function onSecondaryAttack()	

		if ( _PlayerInfo( Owner, "alive" ) == false ) then return; end
        

	_TraceLine( _PlayerGetShootPos( Owner ), _PlayerGetShootAng( Owner ), 999999, Owner );
	if ( _TraceHit() == true ) then
	_PlayerViewModelSequence(Owner, ACT_WALK);
	_EntEmitSound(Owner,"wii/secondary.mp3")
	local lol = 10000001
	local fireforce = vecMul(_PlayerGetShootAng(Owner), vector3(lol, lol, lol));
        _PhysApplyForce(_TraceGetEnt(), fireforce);

        
end					
end

     function Deploy( )
       _PlayerViewModelSequence(Owner, ACT_JUMP);
     end
-------------------------------------------------------------------
--Weapon settings
-------------------------------------------------------------------
function getWeaponSwapHands()			return false						end
function getWeaponFOV()					return 74							end
function getWeaponSlot()				return 3							end
function getWeaponSlotPos()				return 5							end
function getFiresUnderwater()			return true							end
function getReloadsSingly()				return false						end
function getDamage()					return 0							end
-------------------------------------------------------------------
--Primary Attack
-------------------------------------------------------------------
function getPrimaryShotDelay()			return 0.5							end
function getPrimaryIsAutomatic()		return false						end
function getPrimaryAmmoType()			return "Bugbait"					end
function getMaxClipPrimary()			return 10							end
function getDefClipPrimary()			return 10							end
function getPrimaryScriptOverride()		return 1							end
-------------------------------------------------------------------
--Secondary
-------------------------------------------------------------------
function getSecondaryShotDelay()		return 0.5							end
function getSecondaryIsAutomatic()		return false						end
function getSecondaryAmmoType()			return "none"						end
function getMaxClipSecondary()			return -1							end
function getDefClipSecondary()			return -1							end
function getSecondaryScriptOverride()	return 3							end
-------------------------------------------------------------------
--More weapon settings
-------------------------------------------------------------------
function getBulletSpread()				return vector3(0,0,0)				end
function getViewKick()					return vector3(0,0,0)				end
function getViewKickRandom()			return vector3(0,0,0)				end
function getViewModel()					return "models/wii/v_wii_controller.mdl"	end
function getWorldModel()				return "models/wii/w_wii_controller.mdl"	end
function getClassName()					return "weapon_wii_controller"		end
function getAnimPrefix()				return "slam"						end
function getPrintName()					return "Wii Controller"			end 
function getHUDMaterial()				return "gmod/SWEP/wii_controller"	end