@quenty/immediateutils 1.1.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.
Files changed (58) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/LICENSE.md +21 -0
  3. package/README.md +23 -0
  4. package/default.project.json +14 -0
  5. package/package.json +52 -0
  6. package/src/Client/ImmediateServiceClient.lua +30 -0
  7. package/src/Server/ImmediateService.lua +25 -0
  8. package/src/Shared/core/ImmediateCoreComponents.lua +43 -0
  9. package/src/Shared/core/ImmediateCoreUtils.lua +188 -0
  10. package/src/Shared/core/ImmediateInstall.lua +53 -0
  11. package/src/Shared/core/ImmediateJecsUtils.lua +249 -0
  12. package/src/Shared/core/ImmediateTypes.lua +79 -0
  13. package/src/Shared/middleware/ImmediateBuffers/ImmediateBufferUtils.lua +65 -0
  14. package/src/Shared/middleware/ImmediateBuffers/ImmediateBuffersInstall.lua +35 -0
  15. package/src/Shared/middleware/ImmediateHooks/ImmediateCommonHooks.lua +86 -0
  16. package/src/Shared/middleware/ImmediateHooks/ImmediateHookAverageUtils.lua +26 -0
  17. package/src/Shared/middleware/ImmediateHooks/ImmediateHookSpringUtils.lua +31 -0
  18. package/src/Shared/middleware/ImmediateHooks/ImmediateHookUtils.lua +214 -0
  19. package/src/Shared/middleware/ImmediateHooks/ImmediateHooksInstall.lua +46 -0
  20. package/src/Shared/middleware/ImmediateHooks/commonhooks/async.lua +30 -0
  21. package/src/Shared/middleware/ImmediateHooks/commonhooks/cache.lua +34 -0
  22. package/src/Shared/middleware/ImmediateHooks/commonhooks/changed.lua +39 -0
  23. package/src/Shared/middleware/ImmediateHooks/commonhooks/conditionSustained.lua +31 -0
  24. package/src/Shared/middleware/ImmediateHooks/commonhooks/counter.lua +19 -0
  25. package/src/Shared/middleware/ImmediateHooks/commonhooks/delayed.lua +31 -0
  26. package/src/Shared/middleware/ImmediateHooks/commonhooks/delta.lua +41 -0
  27. package/src/Shared/middleware/ImmediateHooks/commonhooks/deltatime.lua +32 -0
  28. package/src/Shared/middleware/ImmediateHooks/commonhooks/difference.lua +31 -0
  29. package/src/Shared/middleware/ImmediateHooks/commonhooks/draw.lua +17 -0
  30. package/src/Shared/middleware/ImmediateHooks/commonhooks/entity.lua +21 -0
  31. package/src/Shared/middleware/ImmediateHooks/commonhooks/filterDescendants.lua +62 -0
  32. package/src/Shared/middleware/ImmediateHooks/commonhooks/findChild.lua +36 -0
  33. package/src/Shared/middleware/ImmediateHooks/commonhooks/gate.lua +23 -0
  34. package/src/Shared/middleware/ImmediateHooks/commonhooks/gatecounter.lua +37 -0
  35. package/src/Shared/middleware/ImmediateHooks/commonhooks/hookEntity.lua +29 -0
  36. package/src/Shared/middleware/ImmediateHooks/commonhooks/linearWalk.lua +143 -0
  37. package/src/Shared/middleware/ImmediateHooks/commonhooks/maid.lua +25 -0
  38. package/src/Shared/middleware/ImmediateHooks/commonhooks/noise.lua +54 -0
  39. package/src/Shared/middleware/ImmediateHooks/commonhooks/random.lua +20 -0
  40. package/src/Shared/middleware/ImmediateHooks/commonhooks/randomChoice.lua +52 -0
  41. package/src/Shared/middleware/ImmediateHooks/commonhooks/rtbuffer.lua +28 -0
  42. package/src/Shared/middleware/ImmediateHooks/commonhooks/scheduledValues.lua +143 -0
  43. package/src/Shared/middleware/ImmediateHooks/commonhooks/scheduler.lua +191 -0
  44. package/src/Shared/middleware/ImmediateHooks/commonhooks/sin.lua +38 -0
  45. package/src/Shared/middleware/ImmediateHooks/commonhooks/slidingAvg.lua +78 -0
  46. package/src/Shared/middleware/ImmediateHooks/commonhooks/spring.lua +118 -0
  47. package/src/Shared/middleware/ImmediateHooks/commonhooks/state.lua +23 -0
  48. package/src/Shared/middleware/ImmediateHooks/commonhooks/subscribe.lua +47 -0
  49. package/src/Shared/middleware/ImmediateHooks/commonhooks/throttle.lua +37 -0
  50. package/src/Shared/middleware/ImmediateHooks/commonhooks/tween.lua +14 -0
  51. package/src/Shared/middleware/ImmediateHooks/commonhooks/useBinder.lua +54 -0
  52. package/src/Shared/middleware/ImmediateHooks/commonhooks/useTieInterface.lua +34 -0
  53. package/src/Shared/scheduler/ImmediateScheduler.lua +307 -0
  54. package/src/jest.config.lua +3 -0
  55. package/src/node_modules.project.json +7 -0
  56. package/test/default.project.json +32 -0
  57. package/test/scripts/Client/ClientMain.client.lua +10 -0
  58. package/test/scripts/Server/ServerMain.server.lua +18 -0
