Skip to content

Commit d661a76

Browse files
Feat: Support parallel animation for setAnimation command; Fix: potential runtime error caused by undefined parameters reported by review bot
1 parent 07a61d1 commit d661a76

File tree

11 files changed

+72
-41
lines changed

11 files changed

+72
-41
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
changeBg:WebGalEnter.webp -next;
2+
changeFigure:stand.webp -id=figure01 -transform={"position":{"x":1000,"y":720}};
3+
;演示setAnimation平行执行
4+
setAnimation:shockwaveIn -target=figure01 -next
5+
setAnimation:move-front-and-back -target=figure01 -parallel
6+
;演示通过-continue接续执行两个常规setTransform正常运作、不被打断
7+
setTransform:{"position":{"x":-1000}} -duration=5000 -target=figure01 -continue
8+
setTransform:{"position":{"x":1000}} -duration=5000 -target=figure01
9+
;演示setTransform平行执行
10+
setTransform:{"position":{"x":-1000},"scale":{"x":0.5},"contrast":0.5} -duration=5000 -target=figure01 -ease=easeOut -next -keep
11+
wait:2000
12+
setTransform:{"position":{"y":0},"scale":{"y":0.5},"saturation":0} -duration=5000 -target=figure01 -ease=linear -parallel -continue
13+
setTransform:{"position":{"y":-720},"scale":{"y":1},"saturation":1} -duration=5000 -target=figure01 -ease=linear -next
14+
setTransform:{"position":{"x":1000},"scale":{"x":1},"contrast":1} -duration=5000 -target=figure01 -ease=easeIn -parallel;
15+
;演示参数解耦改动后setTempAnimation普通运作是否正常
16+
setTempAnimation:[{"duration":0}, {"duration":500,"position":{"x":-1000}}, {"duration":500,"position":{"y":720},"scale":{"y":0.5},"saturation":0}, {"duration":500,"position":{"x":-1000, "y":720}}, {"duration":500}, {"duration":500,"position":{"x":-1000},"scale":{"x":0.5},"contrast":0.5}] -target=figure01
17+
setTempAnimation:[{"duration":0}, {"duration":500,"position":{"x":1000}}, {"duration":500,"position":{"y":720}}, {"duration":500,"position":{"x":1000, "y":720}}, {"duration":500}, {"duration":500,"position":{"x":1000}}] -target=figure01
18+
;演示参数解耦改动后setTransform的-writeDefault参数是否运作正常
19+
setTransform:{} -writeDefault -target=figure01 -duration=500
20+
setTransform:{"position":{"x":1000,"y":720}} -target=figure01 -next;
21+
setAnimation:shockwaveOut -target=figure01 -parallel
22+
;演示setTempAnimation平行执行
23+
setTempAnimation:[{"duration":0},{"position":{"x":-1000},"scale":{"x":0.5},"contrast":0.5,"duration":5000,"ease":"easeOut"},{"position":{"x":-1000},"scale":{"x":0.5},"contrast":0.5,"duration":2000},{"position":{"x":600},"scale":{"x":1},"contrast":1,"duration":5000,"ease":"easeIn"},{"duration":2000}] -target=figure01 -next;
24+
setTempAnimation:[{"duration":2000},{"position":{"y":0},"scale":{"y":0.5},"saturation":0,"duration":5000,"ease":"linear"},{"position":{"y":-720},"scale":{"y":1},"saturation":1,"duration":5000,"ease":"linear"},{"duration":2000}] -target=figure01 -parallel -continue;
25+
;演示并行执行多条终止时间点不一致的setTransform
26+
setTransform:{"position":{"x":-1000}} -duration=5000 -next -target=figure01;
27+
setTransform:{"position":{"y":0}} -duration=3000 -parallel -target=figure01;
28+
setTransform:{"position":{"x":1000}} -duration=3000 -next -target=figure01;
29+
setTransform:{"position":{"y":-720}} -duration=5000 -parallel -target=figure01;
30+
changeBg: -next;
31+
changeFigure: -id=figure01

