include("shared.lua");
include("cl_hud.lua");
include("cl_vgui.lua");
include("cl_scoreboard.lua");


function GM:ShowSplashScreen()

	self:CreateSplash();

end

function GM:CalcView(ply, pos, ang, fov)
	
	if (ply:GetNWBool("IsChimera") || ply:GetNWBool("UCScared")) then
		local view = {};
		local pos2 = (ply:GetAimVector() * -self.UCCamDistance);
		
		local tr = util.QuickTrace(pos, pos2, ply);
		
		local origin = tr.HitPos;
		
		if (tr.Hit) then
			origin = (origin + (ply:GetAimVector() * 16));
		end
		
		view.origin = origin;
		view.angles = ang;
		view.fov = fov;
		
		return view;
	end
	if (ply:GetNWBool("Taunting")) then
		local view = {};
		
		local fwd, right, up = ply:GetForward(), ply:GetRight(), ply:GetUp();
		fwd = (fwd * 92);
		right = (right * 48);
		up = (up * 32);
		
		local dir = (fwd + right + up);
		
		local tr = util.QuickTrace(pos, dir, {ply, ply.PigMask});
		local origin = tr.HitPos;
		if (tr.Hit) then
			origin = (origin + (dir * -.1));
		end
		
		ang = (pos - origin):GetNormal():Angle();
		
		view.origin = origin;
		view.angles = ang;
		view.fov = fov;
		
		return view;
	end
	
end


local function PlayerStartVoice(ply)
	ply.Talkin = true;
end
hook.Add("PlayerStartVoice", "StartWigglePiggy", PlayerStartVoice);

local function PlayerEndVoice(ply)
	ply.Talkin = false;
end
hook.Add("PlayerEndVoice", "EndWigglePiggy", PlayerEndVoice);


local function PlayerHull(um)
	local ply = LocalPlayer();
	ply.UpdateHull = true;
end
usermessage.Hook("UCHull", PlayerHull);


function GM:Think()
	
	local ply = LocalPlayer();
	
	if (!ply.UpdateHull) then
		ply.UpdateHull = false;
	end
	if (ply.UpdateHull) then
		ply.UpdateHull = false;
		if (ply:GetNWBool("IsChimera")) then
			ply:SetHull(self.UCHullSizeMin, self.UCHullSizeMax);
			ply:SetHullDuck(self.UCHullSizeMin, self.UCHullSizeMax);
		else
			ply:SetHull(self.plyHullSizeMin, self.plyHullSizeMax);
			ply:SetHullDuck(self.plyHullSizeMin, self.plyHullSizeMaxD);
		end
	end
	
end


local function GetRagSpaz(um)
	local ply = um:ReadEntity();
	local ply2 = um:ReadEntity();
	timer.Simple(.1, function()
		if (ValidEntity(ply) && ValidEntity(ply:GetRagdollEntity())) then
			local ent = ply:GetRagdollEntity();
			local dir = (ply2:GetForward() + Vector(0, 0, .75));
			for i = 0, (ent:GetPhysicsObjectCount() - 1) do
				ent:GetPhysicsObjectNum(i):ApplyForceCenter((dir * GAMEMODE.RagFlyAmount));
			end
			ent:EmitSound("chimerahunt/pigs/squeal" .. tostring(math.random(1, 3)) .. ".wav", 80, math.random(85, 110));
		end
	end);
end
usermessage.Hook("UCRagSpaz", GetRagSpaz);


local function GetRagSkin(um)
	local ply = um:ReadEntity();
	timer.Simple(.01, function()
		if (ValidEntity(ply) && ValidEntity(ply:GetRagdollEntity())) then
			local ent = ply:GetRagdollEntity();
			if (ply:GetNWBool("IsChimera")) then
				//ent:SetSkin(1);
			else
				ent:SetSkin((ply:GetNWInt("PigRank") - 1));
				ent:SetBodygroup(1, (ply:GetNWInt("PigRank") - 1));
				if (ply:GetNWInt("PigRank", 1) == 4) then
					ent:SetBodygroup(2, 1);
				else
					ent:SetBodygroup(2, 0);
				end
			end
		end
	end);
end
usermessage.Hook("UCRagSkin", GetRagSkin);



function GM:ShouldDrawLocalPlayer(ply)
	return false;
end
