-- When the weapon is deactivated

function weaponTemplate:OnDeactivate()
	
end

function weaponTemplate:OnStartFiring()
	if not self.spoolTimer:IsRunning() and not self.isFiring and self.isClient then
		self.isFiring = true
	end
	
	if not self.spoolTimer:IsRunning() and not self.isFiring and not self.isClient then
		
		self.gun.Barrel.Begin.TimePosition = 0
		self.gun.Barrel.Begin:Play()
		
		spawn(function() 
			for i = 1, 20 do
				self.gun.Barrel.HingeConstraint.AngularVelocity = i
				wait(0.043)
			end
		end)
			
		self.spoolTimer:Start()
	end
end

function weaponTemplate:OnStopFiring()
	
	-- Is Client
	if self.spoolTimer:IsRunning() and self.isFiring and self.isClient then
		self.isFiring = false
	end
	
	-- Is Server
	if self.spoolTimer:IsRunning() and self.isFiring and not self.isClient then
		self.gun.Barrel.End.TimePosition = 0
		self.gun.Barrel.End:Play()
		
		self.gun.Barrel.HingeConstraint.AngularVelocity = 0
		

		self.spoolTimer:Stop()
		self.rewardTimer:Stop()
		self.isFiring = false
		self.gun.Barrel.Loop:Stop()
		
		
		for i = 20, 0, -1 do
			self.gun.Barrel.HingeConstraint.AngularVelocity = i
			wait(0.043)
		end
	end
end

function weaponTemplate:OnFire()
	if self.isFiring then
		
		-- Is Client
		if self.isClient and self.gun.Barrel.Loop.IsPlaying then
			local newProjectile = self.projectileModel:Clone()
			local randomPoint = weaponTemplate.getUnitSpherePoint()
			local newBullet = bullet.new(newProjectile, self.ownerPlayer, 50, self.gun.firePart.Position, self.gun.firePart.CFrame.LookVector + randomPoint * self.coneSpread, 500, "Physics", false)
			newBullet.Name = newProjectile.Name
			replicateProjectile_Re:FireServer(newBullet)
		end
		
		-- Is Server
		if not self.gun.Barrel.Loop.IsPlaying and not self.isClient then
			self.gun.Barrel.Loop:Play()
			self.gun.Barrel.HingeConstraint.AngularVelocity = 50
			
		elseif not self.isClient then
			-- Ensure they don't run multiple timers
			if not self.rewardTimer:IsRunning() and self.rewardTimer.runningTicks == 0 then
				self.rewardTimer:Start()
			end
		end
	end
end