packages/webgal/src/Core/Modules/animationFunctions.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ export function getAnimationObject(animationName: string, target: string, durati
3333
let newEffect;
3434

3535
if (!writeDefault && targetSetEffect && targetSetEffect.transform) {
36-
const targetScale = pickBy(targetSetEffect.transform.scale, (source, key)=> unionScaleKeys.has(key))
37-
const targetPosition = pickBy(targetSetEffect.transform.position, (source, key)=> unionPositionKeys.has(key))
38-
const originalTransform = { ...pickBy(targetSetEffect.transform, (source, key)=> unionKeys.has(key))};
39-
originalTransform.scale = targetScale
40-
originalTransform.position = targetPosition
36+
const targetScale = pickBy(targetSetEffect.transform.scale || {}, (source, key) => unionScaleKeys.has(key));
37+
const targetPosition = pickBy(targetSetEffect.transform.position || {}, (s, key) => unionPositionKeys.has(key));
38+
const originalTransform = { ...pickBy(targetSetEffect.transform, (source, key) => unionKeys.has(key)) };
39+
originalTransform.scale = targetScale;
40+
originalTransform.position = targetPosition;
4141
newEffect = cloneDeep({ ...originalTransform, duration: 0, ease: '' });
4242
} else {
4343
newEffect = cloneDeep({ ...baseTransform, duration: 0, ease: '' });
@@ -66,14 +66,6 @@ export function getAnimateDuration(animationName: string) {
6666
return 0;
6767
}
6868

69-
export function getAnimateDurationFromObj(animation: IUserAnimation) {
70-
let duration = 0;
71-
animation.effects.forEach((e) => {
72-
duration += e.duration;
73-
});
74-
return duration;
75-
}
76-
7769
// eslint-disable-next-line max-params
7870
export function getEnterExitAnimation(
7971
target: string,

packages/webgal/src/Core/Modules/perform/performController.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class PerformController {
1919
public performList: Array<IPerform> = [];
2020

2121
public arrangeNewPerform(perform: IPerform, script: ISentence, syncPerformState = true) {
22-
if (!perform.isParallel){
22+
if (!perform.isParallel) {
2323
// 检查演出列表内是否有相同的演出,如果有,一定是出了什么问题
2424
const dupPerformIndex = this.performList.findIndex((p) => p.performName === perform.performName);
2525
if (dupPerformIndex > -1) {
@@ -106,7 +106,7 @@ export class PerformController {
106106
}
107107

108108
public softUnmountPerformObject(perform: IPerform) {
109-
const idx = this.performList.indexOf(perform)
109+
const idx = this.performList.indexOf(perform);
110110
if (idx < 0) return;
111111
perform.stopFunction();
112112
clearTimeout(perform.stopTimeout as unknown as number);
@@ -122,7 +122,6 @@ export class PerformController {
122122
// nextSentence();
123123
this.goNextWhenOver();
124124
}
125-
126125
}
127126

128127
public erasePerformFromState(name: string) {

packages/webgal/src/Core/Modules/perform/performInterface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export interface IPerform {
2323
arrangePerformPromise?: Promise<IPerform>;
2424
// 跳过由 nextSentence 函数引发的演出回收
2525
skipNextCollect?: boolean;
26-
//
26+
//
2727
isParallel?: boolean;
2828
}
2929

packages/webgal/src/Core/controller/stage/pixi/PixiController.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ export default class PixiStage {
7373
if (!source) return;
7474
const targetScale = target.scale;
7575
const targetPosition = target.position;
76-
if (target.scale) Object.assign(targetScale!, omitBy(source.scale,isUndefined));
77-
if (target.position) Object.assign(targetPosition!, omitBy(source.position,isUndefined));
78-
Object.assign(target, omitBy(source,isUndefined));
76+
if (target.scale) Object.assign(targetScale!, omitBy(source.scale || {}, isUndefined));
77+
if (target.position) Object.assign(targetPosition!, omitBy(source.position || {}, isUndefined));
78+
Object.assign(target, omitBy(source, isUndefined));
7979
target.scale = targetScale;
8080
target.position = targetPosition;
8181
}

packages/webgal/src/Core/controller/stage/pixi/animations/generateTransformAnimationObj.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function generateTransformAnimationObj(
1111
applyFrame: AnimationFrame,
1212
duration: number | string | boolean | null,
1313
ease: string,
14-
writeFullEffect: boolean = true,
14+
writeFullEffect = true,
1515
): AnimationObj {
1616
let animationObj;
1717
// 获取那个 target 的当前变换
@@ -30,13 +30,16 @@ export function generateTransformAnimationObj(
3030
if (writeFullEffect) {
3131
const effectWithDuration = { ...targetEffect!.transform!, duration: 0, ease };
3232
animationObj.unshift(effectWithDuration);
33-
}
34-
else {
35-
const targetScale = pickBy(targetEffect!.transform!.scale, (source, key)=> has(applyFrame.scale, key))
36-
const targetPosition = pickBy(targetEffect!.transform!.position, (source, key)=> has(applyFrame.position, key))
37-
const effectWithDuration = { ...pickBy(targetEffect!.transform!, (source, key)=> has(applyFrame, key) ), duration: 0, ease };
38-
effectWithDuration.scale = targetScale
39-
effectWithDuration.position = targetPosition
33+
} else {
34+
const targetScale = pickBy(targetEffect.transform?.scale || {}, (source, key) => has(applyFrame.scale, key));
35+
const targetPosition = pickBy(targetEffect.transform?.position || {}, (sr, key) => has(applyFrame.position, key));
36+
const effectWithDuration = {
37+
...pickBy(targetEffect.transform || {}, (source, key) => has(applyFrame, key)),
38+
duration: 0,
39+
ease,
40+
};
41+
effectWithDuration.scale = targetScale;
42+
effectWithDuration.position = targetPosition;
4043
animationObj.unshift(effectWithDuration);
4144
}
4245
} else {

packages/webgal/src/Core/gameScripts/setAnimation.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ export const setAnimation = (sentence: ISentence): IPerform => {
2020
target = target !== '' ? target : 'default_id';
2121
const writeDefault = getBooleanArgByKey(sentence, 'writeDefault') ?? false;
2222
const keep = getBooleanArgByKey(sentence, 'keep') ?? false;
23+
const parallel = getBooleanArgByKey(sentence, 'parallel') ?? false;
2324

2425
const key = `${target}-${animationName}-${animationDuration}`;
2526
const performInitName = `animation-${target}`;
2627

27-
WebGAL.gameplay.performController.unmountPerform(performInitName, true);
28+
if (!parallel) WebGAL.gameplay.performController.unmountPerform(performInitName, true);
2829

2930
let stopFunction;
3031
setTimeout(() => {
@@ -56,5 +57,6 @@ export const setAnimation = (sentence: ISentence): IPerform => {
5657
blockingNext: () => false,
5758
blockingAuto: () => !keep,
5859
stopTimeout: undefined, // 暂时不用,后面会交给自动清除
60+
isParallel: parallel,
5961
};
6062
};

packages/webgal/src/Core/gameScripts/setTempAnimation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { generateTimelineObj } from '@/Core/controller/stage/pixi/animations/tim
88
import cloneDeep from 'lodash/cloneDeep';
99
import { baseTransform } from '@/store/stageInterface';
1010
import { IUserAnimation } from '../Modules/animations';
11-
import { getAnimateDurationFromObj, getAnimationObject } from '@/Core/Modules/animationFunctions';
11+
import { getAnimateDuration, getAnimationObject } from '@/Core/Modules/animationFunctions';
1212
import { WebGAL } from '@/Core/WebGAL';
1313
import { v4 as uuid } from 'uuid';
1414

@@ -28,7 +28,7 @@ export const setTempAnimation = (sentence: ISentence): IPerform => {
2828
}
2929
const newAnimation: IUserAnimation = { name: animationName, effects: animationObj };
3030
WebGAL.animationManager.addAnimation(newAnimation);
31-
const animationDuration = getAnimateDurationFromObj(newAnimation);
31+
const animationDuration = getAnimateDuration(animationName);
3232
const target = getStringArgByKey(sentence, 'target') ?? '0';
3333
const writeDefault = getBooleanArgByKey(sentence, 'writeDefault') ?? false;
3434
const keep = getBooleanArgByKey(sentence, 'keep') ?? false;

packages/webgal/src/Core/gameScripts/setTransform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { baseTransform, ITransform } from '@/store/stageInterface';
1010
import { AnimationFrame, IUserAnimation } from '../Modules/animations';
1111
import { generateTransformAnimationObj } from '@/Core/controller/stage/pixi/animations/generateTransformAnimationObj';
1212
import { WebGAL } from '@/Core/WebGAL';
13-
import { getAnimateDurationFromObj, getAnimationObject } from '../Modules/animationFunctions';
13+
import { getAnimateDuration, getAnimationObject } from '../Modules/animationFunctions';
1414
import { v4 as uuid } from 'uuid';
1515
/**
1616
* 设置变换
@@ -45,7 +45,7 @@ export const setTransform = (sentence: ISentence): IPerform => {
4545

4646
const newAnimation: IUserAnimation = { name: animationName, effects: animationObj };
4747
WebGAL.animationManager.addAnimation(newAnimation);
48-
const animationDuration = getAnimateDurationFromObj(newAnimation);
48+
const animationDuration = getAnimateDuration(animationName);
4949
const key = `${target}-${animationName}-${animationDuration}`;
5050
let keepAnimationStopped = false;
5151
setTimeout(() => {

packages/webgal/src/Stage/MainStage/useSetEffects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ function convertTransform(transform: ITransform | undefined) {
4343
return {};
4444
}
4545
const { position, ...rest } = transform;
46-
return omitBy({ ...rest, x: position?.x, y: position?.y },isUndefined);
46+
return omitBy({ ...rest, x: position?.x, y: position?.y }, isUndefined);
4747
}

0 commit comments

Comments
 (0)