Skip to content

Commit

Permalink
refactor!: Use HasTimeScale for Route (#3064)
Browse files Browse the repository at this point in the history
This PR replaces the `Route.timeSpeed` with `HasTimeScale.timeScale`.
  • Loading branch information
ufrshubham committed Mar 3, 2024
1 parent 72678c6 commit 30fde80
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
18 changes: 6 additions & 12 deletions packages/flame/lib/src/components/route.dart
Expand Up @@ -20,7 +20,8 @@ import 'package:meta/meta.dart';
/// any potentially costly initialization operations.
///
/// Routes are managed by the [RouterComponent] component.
class Route extends PositionComponent with ParentIsA<RouterComponent> {
class Route extends PositionComponent
with ParentIsA<RouterComponent>, HasTimeScale {
Route(
Component Function()? builder, {
this.transparent = false,
Expand Down Expand Up @@ -82,25 +83,18 @@ class Route extends PositionComponent with ParentIsA<RouterComponent> {
return _builder!();
}

/// The time "speed" factor.
///
/// The value of 1 means that the time on this page runs normally. The value
/// less than 1 corresponds to time running slower than normal. The speed of
/// zero means the time on this page is stopped.
double timeSpeed = 1.0;

/// Completely stops time for the managed page.
///
/// When the time is stopped, the [updateTree] method of the page is not
/// called at all, which can save computational resources. However, this also
/// means that the lifecycle events on the page will not be processed, and
/// therefore no components will be able to be added or removed from the
/// page.
void stopTime() => timeSpeed = 0;
void stopTime() => timeScale = 0;

/// Resumes normal time progression for the page, if it was previously slowed
/// down or stopped.
void resumeTime() => timeSpeed = 1.0;
void resumeTime() => timeScale = 1.0;

/// Applies the provided [Decorator] to the page.
///
Expand Down Expand Up @@ -157,8 +151,8 @@ class Route extends PositionComponent with ParentIsA<RouterComponent> {

@override
void updateTree(double dt) {
if (timeSpeed > 0) {
super.updateTree(dt * timeSpeed);
if (timeScale > 0) {
super.updateTree(dt);
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/flame/test/components/route_test.dart
Expand Up @@ -274,19 +274,19 @@ void main() {
router.pushNamed('pause');
await game.ready();
expect(router.currentRoute.name, 'pause');
expect(router.previousRoute!.timeSpeed, 0);
expect(router.previousRoute!.timeScale, 0);

game.update(10);
expect(timer.elapsedTime, 1);

router.previousRoute!.timeSpeed = 0.1;
router.previousRoute!.timeScale = 0.1;
game.update(10);
expect(timer.elapsedTime, 2);

router.pop();
await game.ready();
expect(router.currentRoute.name, 'start');
expect(router.currentRoute.timeSpeed, 1);
expect(router.currentRoute.timeScale, 1);

game.update(10);
expect(timer.elapsedTime, 12);
Expand Down

0 comments on commit 30fde80

Please sign in to comment.