-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
AnimationSlotPlayer.lua
236 lines (187 loc) · 6.28 KB
/
AnimationSlotPlayer.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
--[=[
Acts as a priority slot which can be overridden and play any animation in.
See Roblox's animation system for more information.
@class AnimationSlotPlayer
]=]
local require = require(script.Parent.loader).load(script)
local BaseObject = require("BaseObject")
local AnimationUtils = require("AnimationUtils")
local ValueObject = require("ValueObject")
local Maid = require("Maid")
local EnumUtils = require("EnumUtils")
local RbxAssetUtils = require("RbxAssetUtils")
local AnimationSlotPlayer = setmetatable({}, BaseObject)
AnimationSlotPlayer.ClassName = "AnimationSlotPlayer"
AnimationSlotPlayer.__index = AnimationSlotPlayer
--[=[
Creates a new AnimationSlotPlayer with a target to play the animation on.
@param animationTarget Instance? | Observable<Instance>
@return AnimationSlotPlayer
]=]
function AnimationSlotPlayer.new(animationTarget)
local self = setmetatable(BaseObject.new(), AnimationSlotPlayer)
self._animationTarget = self._maid:Add(ValueObject.new(nil))
self._defaultFadeTime = self._maid:Add(ValueObject.new(0.1, "number"))
self._defaultAnimationPriority = self._maid:Add(ValueObject.new(nil))
self._currentAnimationTrackData = self._maid:Add(ValueObject.new(nil))
self._currentAnimationId = self._maid:Add(ValueObject.new(nil))
if animationTarget then
self:SetAnimationTarget(animationTarget)
end
return self
end
--[=[
Sets a new default fade time
@param defaultFadeTime number
]=]
function AnimationSlotPlayer:SetDefaultFadeTime(defaultFadeTime)
self._defaultFadeTime.Value = defaultFadeTime
end
--[=[
Sets a new default animation priority
@param defaultAnimationPriority number
]=]
function AnimationSlotPlayer:SetDefaultAnimationPriority(defaultAnimationPriority)
assert(EnumUtils.isOfType(Enum.AnimationPriority, defaultAnimationPriority) or defaultAnimationPriority == nil, "Bad defaultAnimationPriority")
self._defaultAnimationPriority.Value = defaultAnimationPriority
end
--[=[
Sets an animation target to play the animation on
@param animationTarget Instance | Observable<Instance>
]=]
function AnimationSlotPlayer:SetAnimationTarget(animationTarget)
self._animationTarget:Mount(animationTarget)
end
--[=[
Adjusts the speed of the animation playing in the slot
@param id string | number
@param speed number
@return () -> () -- Callback to clean things up
]=]
function AnimationSlotPlayer:AdjustSpeed(id, speed)
assert(RbxAssetUtils.isConvertableToRbxAsset(id), "Bad id")
assert(type(speed) == "number", "Bad speed")
local animationId = RbxAssetUtils.toRbxAssetId(id)
local topMaid = Maid.new()
topMaid:GiveTask(self._currentAnimationTrackData:ObserveBrio(function(data)
return data and data.animationId == animationId
end):Subscribe(function(brio)
if brio:IsDead() then
return
end
local data = brio:GetValue()
local maid = brio:ToMaid()
data.track:AdjustSpeed(speed)
-- TODO: Use stack here?
-- TODO: Probably need rogue property mechanisms
maid:GiveTask(function()
if math.abs(data.track.Speed - speed) <= 1e-3 then
data.track:AdjustSpeed(data.originalSpeed)
end
end)
end))
-- TODO: Probably per-a-track instead of global like this
self._maid._currentSpeedAdjustment = topMaid
return function()
if self._maid._currentSpeedAdjustment == topMaid then
self._maid._currentSpeedAdjustment = nil
end
end
end
--[=[
Adjusts the weight of the animation playing in the slot
@param id string | number
@param weight number
@param fadeTime number
@return () -> () -- Callback to clean things up
]=]
function AnimationSlotPlayer:AdjustWeight(id, weight, fadeTime)
assert(RbxAssetUtils.isConvertableToRbxAsset(id), "Bad id")
assert(type(weight) == "number", "Bad weight")
assert(type(fadeTime) == "number" or fadeTime == nil, "Bad fadeTime")
local animationId = RbxAssetUtils.toRbxAssetId(id)
local topMaid = Maid.new()
topMaid:GiveTask(self._currentAnimationTrackData:ObserveBrio(function(data)
return data and data.animationId == animationId
end):Subscribe(function(brio)
if brio:IsDead() then
return
end
local data = brio:GetValue()
local maid = brio:ToMaid()
data.track:AdjustWeight(weight, fadeTime)
-- TODO: Use stack here?
-- TODO: Probably need rogue property mechanisms
maid:GiveTask(function()
if math.abs(data.track.Speed - weight) <= 1e-3 then
data.track:AdjustWeight(data.originalWeight, fadeTime)
end
end)
end))
-- TODO: Probably per-a-track instead of global like this
self._maid._currentWeightAdjustment = topMaid
return function()
if self._maid._currentWeightAdjustment == topMaid then
self._maid._currentWeightAdjustment = nil
end
end
end
--[=[
Plays the animation in the slot, overriding any previous animation
@param id string | number
@param fadeTime number?
@param weight number?
@param speed number?
@param priority number?
@return () -> () -- Callback to clean things up
]=]
function AnimationSlotPlayer:Play(id, fadeTime, weight, speed, priority)
fadeTime = fadeTime or self._defaultFadeTime.Value
priority = priority or self._defaultAnimationPriority.Value
weight = weight or 1 -- We need to explicitly adjust the weight here
local topMaid = Maid.new()
local animationId = RbxAssetUtils.toRbxAssetId(id)
topMaid:GiveTask(self._animationTarget:ObserveBrio(function(target)
return target ~= nil
end):Subscribe(function(brio)
if brio:IsDead() then
return
end
local animationTarget = brio:GetValue()
local maid = brio:ToMaid()
local track = AnimationUtils.playAnimation(animationTarget, animationId, fadeTime, weight, speed, priority)
if track then
local data = {
animationId = animationId;
track = track;
originalSpeed = speed;
originalWeight = weight;
originalPriority = priority;
}
self._currentAnimationTrackData.Value = data
maid:GiveTask(function()
if self._currentAnimationTrackData.Value == data then
self._currentAnimationTrackData.Value = nil
end
end)
maid:GiveTask(function()
track:AdjustWeight(0, fadeTime or self._defaultFadeTime.Value)
end)
else
warn("[AnimationSlotPlayer] - Failed to get animation to play")
end
end))
self._maid._current = topMaid
return function()
if self._maid._current == topMaid then
self._maid._current = nil
end
end
end
--[=[
Stops the current animation playing
]=]
function AnimationSlotPlayer:Stop()
self._maid._current = nil
end
return AnimationSlotPlayer