local W={}W.__index=W W.__aeroPreventStart=true local w=debug.profilebegin local g=debug.profileend local l="\067\097\109\101\114\097\083\104\097\107\101\114\085\112\100\097\116\101"local N=Vector3.new local y=CFrame.new local c=CFrame.Angles local B=math.rad local e=N()local a=require(script.CameraShakeInstance)local Q=a.CameraShakeState local X=N(.15,.15,.15)local L=N(1,1,1)W.CameraShakeInstance=a W.Presets=require(script.CameraShakePresets)function W.new(w,g)assert(type(w)=="\110\117\109\098\101\114","\082\101\110\100\101\114\080\114\105\111\114\105\116\121 \109\117\115\116 \098\101 \097 \110\117\109\098\101\114 \040\101\046\103\046\058 \069\110\117\109\046\082\101\110\100\101\114\080\114\105\111\114\105\116\121\046\067\097\109\101\114\097\046\086\097\108\117\101\041")assert(type(g)=="\102\117\110\099\116\105\111\110","\067\097\108\108\098\097\099\107 \109\117\115\116 \098\101 \097 \102\117\110\099\116\105\111\110")local l=setmetatable({_running=false,_renderName="\067\097\109\101\114\097\083\104\097\107\101\114";_renderPriority=w;_posAddShake=e;_rotAddShake=e,_camShakeInstances={},_removeInstances={},_callback=g},W)return l end function W.Start(W)if W._running then return end W._running=true local N=W._callback;(game:GetService("\082\117\110\083\101\114\118\105\099\101")):BindToRenderStep(W._renderName,W._renderPriority,function(y)w(l)local c=W:Update(y)g()N(c)end)end function W.Stop(W)if not W._running then return end;(game:GetService("\082\117\110\083\101\114\118\105\099\101")):UnbindFromRenderStep(W._renderName)W._running=false end function W.Update(w,W)local g=e local l=e local N=w._camShakeInstances for y=1,#N,1 do local c=N[y]local B=c:GetState()if B==Q.Inactive and c.DeleteOnInactive then w._removeInstances[#w._removeInstances+1]=y elseif B~=Q.Inactive then g=g+c:UpdateShake(W)*c.PositionInfluence l=l+c:UpdateShake(W)*c.RotationInfluence end end for W=#w._removeInstances,1,-1 do local g=w._removeInstances[W]table.remove(N,g)w._removeInstances[W]=nil end return(y(g)*c(0,B(l.Y),0))*c(B(l.X),0,B(l.Z))end function W.Shake(w,W)assert(type(W)=="\116\097\098\108\101"and W._camShakeInstance,"\083\104\097\107\101\073\110\115\116\097\110\099\101 \109\117\115\116 \098\101 \111\102 \116\121\112\101 \067\097\109\101\114\097\083\104\097\107\101\073\110\115\116\097\110\099\101")w._camShakeInstances[#w._camShakeInstances+1]=W return W end function W.ShakeSustain(w,W)assert(type(W)=="\116\097\098\108\101"and W._camShakeInstance,"\083\104\097\107\101\073\110\115\116\097\110\099\101 \109\117\115\116 \098\101 \111\102 \116\121\112\101 \067\097\109\101\114\097\083\104\097\107\101\073\110\115\116\097\110\099\101")w._camShakeInstances[#w._camShakeInstances+1]=W W:StartFadeIn(W.fadeInDuration)return W end function W.ShakeOnce(c,W,w,g,l,N,y)local B=a.new(W,w,g,l)B.PositionInfluence=typeof(N)=="\086\101\099\116\111\114\051"and N or X B.RotationInfluence=typeof(y)=="\086\101\099\116\111\114\051"and y or L c._camShakeInstances[#c._camShakeInstances+1]=B return B end function W.StartShake(y,W,w,g,l,N)local c=a.new(W,w,g)c.PositionInfluence=typeof(l)=="\086\101\099\116\111\114\051"and l or X c.RotationInfluence=typeof(N)=="\086\101\099\116\111\114\051"and N or L c:StartFadeIn(g)y._camShakeInstances[#y._camShakeInstances+1]=c return c end return W




local CameraShaker = {}
CameraShaker.__index = CameraShaker
CameraShaker.__aeroPreventStart = true

local profileBegin = debug.profilebegin
local profileEnd = debug.profileend
local profileTag = "CameraShakerUpdate"

local V3 = Vector3.new
local CF = CFrame.new
local ANG = CFrame.Angles
local RAD = math.rad
local v3Zero = V3()

