Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelBarbosatec committed Dec 4, 2023
1 parent 87ca8fd commit 21635d8
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 61 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# next
- Fix flip render problem in `SimpleDirectionAnimation`.

## 3.1.0
- [BREAKING CHANGE] `BlockMovementCollision` big improvements.
- Update `onBlockedMovement` method, adds `CollisionData(normal,depth,direction,intersectionPoints)`
Expand Down
151 changes: 90 additions & 61 deletions lib/util/direction_animations/simple_direction_animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ class SimpleDirectionAnimation {
Vector2 _strokeSize = Vector2.zero();
Vector2 _strokePosition = Vector2.zero();
Vector2? spriteAnimationOffset;
bool _lastFlipX = false;
bool _lastFlipY = false;

bool _isFlipHorizontallyFastAnimation = false;
bool _isFlipVerticallyFastAnimation = false;

bool get _needDoFlip => isFlipHorizontally || isFlipVertically;
bool get _needDoFlipFastAnimation =>
_isFlipHorizontallyFastAnimation || _isFlipVerticallyFastAnimation;

SimpleDirectionAnimation({
required FutureOr<SpriteAnimation> idleRight,
Expand Down Expand Up @@ -316,8 +321,13 @@ class SimpleDirectionAnimation {
}) async {
_fastAnimation?.onFinish?.call();
runToTheEndFastAnimation = runToTheEnd;
_lastFlipX = isFlipHorizontally;
_lastFlipY = isFlipVertically;
if (useCompFlip) {
_isFlipHorizontallyFastAnimation = isFlipHorizontally;
_isFlipVerticallyFastAnimation = isFlipVertically;
} else {
_isFlipHorizontallyFastAnimation = flipX;
_isFlipVerticallyFastAnimation = flipY;
}
_fastAnimation = SpriteAnimationRender(
size: size ?? this.size,
position: offset,
Expand All @@ -326,16 +336,8 @@ class SimpleDirectionAnimation {
onFinish: () {
onFinish?.call();
_fastAnimation = null;
if (!useCompFlip) {
isFlipHorizontally = _lastFlipX;
isFlipVertically = _lastFlipY;
}
},
);
if (!useCompFlip) {
isFlipVertically = flipY;
isFlipHorizontally = flipX;
}
onStart?.call();
}

Expand All @@ -356,8 +358,14 @@ class SimpleDirectionAnimation {
}
_fastAnimation?.onFinish?.call();
runToTheEndFastAnimation = runToTheEnd;
_lastFlipX = isFlipHorizontally;
_lastFlipY = isFlipVertically;
if (useCompFlip) {
_isFlipHorizontallyFastAnimation = isFlipHorizontally;
_isFlipVerticallyFastAnimation = isFlipVertically;
} else {
_isFlipHorizontallyFastAnimation = flipX;
_isFlipVerticallyFastAnimation = flipY;
}

_fastAnimation = SpriteAnimationRender(
size: size ?? this.size,
position: offset,
Expand All @@ -366,16 +374,8 @@ class SimpleDirectionAnimation {
onFinish: () {
onFinish?.call();
_fastAnimation = null;
if (!useCompFlip) {
isFlipHorizontally = _lastFlipX;
isFlipVertically = _lastFlipY;
}
},
);
if (!useCompFlip) {
isFlipVertically = flipY;
isFlipHorizontally = flipX;
}
onStart?.call();
}

Expand All @@ -387,48 +387,11 @@ class SimpleDirectionAnimation {
others[key] = await animation;
}

bool get needDoFlip => isFlipHorizontally || isFlipVertically;

void render(Canvas canvas, Paint paint) {
if (needDoFlip) {
Vector2 center = (size / 2);
canvas.save();
canvas.translate(center.x, center.y);
canvas.scale(isFlipHorizontally ? -1 : 1, isFlipVertically ? -1 : 1);
canvas.translate(-center.x, -center.y);
}

if (_fastAnimation != null) {
if (_strockePaint != null) {
_fastAnimation?.render(
canvas,
overridePaint: _strockePaint!,
size: _strokeSize,
position: _strokePosition + (spriteAnimationOffset ?? Vector2.zero()),
);
}
_fastAnimation?.render(
canvas,
overridePaint: paint,
position: spriteAnimationOffset,
);
_renderFastAnimation(canvas, paint);
} else {
if (_strockePaint != null) {
_current.render(
canvas,
overridePaint: _strockePaint,
size: _strokeSize,
position: _strokePosition + (spriteAnimationOffset ?? Vector2.zero()),
);
}
_current.render(
canvas,
overridePaint: paint,
position: spriteAnimationOffset,
);
}
if (needDoFlip) {
canvas.restore();
_renderCurrentAnimation(canvas, paint);
}
}

Expand Down Expand Up @@ -554,4 +517,70 @@ class SimpleDirectionAnimation {
void hideStroke() {
_strockePaint = null;
}

void _renderCurrentAnimation(Canvas canvas, Paint paint) {
if (_needDoFlip) {
_applyFlip(
canvas,
isFlipHorizontally,
isFlipVertically,
);
}

if (_strockePaint != null) {
_current.render(
canvas,
overridePaint: _strockePaint,
size: _strokeSize,
position: _strokePosition + (spriteAnimationOffset ?? Vector2.zero()),
);
}
_current.render(
canvas,
overridePaint: paint,
position: spriteAnimationOffset,
);
if (_needDoFlip) {
canvas.restore();
}
}

void _renderFastAnimation(Canvas canvas, Paint paint) {
if (_needDoFlipFastAnimation) {
_applyFlip(
canvas,
_isFlipHorizontallyFastAnimation,
_isFlipVerticallyFastAnimation,
);
}

if (_strockePaint != null) {
_fastAnimation?.render(
canvas,
overridePaint: _strockePaint!,
size: _strokeSize,
position: _strokePosition + (spriteAnimationOffset ?? Vector2.zero()),
);
}
_fastAnimation?.render(
canvas,
overridePaint: paint,
position: spriteAnimationOffset,
);

if (_needDoFlipFastAnimation) {
canvas.restore();
}
}

void _applyFlip(Canvas canvas, bool horizontal, bool vertical) {
Vector2 center = (size / 2);
canvas.save();
canvas.translate(center.x, center.y);
canvas.scale(
horizontal ? -1 : 1,
vertical ? -1 : 1,
);
canvas.translate(-center.x, -center.y);
}
}

0 comments on commit 21635d8

Please sign in to comment.