@quenty/camera 9.1.1 → 9.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [9.2.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/camera@9.1.1...@quenty/camera@9.2.0) (2022-10-23)
7
+
8
+
9
+ ### Features
10
+
11
+ * Move CameraStack to its own reusable class ([7020502](https://github.com/Quenty/NevermoreEngine/commit/70205024acb508552c0c922a0a8119a27b7f38cd))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [9.1.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/camera@9.1.0...@quenty/camera@9.1.1) (2022-10-16)
7
18
 
8
19
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/camera",
3
- "version": "9.1.1",
3
+ "version": "9.2.0",
4
4
  "description": "Quenty's camera system for Roblox",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -43,5 +43,5 @@
43
43
  "publishConfig": {
44
44
  "access": "public"
45
45
  },
46
- "gitHead": "5fc8df6d4a62c0c774ebc78b275898f7f383935b"
46
+ "gitHead": "f167412233d2a0bcb7f91bdc6aa2f3c754bdbe79"
47
47
  }
@@ -0,0 +1,180 @@
1
+ --[=[
2
+ @class CameraStack
3
+ ]=]
4
+
5
+ local require = require(script.Parent.loader).load(script)
6
+
7
+ local HttpService = game:GetService("HttpService")
8
+
9
+ local BaseObject = require("BaseObject")
10
+ local CustomCameraEffect = require("CustomCameraEffect")
11
+
12
+ local CameraStack = setmetatable({}, BaseObject)
13
+ CameraStack.ClassName = "CameraStack"
14
+ CameraStack.__index = CameraStack
15
+
16
+ function CameraStack.new()
17
+ local self = setmetatable(BaseObject.new(), CameraStack)
18
+
19
+ self._stack = {}
20
+ self._disabledSet = {}
21
+
22
+ return self
23
+ end
24
+
25
+ --[=[
26
+ Pushes a disable state onto the camera stack
27
+ @return function -- Function to cancel disable
28
+ ]=]
29
+ function CameraStack:PushDisable()
30
+ assert(self._stack, "Not initialized")
31
+
32
+ local disabledKey = HttpService:GenerateGUID(false)
33
+ self._disabledSet[disabledKey] = true
34
+
35
+ return function()
36
+ self._disabledSet[disabledKey] = nil
37
+ end
38
+ end
39
+
40
+ --[=[
41
+ Outputs the camera stack. Intended for diagnostics.
42
+ ]=]
43
+ function CameraStack:PrintCameraStack()
44
+ assert(self._stack, "Stack is not initialized yet")
45
+
46
+ for _, value in pairs(self._stack) do
47
+ print(tostring(type(value) == "table" and value.ClassName or tostring(value)))
48
+ end
49
+ end
50
+
51
+ --[=[
52
+ Gets the camera current on the top of the stack
53
+ @return CameraEffect
54
+ ]=]
55
+ function CameraStack:GetTopCamera()
56
+ assert(self._stack, "Not initialized")
57
+
58
+ return self._stack[#self._stack]
59
+ end
60
+
61
+ --[=[
62
+ Retrieves the top state off the stack at this time
63
+ @return CameraState?
64
+ ]=]
65
+ function CameraStack:GetTopState()
66
+ assert(self._stack, "Stack is not initialized yet")
67
+
68
+ if next(self._disabledSet) then
69
+ return
70
+ end
71
+
72
+ if #self._stack > 10 then
73
+ warn(("[CameraStack] - Stack is bigger than 10 in CameraStack (%d)"):format(#self._stack))
74
+ end
75
+ local topState = self._stack[#self._stack]
76
+
77
+ if type(topState) == "table" then
78
+ local state = topState.CameraState or topState
79
+ if state then
80
+ return state
81
+ else
82
+ warn("[CameraStack] - No top state!")
83
+ return nil
84
+ end
85
+ elseif topState ~= nil then
86
+ warn("[CameraStack] - Bad type on top of stack")
87
+ return nil
88
+ end
89
+ end
90
+
91
+ --[=[
92
+ Returns a new camera state that retrieves the state below its set state.
93
+
94
+ @return CustomCameraEffect -- Effect below
95
+ @return (CameraState) -> () -- Function to set the state
96
+ ]=]
97
+ function CameraStack:GetNewStateBelow()
98
+ assert(self._stack, "Stack is not initialized yet")
99
+
100
+ local _stateToUse = nil
101
+
102
+ return CustomCameraEffect.new(function()
103
+ local index = self:GetIndex(_stateToUse)
104
+ if index then
105
+ local below = self._stack[index-1]
106
+ if below then
107
+ return below.CameraState or below
108
+ else
109
+ warn("[CameraStack] - Could not get state below, found current state. Returning default.")
110
+ return self._stack[1].CameraState
111
+ end
112
+ else
113
+ warn(("[CameraStack] - Could not get state from %q, returning default"):format(tostring(_stateToUse)))
114
+ return self._stack[1].CameraState
115
+ end
116
+ end), function(newStateToUse)
117
+ _stateToUse = newStateToUse
118
+ end
119
+ end
120
+
121
+ --[=[
122
+ Retrieves the index of a state
123
+ @param state CameraEffect
124
+ @return number? -- index
125
+
126
+ ]=]
127
+ function CameraStack:GetIndex(state)
128
+ assert(self._stack, "Stack is not initialized yet")
129
+
130
+ for index, value in pairs(self._stack) do
131
+ if value == state then
132
+ return index
133
+ end
134
+ end
135
+ end
136
+
137
+ --[=[
138
+ Returns the current stack.
139
+
140
+ :::warning
141
+ Do not modify this stack, this is the raw memory of the stack
142
+ :::
143
+
144
+ @return { CameraState<T> }
145
+ ]=]
146
+ function CameraStack:GetStack()
147
+ assert(self._stack, "Not initialized")
148
+
149
+ return self._stack
150
+ end
151
+
152
+ --[=[
153
+ Removes the state from the stack
154
+ @param state CameraState
155
+ ]=]
156
+ function CameraStack:Remove(state)
157
+ assert(self._stack, "Stack is not initialized yet")
158
+
159
+ local index = self:GetIndex(state)
160
+
161
+ if index then
162
+ table.remove(self._stack, index)
163
+ end
164
+ end
165
+
166
+ --[=[
167
+ Adds the state from the stack
168
+ @param state CameraState
169
+ ]=]
170
+ function CameraStack:Add(state)
171
+ assert(self._stack, "Stack is not initialized yet")
172
+
173
+ table.insert(self._stack, state)
174
+
175
+ return function()
176
+ self:Remove(state)
177
+ end
178
+ end
179
+
180
+ return CameraStack
@@ -11,11 +11,11 @@ local RunService = game:GetService("RunService")
11
11
  local Workspace = game:GetService("Workspace")
12
12
  local HttpService = game:GetService("HttpService")
13
13
 
14
- local CustomCameraEffect = require("CustomCameraEffect")
15
14
  local DefaultCamera = require("DefaultCamera")
16
15
  local ImpulseCamera = require("ImpulseCamera")
17
16
  local ServiceBag = require("ServiceBag")
18
17
  local Maid = require("Maid")
18
+ local CameraStack = require("CameraStack")
19
19
 
20
20
  assert(RunService:IsClient(), "[CameraStackService] - Only require CameraStackService on client")
21
21
 
@@ -32,8 +32,7 @@ function CameraStackService:Init(serviceBag)
32
32
  self._maid = Maid.new()
33
33
  self._key = HttpService:GenerateGUID(false)
34
34
 
35
- self._stack = {}
36
- self._disabledSet = {}
35
+ self._cameraStack = CameraStack.new()
37
36
 
38
37
  -- Initialize default cameras
39
38
  self._rawDefaultCamera = DefaultCamera.new()
@@ -46,12 +45,7 @@ function CameraStackService:Init(serviceBag)
46
45
  self:Add(self._defaultCamera)
47
46
 
48
47
  RunService:BindToRenderStep("CameraStackUpdateInternal" .. self._key, Enum.RenderPriority.Camera.Value + 75, function()
49
- debug.profilebegin("camerastack")
50
-
51
- if next(self._disabledSet) then
52
- debug.profileend()
53
- return
54
- end
48
+ debug.profilebegin("camerastackservice")
55
49
 
56
50
  local state = self:GetTopState()
57
51
  if state then
@@ -97,26 +91,18 @@ end
97
91
  @return function -- Function to cancel disable
98
92
  ]=]
99
93
  function CameraStackService:PushDisable()
100
- assert(self._stack, "Not initialized")
101
-
102
- local disabledKey = HttpService:GenerateGUID(false)
103
-
104
- self._disabledSet[disabledKey] = true
94
+ assert(self._cameraStack, "Not initialized")
105
95
 
106
- return function()
107
- self._disabledSet[disabledKey] = nil
108
- end
96
+ return self._cameraStack:PushDisable()
109
97
  end
110
98
 
111
99
  --[=[
112
100
  Outputs the camera stack. Intended for diagnostics.
113
101
  ]=]
114
102
  function CameraStackService:PrintCameraStack()
115
- assert(self._stack, "Stack is not initialized yet")
103
+ assert(self._cameraStack, "Not initialized")
116
104
 
117
- for _, value in pairs(self._stack) do
118
- print(tostring(type(value) == "table" and value.ClassName or tostring(value)))
119
- end
105
+ return self._cameraStack:PrintCameraStack()
120
106
  end
121
107
 
122
108
  --[=[
@@ -169,9 +155,9 @@ end
169
155
  @return CameraEffect
170
156
  ]=]
171
157
  function CameraStackService:GetTopCamera()
172
- assert(self._stack, "Not initialized")
158
+ assert(self._cameraStack, "Not initialized")
173
159
 
174
- return self._stack[#self._stack]
160
+ return self._cameraStack:GetTopCamera()
175
161
  end
176
162
 
177
163
  --[=[
@@ -179,23 +165,9 @@ end
179
165
  @return CameraState?
180
166
  ]=]
181
167
  function CameraStackService:GetTopState()
182
- assert(self._stack, "Stack is not initialized yet")
168
+ assert(self._cameraStack, "Not initialized")
183
169
 
184
- if #self._stack > 10 then
185
- warn(("[CameraStackService] - Stack is bigger than 10 in camerastackService (%d)"):format(#self._stack))
186
- end
187
- local topState = self._stack[#self._stack]
188
-
189
- if type(topState) == "table" then
190
- local state = topState.CameraState or topState
191
- if state then
192
- return state
193
- else
194
- warn("[CameraStackService] - No top state!")
195
- end
196
- else
197
- warn("[CameraStackService] - Bad type on top of stack")
198
- end
170
+ return self._cameraStack:GetTopState()
199
171
  end
200
172
 
201
173
  --[=[
@@ -205,27 +177,9 @@ end
205
177
  @return (CameraState) -> () -- Function to set the state
206
178
  ]=]
207
179
  function CameraStackService:GetNewStateBelow()
208
- assert(self._stack, "Stack is not initialized yet")
209
-
210
- local _stateToUse = nil
211
-
212
- return CustomCameraEffect.new(function()
213
- local index = self:GetIndex(_stateToUse)
214
- if index then
215
- local below = self._stack[index-1]
216
- if below then
217
- return below.CameraState or below
218
- else
219
- warn("[CameraStackService] - Could not get state below, found current state. Returning default.")
220
- return self._stack[1].CameraState
221
- end
222
- else
223
- warn(("[CameraStackService] - Could not get state from %q, returning default"):format(tostring(_stateToUse)))
224
- return self._stack[1].CameraState
225
- end
226
- end), function(newStateToUse)
227
- _stateToUse = newStateToUse
228
- end
180
+ assert(self._cameraStack, "Not initialized")
181
+
182
+ return self._cameraStack:GetNewStateBelow()
229
183
  end
230
184
 
231
185
  --[=[
@@ -235,13 +189,9 @@ end
235
189
 
236
190
  ]=]
237
191
  function CameraStackService:GetIndex(state)
238
- assert(self._stack, "Stack is not initialized yet")
192
+ assert(self._cameraStack, "Not initialized")
239
193
 
240
- for index, value in pairs(self._stack) do
241
- if value == state then
242
- return index
243
- end
244
- end
194
+ return self._cameraStack:GetIndex(state)
245
195
  end
246
196
 
247
197
  --[=[
@@ -253,10 +203,21 @@ end
253
203
 
254
204
  @return { CameraState<T> }
255
205
  ]=]
256
- function CameraStackService:GetStack()
257
- assert(self._stack, "Not initialized")
206
+ function CameraStackService:GetRawStack()
207
+ assert(self._cameraStack, "Not initialized")
208
+
209
+ return self._cameraStack:GetRawStack()
210
+ end
211
+
212
+ --[=[
213
+ Gets the current camera stack
214
+
215
+ @return CameraStack
216
+ ]=]
217
+ function CameraStackService:GetCameraStack()
218
+ assert(self._cameraStack, "Not initialized")
258
219
 
259
- return self._stack
220
+ return self._cameraStack:GetStack()
260
221
  end
261
222
 
262
223
  --[=[
@@ -264,13 +225,9 @@ end
264
225
  @param state CameraState
265
226
  ]=]
266
227
  function CameraStackService:Remove(state)
267
- assert(self._stack, "Stack is not initialized yet")
268
-
269
- local index = self:GetIndex(state)
228
+ assert(self._cameraStack, "Not initialized")
270
229
 
271
- if index then
272
- table.remove(self._stack, index)
273
- end
230
+ return self._cameraStack:Remove(state)
274
231
  end
275
232
 
276
233
  --[=[
@@ -278,9 +235,9 @@ end
278
235
  @param state CameraState
279
236
  ]=]
280
237
  function CameraStackService:Add(state)
281
- assert(self._stack, "Stack is not initialized yet")
238
+ assert(self._cameraStack, "Not initialized")
282
239
 
283
- table.insert(self._stack, state)
240
+ return self._cameraStack:Add(state)
284
241
  end
285
242
 
286
243
  function CameraStackService:Destroy()