Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip sync tweens #6793

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Extensions/TweenBehavior/TweenManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -267,6 +311,7 @@ namespace gdjs {
stop(jumpToDest: boolean): void;
resume(): void;
pause(): void;
setProgress(progress: float): void;
getProgress(): float;
getValue(): float;
}
Expand Down Expand Up @@ -339,6 +384,11 @@ namespace gdjs {
getProgress(): float {
return this.elapsedTime / this.totalDuration;
}

setProgress(progress: float): void {
this.elapsedTime = progress * this.totalDuration;
this._updateValue();
}
}

/**
Expand Down
32 changes: 32 additions & 0 deletions Extensions/TweenBehavior/tweenruntimebehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ Copyright (c) 2010-2023 Florian Rival ([email protected])
*/
namespace gdjs {
const logger = new gdjs.Logger('Tween');

interface TweenData {
progress: number;
value: number;
isPlaying: boolean;
hasFinished: boolean;
}
export type TweenManagerSyncData = Record<string, TweenData>;

interface TweenNetworkSyncDataType {
tweens: TweenManagerSyncData;
}

export interface TweenNetworkSyncData extends BehaviorNetworkSyncData {
props: TweenNetworkSyncDataType;
}

interface IColorable extends gdjs.RuntimeObject {
setColor(color: string): void;
getColor(): string;
Expand Down Expand Up @@ -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();
}
Expand Down
Loading