Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
feat: add slow down with inventory size (#275)
Browse files Browse the repository at this point in the history
* feat: add slow down with inventory size

* feat: pubspec regen
  • Loading branch information
OlliePugh authored Mar 12, 2024
1 parent 80f45d9 commit be83d83
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ final class PlayerMovingBehavior extends Behavior<Player>
FlameBlocReader<GameBloc, GameState>,
ParentIsA<Player>,
HasGameReference<TrashyRoadGame> {
/// 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;
Expand Down Expand Up @@ -94,6 +101,12 @@ final class PlayerMovingBehavior extends Behavior<Player>
}
parent.hop(direction);
_isMoving = true;

final delayMilliseconds =
(bloc.state.inventory.items.length * delayPerItem.inMilliseconds) +
baseMoveTime.inMilliseconds;
moveDelay = Duration(milliseconds: delayMilliseconds);

_nextMoveTime = now.add(moveDelay);
}

Expand Down
36 changes: 27 additions & 9 deletions packages/trashy_road/lib/src/game/entities/player/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Player extends PositionedEntity with ZIndex {
}

class _PlayerSpriteComponent extends SpriteAnimationComponent
with HasGameReference {
with HasGameReference, ParentIsA<Player> {
_PlayerSpriteComponent()
: super(
position: Vector2(-0.4, -2.5)..toGameSize(),
Expand Down Expand Up @@ -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<SpriteAnimation> _createAnimation(AssetGenImage image) async {
final spriteSheet = await game.images.load(image.path);
const frameCount = 7;
Expand All @@ -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,
),
Expand All @@ -164,19 +167,29 @@ class _PlayerSpriteComponent extends SpriteAnimationComponent
void hop(Direction direction) {
animationTicker!.reset();
animation = _animations[_previousDirection]![direction];
animation!.stepTime = parent.children
.whereType<PlayerMovingBehavior>()
.first
.moveDelay
.inMilliseconds /
1000 /
_frameCount;
_previousDirection = direction;
playing = true;
}
}

class _PlayerShadowSpriteComponent extends SpriteAnimationComponent
with HasGameReference {
with HasGameReference, ParentIsA<Player> {
_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<void> onLoad() async {
await super.onLoad();
Expand All @@ -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,
),
);
Expand All @@ -207,6 +218,13 @@ class _PlayerShadowSpriteComponent extends SpriteAnimationComponent
}

void hop() {
animation!.stepTime = parent.children
.whereType<PlayerMovingBehavior>()
.first
.moveDelay
.inMilliseconds /
1000 /
_frameCount;
playing = true;
}
}

0 comments on commit be83d83

Please sign in to comment.