Skip to content

Commit

Permalink
Fixes animations being desynced (#1985)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyberboss authored Sep 30, 2024
1 parent 6b27c39 commit 3b14bf7
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions OpenDreamClient/Rendering/DreamIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ private set {
public Texture? LastRenderedTexture;

private int _animationFrame;
private TimeSpan _animationFrameTime = gameTiming.CurTime;
private List<AppearanceAnimation>? _appearanceAnimations;
private int _appearanceAnimationsLoops;
private Box2? _cachedAABB;
private bool _textureDirty = true;
private bool _animationComplete;
private IRenderTexture? _cachedTexture;

public DreamIcon(RenderTargetPool renderTargetPool, IGameTiming gameTiming, IClyde clyde, ClientAppearanceSystem appearanceSystem, int appearanceId,
Expand Down Expand Up @@ -199,7 +199,7 @@ public void GetWorldAABB(Vector2 worldPos, ref Box2? aabb) {
}

private void UpdateAnimation() {
if(DMI == null || Appearance == null)
if(DMI == null || Appearance == null || _animationComplete)
return;

DMIParser.ParsedDMIState? dmiState = DMI.Description.GetStateOrDefault(Appearance.IconState);
Expand All @@ -208,17 +208,28 @@ private void UpdateAnimation() {
DMIParser.ParsedDMIFrame[] frames = dmiState.GetFrames(Appearance.Direction);

if (frames.Length <= 1) return;
if (_animationFrame == frames.Length - 1 && !dmiState.Loop) return;

TimeSpan elapsedTime = gameTiming.CurTime.Subtract(_animationFrameTime);
while (elapsedTime >= frames[_animationFrame].Delay) {
elapsedTime -= frames[_animationFrame].Delay;
_animationFrameTime += frames[_animationFrame].Delay;
var oldFrame = _animationFrame;
var currentGameTicks = gameTiming.CurTime.Ticks;
var sequenceDuration = frames.Aggregate(TimeSpan.Zero, (duration, frame) => duration + frame.Delay);
var durationDiff = new TimeSpan(currentGameTicks % sequenceDuration.Ticks);
var noLoop = !dmiState.Loop;

_animationFrame = 0;
while (durationDiff >= frames[_animationFrame].Delay) {
durationDiff -= frames[_animationFrame].Delay;

_animationFrame++;
DirtyTexture();

if (_animationFrame >= frames.Length) _animationFrame -= frames.Length;
if (noLoop && _animationFrame == frames.Length - 1) {
_animationComplete = true;
break;
} else if (_animationFrame == frames.Length)
_animationFrame = 0;
}

if (oldFrame != _animationFrame)
DirtyTexture();
}

private IconAppearance? CalculateAnimatedAppearance() {
Expand Down Expand Up @@ -459,7 +470,7 @@ private void UpdateIcon() {
dmi.OnUpdateCallbacks.Add(DirtyTexture);
DMI = dmi;
_animationFrame = 0;
_animationFrameTime = gameTiming.CurTime;
_animationComplete = false;
});
}

Expand Down

0 comments on commit 3b14bf7

Please sign in to comment.