local l={}function l.Create(l,c)local g={Item=c;Equipped=false;Connections={}}local A={}local n=c.Parent.Parent local Y=c:WaitForChild("\072\097\110\100\108\101")local i=Y:WaitForChild("\077\117\122\122\108\101")local s=CONFIG:GetConfig(c)local T=true local L=false local D=false local r=false local E=0 local I=(c:WaitForChild("\065\109\109\111")).Value local e=false local function w()return g.Equipped and(T and I>0)end local function M()if not D and(g.Equipped and I<s.Magazine)then D=true r=false local l=n.Ammo[s.Size].Value if l>0 then MOUSE.Reticle="\082\101\108\111\097\100\105\110\103"REMOTES.Reload:FireServer(c)EFFECTS:Effect("\082\101\108\111\097\100",c)A.Reload:Play(.1,1,1/s.ReloadTime)local n=tick()local Y=0 repeat Y=tick()-n RunService.Stepped:wait()until Y>=s.ReloadTime or r or not g.Equipped A.Reload:Stop()if g.Equipped then if Y>=s.ReloadTime then local c=s.Magazine local g=c-I if l>=g then I=I+g else I=I+l end end MOUSE.Reticle=s.Reticle or"\071\117\110"EVENTS.Gun:Fire("\085\112\100\097\116\101",I)end end D=false end end local function m()I=I-1 local l=(MOUSE.WorldPosition-i.WorldPosition).Unit if s.AimCorrection then l=(l+Vector3.new(0,s.AimCorrection/100,0)).Unit end local g=EVENTS.GetProjectileID:Invoke()EVENTS.Projectile:Fire(PLAYER,c,g,i.WorldPosition,l)REMOTES.RocketLauncher:FireServer(c,"\070\105\114\101",g,i.WorldPosition,l)if e then A.AimShoot:Play(0,math.random(5,10)/10,1)else A.Shoot:Play(0,math.random(5,10)/10,1)end EFFECTS:Effect("\082\111\099\107\101\116\076\097\117\110\099\104\101\114",c,"\070\105\114\101",I>0)EVENTS.Gun:Fire("\085\112\100\097\116\101",I)EVENTS.Recoil:Fire(Vector3.new(math.random(-s.Recoil,s.Recoil)/4,0,math.random(s.Recoil/2,s.Recoil)))if I>0 then A.Reload:Play(.1,1,1/s.Cooldown)end end function g.Connect(l)local g=PLAYER.Character local n=g:WaitForChild("\072\117\109\097\110\111\105\100")for l,c in pairs((l.Item:WaitForChild("\065\110\105\109\097\116\105\111\110\115")):GetChildren())do A[c.Name]=n:LoadAnimation(c)end table.insert(l.Connections,INPUT.ActionBegan:connect(function(c,g)if l.Equipped and not g then if c=="\082\101\108\111\097\100"then M()end end end))table.insert(l.Connections,c.Attachments.ChildAdded:connect(function()s=CONFIG:GetConfig(c)if l.Equipped then EVENTS.Zoom:Fire(s.Zoom)EVENTS.Scope:Fire(s.Scope)end end))table.insert(l.Connections,c.Attachments.ChildRemoved:connect(function()s=CONFIG:GetConfig(c)if l.Equipped then EVENTS.Zoom:Fire(s.Zoom)EVENTS.Scope:Fire(s.Scope)end end))table.insert(l.Connections,EVENTS.Aim.Event:connect(function(c)e=c if l.Equipped then if e then A.Idle:Stop()A.Aim:Play()else A.Aim:Stop()A.Idle:Play()end end end))end function g.Disconnect(l)for l,c in pairs(l.Connections)do c:Disconnect()end l.Connections={}end function g.Equip(l)EVENTS.Zoom:Fire(s.Zoom)EVENTS.Scope:Fire(s.Scope)MOUSE.Reticle=s.Reticle or"\071\117\110"if e then A.Aim:Play()else A.Idle:Play()end A.Equip:Play(0,1,1)I=c.Ammo.Value EVENTS.Gun:Fire("\069\110\097\098\108\101",s.Size,I)l.Equipped=true E=tick()if I==0 then spawn(function()M()end)end end function g.Unequip(l)EVENTS.Zoom:Fire()EVENTS.Scope:Fire(false)MOUSE.Reticle="\068\101\102\097\117\108\116"for l,c in pairs(A)do c:Stop()end EVENTS.Gun:Fire("\068\105\115\097\098\108\101",s.Size,I)l.Equipped=false end function g.Activate(l)L=true if w()then r=true T=false m()wait(s.Cooldown)T=true end end function g.Deactivate(l)L=false end return g end return l


local module	= {}

