From 9a6bf24731da09eaffc8120ad0c9ec752c04e8e9 Mon Sep 17 00:00:00 2001 From: Rafael Date: Fri, 26 Apr 2024 09:01:01 -0300 Subject: [PATCH 1/3] internet permission --- example/android/app/src/main/AndroidManifest.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 19b862ec8..b8ed388b9 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -1,4 +1,7 @@ + + + Date: Tue, 30 Apr 2024 14:13:42 -0300 Subject: [PATCH 2/3] adds param centerAnchor --- CHANGELOG.md | 3 + example/pubspec.lock | 2 +- lib/mixins/direction_animation.dart | 18 +- lib/player/platform_player.dart | 43 ++- .../platform_animations.dart | 2 + .../render_transform_warpper.dart | 89 +++++ .../simple_direction_animation.dart | 320 ++++++++++-------- pubspec.yaml | 2 +- 8 files changed, 302 insertions(+), 177 deletions(-) create mode 100644 lib/util/direction_animations/render_transform_warpper.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cb56cac7..227604b5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 3.8.1 +- Adds param `centerAnchor` in `SimpleDirectionAnimation` and `PlatformAnimations`. It's useful to correct spritesheet not centered. + # 3.8.0 - Adds `DamageHitbox`. Use it to do damage. - `GameMap` Improvements. Now you can access the layers diff --git a/example/pubspec.lock b/example/pubspec.lock index a317cea51..a043cbb4e 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -47,7 +47,7 @@ packages: path: ".." relative: true source: path - version: "3.7.1" + version: "3.8.0" boolean_selector: dependency: transitive description: diff --git a/lib/mixins/direction_animation.dart b/lib/mixins/direction_animation.dart index 46b864a66..290245c56 100644 --- a/lib/mixins/direction_animation.dart +++ b/lib/mixins/direction_animation.dart @@ -105,8 +105,7 @@ mixin DirectionAnimation on Movement { } void onPlayRunDownAnimation() { - if (animation?.runDown != null || - (animation?.runUp != null && animation?.enabledFlipY == true)) { + if (animation?.canRunDown == true) { animation?.play(SimpleAnimationEnum.runDown); } else { if (lastDirectionHorizontal == Direction.left) { @@ -118,7 +117,7 @@ mixin DirectionAnimation on Movement { } void onPlayRunUpAnimation() { - if (animation?.runUp != null) { + if (animation?.canRunUp == true) { animation?.play(SimpleAnimationEnum.runUp); } else { if (lastDirectionHorizontal == Direction.left) { @@ -130,7 +129,7 @@ mixin DirectionAnimation on Movement { } void onPlayRunUpLeftAnimation() { - if (animation?.runUpLeft != null) { + if (animation?.canRunUpLeft == true) { animation?.play(SimpleAnimationEnum.runUpLeft); } else { animation?.play(SimpleAnimationEnum.runLeft); @@ -138,7 +137,7 @@ mixin DirectionAnimation on Movement { } void onPlayRunUpRightAnimation() { - if (animation?.runUpRight != null) { + if (animation?.canRunUpRight == true) { animation?.play(SimpleAnimationEnum.runUpRight); } else { animation?.play(SimpleAnimationEnum.runRight); @@ -146,7 +145,7 @@ mixin DirectionAnimation on Movement { } void onPlayRunDownLeftAnimation() { - if (animation?.runDownLeft != null) { + if (animation?.canRunDownLeft == true) { animation?.play(SimpleAnimationEnum.runDownLeft); } else { animation?.play(SimpleAnimationEnum.runLeft); @@ -154,7 +153,7 @@ mixin DirectionAnimation on Movement { } void onPlayRunDownRightAnimation() { - if (animation?.runDownRight != null) { + if (animation?.canRunDownRight == true) { animation?.play(SimpleAnimationEnum.runDownRight); } else { animation?.play(SimpleAnimationEnum.runRight); @@ -170,7 +169,7 @@ mixin DirectionAnimation on Movement { } void onPlayIdleUpAnimation() { - if (animation?.idleUp != null) { + if (animation?.canIdleUp == true) { animation?.play(SimpleAnimationEnum.idleUp); } else { if (lastDirectionHorizontal == Direction.left) { @@ -182,8 +181,7 @@ mixin DirectionAnimation on Movement { } void onPlayIdleDownAnimation() { - if (animation?.idleDown != null || - (animation?.idleUp != null && animation?.enabledFlipY == true)) { + if (animation?.canIdleDown == true) { animation?.play(SimpleAnimationEnum.idleDown); } else { if (lastDirectionHorizontal == Direction.left) { diff --git a/lib/player/platform_player.dart b/lib/player/platform_player.dart index c3bbbdb48..5a011d588 100644 --- a/lib/player/platform_player.dart +++ b/lib/player/platform_player.dart @@ -5,7 +5,7 @@ class PlatformPlayer extends SimplePlayer PlatformPlayer({ required super.position, required super.size, - required PlatformAnimations animation, + PlatformAnimations? animation, Direction initDirection = Direction.right, double? speed, double life = 100, @@ -14,23 +14,30 @@ class PlatformPlayer extends SimplePlayer initDirection: initDirection, speed: speed, life: life, - animation: SimpleDirectionAnimation( - idleRight: animation.idleRight, - runRight: animation.runRight, - idleLeft: animation.idleLeft, - runLeft: animation.runLeft, - others: { - if (animation.jump?.jumpUpRight != null) - JumpAnimationsEnum.jumpUpRight: animation.jump!.jumpUpRight, - if (animation.jump?.jumpUpLeft != null) - JumpAnimationsEnum.jumpUpLeft: animation.jump!.jumpUpLeft!, - if (animation.jump?.jumpDownRight != null) - JumpAnimationsEnum.jumpDownRight: animation.jump!.jumpDownRight, - if (animation.jump?.jumpDownLeft != null) - JumpAnimationsEnum.jumpDownLeft: animation.jump!.jumpDownLeft!, - ...animation.others ?? {}, - }, - ), + animation: animation != null + ? SimpleDirectionAnimation( + idleRight: animation.idleRight, + runRight: animation.runRight, + idleLeft: animation.idleLeft, + runLeft: animation.runLeft, + centerAnchor: animation.centerAnchor, + others: { + if (animation.jump?.jumpUpRight != null) + JumpAnimationsEnum.jumpUpRight: + animation.jump!.jumpUpRight, + if (animation.jump?.jumpUpLeft != null) + JumpAnimationsEnum.jumpUpLeft: + animation.jump!.jumpUpLeft!, + if (animation.jump?.jumpDownRight != null) + JumpAnimationsEnum.jumpDownRight: + animation.jump!.jumpDownRight, + if (animation.jump?.jumpDownLeft != null) + JumpAnimationsEnum.jumpDownLeft: + animation.jump!.jumpDownLeft!, + ...animation.others ?? {}, + }, + ) + : null, ) { setupJumper(maxJump: countJumps); } diff --git a/lib/util/direction_animations/platform_animations.dart b/lib/util/direction_animations/platform_animations.dart index df7b63ae6..2d3b8a1b8 100644 --- a/lib/util/direction_animations/platform_animations.dart +++ b/lib/util/direction_animations/platform_animations.dart @@ -23,6 +23,7 @@ class PlatformAnimations { final FutureOr? runLeft; final PlatformJumpAnimations? jump; final Map>? others; + final Vector2? centerAnchor; PlatformAnimations({ required this.idleRight, @@ -31,5 +32,6 @@ class PlatformAnimations { this.runLeft, this.jump, this.others, + this.centerAnchor, }); } diff --git a/lib/util/direction_animations/render_transform_warpper.dart b/lib/util/direction_animations/render_transform_warpper.dart new file mode 100644 index 000000000..7dbfc9206 --- /dev/null +++ b/lib/util/direction_animations/render_transform_warpper.dart @@ -0,0 +1,89 @@ +import 'dart:ui'; + +import 'package:bonfire/bonfire.dart'; + +class RenderTransformWrapper { + final List transforms; + final void Function(Canvas canvas, Paint paint) render; + + RenderTransformWrapper({required this.transforms, required this.render}); + + void execRender(Canvas canvas, Paint paint) { + int index = 0; + for (final transform in transforms) { + if (transform.transform(canvas)) { + index++; + } + } + render(canvas, paint); + for (int i = 0; i < index; i++) { + canvas.restore(); + } + } +} + +abstract class RenderTransformer { + bool transform(Canvas canvas); +} + +class CenterAdjustRenderTransform extends RenderTransformer { + final CenterAdjustRenderData? Function() onTransform; + + CenterAdjustRenderTransform(this.onTransform); + + @override + bool transform(Canvas canvas) { + final data = onTransform(); + if (data == null) { + return false; + } + canvas.save(); + Vector2 diff = data.center - data.newCenter; + canvas.translate(diff.x, diff.y); + return true; + } +} + +class CenterAdjustRenderData { + final Vector2 center; + final Vector2 newCenter; + + CenterAdjustRenderData({ + required this.center, + required this.newCenter, + }); +} + +class FlipRenderTransform extends RenderTransformer { + final FlipRenderTransformData? Function() onTransform; + + FlipRenderTransform(this.onTransform); + + @override + bool transform(Canvas canvas) { + final data = onTransform(); + if (data == null) { + return false; + } + canvas.save(); + canvas.translate(data.center.x, data.center.y); + canvas.scale( + data.horizontal ? -1 : 1, + data.vertical ? -1 : 1, + ); + canvas.translate(-data.center.x, -data.center.y); + return true; + } +} + +class FlipRenderTransformData { + final Vector2 center; + final bool horizontal; + final bool vertical; + + FlipRenderTransformData({ + required this.center, + required this.horizontal, + required this.vertical, + }); +} diff --git a/lib/util/direction_animations/simple_direction_animation.dart b/lib/util/direction_animations/simple_direction_animation.dart index 46c3020a5..70c031cec 100644 --- a/lib/util/direction_animations/simple_direction_animation.dart +++ b/lib/util/direction_animations/simple_direction_animation.dart @@ -2,27 +2,29 @@ import 'dart:async'; import 'dart:ui'; import 'package:bonfire/bonfire.dart'; +import 'package:bonfire/util/direction_animations/render_transform_warpper.dart'; import 'package:bonfire/util/sprite_animation_render.dart'; /// Class responsible to manager animation on `SimplePlayer` and `SimpleEnemy` class SimpleDirectionAnimation { - SpriteAnimation? idleLeft; - SpriteAnimation? idleRight; - SpriteAnimation? runLeft; - SpriteAnimation? runRight; - - SpriteAnimation? idleUp; - SpriteAnimation? idleDown; - SpriteAnimation? idleUpLeft; - SpriteAnimation? idleUpRight; - SpriteAnimation? idleDownLeft; - SpriteAnimation? idleDownRight; - SpriteAnimation? runUp; - SpriteAnimation? runDown; - SpriteAnimation? runUpLeft; - SpriteAnimation? runUpRight; - SpriteAnimation? runDownLeft; - SpriteAnimation? runDownRight; + SpriteAnimation? _idleLeftAnim; + SpriteAnimation? _idleRightAnim; + SpriteAnimation? _runLeftAnim; + SpriteAnimation? _runRightAnim; + + SpriteAnimation? _idleUpAnim; + SpriteAnimation? _idleDownAnim; + SpriteAnimation? _idleUpLeftAnim; + SpriteAnimation? _idleUpRightAnim; + SpriteAnimation? _idleDownLeftAnim; + SpriteAnimation? _idleDownRightAnim; + SpriteAnimation? _runUpAnim; + SpriteAnimation? _runDownAnim; + SpriteAnimation? _runUpLeftAnim; + SpriteAnimation? _runUpRightAnim; + SpriteAnimation? _runDownLeftAnim; + SpriteAnimation? _runDownRightAnim; + Vector2? centerAnchor; Map others = {}; @@ -63,6 +65,17 @@ class SimpleDirectionAnimation { bool get _needDoFlipFastAnimation => _isFlipHorizontallyFastAnimation || _isFlipVerticallyFastAnimation; + bool get canRunDown => + _runDownAnim != null || (_runUpAnim != null && enabledFlipY == true); + bool get canIdleDown => + _idleDownAnim != null || (_idleDownAnim != null && enabledFlipY == true); + bool get canRunUp => _runUpAnim != null; + bool get canRunUpLeft => _runUpLeftAnim != null; + bool get canRunUpRight => _runUpRightAnim != null; + bool get canRunDownLeft => _runDownLeftAnim != null; + bool get canRunDownRight => _runDownRightAnim != null; + bool get canIdleUp => _idleUpAnim != null; + SimpleDirectionAnimation({ required FutureOr idleRight, required FutureOr runRight, @@ -84,28 +97,29 @@ class SimpleDirectionAnimation { this.enabledFlipX = true, this.enabledFlipY = false, this.eightDirection = false, + this.centerAnchor, }) { - _loader?.add(AssetToLoad(idleLeft, (value) => this.idleLeft = value)); - _loader?.add(AssetToLoad(idleRight, (value) => this.idleRight = value)); - _loader?.add(AssetToLoad(idleDown, (value) => this.idleDown = value)); - _loader?.add(AssetToLoad(idleUp, (value) => this.idleUp = value)); - _loader?.add(AssetToLoad(idleUpLeft, (value) => this.idleUpLeft = value)); - _loader?.add(AssetToLoad(idleUpRight, (value) => this.idleUpRight = value)); + _loader?.add(AssetToLoad(idleLeft, (value) => _idleLeftAnim = value)); + _loader?.add(AssetToLoad(idleRight, (value) => _idleRightAnim = value)); + _loader?.add(AssetToLoad(idleDown, (value) => _idleDownAnim = value)); + _loader?.add(AssetToLoad(idleUp, (value) => _idleUpAnim = value)); + _loader?.add(AssetToLoad(idleUpLeft, (value) => _idleUpLeftAnim = value)); + _loader?.add(AssetToLoad(idleUpRight, (value) => _idleUpRightAnim = value)); _loader?.add( - AssetToLoad(idleDownLeft, (value) => this.idleDownLeft = value), + AssetToLoad(idleDownLeft, (value) => _idleDownLeftAnim = value), ); _loader?.add( - AssetToLoad(idleDownRight, (value) => this.idleDownRight = value), + AssetToLoad(idleDownRight, (value) => _idleDownRightAnim = value), ); - _loader?.add(AssetToLoad(runUp, (value) => this.runUp = value)); - _loader?.add(AssetToLoad(runRight, (value) => this.runRight = value)); - _loader?.add(AssetToLoad(runDown, (value) => this.runDown = value)); - _loader?.add(AssetToLoad(runLeft, (value) => this.runLeft = value)); - _loader?.add(AssetToLoad(runUpLeft, (value) => this.runUpLeft = value)); - _loader?.add(AssetToLoad(runUpRight, (value) => this.runUpRight = value)); - _loader?.add(AssetToLoad(runDownLeft, (value) => this.runDownLeft = value)); + _loader?.add(AssetToLoad(runUp, (value) => _runUpAnim = value)); + _loader?.add(AssetToLoad(runRight, (value) => _runRightAnim = value)); + _loader?.add(AssetToLoad(runDown, (value) => _runDownAnim = value)); + _loader?.add(AssetToLoad(runLeft, (value) => _runLeftAnim = value)); + _loader?.add(AssetToLoad(runUpLeft, (value) => _runUpLeftAnim = value)); + _loader?.add(AssetToLoad(runUpRight, (value) => _runUpRightAnim = value)); + _loader?.add(AssetToLoad(runDownLeft, (value) => _runDownLeftAnim = value)); _loader?.add( - AssetToLoad(runDownRight, (value) => this.runDownRight = value), + AssetToLoad(runDownRight, (value) => _runDownRightAnim = value), ); others?.forEach((key, anim) { @@ -113,8 +127,50 @@ class SimpleDirectionAnimation { return this.others[key] = value; })); }); + + _renderWrapper = RenderTransformWrapper( + transforms: [ + FlipRenderTransform( + _flipRenderTransform, + ), + CenterAdjustRenderTransform( + _adjustRenderTransform, + ) + ], + render: _myRender, + ); + } + + CenterAdjustRenderData? _adjustRenderTransform() { + if (centerAnchor != null) { + return CenterAdjustRenderData( + center: (size / 2), + newCenter: centerAnchor!, + ); + } + return null; + } + + FlipRenderTransformData? _flipRenderTransform() { + if (_needDoFlip) { + return FlipRenderTransformData( + center: (size / 2), + horizontal: isFlipHorizontally, + vertical: isFlipVertically, + ); + } + if (_fastAnimation != null && _needDoFlipFastAnimation) { + return FlipRenderTransformData( + center: (size / 2), + horizontal: _isFlipHorizontallyFastAnimation, + vertical: _isFlipVerticallyFastAnimation, + ); + } + return null; } + late RenderTransformWrapper _renderWrapper; + /// Method used to play specific default animation void play(SimpleAnimationEnum animation) { if (_currentType == animation) return; @@ -132,51 +188,51 @@ class SimpleDirectionAnimation { _idleLeft(); break; case SimpleAnimationEnum.idleRight: - _current.animation = idleRight; + _current.animation = _idleRightAnim; break; case SimpleAnimationEnum.idleUp: - if (idleUp != null) _current.animation = idleUp; + if (_idleUpAnim != null) _current.animation = _idleUpAnim; break; case SimpleAnimationEnum.idleDown: - if (idleDown != null) { - _current.animation = idleDown; - } else if (enabledFlipY && idleUp != null) { + if (_idleDownAnim != null) { + _current.animation = _idleDownAnim; + } else if (enabledFlipY && _idleUpAnim != null) { isFlipVertically = true; - _current.animation = idleUp; + _current.animation = _idleUpAnim; } break; case SimpleAnimationEnum.idleUpLeft: - if (idleUpLeft != null) { - _current.animation = idleUpLeft; - } else if (idleUpRight != null) { - _current.animation = idleUpRight; + if (_idleUpLeftAnim != null) { + _current.animation = _idleUpLeftAnim; + } else if (_idleUpRightAnim != null) { + _current.animation = _idleUpRightAnim; isFlipHorizontally = true; } else { _idleLeft(); } break; case SimpleAnimationEnum.idleUpRight: - if (idleUpRight != null) { - _current.animation = idleUpRight; + if (_idleUpRightAnim != null) { + _current.animation = _idleUpRightAnim; } else { - _current.animation = idleRight; + _current.animation = _idleRightAnim; } break; case SimpleAnimationEnum.idleDownLeft: - if (idleDownLeft != null) { - _current.animation = idleDownLeft; - } else if (idleDownRight != null) { - _current.animation = idleDownRight; + if (_idleDownLeftAnim != null) { + _current.animation = _idleDownLeftAnim; + } else if (_idleDownRightAnim != null) { + _current.animation = _idleDownRightAnim; isFlipHorizontally = true; } else { _idleLeft(); } break; case SimpleAnimationEnum.idleDownRight: - if (idleDownRight != null) { - _current.animation = idleDownRight; + if (_idleDownRightAnim != null) { + _current.animation = _idleDownRightAnim; } else { - _current.animation = idleRight; + _current.animation = _idleRightAnim; } break; case SimpleAnimationEnum.runUp: @@ -184,19 +240,19 @@ class SimpleDirectionAnimation { if (lastPlayedAnimation == SimpleAnimationEnum.runRight || lastPlayedAnimation == SimpleAnimationEnum.runLeft) { if (beforeLastPlayedAnimation == SimpleAnimationEnum.runUpRight) { - _current.animation = runUpRight; + _current.animation = _runUpRightAnim; } else if (beforeLastPlayedAnimation == SimpleAnimationEnum.runUpLeft) { - _current.animation = runUpLeft; - } else if (runUp != null) { - _current.animation = runUp; + _current.animation = _runUpLeftAnim; + } else if (_runUpAnim != null) { + _current.animation = _runUpAnim; } - } else if (runUp != null) { - _current.animation = runUp; + } else if (_runUpAnim != null) { + _current.animation = _runUpAnim; } _changeLastAnimation(SimpleAnimationEnum.runUp); - } else if (runUp != null) { - _current.animation = runUp; + } else if (_runUpAnim != null) { + _current.animation = _runUpAnim; } break; case SimpleAnimationEnum.runRight: @@ -207,33 +263,33 @@ class SimpleDirectionAnimation { if (lastPlayedAnimation == SimpleAnimationEnum.runRight || lastPlayedAnimation == SimpleAnimationEnum.runLeft) { if (beforeLastPlayedAnimation == SimpleAnimationEnum.runDownRight) { - _current.animation = runDownRight; + _current.animation = _runDownRightAnim; } else if (beforeLastPlayedAnimation == SimpleAnimationEnum.runDownLeft) { - _current.animation = runDownLeft; + _current.animation = _runDownLeftAnim; } else { - if (runDown != null) { - _current.animation = runDown; - } else if (enabledFlipY && runUp != null) { + if (_runDownAnim != null) { + _current.animation = _runDownAnim; + } else if (enabledFlipY && _runUpAnim != null) { isFlipVertically = true; - _current.animation = runUp; + _current.animation = _runUpAnim; } } } else { - if (runDown != null) { - _current.animation = runDown; - } else if (enabledFlipY && runUp != null) { + if (_runDownAnim != null) { + _current.animation = _runDownAnim; + } else if (enabledFlipY && _runUpAnim != null) { isFlipVertically = true; - _current.animation = runUp; + _current.animation = _runUpAnim; } } _changeLastAnimation(SimpleAnimationEnum.runDown); } else { - if (runDown != null) { - _current.animation = runDown; - } else if (enabledFlipY && runUp != null) { + if (_runDownAnim != null) { + _current.animation = _runDownAnim; + } else if (enabledFlipY && _runUpAnim != null) { isFlipVertically = true; - _current.animation = runUp; + _current.animation = _runUpAnim; } } break; @@ -241,11 +297,11 @@ class SimpleDirectionAnimation { _runLeft(); break; case SimpleAnimationEnum.runUpLeft: - if (runUpLeft != null) { - _current.animation = runUpLeft; + if (_runUpLeftAnim != null) { + _current.animation = _runUpLeftAnim; _changeLastAnimation(SimpleAnimationEnum.runUpLeft); - } else if (runUpRight != null) { - _current.animation = runUpRight; + } else if (_runUpRightAnim != null) { + _current.animation = _runUpRightAnim; isFlipHorizontally = true; _changeLastAnimation(SimpleAnimationEnum.runUpLeft); } else { @@ -253,19 +309,19 @@ class SimpleDirectionAnimation { } break; case SimpleAnimationEnum.runUpRight: - if (runUpRight != null) { - _current.animation = runUpRight; + if (_runUpRightAnim != null) { + _current.animation = _runUpRightAnim; _changeLastAnimation(SimpleAnimationEnum.runUpRight); } else { _runRight(); } break; case SimpleAnimationEnum.runDownLeft: - if (runDownLeft != null) { - _current.animation = runDownLeft; + if (_runDownLeftAnim != null) { + _current.animation = _runDownLeftAnim; _changeLastAnimation(SimpleAnimationEnum.runDownLeft); - } else if (runDownRight != null) { - _current.animation = runDownRight; + } else if (_runDownRightAnim != null) { + _current.animation = _runDownRightAnim; isFlipHorizontally = true; _changeLastAnimation(SimpleAnimationEnum.runDownLeft); } else { @@ -273,8 +329,8 @@ class SimpleDirectionAnimation { } break; case SimpleAnimationEnum.runDownRight: - if (runDownRight != null) { - _current.animation = runDownRight; + if (_runDownRightAnim != null) { + _current.animation = _runDownRightAnim; _changeLastAnimation(SimpleAnimationEnum.runDownRight); } else { _runRight(); @@ -387,14 +443,6 @@ class SimpleDirectionAnimation { others[key] = await animation; } - void render(Canvas canvas, Paint paint) { - if (_fastAnimation != null) { - _renderFastAnimation(canvas, paint); - } else { - _renderCurrentAnimation(canvas, paint); - } - } - void update( double dt, Vector2 size, @@ -435,32 +483,32 @@ class SimpleDirectionAnimation { if (lastPlayedAnimation == SimpleAnimationEnum.runUpLeft || lastPlayedAnimation == SimpleAnimationEnum.runDownLeft) { if (beforeLastPlayedAnimation == SimpleAnimationEnum.runDown) { - _current.animation = runDownLeft; + _current.animation = _runDownLeftAnim; } else if (beforeLastPlayedAnimation == SimpleAnimationEnum.runUp) { - _current.animation = runUpLeft; + _current.animation = _runUpLeftAnim; } else { - if (runLeft != null) { - _current.animation = runLeft; + if (_runLeftAnim != null) { + _current.animation = _runLeftAnim; } else if (enabledFlipX) { isFlipHorizontally = true; - _current.animation = runRight; + _current.animation = _runRightAnim; } } } else { - if (runLeft != null) { - _current.animation = runLeft; + if (_runLeftAnim != null) { + _current.animation = _runLeftAnim; } else if (enabledFlipX) { isFlipHorizontally = true; - _current.animation = runRight; + _current.animation = _runRightAnim; } } _changeLastAnimation(SimpleAnimationEnum.runLeft); } else { - if (runLeft != null) { - _current.animation = runLeft; + if (_runLeftAnim != null) { + _current.animation = _runLeftAnim; } else if (enabledFlipX) { isFlipHorizontally = true; - _current.animation = runRight; + _current.animation = _runRightAnim; } } } @@ -470,27 +518,27 @@ class SimpleDirectionAnimation { if (lastPlayedAnimation == SimpleAnimationEnum.runUpRight || lastPlayedAnimation == SimpleAnimationEnum.runDownRight) { if (beforeLastPlayedAnimation == SimpleAnimationEnum.runDown) { - _current.animation = runDownRight; + _current.animation = _runDownRightAnim; } else if (beforeLastPlayedAnimation == SimpleAnimationEnum.runUp) { - _current.animation = runUpRight; + _current.animation = _runUpRightAnim; } else { - _current.animation = runRight; + _current.animation = _runRightAnim; } } else { - _current.animation = runRight; + _current.animation = _runRightAnim; } _changeLastAnimation(SimpleAnimationEnum.runRight); } else { - _current.animation = runRight; + _current.animation = _runRightAnim; } } void _idleLeft() { - if (idleLeft != null) { - _current.animation = idleLeft; + if (_idleLeftAnim != null) { + _current.animation = _idleLeftAnim; } else if (enabledFlipX) { isFlipHorizontally = true; - _current.animation = idleRight; + _current.animation = _idleRightAnim; } } @@ -518,15 +566,19 @@ class SimpleDirectionAnimation { _strockePaint = null; } - void _renderCurrentAnimation(Canvas canvas, Paint paint) { - if (_needDoFlip) { - _applyFlip( - canvas, - isFlipHorizontally, - isFlipVertically, - ); + void render(Canvas canvas, Paint paint) { + _renderWrapper.execRender(canvas, paint); + } + + void _myRender(Canvas canvas, Paint paint) { + if (_fastAnimation != null) { + _renderFastAnimation(canvas, paint); + } else { + _renderCurrentAnimation(canvas, paint); } + } + void _renderCurrentAnimation(Canvas canvas, Paint paint) { if (_strockePaint != null) { _current.render( canvas, @@ -540,20 +592,9 @@ class SimpleDirectionAnimation { 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, @@ -567,20 +608,5 @@ class SimpleDirectionAnimation { 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); } } diff --git a/pubspec.yaml b/pubspec.yaml index 3e9811106..b7df28c3a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: bonfire description: (RPG maker) Create RPG-style or similar games more simply with Flame. -version: 3.8.0 +version: 3.8.1 homepage: https://bonfire-engine.github.io repository: https://github.com/RafaelBarbosatec/bonfire issue_tracker: https://github.com/RafaelBarbosatec/bonfire/issues From a613830a582a5ce9c859e3d590a6e9ba8667d8d0 Mon Sep 17 00:00:00 2001 From: Rafael Almeida Barbosa Date: Tue, 30 Apr 2024 14:24:33 -0300 Subject: [PATCH 3/3] format --- lib/util/direction_animations/simple_direction_animation.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/util/direction_animations/simple_direction_animation.dart b/lib/util/direction_animations/simple_direction_animation.dart index 70c031cec..67e9c6d60 100644 --- a/lib/util/direction_animations/simple_direction_animation.dart +++ b/lib/util/direction_animations/simple_direction_animation.dart @@ -76,6 +76,8 @@ class SimpleDirectionAnimation { bool get canRunDownRight => _runDownRightAnim != null; bool get canIdleUp => _idleUpAnim != null; + late RenderTransformWrapper _renderWrapper; + SimpleDirectionAnimation({ required FutureOr idleRight, required FutureOr runRight, @@ -169,8 +171,6 @@ class SimpleDirectionAnimation { return null; } - late RenderTransformWrapper _renderWrapper; - /// Method used to play specific default animation void play(SimpleAnimationEnum animation) { if (_currentType == animation) return;