@@ -0,0 +1,23 @@
1
+ --!strict
2
+ --[=[
3
+ @class gate
4
+
5
+ Like once, but cleans up if not in the hot path anymore, and will be able to
6
+ return true if reached again.
7
+ ]=]
8
+
9
+ local require = require(script.Parent.loader).load(script)
10
+
11
+ local ImmediateTypes = require("ImmediateTypes")
12
+ local getOrCreateHookState = require("ImmediateHookUtils").getOrCreateHookState
13
+
14
+ return function(rt: ImmediateTypes.ImmediateRuntime)
15
+ return function(dis)
16
+ local hookState, _hookMaid = getOrCreateHookState(rt, dis)
17
+ if hookState.ran then
18
+ return false
19
+ end
20
+ hookState.ran = true
21
+ return true
22
+ end :: (any) -> boolean
23
+ end
@@ -0,0 +1,37 @@
1
+ --!nonstrict
2
+ --[=[
3
+ @class gatecounter
4
+
5
+ A stable discriminator epoch for code paths that run every frame while "active".
6
+ Starts at 1. Returns the same number for every call in a continuous run; after at
7
+ least one frame without a call (evaluateAndCleanupHooks), the epoch increments so
8
+ the next run can use a fresh discriminator for cache/subscribe/etc.
9
+
10
+ Careful, because this needs to store counter as state no matter if it's called or not,
11
+ so this may leak.
12
+ ]=]
13
+
14
+ local require = require(script.Parent.loader).load(script)
15
+
16
+ local ImmediateTypes = require("ImmediateTypes")
17
+ local getOrCreateHookState = require("ImmediateHookUtils").getOrCreateHookState
18
+
19
+ return function(rt: ImmediateTypes.ImmediateRuntime)
20
+ return function(dis)
21
+ local hookState, _hookMaid = getOrCreateHookState(rt, dis, function(state)
22
+ if state._activeThisFrame then
23
+ state._onHotPath = true
24
+ state._activeThisFrame = nil
25
+ elseif state._onHotPath then
26
+ state.counter = (state.counter or 1) + 1
27
+ state._onHotPath = false
28
+ end
29
+ return false
30
+ end)
31
+ if hookState.counter == nil then
32
+ hookState.counter = 1
33
+ end
34
+ hookState._activeThisFrame = true
35
+ return hookState.counter
36
+ end
37
+ end
@@ -0,0 +1,29 @@
1
+ --!nonstrict
2
+ --[=[
3
+ @class hookEntity
4
+
5
+ Returns a brand new entity that is a child of the hook entity.
6
+ Will thus be instantly deleted if the hook is cleaned up/not visited,
7
+ so you don't have to write the cleanup/entity delete.
8
+ ]=]
9
+
10
+ local require = require(script.Parent.loader).load(script)
11
+
12
+ local ImmediateTypes = require("ImmediateTypes")
13
+ local Jecs = require("Jecs")
14
+ local Jecst = require("Jecst")
15
+ local getOrCreateHookState = require("ImmediateHookUtils").getOrCreateHookState
16
+
17
+ return function(rt: ImmediateTypes.ImmediateRuntime)
18
+ return function(dis, initDecorator: (Jecst.Entity) -> any?)
19
+ local hookState, _hookMaid, hookStateEntity = getOrCreateHookState(rt, dis)
20
+ if not hookState.childEntity then
21
+ hookState.childEntity = rt.world:entity()
22
+ rt.world:add(hookState.childEntity, Jecs.pair(rt.comps.ChildOf, hookStateEntity))
23
+ if initDecorator then
24
+ initDecorator(hookState.childEntity)
25
+ end
26
+ end
27
+ return hookState.childEntity
28
+ end
29
+ end
@@ -0,0 +1,143 @@
1
+ --!nonstrict
2
+ --[=[
3
+ @class linearWalk
4
+
5
+ Moves the current value toward `goal` at a constant rate of `speed` units/second.
6
+ Supports number / Vector2 / Vector3 / Color3 / CFrame.
7
+ Clamps onto the goal; snaps when within epsilon. Retargets immediately if goal changes.
8
+
9
+ CFrame: speed is studs/sec along Position; orientation follows via CFrame:Lerp with the
10
+ same alpha (so pose finishes with the position walk).
11
+
12
+ Optional `duration` (first call only): measures distance to the initial goal and locks
13
+ speed to distance/duration for the lifetime of this hook. Later speed args are ignored.
14
+ ]=]
15
+
16
+ local require = require(script.Parent.loader).load(script)
17
+
18
+ local ImmediateHookSpringUtils = require("ImmediateHookSpringUtils")
19
+ local ImmediateTypes = require("ImmediateTypes")
20
+ local getOrCreateHookState = require("ImmediateHookUtils").getOrCreateHookState
21
+
22
+ local LINEAR_WALK_EPSILON = 1e-4
23
+
24
+ return function(rt: ImmediateTypes.ImmediateRuntime)
25
+ return function(dis: any?, goal: any?, value: any?, speed: number?, duration: number?): any
26
+ local hookState, _hookMaid = getOrCreateHookState(rt, dis)
27
+
28
+ if hookState.linearWalkIsCFrame == nil then
29
+ hookState.linearWalkIsCFrame = (goal ~= nil and typeof(goal) == "CFrame")
30
+ or (value ~= nil and typeof(value) == "CFrame")
31
+ end
32
+ local isCFrame = hookState.linearWalkIsCFrame
33
+
34
+ if isCFrame then
35
+ if hookState.position == nil then
36
+ local initialSource = value
37
+ if initialSource == nil then
38
+ initialSource = goal
39
+ end
40
+ if initialSource == nil then
41
+ initialSource = CFrame.new()
42
+ end
43
+ hookState.position = initialSource
44
+ hookState.lastAtClock = os.clock()
45
+
46
+ if duration ~= nil and goal ~= nil then
47
+ local distance = (goal.Position - hookState.position.Position).Magnitude
48
+ if duration <= 0 then
49
+ hookState.position = goal
50
+ hookState.speed = 0
51
+ else
52
+ hookState.speed = distance / duration
53
+ end
54
+ hookState.speedLockedFromDuration = true
55
+ else
56
+ hookState.speed = if speed ~= nil then speed else 1
57
+ end
58
+ end
59
+
60
+ if speed ~= nil and not hookState.speedLockedFromDuration then
61
+ hookState.speed = speed
62
+ end
63
+
64
+ local now = os.clock()
65
+ local dt = now - hookState.lastAtClock
66
+ hookState.lastAtClock = now
67
+
68
+ if goal ~= nil then
69
+ local current = hookState.position
70
+ local distance = (goal.Position - current.Position).Magnitude
71
+
72
+ if distance <= LINEAR_WALK_EPSILON or hookState.speed * dt >= distance then
73
+ hookState.position = goal
74
+ elseif dt > 0 and hookState.speed ~= 0 then
75
+ local maxStep = hookState.speed * dt
76
+ hookState.position = current:Lerp(goal, math.clamp(maxStep / distance, 0, 1))
77
+ end
78
+ end
79
+
80
+ return hookState.position
81
+ end
82
+
83
+ if hookState.linearWalkIsColor3 == nil then
84
+ hookState.linearWalkIsColor3 = ImmediateHookSpringUtils.usesColor3(goal, value)
85
+ end
86
+ local isColor3 = hookState.linearWalkIsColor3
87
+
88
+ if hookState.position == nil then
89
+ local initialSource = value
90
+ if initialSource == nil then
91
+ initialSource = goal
92
+ end
93
+ if initialSource == nil then
94
+ initialSource = if isColor3 then Vector3.zero else 0
95
+ end
96
+ hookState.position = ImmediateHookSpringUtils.internalValue(initialSource)
97
+ hookState.lastAtClock = os.clock()
98
+
99
+ if duration ~= nil and goal ~= nil then
100
+ local target = ImmediateHookSpringUtils.internalValue(goal)
101
+ local delta = target - hookState.position
102
+ local distance = if typeof(delta) == "number" then math.abs(delta) else delta.Magnitude
103
+ if duration <= 0 then
104
+ hookState.position = target
105
+ hookState.speed = 0
106
+ else
107
+ hookState.speed = distance / duration
108
+ end
109
+ hookState.speedLockedFromDuration = true
110
+ else
111
+ hookState.speed = if speed ~= nil then speed else 1
112
+ end
113
+ end
114
+
115
+ if speed ~= nil and not hookState.speedLockedFromDuration then
116
+ hookState.speed = speed
117
+ end
118
+
119
+ local now = os.clock()
120
+ local dt = now - hookState.lastAtClock
121
+ hookState.lastAtClock = now
122
+
123
+ if goal ~= nil then
124
+ local target = ImmediateHookSpringUtils.internalValue(goal)
125
+ local current = hookState.position
126
+ local delta = target - current
127
+ local distance = if typeof(delta) == "number" then math.abs(delta) else delta.Magnitude
128
+
129
+ if distance <= LINEAR_WALK_EPSILON or hookState.speed * dt >= distance then
130
+ hookState.position = target
131
+ elseif dt > 0 and hookState.speed ~= 0 then
132
+ local maxStep = hookState.speed * dt
133
+ if typeof(delta) == "number" then
134
+ hookState.position = current + (if delta > 0 then maxStep else -maxStep)
135
+ else
136
+ hookState.position = current + delta.Unit * maxStep
137
+ end
138
+ end
139
+ end
140
+
141
+ return ImmediateHookSpringUtils.externalValue(hookState.position, isColor3)
142
+ end
143
+ end
@@ -0,0 +1,25 @@
1
+ --!nonstrict
2
+ --[=[
3
+ @class maid
4
+ ]=]
5
+
6
+ local require = require(script.Parent.loader).load(script)
7
+
8
+ local ImmediateTypes = require("ImmediateTypes")
9
+ local Maid = require("Maid")
10
+ local getOrCreateHookState = require("ImmediateHookUtils").getOrCreateHookState
11
+
12
+ return function(rt: ImmediateTypes.ImmediateRuntime)
13
+ return function(dis: any?, callback: (maid: Maid.Maid) -> any?)
14
+ local _hookState, hookMaid = getOrCreateHookState(rt, dis, function(_state)
15
+ if callback then
16
+ callback(_state.hookMaid)
17
+ end
18
+ return true
19
+ end)
20
+ if not _hookState.hookMaid then
21
+ _hookState.hookMaid = hookMaid
22
+ end
23
+ return hookMaid
24
+ end
25
+ end
@@ -0,0 +1,54 @@
1
+ --!nonstrict
2
+ --[=[
3
+ @class noise
4
+
5
+ Smooth Perlin-like value that wanders in [min, max] over time.
6
+ frequency = cycles per second (default 1). Uses Roblox math.noise.
7
+
8
+ Frequency may change every frame without popping: we integrate
9
+ phase += dt * frequency instead of sampling (clock * frequency), which
10
+ would jump when frequency changes.
11
+
12
+ Call every frame like spring (use a distinct `dis` per independent channel).
13
+
14
+ Why math.noise (Perlin-ish): cheap, continuous, good for rock bob / FOV wobble /
15
+ ambient motion. Pitfall — returns 0 when x,y,z are ALL integers — so we keep
16
+ fixed non-integer phase offsets on y/z and only advance x with phase.
17
+ Range is ~[-0.5, 0.5]; we remap + clamp into [min, max].
18
+ ]=]
19
+
20
+ local require = require(script.Parent.loader).load(script)
21
+
22
+ local ImmediateTypes = require("ImmediateTypes")
23
+ local getOrCreateHookState = require("ImmediateHookUtils").getOrCreateHookState
24
+
25
+ return function(rt: ImmediateTypes.ImmediateRuntime)
26
+ return function(dis: any?, min: number?, max: number?, frequency: number?): number
27
+ local hookState, _hookMaid = getOrCreateHookState(rt, dis)
28
+
29
+ local lo = if min ~= nil then min else 0
30
+ local hi = if max ~= nil then max else 1
31
+ local freq = if frequency ~= nil then frequency else 1
32
+
33
+ local now = os.clock()
34
+ if not hookState.noiseInit then
35
+ hookState.noiseInit = true
36
+ hookState.noisePhase = 0
37
+ hookState.noiseLastClock = now
38
+ -- Non-integer y/z so we never hit the all-integer → 0 lattice.
39
+ local rng = Random.new()
40
+ hookState.noiseOy = rng:NextNumber(0.1, 1000)
41
+ hookState.noiseOz = rng:NextNumber(0.1, 1000)
42
+ hookState.noiseOx = rng:NextNumber(0.1, 1000)
43
+ end
44
+
45
+ local dt = now - hookState.noiseLastClock
46
+ hookState.noiseLastClock = now
47
+ -- Integrate frequency so changing it mid-flight stays continuous.
48
+ hookState.noisePhase += math.max(dt, 0) * freq
49
+
50
+ local n = math.noise(hookState.noisePhase + hookState.noiseOx, hookState.noiseOy, hookState.noiseOz)
51
+ local u = math.clamp(n + 0.5, 0, 1)
52
+ return lo + (hi - lo) * u
53
+ end
54
+ end
@@ -0,0 +1,20 @@
1
+ --!nonstrict
2
+ --[=[
3
+ @class random
4
+ ]=]
5
+
6
+ local require = require(script.Parent.loader).load(script)
7
+
8
+ local ImmediateTypes = require("ImmediateTypes")
9
+ local getOrCreateHookState = require("ImmediateHookUtils").getOrCreateHookState
10
+
11
+ return function(rt: ImmediateTypes.ImmediateRuntime)
12
+ return function(min: number?, max: number?, dis: any?, seed: number?)
13
+ local hookState, _hookMaid = getOrCreateHookState(rt, dis)
14
+ if hookState.randomNumber == nil then
15
+ local rng = Random.new(seed)
16
+ hookState.randomNumber = rng:NextNumber(min or 0, max or 1)
17
+ end
18
+ return hookState.randomNumber
19
+ end
20
+ end
@@ -0,0 +1,52 @@
1
+ --!nonstrict
2
+ --[=[
3
+ @class randomChoice
4
+
5
+ Give it a dictionary:
6
+ key = any value,
7
+ value = number weight
8
+ will choose based on weights,
9
+ can choose to be persistent or pass in a clock() to dis
10
+ if you pass in something like callbacks, do noCache to avoid closing callbacks
11
+ ]=]
12
+
13
+ local require = require(script.Parent.loader).load(script)
14
+
15
+ local ImmediateTypes = require("ImmediateTypes")
16
+ local RandomUtils = require("RandomUtils")
17
+ local getOrCreateHookState = require("ImmediateHookUtils").getOrCreateHookState
18
+
19
+ return function(rt: ImmediateTypes.ImmediateRuntime)
20
+ return function(choices: { [any]: number }, dis: any?, alwaysRandom: boolean?, noCache: boolean?, rng: Random?)
21
+ local hookState, _hookMaid = getOrCreateHookState(rt, dis)
22
+ if not hookState.init then
23
+ hookState.init = true
24
+ hookState.arrayOfValues = {}
25
+ hookState.arrayOfWeights = {}
26
+ for value, weight in choices do
27
+ table.insert(hookState.arrayOfValues, value)
28
+ table.insert(hookState.arrayOfWeights, weight)
29
+ end
30
+ hookState.chosen = RandomUtils.weightedChoice(hookState.arrayOfValues, hookState.arrayOfWeights, rng)
31
+ _hookMaid:GiveTask(function()
32
+ table.clear(hookState)
33
+ end)
34
+ end
35
+ if noCache then
36
+ if alwaysRandom == true then
37
+ hookState.arrayOfValues = {}
38
+ hookState.arrayOfWeights = {}
39
+ for value, weight in choices do
40
+ table.insert(hookState.arrayOfValues, value)
41
+ table.insert(hookState.arrayOfWeights, weight)
42
+ end
43
+ hookState.chosen = RandomUtils.weightedChoice(hookState.arrayOfValues, hookState.arrayOfWeights, rng)
44
+ end
45
+ else
46
+ if alwaysRandom == true then
47
+ hookState.chosen = RandomUtils.weightedChoice(hookState.arrayOfValues, hookState.arrayOfWeights, rng)
48
+ end
49
+ end
50
+ return hookState.chosen
51
+ end
52
+ end
@@ -0,0 +1,28 @@
1
+ --!nonstrict
2
+ --[=[
3
+ @class rtbuffer
4
+
5
+ Returns a consistent reference to a state table that, if called by a system,
6
+ is cleared when that system ends. (still discriminated by dis, filename, line number).
7
+
8
+ Technically not an average hook, because it persists through hot reloads
9
+ and will never clean up. Thus, any call to this hook function will return the same
10
+ state table whether called from any code anywhere (because it uses rt.), whether it's
11
+ from a Maid cleanup, an Observer subscription, a system call, etc.
12
+ ]=]
13
+
14
+ local require = require(script.Parent.loader).load(script)
15
+
16
+ local ImmediateTypes = require("ImmediateTypes")
17
+ local getOrCreateHookState = require("ImmediateHookUtils").getOrCreateHookState
18
+
19
+ return function(rt: ImmediateTypes.ImmediateRuntime)
20
+ return function(dis: any?)
21
+ local hookState, hookMaid = getOrCreateHookState(rt, dis, nil, true)
22
+ hookState.calledThisFrame = true
23
+ if hookState.tab == nil then
24
+ hookState.tab = {}
25
+ end
26
+ return hookState.tab, hookMaid
27
+ end
28
+ end
@@ -0,0 +1,143 @@
1
+ --!nonstrict
2
+ --[=[
3
+ @class scheduledValues
4
+ ]=]
5
+
6
+ local require = require(script.Parent.loader).load(script)
7
+
8
+ local ImmediateTypes = require("ImmediateTypes")
9
+ local getOrCreateHookState = require("ImmediateHookUtils").getOrCreateHookState
10
+
11
+ return function(rt: ImmediateTypes.ImmediateRuntime)
12
+ return function(
13
+ phases: {
14
+ [number]: any,
15
+ },
16
+ dis: any?,
17
+ -- forceInOrder: if true, will force the scheduler
18
+ -- to call callbacks in order of time keys even if
19
+ -- current time already elapsed (will call in a sequence at least once.)
20
+ forceInOrder: boolean?
21
+ ): {
22
+ lastCalledAt: number,
23
+ currentOverallTime: number,
24
+ currentStageTime: number,
25
+ currentStageDuration: number?,
26
+ currentStageAlpha: number,
27
+ currentValue: any,
28
+ currentTimeKey: number | nil,
29
+ }
30
+ local _hookState, _hookMaid = getOrCreateHookState(rt, dis)
31
+ local currClock = os.clock()
32
+
33
+ if not _hookState._init then
34
+ _hookState._init = true
35
+ _hookState._timekeyToValue = phases
36
+ _hookState._sortedTimeKeys = {}
37
+ _hookState._timeKeysVisited = {}
38
+ _hookState.lastCalledAt = currClock
39
+ _hookState.currentOverallTime = 0
40
+ _hookState.currentStageTime = 0
41
+ _hookState.currentStageDuration = nil
42
+ _hookState.currentStageAlpha = 0
43
+ _hookState.currentValue = nil
44
+ _hookState.currentTimeKey = nil
45
+
46
+ for timestamp, _value in phases do
47
+ table.insert(_hookState._sortedTimeKeys, timestamp)
48
+ _hookState._timeKeysVisited[timestamp] = false
49
+ end
50
+ table.sort(_hookState._sortedTimeKeys)
51
+
52
+ _hookMaid:GiveTask(function()
53
+ table.clear(_hookState)
54
+ end)
55
+
56
+ _hookState.evaluateCurrentTimeKey = function()
57
+ -- From earliest to latest
58
+ for i, timeKey in _hookState._sortedTimeKeys do
59
+ if _hookState._forceInOrder then
60
+ local weArePastIt = _hookState.currentOverallTime >= timeKey
61
+ local weHaventExecutedBefore = not _hookState._timeKeysVisited[timeKey]
62
+ if weArePastIt and weHaventExecutedBefore then
63
+ return timeKey
64
+ end
65
+ end
66
+ if _hookState.currentOverallTime < timeKey then
67
+ local prevTimeKey = _hookState._sortedTimeKeys[i - 1]
68
+ if prevTimeKey then
69
+ return prevTimeKey
70
+ else
71
+ return nil
72
+ end
73
+ end
74
+ end
75
+ -- If we've gone through all the time keys and didn't find one,
76
+ -- return the last time key since we're past all of them.
77
+ return _hookState._sortedTimeKeys[#_hookState._sortedTimeKeys]
78
+ end
79
+ _hookState.evaluateAndReturnValue = function()
80
+ local timeKey = _hookState.evaluateCurrentTimeKey()
81
+ if timeKey then
82
+ if _hookState.currentTimeKey ~= timeKey then
83
+ _hookState.currentStageTime = 0
84
+ end
85
+ _hookState.currentTimeKey = timeKey
86
+ local value = _hookState._timekeyToValue[timeKey]
87
+ _hookState.currentValue = value
88
+ _hookState._timeKeysVisited[timeKey] = true
89
+ return value
90
+ end
91
+ return nil
92
+ end
93
+ _hookState.evaluateCurrentStageDuration = function()
94
+ local currentTimeKey = _hookState.currentTimeKey
95
+ if currentTimeKey == nil then
96
+ return nil
97
+ end
98
+
99
+ local sortedTimeKeys = _hookState._sortedTimeKeys
100
+ local currentIndex = table.find(sortedTimeKeys, currentTimeKey)
101
+ local nextTimeKey = currentIndex and sortedTimeKeys[currentIndex + 1]
102
+ if nextTimeKey == nil then
103
+ return nil
104
+ end
105
+
106
+ local duration = nextTimeKey - currentTimeKey
107
+ if duration <= 0 then
108
+ return nil
109
+ end
110
+
111
+ return duration
112
+ end
113
+ end
114
+
115
+ _hookState._forceInOrder = forceInOrder == true
116
+
117
+ local timeSinceLastCall = currClock - _hookState.lastCalledAt
118
+ _hookState.currentOverallTime += timeSinceLastCall
119
+ _hookState.currentStageTime += timeSinceLastCall
120
+ _hookState.lastCalledAt = currClock
121
+ _hookState.evaluateAndReturnValue()
122
+ if _hookState.currentTimeKey ~= nil then
123
+ _hookState.currentStageTime = math.max(_hookState.currentOverallTime - _hookState.currentTimeKey, 0)
124
+ end
125
+ _hookState.currentStageDuration = _hookState.evaluateCurrentStageDuration()
126
+ if _hookState.currentStageDuration then
127
+ _hookState.currentStageAlpha =
128
+ math.clamp(_hookState.currentStageTime / _hookState.currentStageDuration, 0, 1)
129
+ else
130
+ _hookState.currentStageAlpha = if _hookState.currentTimeKey == nil then 0 else 1
131
+ end
132
+
133
+ return {
134
+ lastCalledAt = _hookState.lastCalledAt,
135
+ currentOverallTime = _hookState.currentOverallTime,
136
+ currentStageTime = _hookState.currentStageTime,
137
+ currentStageDuration = _hookState.currentStageDuration,
138
+ currentStageAlpha = _hookState.currentStageAlpha,
139
+ currentValue = _hookState.currentValue,
140
+ currentTimeKey = _hookState.currentTimeKey,
141
+ }
142
+ end
143
+ end