feat: adding tests

pull/19/head
Erick Zanardo 3 years ago
parent ac0aca5f8f
commit a2093811fa

@ -7,13 +7,12 @@ class Ball extends PositionBodyComponent<PinballGame, SpriteComponent>
with BlocComponent<GameBloc, GameState> {
Ball({
required Vector2 position,
required Vector2 size,
}) : _position = position,
_size = size,
super(size: size);
super(size: ballSize);
static final ballSize = Vector2.all(2);
final Vector2 _position;
final Vector2 _size;
static const spritePath = 'components/ball.png';
@ -21,12 +20,12 @@ class Ball extends PositionBodyComponent<PinballGame, SpriteComponent>
Future<void> onLoad() async {
await super.onLoad();
final sprite = await gameRef.loadSprite(spritePath);
positionComponent = SpriteComponent(sprite: sprite, size: _size);
positionComponent = SpriteComponent(sprite: sprite, size: ballSize);
}
@override
Body createBody() {
final shape = CircleShape()..radius = _size.x / 2;
final shape = CircleShape()..radius = ballSize.x / 2;
final fixtureDef = FixtureDef(shape)..density = 1;

@ -2,7 +2,6 @@ import 'package:pinball/game/game.dart';
/// Add methods to help loading and caching game assets
extension PinballGameAssetsX on PinballGame {
/// Pre load the initial assets of the game
Future<void> preLoadAssets() async {
await Future.wait([

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball/game/game.dart';
@ -5,10 +7,7 @@ import 'package:pinball/game/game.dart';
class PinballGame extends Forge2DGame with FlameBloc {
void spawnBall() {
add(
Ball(
position: ballStartingPosition,
size: Vector2.all(4),
),
Ball(position: ballStartingPosition),
);
}
@ -23,8 +22,6 @@ class PinballGame extends Forge2DGame with FlameBloc {
@override
Future<void> onLoad() async {
await preLoadAssets();
addContactCallback(BallScorePointsCallback());
await add(BottomWall(this));

@ -23,9 +23,26 @@ class PinballGamePage extends StatelessWidget {
}
}
class PinballGameView extends StatelessWidget {
class PinballGameView extends StatefulWidget {
const PinballGameView({Key? key}) : super(key: key);
@override
State<PinballGameView> createState() => _PinballGameViewState();
}
class _PinballGameViewState extends State<PinballGameView> {
late PinballGame _game;
@override
void initState() {
super.initState();
// TODO(erickzanardo): Revisit this when we start to have more assets
// this could expose a Stream (maybe even a cubit?) so we could show the
// the loading progress with some fancy widgets.
_game = PinballGame()..preLoadAssets();
}
@override
Widget build(BuildContext context) {
return BlocListener<GameBloc, GameState>(
@ -39,7 +56,7 @@ class PinballGameView extends StatelessWidget {
);
}
},
child: GameWidget<PinballGame>(game: PinballGame()),
child: GameWidget<PinballGame>(game: _game),
);
}
}

@ -79,7 +79,7 @@ void main() {
final fixture = ball.body.fixtures[0];
expect(fixture.shape.shapeType, equals(ShapeType.circle));
expect(fixture.shape.radius, equals(2));
expect(fixture.shape.radius, equals(1));
},
);
});

Loading…
Cancel
Save