diff --git a/Extensions/TweenBehavior/TweenManager.ts b/Extensions/TweenBehavior/TweenManager.ts index 323341d3adc4..5eaf2c463246 100644 --- a/Extensions/TweenBehavior/TweenManager.ts +++ b/Extensions/TweenBehavior/TweenManager.ts @@ -17,6 +17,50 @@ namespace gdjs { constructor() {} + getNetworkSyncData(): TweenManagerSyncData { + const tweens: TweenManagerSyncData = {}; + for (const [identifier, tween] of this._tweens) { + tweens[identifier] = { + progress: tween.getProgress(), + value: tween.getValue(), + isPlaying: tween.isPlaying(), + hasFinished: tween.hasFinished(), + }; + } + return tweens; + } + + // We only handle tweens partially for network sync. + // We only sync the props of existing tweens, but do not handle + // adding or removing tweens. + updateFromNetworkSyncData(networkSyncData: TweenManagerSyncData) { + for (const [identifier, tweenData] of Object.entries( + networkSyncData + )) { + const tween = this._tweens.get(identifier); + if (!tween) { + continue; + } + if ( + tweenData.isPlaying !== undefined && + tweenData.isPlaying !== tween.isPlaying() + ) { + tweenData.isPlaying + ? this.resumeTween(identifier) + : this.pauseTween(identifier); + } + if ( + tweenData.hasFinished === true && + tweenData.hasFinished !== tween.hasFinished() + ) { + this.stopTween(identifier, false); + } + if (tweenData.progress !== undefined) { + tween.setProgress(tweenData.progress); + } + } + } + /** * Make all active tween step toward the end. * @param timeDelta the duration from the previous step in seconds @@ -267,6 +311,7 @@ namespace gdjs { stop(jumpToDest: boolean): void; resume(): void; pause(): void; + setProgress(progress: float): void; getProgress(): float; getValue(): float; } @@ -339,6 +384,11 @@ namespace gdjs { getProgress(): float { return this.elapsedTime / this.totalDuration; } + + setProgress(progress: float): void { + this.elapsedTime = progress * this.totalDuration; + this._updateValue(); + } } /** diff --git a/Extensions/TweenBehavior/tweenruntimebehavior.ts b/Extensions/TweenBehavior/tweenruntimebehavior.ts index eb844e0c930e..f30e04cb2a20 100644 --- a/Extensions/TweenBehavior/tweenruntimebehavior.ts +++ b/Extensions/TweenBehavior/tweenruntimebehavior.ts @@ -4,6 +4,23 @@ Copyright (c) 2010-2023 Florian Rival (Florian.Rival@gmail.com) */ namespace gdjs { const logger = new gdjs.Logger('Tween'); + + interface TweenData { + progress: number; + value: number; + isPlaying: boolean; + hasFinished: boolean; + } + export type TweenManagerSyncData = Record; + + interface TweenNetworkSyncDataType { + tweens: TweenManagerSyncData; + } + + export interface TweenNetworkSyncData extends BehaviorNetworkSyncData { + props: TweenNetworkSyncDataType; + } + interface IColorable extends gdjs.RuntimeObject { setColor(color: string): void; getColor(): string; @@ -84,6 +101,21 @@ namespace gdjs { return true; } + getNetworkSyncData(): TweenNetworkSyncData { + return { + ...super.getNetworkSyncData(), + props: { + tweens: this._tweens.getNetworkSyncData(), + }, + }; + } + + updateFromNetworkSyncData(networkSyncData: TweenNetworkSyncData) { + super.updateFromNetworkSyncData(networkSyncData); + + this._tweens.updateFromNetworkSyncData(networkSyncData.props.tweens); + } + doStepPreEvents(instanceContainer: gdjs.RuntimeInstanceContainer): void { this._tweens.step(); }