if (SERVER) then
	AddCSLuaFile("shared.lua");
	SWEP.Weight				= 5;
	SWEP.AutoSwitchTo		= true;
	SWEP.AutoSwitchFrom		= true;
end


if (CLIENT) then
	SWEP.PrintName			= "Ultimate Chimera";	
	SWEP.Slot				= 0;
	SWEP.SlotPos			= 0;
	SWEP.DrawAmmo			= false;
	SWEP.DrawCrosshair		= false;
	SWEP.WepSelectIcon = surface.GetTextureID("chimerahunt/hud_chimera_weapon");
	
	function SWEP:DrawWorldModel()
		//nothing
	end
	
end

SWEP.Primary.Automatic = false;
SWEP.Secondary.Automatic = false;


function SWEP:Initialize()

	if (SERVER) then
	
		local ply = self.Owner;
		ply.Biting = false;
		ply.Roaring = false;
		
	else
		self.SecondDelay = 0;
		self.SecondClr = 255;
	end
	
end


function SWEP:BiteTimer()
	
	timer.Create(tostring(self.Owner:UniqueID()) .. "UCBiteTimer", GAMEMODE.UCBiteTime, 1, function()
		if (ValidEntity(self.Owner)) then
			if (SERVER) then
				self.Owner.Biting = false;
				self.Owner:UnLock();
			end
		end
	end);
	
end

function SWEP:RoarTimer()
	
	util.ScreenShake(self.Owner:GetPos(), 5, 5, GAMEMODE.UCRoarTime, GAMEMODE.UCRoarDistance);
	timer.Create(tostring(self.Owner:UniqueID()) .. "UCRoarTimer", GAMEMODE.UCRoarTime, 1, function()
		if (ValidEntity(self.Owner)) then
			if (SERVER) then
				self.Owner.Roaring = false;
				self.Owner:UnLock();
			end
		end
	end);
	
end


function SWEP:PrimaryAttack()
		
	local ply = self.Owner;
	if (!ply.Biting && !ply.Roaring && ply:IsOnGround() && !ply:Crouching()) then
		if (SERVER) then
			ply.Biting = true;
			ply:EmitSound("chimerahunt/chimera/bite.wav");
			ply.Chimera:SetCycle(0);
			self:BiteTimer();
			ply:SetLocalVelocity(Vector(0, 0, 0));
			ply:Lock();
			
			local dir = ply:GetForward();
			dir.z = 0;
			dir:Normalize();
			
			local stuff = ents.FindInSphere(((ply:GetShootPos() + Vector(0, 0, -10)) + (dir * 80)), 64);
			for k, v in ipairs(stuff) do
				if (ValidEntity(v) && v != ply) then
					if (v:IsPlayer() || v:IsNPC()) then
						local tr = util.QuickTrace(ply:GetShootPos(), ((v:GetShootPos() - ply:GetShootPos()) * 10000), ply);
						if (ValidEntity(tr.Entity) && tr.Entity == v) then
							v:Freeze(true);
							timer.Simple(.25, function()
								if (v:IsValid() && v:Alive()) then
									v:Kill();
									GAMEMODE:SendKillNotice(ply, v, "chimera");
									v:Freeze(false);
									umsg.Start("UCRagSpaz")
										umsg.Entity(v);
										umsg.Entity(ply);
									umsg.End()
								end
							end);
						end
					else
						timer.Simple(.25, function()
							if (v:IsValid()) then
								local dir = (ply:GetForward() + Vector(0, 0, .75));
								if (v:GetPhysicsObject():IsValid()) then
									v:GetPhysicsObject():ApplyForceCenter((dir * GAMEMODE.RagFlyAmount));
								end
								v:TakeDamage(1);
								if (v:Health() > 0) then
									v:Fire("break");
								end
							end
						end);
					end
				end
			end
			
		end
	end
		
end


function SWEP:SecondaryAttack()

	if (!self.Owner:IsOnGround() || self.Owner:Crouching()) then
		return;
	end

	self:SetNextSecondaryFire((CurTime() + GAMEMODE.UCRoarDelay));
	
	if (CLIENT && CurTime() >= self.SecondDelay) then
		self.SecondDelay = (CurTime() + GAMEMODE.UCRoarDelay);
	end
	
	local ply = self.Owner;
	if (!ply.Biting && !ply.Roaring) then
		if (SERVER) then
			ply.Roaring = true;
			ply:EmitSound("chimerahunt/chimera/roar.wav");
			ply.Chimera:SetCycle(0);
			self:RoarTimer();
			ply:SetLocalVelocity(Vector(0, 0, 0));
			ply:Lock();
		end
	end
	
end


if (CLIENT) then
	
	local sw, sh = ScrW(), ScrH();

	local function DrawShadowedText(txt, font, x, y, clr, alignx, aligny, dis, alpha)
		
		local tbl = {};
		tbl.pos = {};
		tbl.pos[1] = x;
		tbl.pos[2] = y;
		tbl.color = clr;
		tbl.text = txt;
		tbl.font = font;
		tbl.xalign = alignx;
		tbl.yalign = aligny;
		
		draw.TextShadow(tbl, dis, alpha);
		
	end
	
	function SWEP:DrawHUD()
		
		if (CurTime() <= self.SecondDelay) then
			self.SecondClr = math.Approach(self.SecondClr, 125, (FrameTime() * 325));
		else
			self.SecondClr = math.Approach(self.SecondClr, 255, (FrameTime() * 325));
		end
		
		DrawShadowedText("Primary: Bite", "UCText", 5, 0, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP, 2, 255);
		DrawShadowedText("Secondary: Roar", "UCText", 5, 42, Color(self.SecondClr, self.SecondClr, self.SecondClr, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP, 2, 255);
		
	end
	
end


------------General Swep Info---------------
SWEP.Author = "Aska";
SWEP.Purpose = "Nom on da piggies";
SWEP.Instructions = "Primary Fire - Chomp\nSecondary Fire - Roar";
SWEP.ViewModel = "";
SWEP.WorldModel = "";
-----------------------------------------------