Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add WorldRoute to enable swapping worlds from the RouterComponent #3372

Merged
merged 2 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ desktop/
coverage/
pubspec.lock
pubspec_overrides.yaml
.fvmrc

# Sphinx related
__pycache__/
Expand Down
124 changes: 73 additions & 51 deletions doc/flame/examples/lib/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,22 @@ class RouterGame extends FlameGame {
add(
router = RouterComponent(
routes: {
'splash': Route(SplashScreenPage.new),
'home': Route(StartPage.new),
'level1': Route(Level1Page.new),
'level2': Route(Level2Page.new),
'level1': WorldRoute(Level1Page.new),
'level2': WorldRoute(Level2Page.new, maintainState: false),
'pause': PauseRoute(),
},
initialRoute: 'splash',
initialRoute: 'home',
),
);
}
}

class SplashScreenPage extends Component
with TapCallbacks, HasGameReference<RouterGame> {
@override
Future<void> onLoad() async {
addAll([
Background(const Color(0xff282828)),
TextBoxComponent(
text: '[Router demo]',
textRenderer: TextPaint(
style: const TextStyle(
color: Color(0x66ffffff),
fontSize: 16,
),
),
align: Anchor.center,
size: game.canvasSize,
),
]);
}

@override
bool containsLocalPoint(Vector2 point) => true;

@override
void onTapUp(TapUpEvent event) => game.router.pushNamed('home');
}