function module.Create(self, item)
	local itemModule	= {
		Item		= item;
		Equipped	= false;
		Connections	= {};
	}
	
	-- variables
	
	local animations	= {}
	
	local character	= item.Parent.Parent
	local handle	= item:WaitForChild("Handle")
	local muzzle	= handle:WaitForChild("Muzzle")
	
	local config		= CONFIG:GetConfig(item)
	local canShoot		= true
	local clicking		= false
	local reloading		= false
	local rCancelled	= false
	local equipTime		= 0
	
	local ammo	= item:WaitForChild("Ammo").Value
	
	local aiming	= false
	
	-- functions
	
	local function CanShoot()
		return itemModule.Equipped and canShoot and ammo > 0
	end
	
	local function Reload()
		if (not reloading) and itemModule.Equipped and ammo < config.Magazine then
			reloading	= true
			rCancelled	= false
			
			local storedAmmo	= character.Ammo[config.Size].Value
			if storedAmmo > 0 then
				MOUSE.Reticle	= "Reloading"
				REMOTES.Reload:FireServer(item)
				EFFECTS:Effect("Reload", item)
				animations.Reload:Play(0.1, 1, 1/config.ReloadTime)
				
				local start		= tick()
				local elapsed	= 0
				repeat
					elapsed	= tick() - start
					RunService.Stepped:wait()
				until elapsed >= config.ReloadTime or rCancelled or (not itemModule.Equipped)
				
				animations.Reload:Stop()
				
				if itemModule.Equipped then
					if elapsed >= config.ReloadTime then
						local magazine	= config.Magazine
						local needed	= magazine - ammo
						
						if storedAmmo >= needed then
							ammo	= ammo + needed
						else
							ammo	= ammo + storedAmmo
						end
					end
					
					MOUSE.Reticle	= config.Reticle or "Gun"
					EVENTS.Gun:Fire("Update", ammo)
				end
			end
			reloading	= false
		end
	end
	
	local function Shoot()
		ammo	= ammo - 1
		
		local direction		= (MOUSE.WorldPosition - muzzle.WorldPosition).Unit
		
		if config.AimCorrection then
			direction	= (direction + Vector3.new(0, config.AimCorrection / 100, 0)).Unit
		end
		
		local projectileID	= EVENTS.GetProjectileID:Invoke()
		
		EVENTS.Projectile:Fire(PLAYER, item, projectileID, muzzle.WorldPosition, direction)
		REMOTES.RocketLauncher:FireServer(item, "Fire", projectileID, muzzle.WorldPosition, direction)
		
		if aiming then
			animations.AimShoot:Play(0, math.random(5, 10) / 10, 1)
		else
			animations.Shoot:Play(0, math.random(5, 10) / 10, 1)
		end
		
		EFFECTS:Effect("RocketLauncher", item, "Fire", ammo > 0)
		EVENTS.Gun:Fire("Update", ammo)
		EVENTS.Recoil:Fire(Vector3.new(math.random(-config.Recoil, config.Recoil) / 4, 0, math.random(config.Recoil / 2, config.Recoil)))
		
		if ammo > 0 then
			animations.Reload:Play(0.1, 1, 1 / config.Cooldown)
		end
	end
	
	-- module functions
	
	function itemModule.Connect(self)
		local character		= PLAYER.Character
		local humanoid		= character:WaitForChild("Humanoid")
		
		for _, animation in pairs(self.Item:WaitForChild("Animations"):GetChildren()) do
			animations[animation.Name]	= humanoid:LoadAnimation(animation)
		end
		
		table.insert(self.Connections, INPUT.ActionBegan:connect(function(action, processed)
			if self.Equipped and (not processed) then
				if action == "Reload" then
					Reload()
				end
			end
		end))
		
		table.insert(self.Connections, item.Attachments.ChildAdded:connect(function()
			config	= CONFIG:GetConfig(item)
			if self.Equipped then
				EVENTS.Zoom:Fire(config.Zoom)
				EVENTS.Scope:Fire(config.Scope)
			end
		end))
		
		table.insert(self.Connections, item.Attachments.ChildRemoved:connect(function()
			config	= CONFIG:GetConfig(item)
			if self.Equipped then
				EVENTS.Zoom:Fire(config.Zoom)
				EVENTS.Scope:Fire(config.Scope)
			end
		end))
		
		table.insert(self.Connections, EVENTS.Aim.Event:connect(function(a)
			aiming	= a
			
			if self.Equipped then
				if aiming then
					animations.Idle:Stop()
					animations.Aim:Play()
				else
					animations.Aim:Stop()
					animations.Idle:Play()
				end
			end
		end))
	end
	
	function itemModule.Disconnect(self)
		for _, connection in pairs(self.Connections) do
			connection:Disconnect()
		end
		self.Connections	= {}
	end
	
	function itemModule.Equip(self)
		EVENTS.Zoom:Fire(config.Zoom)
		EVENTS.Scope:Fire(config.Scope)
		MOUSE.Reticle	= config.Reticle or "Gun"
		if aiming then
			animations.Aim:Play()
		else
			animations.Idle:Play()
		end
		animations.Equip:Play(0, 1, 1)
		
		ammo	= item.Ammo.Value
		
		EVENTS.Gun:Fire("Enable", config.Size, ammo)
		
		self.Equipped	= true
		equipTime		= tick()
		
		if ammo == 0 then
			spawn(function()
				Reload()
			end)
		end
	end
	
	function itemModule.Unequip(self)
		EVENTS.Zoom:Fire()
		EVENTS.Scope:Fire(false)
		MOUSE.Reticle	= "Default"
		
		for _, animation in pairs(animations) do
			animation:Stop()
		end
		
		EVENTS.Gun:Fire("Disable", config.Size, ammo)
		
		self.Equipped	= false
	end
	
	function itemModule.Activate(self)
		clicking	= true
		
		if CanShoot() then
			rCancelled	= true
			canShoot	= false
			Shoot()
			wait(config.Cooldown)
			canShoot	= true
		end
	end
	
	function itemModule.Deactivate(self)
		clicking	= false
	end
	
	return itemModule
end

return module
