diff --git a/packages/trashy_road/lib/src/game/entities/player/behaviors/player_moving_behavior.dart b/packages/trashy_road/lib/src/game/entities/player/behaviors/player_moving_behavior.dart index 885949c8..6098b5cb 100644 --- a/packages/trashy_road/lib/src/game/entities/player/behaviors/player_moving_behavior.dart +++ b/packages/trashy_road/lib/src/game/entities/player/behaviors/player_moving_behavior.dart @@ -15,8 +15,15 @@ final class PlayerMovingBehavior extends Behavior FlameBlocReader, ParentIsA, HasGameReference { + /// The delay between player moves for each item in the inventory. + static const delayPerItem = Duration(milliseconds: 25); + + /// The base time it takes for the player to move from one position to + /// another. + static const baseMoveTime = Duration(milliseconds: 150); + /// The delay between player moves. - static const moveDelay = Duration(milliseconds: 200); + Duration moveDelay = baseMoveTime; /// States whether the player is currently moving. bool _isMoving = false; @@ -94,6 +101,12 @@ final class PlayerMovingBehavior extends Behavior } parent.hop(direction); _isMoving = true; + + final delayMilliseconds = + (bloc.state.inventory.items.length * delayPerItem.inMilliseconds) + + baseMoveTime.inMilliseconds; + moveDelay = Duration(milliseconds: delayMilliseconds); + _nextMoveTime = now.add(moveDelay); } diff --git a/packages/trashy_road/lib/src/game/entities/player/player.dart b/packages/trashy_road/lib/src/game/entities/player/player.dart index 935ca3c5..5ddc4b1e 100644 --- a/packages/trashy_road/lib/src/game/entities/player/player.dart +++ b/packages/trashy_road/lib/src/game/entities/player/player.dart @@ -98,7 +98,7 @@ class Player extends PositionedEntity with ZIndex { } class _PlayerSpriteComponent extends SpriteAnimationComponent - with HasGameReference { + with HasGameReference, ParentIsA { _PlayerSpriteComponent() : super( position: Vector2(-0.4, -2.5)..toGameSize(), @@ -129,6 +129,9 @@ class _PlayerSpriteComponent extends SpriteAnimationComponent Direction _previousDirection = Direction.up; + /// The amount of frames in the player shadow sprite sheet. + static const _frameCount = 7; + Future _createAnimation(AssetGenImage image) async { final spriteSheet = await game.images.load(image.path); const frameCount = 7; @@ -137,8 +140,8 @@ class _PlayerSpriteComponent extends SpriteAnimationComponent spriteSheet, SpriteAnimationData.sequenced( amount: frameCount, - stepTime: - (PlayerMovingBehavior.moveDelay.inMilliseconds / 1000) / frameCount, + stepTime: (PlayerMovingBehavior.baseMoveTime.inMilliseconds / 1000) / + frameCount, textureSize: Vector2.all(256), loop: false, ), @@ -164,19 +167,29 @@ class _PlayerSpriteComponent extends SpriteAnimationComponent void hop(Direction direction) { animationTicker!.reset(); animation = _animations[_previousDirection]![direction]; + animation!.stepTime = parent.children + .whereType() + .first + .moveDelay + .inMilliseconds / + 1000 / + _frameCount; _previousDirection = direction; playing = true; } } class _PlayerShadowSpriteComponent extends SpriteAnimationComponent - with HasGameReference { + with HasGameReference, ParentIsA { _PlayerShadowSpriteComponent() : super( position: Vector2(-0.4, -2.5)..toGameSize(), scale: Vector2.all(1.8), ); + /// The amount of frames in the player shadow sprite sheet. + static const _frameCount = 7; + @override Future onLoad() async { await super.onLoad(); @@ -186,16 +199,14 @@ class _PlayerShadowSpriteComponent extends SpriteAnimationComponent () => game.images.load(Assets.images.sprites.playerHopShadow.path), ); - const frameCount = 7; - animation = SpriteAnimation.fromFrameData( image, SpriteAnimationData.sequenced( - amount: frameCount, + amount: _frameCount, amountPerRow: 3, textureSize: Vector2.all(128), - stepTime: - (PlayerMovingBehavior.moveDelay.inMilliseconds / 1000) / frameCount, + stepTime: (PlayerMovingBehavior.baseMoveTime.inMilliseconds / 1000) / + _frameCount, loop: false, ), ); @@ -207,6 +218,13 @@ class _PlayerShadowSpriteComponent extends SpriteAnimationComponent } void hop() { + animation!.stepTime = parent.children + .whereType() + .first + .moveDelay + .inMilliseconds / + 1000 / + _frameCount; playing = true; } }