AddCSLuaFile("cl_init.lua");
AddCSLuaFile("shared.lua");
include('shared.lua');


function ENT:Initialize()
	
	self:SetModel("models/nintendo/Mother/UchimeraGM.mdl");
	self:SetColor(255, 255, 255, 255);
	
end

function ENT:SetPlayer(ply)
	self:SetNWEntity("UCPlayer", ply);
end

function ENT:GetPlayer()
	return self:GetNWEntity("UCPlayer");
end

function ENT:Think()
	
	/*if (ValidEntity(self:GetPlayer())) then
		self:SetPos(self:GetPlayer():GetPos());
		self:SetAngles(self:GetPlayer():GetAngles());
		self:GetPlayer():SetColor(255, 255, 255, 255);
	end*/
	
	local ply = self:GetPlayer();
	local seq = "idle";
	
	if (ply.Biting) then
		seq = "bite";
	elseif (ply.Roaring) then
		seq = "idle3";
		
		for k, v in ipairs(ply:ExcludeMeInTable()) do
			
			local dis = v:GetPos():Distance(ply:GetPos());
			if (dis <= GAMEMODE.UCRoarDistance && v:Alive() && !v:IsScared()) then //scare the crap out of them
				
				local tr = util.QuickTrace(ply:GetShootPos(), ((v:GetShootPos() - ply:GetShootPos()) * 10000), ply);
				if (ValidEntity(tr.Entity) && tr.Entity == v) then
					v:SetScared(true); //shit your pants
					v.UCSprinting = false;
					umsg.Start("UCStopSprint", v)
					umsg.End()
					v.PigMask:DrawShadow(true);
					
					if (v:IsSprintTimer()) then
						v:DestroySprintTimer();
					end
					if (timer.IsTimer(tostring(ply) .. "TauntTimer")) then
						timer.Destroy(tostring(ply) .. "TauntTimer");
					end
					if (v:GetNWBool("Taunting")) then
						v:SetNWBool("Taunting", false);
					end
					
					local rank = v:GetNWInt("PigRank", 1);
					
					local t = math.Round((GAMEMODE.UCRoarDistance / (dis * .3)));
					t = math.Clamp(t, GAMEMODE.UCRoarMin, GAMEMODE.UCRoarMax);
					t = (t - (rank * .26));
					t = math.Clamp(t, GAMEMODE.UCRoarMin, GAMEMODE.UCRoarMax);
					if (timer.IsTimer(tostring(v) .. "UCScared")) then
						timer.Destroy(tostring(v) .. "UCScared");
					end
					timer.Create(tostring(v) .. "UCScared", t, 1, function()
						if (ValidEntity(v)) then
							v:SetScared(false);
							v.PigMask:DrawShadow(false);
							v:SetSprint(math.Clamp((v:GetSprint() - .5), 0, 1));
							if (v:GetSprint() > 0) then
								v.SprintRegen = true;
							end
							v:SetSpeed(GAMEMODE.plySpeed);
						end
					end);
				end
				
			end
			
		end
		
	else
		if (ply:IsOnGround()) then
			if (ply.ShouldStomp) then
				ply.ShouldStomp = false;
				ply:EmitSound("chimerahunt/chimera/step.wav", 62, 100);
				util.ScreenShake(self:GetPos(), 5, 5, .8, 825);
			end
			if (ply:KeyDown(IN_FORWARD) || ply:KeyDown(IN_MOVELEFT) || ply:KeyDown(IN_MOVERIGHT) || ply:KeyDown(IN_BACK)) then
				if (ply:KeyDown(IN_SPEED) && !ply:Crouching() && ply:GetSprint() > 0 && !ply:KeyDown(IN_BACK)) then
					seq = "run";
				else
					seq = "walk";
				end
			end
		else
			local vel = ply:GetVelocity().z;
			if (vel <= -15) then
				ply.ShouldStomp = true;
			end
			seq = "jump2";
		end
	end
	
	self:ResetSequence(self:LookupSequence(seq));
	
	local frame, _ = math.modf((self:GetCycle() * 40));
	if ((seq == "run" && (frame == 6 || frame == 31)) || (seq == "walk" && (frame == 19 || frame == 38))) then
		local shake = ((seq == "run" && 3) || (seq == "walk" && 2));
		local stime = ((seq == "run" && .625) || (seq == "walk" && .5));
		util.ScreenShake(self:GetPos(), shake, shake, stime, 825);
	end
	
	self:SetPoseParameter("move_yaw", ply:GetPoseParameter("move_yaw"));
	
	self:NextThink(CurTime());
	return true;
	
end
