Skip to content

Commit 715eab3

Browse files
committed
feat: Update example template to use more modern Flame constructs
1 parent 1eb81b8 commit 715eab3

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

bricks/example/__brick__/lib/main.dart

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,40 @@ void main() {
1212
runApp(GameWidget(game: MyGame()));
1313
}
1414

15-
class MyGame extends FlameGame with TapCallbacks {
15+
class MyGame extends FlameGame<MyWorld> {
16+
MyGame() : super(world: MyWorld());
17+
}
18+
19+
class MyWorld extends World {
1620
late final MyComponent myComponent;
1721

1822
@override
1923
Future<void> onLoad() async {
20-
await world.add(myComponent = MyComponent());
24+
await add(myComponent = MyComponent());
2125
return super.onLoad();
2226
}
23-
24-
@override
25-
void onTapUp(TapUpEvent event) {
26-
myComponent.speed.x = -1 + 2 * _rng.nextDouble();
27-
myComponent.speed.y = -1 + 2 * _rng.nextDouble();
28-
}
2927
}
3028

31-
class MyComponent extends PositionComponent with HasGameRef<MyGame> {
32-
static final _paint = BasicPalette.white.paint();
29+
class MyComponent extends RectangleComponent with TapCallbacks {
3330
final Vector2 speed = Vector2.zero();
3431

32+
@override
33+
final Paint paint = BasicPalette.magenta.paint();
34+
3535
MyComponent()
36-
: super(
36+
: super.square(
37+
size: 32,
3738
anchor: Anchor.center,
38-
size: Vector2.all(32),
3939
);
4040

4141
@override
42-
void render(Canvas c) {
43-
c.drawRect(size.toRect(), _paint);
42+
void update(double dt) {
43+
position += speed * 32.0 * dt;
4444
}
4545

4646
@override
47-
void update(double dt) {
48-
position += speed * 32.0 * dt;
47+
void onTapUp(TapUpEvent event) {
48+
speed.x = -1 + 2 * _rng.nextDouble();
49+
speed.y = -1 + 2 * _rng.nextDouble();
4950
}
5051
}

bricks/example/__brick__/test/widget_test.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ void main() {
1010
myGame.testGameWidget(
1111
'game will load its child',
1212
verify: (game, tester) async {
13-
game.update(0.0);
13+
await tester.pumpWidget(GameWidget(game: game));
1414

15-
expect(game.children.length, 1);
16-
expect(game.myComponent.speed, Vector2.zero());
15+
expect(game.world.children.length, 1);
16+
expect(game.world.myComponent.speed, Vector2.zero());
1717

1818
await tester.tapAt(const Offset(10, 10));
19-
expect(game.myComponent.speed, isNot(equals(Vector2.zero())));
19+
await tester.pump(const Duration(milliseconds: 100));
20+
21+
expect(game.world.myComponent.speed, isNot(equals(Vector2.zero())));
2022
},
2123
);
2224
}

lib/templates/bricks/example_bundle.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)