-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.luau
150 lines (122 loc) · 5.02 KB
/
init.luau
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
--!strict
local types = require(script.Parent:WaitForChild("types"))
--- ### Animation.luau
---
--- animation player/controller
local Animation = {
Library = {} :: {[string]: types.AnimLibraryEntry};
FadeTime = 1/24;
}
--- returns animation controller table
function Animation.createcontroller(rig: Model): types.AnimController
local animplayer = rig:FindFirstChildOfClass("AnimationController") or rig:FindFirstChildOfClass("Humanoid")
assert(animplayer, "no AnimationController or Humanoid found in rig")
local animator = animplayer:FindFirstChildOfClass("Animator")
if not animator then
local a = Instance.new("Animator")
a.Parent = animplayer
animator = a
end
return {
Rig = rig;
AnimPlayer = animplayer;
Animator = animator::Animator;
ActionTrack = nil :: AnimationTrack?;
LoopTrack = nil :: AnimationTrack?;
}
end
--- plays animation \
--- `priority (?=Library[name].Priority|Enum.AnimationPriority.Action)`: animation priority
function Animation.play(animator: Animator, name: string, priority: Enum.AnimationPriority?, speed: number?, fadetime: number?, weight: number?)
local entry = Animation.Library[name]
assert(entry, `animation "{name}" not defined`)
local animobject = entry.Animation
assert(animobject)
local animtrack = animator:LoadAnimation(animobject)
animtrack:Play(fadetime or Animation.FadeTime, weight, speed)
animtrack.Priority = priority or entry.Priority or Enum.AnimationPriority.Action
return animtrack
end
--- sets and plays action track of animation controller \
--- `priority (?=Library[name].Priority|Enum.AnimationPriority.Action)`: animation priority \
--- returns `controller.ActionTrack` for convenience
function Animation.playactiontrack(controller: types.AnimController, name: string, priority: Enum.AnimationPriority?, speed: number?, fadetime: number?, weight: number?)
if controller.ActionTrack then
controller.ActionTrack:Stop(fadetime or Animation.FadeTime)
controller.ActionTrack = nil
end
local entry = Animation.Library[name]
assert(entry, `animation "{name}" not defined`)
local animobject = entry.Animation
assert(animobject)
controller.ActionTrack = controller.Animator:LoadAnimation(animobject)
assert(controller.ActionTrack, "lf solver")
controller.ActionTrack:Play(fadetime or Animation.FadeTime, weight, speed)
controller.ActionTrack.Priority = priority or entry.Priority or Enum.AnimationPriority.Action
return controller.ActionTrack
end
--- sets and plays loop track of animation controller \
--- `priority (?=Library[name].Priority|Enum.AnimationPriority.Idle)`: animation priority \
--- returns `controller.LoopTrack` for convenience
function Animation.playlooptrack(controller: types.AnimController, name: string, priority: Enum.AnimationPriority?, speed: number?, fadetime: number?, weight: number?)
if controller.LoopTrack then
controller.LoopTrack:Stop(fadetime or Animation.FadeTime)
controller.LoopTrack = nil
end
local entry = Animation.Library[name]
assert(entry, `animation "{name}" not defined`)
local animobject = entry.Animation
assert(animobject)
controller.LoopTrack = controller.Animator:LoadAnimation(animobject)
assert(controller.LoopTrack, "lf solver")
controller.LoopTrack:Play(fadetime or Animation.FadeTime, weight, speed)
controller.LoopTrack.Priority = priority or entry.Priority or Enum.AnimationPriority.Idle
return controller.LoopTrack
end
--- adjusts animationtrack speed to fit *played* time \
--- `speed = tracklength / time`
function Animation.fitplayed(animtrack: AnimationTrack, time: number)
animtrack:AdjustSpeed(animtrack.Length / time)
end
--- adjusts animationtrack speed to fit *remaining* time \
--- `speed = (tracklength - tracktime) / time`
function Animation.fitremaining(animtrack: AnimationTrack, time: number)
animtrack:AdjustSpeed((animtrack.Length - animtrack.TimePosition) / time)
end
--- returns true if passed animtationtrack can be overwritten by a mismatching animation \
--- (exists to avoid cases where the same animation can be played over itself)
function Animation.evaluate(animtrack: AnimationTrack?, name: string?)
if (animtrack == nil) and (name == nil) then return false end
if (animtrack == nil) then return true end
return animtrack.Name ~= name
end
--- defines animation library
function Animation.define(library: {[string]: types.AnimLibraryEntry})
for name, entry in library do
if Animation.Library[name] then
warn(`overwrote preexisting animation "{name}" to {entry.Id}`)
local preexisting = Animation.Library[name].Animation
if preexisting then
preexisting.AnimationId = entry.Id
end
continue
end
Animation.Library[name] = entry
local animobject = Instance.new("Animation")
animobject.Name = name
animobject.AnimationId = entry.Id
animobject.Parent = script
Animation.Library[name].Animation = animobject
end
end
--- returns array of animation instances for preloading
function Animation.compile()
local a = table.create(64)
for _, entry in Animation.Library do
local anim = entry.Animation
if not anim then continue end
table.insert(a, anim)
end
return a
end
return Animation