/
HeartbeatCamera.lua
62 lines (48 loc) · 1.54 KB
/
HeartbeatCamera.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
--[=[
Update on heartbeat, must GC this camera state, unlike others. This
allows for camera effects to run on heartbeat and cache information once instead
of potentially going deeep into a tree and getting invoked multiple times
@class HeartbeatCamera
]=]
local require = require(script.Parent.loader).load(script)
local RunService = game:GetService("RunService")
local SummedCamera = require("SummedCamera")
local Maid = require("Maid")
local HeartbeatCamera = {}
HeartbeatCamera.ClassName = "HeartbeatCamera"
HeartbeatCamera.ProfileName = "HeartbeatCamera"
function HeartbeatCamera.new(camera)
local self = setmetatable({}, HeartbeatCamera)
self._camera = camera or error("No camera")
self._maid = Maid.new()
self._currentStateCache = self._camera.CameraState or error("Camera state returned null")
self._maid:GiveTask(RunService.Heartbeat:Connect(function()
debug.profilebegin(self.ProfileName)
self._currentStateCache = self._camera.CameraState or error("Camera state returned null")
debug.profileend()
end))
return self
end
function HeartbeatCamera:__add(other)
return SummedCamera.new(self, other)
end
function HeartbeatCamera:ForceUpdateCache()
self._currentStateCache = self._camera.CameraState
end
--[=[
The current state.
@readonly
@prop CameraState CameraState
@within HeartbeatCamera
]=]
function HeartbeatCamera:__index(index)
if index == "CameraState" then
return self._currentStateCache
else
return HeartbeatCamera[index]
end
end
function HeartbeatCamera:Destroy()
self._maid:DoCleaning()
end
return HeartbeatCamera