class StartPage extends Component with HasGameReference<RouterGame> {
StartPage() {
addAll([
_logo = TextComponent(
text: 'Syzygy',
text: 'Your Game',
textRenderer: TextPaint(
style: const TextStyle(
fontSize: 64,
Expand Down Expand Up @@ -231,22 +203,28 @@ class PauseButton extends SimpleButton with HasGameReference<RouterGame> {
..lineTo(26, 30),
position: Vector2(60, 10),
);

bool isPaused = false;

@override
void action() => game.router.pushNamed('pause');
void action() {
if (isPaused) {
game.router.pop();
} else {
game.router.pushNamed('pause');
}
isPaused = !isPaused;
}
}

class Level1Page extends Component {
class Level1Page extends DecoratedWorld with HasGameReference {
@override
Future<void> onLoad() async {
final game = findGame()!;
addAll([
Background(const Color(0xbb2a074f)),
BackButton(),
PauseButton(),
Planet(
radius: 25,
color: const Color(0xfffff188),
position: game.size / 2,
children: [
Orbit(
radius: 110,
Expand All @@ -267,20 +245,33 @@ class Level1Page extends Component {
),
]);
}

final hudComponents = <Component>[];

@override
void onMount() {
hudComponents.addAll([
BackButton(),
PauseButton(),
]);
game.camera.viewport.addAll(hudComponents);
}

@override
void onRemove() {
game.camera.viewport.removeAll(hudComponents);
super.onRemove();
}
}

class Level2Page extends Component {
class Level2Page extends DecoratedWorld with HasGameReference {
@override
Future<void> onLoad() async {
final game = findGame()!;
addAll([
Background(const Color(0xff052b44)),
BackButton(),
PauseButton(),
Planet(
radius: 30,
color: const Color(0xFFFFFFff),
position: game.size / 2,
children: [
Orbit(
radius: 60,
Expand Down Expand Up @@ -311,6 +302,23 @@ class Level2Page extends Component {
),
]);
}

final hudComponents = <Component>[];

@override
void onMount() {
hudComponents.addAll([
BackButton(),
PauseButton(),
]);
game.camera.viewport.addAll(hudComponents);
}

@override
void onRemove() {
game.camera.viewport.removeAll(hudComponents);
super.onRemove();
}
}

class Planet extends PositionComponent {
Expand Down Expand Up @@ -367,18 +375,19 @@ class PauseRoute extends Route {

@override
void onPush(Route? previousRoute) {
previousRoute!
..stopTime()
..addRenderEffect(
PaintDecorator.grayscale(opacity: 0.5)..addBlur(3.0),
);
if (previousRoute is WorldRoute && previousRoute.world is DecoratedWorld) {
(previousRoute.world! as DecoratedWorld).timeScale = 0;
(previousRoute.world! as DecoratedWorld).decorator =
PaintDecorator.grayscale(opacity: 0.5)..addBlur(3.0);
}
}

@override
void onPop(Route nextRoute) {
nextRoute
..resumeTime()
..removeRenderEffect();
if (nextRoute is WorldRoute && nextRoute.world is DecoratedWorld) {
(nextRoute.world! as DecoratedWorld).timeScale = 1;
(nextRoute.world! as DecoratedWorld).decorator = null;
}
}
}

Expand Down Expand Up @@ -412,3 +421,16 @@ class PausePage extends Component
@override
void onTapUp(TapUpEvent event) => game.router.pop();
}

class DecoratedWorld extends World with HasTimeScale {
PaintDecorator? decorator;

@override
void renderFromCamera(Canvas canvas) {
if (decorator == null) {
super.renderFromCamera(canvas);
} else {
decorator!.applyChain(super.renderFromCamera, canvas);
}
}
}
40 changes: 40 additions & 0 deletions doc/flame/router.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,46 @@ The current route can be replaced using `pushReplacementNamed` or `pushReplaceme
simply executes `pop` on the current route and then `pushNamed` or `pushRoute`.


## WorldRoute

The **WorldRoute** is a special route that allows setting active game worlds via the router. These
Such routes can be used, for example, for swapping levels implemented as separate worlds in your
game.

By default, the `WorldRoute` will replace the current world with the new one and by default it will
keep the state of the world after being popped from the stack. If you want the world to be recreated
each time the route is activated, set `maintainState` to `false`.

If you are not using the built-in `CameraComponent` you can pass in the camera that you want to use
explicitly in the constructor.

```dart
final router = RouterComponent(
routes: {
'level1': WorldRoute(MyWorld1.new),
'level2': WorldRoute(MyWorld2.new, maintainState: false),
},
);

class MyWorld1 extends World {
@override
Future<void> onLoad() async {
add(BackgroundComponent());
add(PlayerComponent());
}
}

class MyWorld2 extends World {
@override
Future<void> onLoad() async {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: add super call

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The world's default onLoad is empty?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I don't remember, if it is so, them no worries, was just a nit anyway

add(BackgroundComponent());
add(PlayerComponent());
add(EnemyComponent());
}
}
```


## OverlayRoute

The **OverlayRoute** is a special route that allows adding game overlays via the router. These
Expand Down
2 changes: 2 additions & 0 deletions examples/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import 'package:examples/stories/input/input.dart';
import 'package:examples/stories/layout/layout.dart';
import 'package:examples/stories/parallax/parallax.dart';
import 'package:examples/stories/rendering/rendering.dart';
import 'package:examples/stories/router/router.dart';
import 'package:examples/stories/sprites/sprites.dart';
import 'package:examples/stories/structure/structure.dart';
import 'package:examples/stories/svg/svg.dart';
Expand Down Expand Up @@ -88,6 +89,7 @@ void runAsDashbook() {
addLayoutStories(dashbook);
addParallaxStories(dashbook);
addRenderingStories(dashbook);
addRouterStories(dashbook);
addTiledStories(dashbook);
addSpritesStories(dashbook);
addSvgStories(dashbook);
Expand Down
13 changes: 13 additions & 0 deletions examples/lib/stories/router/router.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:dashbook/dashbook.dart';
import 'package:examples/commons/commons.dart';
import 'package:examples/stories/router/router_world_example.dart';
import 'package:flame/game.dart';

void addRouterStories(Dashbook dashbook) {
dashbook.storiesOf('Router').add(
'Router with multiple worlds',
(_) => GameWidget(game: RouterWorldExample()),
codeLink: baseLink('router/router_world_example.dart'),
info: RouterWorldExample.description,
);
}
Loading