Skip to content

Commit c749e39

Browse files
committed
🚧 Add backend support for multiple variants per variant keyframe.
1 parent f50b19a commit c749e39

File tree

3 files changed

+59
-41
lines changed

3 files changed

+59
-41
lines changed

src/systems/animationRenderer.ts

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,20 @@ export interface INodeTransform {
6868
interpolation?: 'step' | 'pre-post'
6969

7070
commands?: string
71-
execute_condition?: string
71+
commands_execute_condition?: string
7272
}
7373

7474
export interface IRenderedFrame {
7575
time: number
7676
node_transforms: Record<string, INodeTransform>
77-
variant?: {
78-
uuid: string
79-
execute_condition?: string
80-
}
81-
commands?: {
82-
commands: string
83-
execute_condition?: string
84-
}
77+
/** A list of Variants (by UUID) to apply this frame */
78+
variants?: string[]
79+
/** The condition to check before applying variants */
80+
variants_execute_condition?: string
81+
/** A mcfunction to run as the root on this frame. (Supports MCB syntax) */
82+
commands?: string
83+
/** The condition to check before running commands */
84+
commands_execute_condition?: string
8585
}
8686

8787
export interface IRenderedAnimation {
@@ -124,8 +124,8 @@ export function getFrame(
124124
const frame: IRenderedFrame = {
125125
time,
126126
node_transforms: {},
127-
variant: getVariantKeyframe(animation, time),
128-
commands: getCommandsKeyframe(animation, time),
127+
...getVariantKeyframe(animation, time),
128+
...getCommandsKeyframe(animation, time),
129129
}
130130

131131
if (lastAnimation !== animation) {
@@ -242,39 +242,52 @@ export function getFrame(
242242
head_rot: threeAxisRotationToTwoAxisRotation(rot),
243243
interpolation,
244244
commands,
245-
execute_condition: executeCondition,
245+
commands_execute_condition: executeCondition?.trim(),
246246
}
247247
}
248248

249249
return frame
250250
}
251251

252-
function getVariantKeyframe(animation: _Animation, time: number): IRenderedFrame['variant'] {
252+
function getVariantKeyframe(
253+
animation: _Animation,
254+
time: number
255+
): Pick<IRenderedFrame, 'variants' | 'variants_execute_condition'> {
253256
const variantKeyframes = animation.animators.effects?.variant as _Keyframe[]
254-
if (!variantKeyframes) return
255-
for (const kf of variantKeyframes) {
256-
if (kf.time !== time) continue
257-
const uuid = getKeyframeVariant(kf)
258-
if (!uuid) return
259-
return {
260-
uuid,
261-
execute_condition: getKeyframeExecuteCondition(kf),
257+
if (variantKeyframes) {
258+
const kf = variantKeyframes.find(kf => kf.time === time)
259+
if (kf) {
260+
// REVIEW - Variant keyframes do not support multiple variants yet.
261+
const uuid = getKeyframeVariant(kf)
262+
if (uuid) {
263+
return {
264+
variants: [uuid],
265+
variants_execute_condition: getKeyframeExecuteCondition(kf)?.trim(),
266+
}
267+
}
262268
}
263269
}
270+
return {}
264271
}
265272

266-
function getCommandsKeyframe(animation: _Animation, time: number): IRenderedFrame['commands'] {
273+
function getCommandsKeyframe(
274+
animation: _Animation,
275+
time: number
276+
): Pick<IRenderedFrame, 'commands' | 'commands_execute_condition'> {
267277
const commandsKeyframes = animation.animators.effects?.commands as _Keyframe[]
268-
if (!commandsKeyframes) return
269-
for (const kf of commandsKeyframes) {
270-
if (kf.time !== time) continue
271-
const commands = getKeyframeCommands(kf)
272-
if (!commands) return
273-
return {
274-
commands,
275-
execute_condition: getKeyframeExecuteCondition(kf),
278+
if (commandsKeyframes) {
279+
const kf = commandsKeyframes.find(kf => kf.time === time)
280+
if (kf) {
281+
const commands = getKeyframeCommands(kf)?.trim()
282+
if (commands) {
283+
return {
284+
commands,
285+
commands_execute_condition: getKeyframeExecuteCondition(kf)?.trim(),
286+
}
287+
}
276288
}
277289
}
290+
return {}
278291
}
279292

280293
export function updatePreview(animation: _Animation, time: number) {
@@ -348,13 +361,17 @@ export function hashAnimations(animations: IRenderedAnimation[]) {
348361
hash.update(';' + node.scale.join(';'))
349362
node.interpolation && hash.update(';' + node.interpolation)
350363
if (node.commands) hash.update(';' + node.commands)
351-
if (node.execute_condition) hash.update(';' + node.execute_condition)
364+
if (node.commands_execute_condition)
365+
hash.update(';' + node.commands_execute_condition)
352366
}
353-
if (frame.variant) {
354-
hash.update(';' + frame.variant.uuid)
355-
if (frame.variant.execute_condition)
356-
hash.update(';' + frame.variant.execute_condition)
367+
if (frame.variants) {
368+
hash.update(';' + frame.variants)
369+
if (frame.variants_execute_condition)
370+
hash.update(';' + frame.variants_execute_condition)
357371
}
372+
if (frame.commands) hash.update(';' + frame.commands)
373+
if (frame.commands_execute_condition)
374+
hash.update(';' + frame.commands_execute_condition)
358375
}
359376
}
360377
return hash.digest('hex')

src/systems/datapackCompiler/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ namespace TELLRAW {
476476
{ text: " must be executed as the rig's root entity.", color: 'red' },
477477
'\n',
478478
TELLRAW_LEARN_MORE_LINK(
479-
'https://animated-java.dev/docs/exported-rigs/controlling-a-rig-instance'
479+
'https://animated-java.dev/docs/rigs/controlling-a-rig-instance'
480480
),
481481
TELLRAW_SUFFIX(),
482482
])
@@ -880,16 +880,17 @@ async function createAnimationStorage(rig: IRenderedRig, animations: IRenderedAn
880880
)
881881
}
882882
}
883-
if (frame.variant) {
883+
if (frame.variants?.length) {
884+
const uuid = frame.variants[0]
884885
thisFrame.set(
885886
'variant',
886887
new NbtCompound()
887-
.set('name', new NbtString(rig.variants[frame.variant.uuid].name))
888+
.set('name', new NbtString(rig.variants[uuid].name))
888889
.set(
889890
'condition',
890891
new NbtString(
891-
frame.variant.execute_condition
892-
? `${frame.variant.execute_condition} `
892+
frame.variants_execute_condition
893+
? `${frame.variants_execute_condition} `
893894
: ''
894895
)
895896
)

src/systems/jsonCompiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ function serailizeNodeTransform(node: INodeTransform): ExportedNodetransform {
314314
scale: node.scale,
315315
interpolation: node.interpolation,
316316
commands: node.commands,
317-
execute_condition: node.execute_condition,
317+
commands_execute_condition: node.commands_execute_condition,
318318
}
319319
return json
320320
}

0 commit comments

Comments
 (0)