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> { with BlocComponent<GameBloc, GameState> {
Ball({ Ball({
required Vector2 position, required Vector2 position,
required Vector2 size,
}) : _position = position, }) : _position = position,
_size = size, super(size: ballSize);
super(size: size);
static final ballSize = Vector2.all(2);
final Vector2 _position; final Vector2 _position;
final Vector2 _size;
static const spritePath = 'components/ball.png'; static const spritePath = 'components/ball.png';
@ -21,12 +20,12 @@ class Ball extends PositionBodyComponent<PinballGame, SpriteComponent>
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); await super.onLoad();
final sprite = await gameRef.loadSprite(spritePath); final sprite = await gameRef.loadSprite(spritePath);
positionComponent = SpriteComponent(sprite: sprite, size: _size); positionComponent = SpriteComponent(sprite: sprite, size: ballSize);
} }
@override @override
Body createBody() { Body createBody() {
final shape = CircleShape()..radius = _size.x / 2; final shape = CircleShape()..radius = ballSize.x / 2;
final fixtureDef = FixtureDef(shape)..density = 1; 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 /// Add methods to help loading and caching game assets
extension PinballGameAssetsX on PinballGame { extension PinballGameAssetsX on PinballGame {
/// Pre load the initial assets of the game /// Pre load the initial assets of the game
Future<void> preLoadAssets() async { Future<void> preLoadAssets() async {
await Future.wait([ await Future.wait([

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
@ -5,10 +7,7 @@ import 'package:pinball/game/game.dart';
class PinballGame extends Forge2DGame with FlameBloc { class PinballGame extends Forge2DGame with FlameBloc {
void spawnBall() { void spawnBall() {
add( add(
Ball( Ball(position: ballStartingPosition),
position: ballStartingPosition,
size: Vector2.all(4),
),
); );
} }
@ -23,8 +22,6 @@ class PinballGame extends Forge2DGame with FlameBloc {
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await preLoadAssets();
addContactCallback(BallScorePointsCallback()); addContactCallback(BallScorePointsCallback());
await add(BottomWall(this)); 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); 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocListener<GameBloc, GameState>( 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]; final fixture = ball.body.fixtures[0];
expect(fixture.shape.shapeType, equals(ShapeType.circle)); expect(fixture.shape.shapeType, equals(ShapeType.circle));
expect(fixture.shape.radius, equals(2)); expect(fixture.shape.radius, equals(1));
}, },
); );
}); });

Loading…
Cancel
Save