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

Commit

Permalink
Merge branch 'main' into alestiago/game-state-identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago authored Feb 16, 2024
2 parents 866b77e + 2d71ec6 commit 6583ecc
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
7 changes: 2 additions & 5 deletions packages/trashy_road/lib/src/game/bloc/game_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class GameBloc extends Bloc<GameEvent, GameState> {
inventory: inventory,
collectedTrash: collectedTrash,
status: hasWon ? GameStatus.completed : GameStatus.playing,
score:
hasWon ? clock.now().difference(state.startedAt!).inSeconds : null,
),
);
}
Expand Down Expand Up @@ -131,9 +133,4 @@ class GameBloc extends Bloc<GameEvent, GameState> {
),
);
}

@override
void onTransition(Transition<GameEvent, GameState> transition) {
super.onTransition(transition);
}
}
13 changes: 13 additions & 0 deletions packages/trashy_road/lib/src/game/bloc/game_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class GameState extends Equatable {
this.pausedDuration = Duration.zero,
this.startedAt,
this.pausedAt,
this.score = -1,
}) :
// TODO(alestiago): Remove magic string.
_initialTrash =
Expand All @@ -63,6 +64,7 @@ class GameState extends Equatable {
inventory: Inventory.empty(),
pausedDuration: Duration.zero,
collectedTrash: 0,
score: -1,
);

/// {@macro GameStatus}
Expand Down Expand Up @@ -99,13 +101,22 @@ class GameState extends Equatable {
/// The total amount of time that the game has been paused for.
final Duration pausedDuration;

/// The final score of the game.
///
/// If the game has not been completed, the score is `-1`.
///
/// The larger the score, the worse the player did. The score is calculated
/// based on the amount of time that the player took to complete the game.
final int score;

GameState copyWith({
GameStatus? status,
Inventory? inventory,
int? collectedTrash,
DateTime? Function()? startedAt,
DateTime? Function()? pausedAt,
Duration? pausedDuration,
int? score,
}) {
return GameState(
identifier: identifier,
Expand All @@ -116,6 +127,7 @@ class GameState extends Equatable {
startedAt: startedAt != null ? startedAt() : this.startedAt,
pausedDuration: pausedDuration ?? this.pausedDuration,
pausedAt: pausedAt != null ? pausedAt() : this.pausedAt,
score: score ?? this.score,
);
}

Expand All @@ -128,6 +140,7 @@ class GameState extends Equatable {
startedAt,
pausedDuration,
pausedAt,
score,
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ enum Direction { up, down, left, right }
/// A behavior that allows the player to move around the game.
final class PlayerMovingBehavior extends Behavior<Player>
with FlameBlocReader<GameBloc, GameState> {
/// The delay between player moves.
static const _moveDelay = Duration(milliseconds: 100);

/// The lerp time for player movement.
static const _playerMoveAnimationSpeed = 15;

/// The position the player is trying to move to.
///
/// When its values are different than the current [Player.position]
/// it will be lerped until [_targetPosition] is reached by the [Player].
final Vector2 _targetPosition = Vector2.zero();

/// The delay between player moves.
static const moveDelay = Duration(milliseconds: 100);

/// The lerp time for player movement.
static const playerMoveAnimationSpeed = 15;
/// The position the player was at before the current [_targetPosition].
final Vector2 _previousPosition = Vector2.zero();

/// A int that contains the time when the next move can be made.
DateTime _nextMoveTime = DateTime.fromMicrosecondsSinceEpoch(0);
Expand All @@ -29,16 +32,25 @@ final class PlayerMovingBehavior extends Behavior<Player>
Future<void> onLoad() async {
await super.onLoad();
_targetPosition.setFrom(parent.position);
_previousPosition.setFrom(parent.position);
}

@override
void update(double dt) {
super.update(dt);

if (parent.position.distanceTo(_targetPosition) > 0.01) {
parent.position.lerp(_targetPosition, playerMoveAnimationSpeed * dt);
final hasArrived = parent.position.distanceTo(_targetPosition) < 0.01;
if (!hasArrived) {
parent.position.lerp(_targetPosition, _playerMoveAnimationSpeed * dt);
parent.priority = parent.position.y.floor();
}

final isMidThrough = parent.position.distanceTo(_targetPosition) <
GameSettings.gridDimensions.y / 2;
final movingElsewhere = _targetPosition != _previousPosition;
if (isMidThrough && movingElsewhere) {
_previousPosition.setFrom(_targetPosition);
}
}

void move(Direction direction) {
Expand All @@ -62,10 +74,10 @@ final class PlayerMovingBehavior extends Behavior<Player>
} else if (direction == Direction.up) {
_targetPosition.y -= GameSettings.gridDimensions.y;
}
_nextMoveTime = now.add(moveDelay);
_nextMoveTime = now.add(_moveDelay);
}

void bounceBack() {
_targetPosition.setFrom(Player.snapToGrid(parent.position));
_targetPosition.setFrom(_previousPosition);
}
}
4 changes: 2 additions & 2 deletions packages/trashy_road/lib/src/game/widgets/game_stopwatch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class _GameStopwatchState extends State<GameStopwatch>
),
BlocListener<GameBloc, GameState>(
listenWhen: (previous, current) {
final hasReset = previous.status != GameStatus.playing &&
current.status == GameStatus.ready;
final hasReset = previous.status == GameStatus.playing &&
current.status == GameStatus.resetting;
return hasReset;
},
listener: (_, __) => _reset(),
Expand Down
6 changes: 5 additions & 1 deletion packages/trashy_road/test/src/game/bloc/game_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,10 @@ void main() {
'completes the game when all the trash is deposited ',
build: () {
return withClock<GameBloc>(
Clock.fixed(DateTime(0)),
_IncremetalClock(
initialTime: DateTime(0),
increment: const Duration(seconds: 1),
),
() => GameBloc(identifier: identifier, map: map),
);
},
Expand Down Expand Up @@ -312,6 +315,7 @@ void main() {
inventory: Inventory.empty(),
collectedTrash: 2,
startedAt: DateTime(0),
score: 1,
),
],
);
Expand Down

0 comments on commit 6583ecc

Please sign in to comment.