local CameraShakeInstance = require(script.CameraShakeInstance)
local CameraShakeState = CameraShakeInstance.CameraShakeState

local defaultPosInfluence = V3(0.15, 0.15, 0.15)
local defaultRotInfluence = V3(1, 1, 1)


CameraShaker.CameraShakeInstance = CameraShakeInstance
CameraShaker.Presets = require(script.CameraShakePresets)


function CameraShaker.new(renderPriority, callback)
	
	assert(type(renderPriority) == "number", "RenderPriority must be a number (e.g.: Enum.RenderPriority.Camera.Value)")
	assert(type(callback) == "function", "Callback must be a function")
	
	local self = setmetatable({
		_running = false;
		_renderName = "CameraShaker";
		_renderPriority = renderPriority;
		_posAddShake = v3Zero;
		_rotAddShake = v3Zero;
		_camShakeInstances = {};
		_removeInstances = {};
		_callback = callback;
	}, CameraShaker)
	
	return self
	
end


function CameraShaker:Start()
	if (self._running) then return end
	self._running = true
	local callback = self._callback
	game:GetService("RunService"):BindToRenderStep(self._renderName, self._renderPriority, function(dt)
		profileBegin(profileTag)
		local cf = self:Update(dt)
		profileEnd()
		callback(cf)
	end)
end


function CameraShaker:Stop()
	if (not self._running) then return end
	game:GetService("RunService"):UnbindFromRenderStep(self._renderName)
	self._running = false
end


function CameraShaker:Update(dt)
	
	local posAddShake = v3Zero
	local rotAddShake = v3Zero
	
	local instances = self._camShakeInstances
	
	-- Update all instances:
	for i = 1,#instances do
		
		local c = instances[i]
		local state = c:GetState()
		
		if (state == CameraShakeState.Inactive and c.DeleteOnInactive) then
			self._removeInstances[#self._removeInstances + 1] = i
		elseif (state ~= CameraShakeState.Inactive) then
			posAddShake = posAddShake + (c:UpdateShake(dt) * c.PositionInfluence)
			rotAddShake = rotAddShake + (c:UpdateShake(dt) * c.RotationInfluence)
		end
		
	end
	
	-- Remove dead instances:
	for i = #self._removeInstances,1,-1 do
		local instIndex = self._removeInstances[i]
		table.remove(instances, instIndex)
		self._removeInstances[i] = nil
	end
	
	return CF(posAddShake) *
			ANG(0, RAD(rotAddShake.Y), 0) *
			ANG(RAD(rotAddShake.X), 0, RAD(rotAddShake.Z))
	
end


function CameraShaker:Shake(shakeInstance)
	assert(type(shakeInstance) == "table" and shakeInstance._camShakeInstance, "ShakeInstance must be of type CameraShakeInstance")
	self._camShakeInstances[#self._camShakeInstances + 1] = shakeInstance
	return shakeInstance
end


function CameraShaker:ShakeSustain(shakeInstance)
	assert(type(shakeInstance) == "table" and shakeInstance._camShakeInstance, "ShakeInstance must be of type CameraShakeInstance")
	self._camShakeInstances[#self._camShakeInstances + 1] = shakeInstance
	shakeInstance:StartFadeIn(shakeInstance.fadeInDuration)
	return shakeInstance
end


function CameraShaker:ShakeOnce(magnitude, roughness, fadeInTime, fadeOutTime, posInfluence, rotInfluence)
	local shakeInstance = CameraShakeInstance.new(magnitude, roughness, fadeInTime, fadeOutTime)
	shakeInstance.PositionInfluence = (typeof(posInfluence) == "Vector3" and posInfluence or defaultPosInfluence)
	shakeInstance.RotationInfluence = (typeof(rotInfluence) == "Vector3" and rotInfluence or defaultRotInfluence)
	self._camShakeInstances[#self._camShakeInstances + 1] = shakeInstance
	return shakeInstance
end


function CameraShaker:StartShake(magnitude, roughness, fadeInTime, posInfluence, rotInfluence)
	local shakeInstance = CameraShakeInstance.new(magnitude, roughness, fadeInTime)
	shakeInstance.PositionInfluence = (typeof(posInfluence) == "Vector3" and posInfluence or defaultPosInfluence)
	shakeInstance.RotationInfluence = (typeof(rotInfluence) == "Vector3" and rotInfluence or defaultRotInfluence)
	shakeInstance:StartFadeIn(fadeInTime)
	self._camShakeInstances[#self._camShakeInstances + 1] = shakeInstance
	return shakeInstance
end


return CameraShaker
