Files
flame/examples/lib/stories/components/game_in_game_example.dart
Pasha Stetsenko 4f5e56f05f feat: Added AlignComponent layout component (#2350)
This PR adds first layout component: AlignComponent, an equivalent of Align widget in Flutter.

AlignComponent sizes itself to its parent, and then keeps its child aligned to the specified anchor within its own bounding box.

Also adding onParentResize() lifecycle method, which is similar to onGameResize, but fires whenever the parent of the current component changes its size for any reason. (FlameGame is assumed to have the size canvasSize, and will invoke onParentResize whenever the canvas size changes).

Additional layout components are planned to be added in future PRs.
2023-03-02 22:51:02 +01:00

45 lines
1.5 KiB
Dart

import 'package:examples/stories/components/composability_example.dart';
import 'package:examples/stories/input/draggables_example.dart';
import 'package:flame/components.dart';
import 'package:flame/game.dart';
class GameInGameExample extends FlameGame with HasDraggables {
static const String description = '''
This example shows two games having another game as a parent.
One game contains draggable components and the other is a rotating square
with other square children.
After 5 seconds, one of the components from the game with draggable squares
changes its parent from its original game to the component that is rotating.
After another 5 seconds it changes back to its original parent, and so on.
''';
@override
bool debugMode = true;
late final ComposabilityExample composedGame;
late final DraggablesExample draggablesGame;
@override
Future<void> onLoad() async {
composedGame = ComposabilityExample();
draggablesGame = DraggablesExample(zoom: 1.0);
await add(composedGame);
await add(draggablesGame);
add(GameChangeTimer());
}
}
class GameChangeTimer extends TimerComponent
with HasGameRef<GameInGameExample> {
GameChangeTimer() : super(period: 5, repeat: true);
@override
void onTick() {
final child = gameRef.draggablesGame.square;
final newParent = child.parent == gameRef.draggablesGame
? gameRef.composedGame.parentSquare as Component
: gameRef.draggablesGame;
child.changeParent(newParent);
}
}