Merge pull request #19 from VGVentures/feat/ball-placeholder-asset

feat: adding ball component placeholder asset
pull/21/head
Erick 4 years ago committed by GitHub
commit aca7022fdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

@ -1,23 +1,31 @@
import 'package:flame/components.dart';
import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_forge2d/body_component.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart';
import 'package:forge2d/forge2d.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
class Ball extends BodyComponent<PinballGame> class Ball extends PositionBodyComponent<PinballGame, SpriteComponent>
with BlocComponent<GameBloc, GameState> { with BlocComponent<GameBloc, GameState> {
Ball({ Ball({
required Vector2 position, required Vector2 position,
}) : _position = position { }) : _position = position,
// TODO(alestiago): Use asset instead of color when provided. super(size: ballSize);
paint = Paint()..color = const Color(0xFFFFFFFF);
} static final ballSize = Vector2.all(2);
final Vector2 _position; final Vector2 _position;
static const spritePath = 'components/ball.png';
@override
Future<void> onLoad() async {
await super.onLoad();
final sprite = await gameRef.loadSprite(spritePath);
positionComponent = SpriteComponent(sprite: sprite, size: ballSize);
}
@override @override
Body createBody() { Body createBody() {
final shape = CircleShape()..radius = 2; final shape = CircleShape()..radius = ballSize.x / 2;
final fixtureDef = FixtureDef(shape)..density = 1; final fixtureDef = FixtureDef(shape)..density = 1;

@ -1,4 +1,5 @@
export 'bloc/game_bloc.dart'; export 'bloc/game_bloc.dart';
export 'components/components.dart'; export 'components/components.dart';
export 'game_assets.dart';
export 'pinball_game.dart'; export 'pinball_game.dart';
export 'view/view.dart'; export 'view/view.dart';

@ -0,0 +1,11 @@
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([
images.load(Ball.spritePath),
]);
}
}

@ -1,10 +1,14 @@
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';
class PinballGame extends Forge2DGame with FlameBloc { class PinballGame extends Forge2DGame with FlameBloc {
void spawnBall() { void spawnBall() {
add(Ball(position: ballStartingPosition)); add(
Ball(position: ballStartingPosition),
);
} }
// TODO(erickzanardo): Change to the plumber position // TODO(erickzanardo): Change to the plumber position

@ -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),
); );
} }
} }

@ -140,21 +140,21 @@ packages:
name: flame name: flame
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0-releasecandidate.1" version: "1.1.0-releasecandidate.2"
flame_bloc: flame_bloc:
dependency: "direct main" dependency: "direct main"
description: description:
name: flame_bloc name: flame_bloc
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.0-releasecandidate.1" version: "1.2.0-releasecandidate.2"
flame_forge2d: flame_forge2d:
dependency: "direct main" dependency: "direct main"
description: description:
name: flame_forge2d name: flame_forge2d
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.9.0-releasecandidate.1" version: "0.9.0-releasecandidate.2"
flame_test: flame_test:
dependency: "direct dev" dependency: "direct dev"
description: description:

@ -9,9 +9,9 @@ environment:
dependencies: dependencies:
bloc: ^8.0.2 bloc: ^8.0.2
equatable: ^2.0.3 equatable: ^2.0.3
flame: ^1.1.0-releasecandidate.1 flame: ^1.1.0-releasecandidate.2
flame_bloc: ^1.2.0-releasecandidate.1 flame_bloc: ^1.2.0-releasecandidate.2
flame_forge2d: ^0.9.0-releasecandidate.1 flame_forge2d: ^0.9.0-releasecandidate.2
flutter: flutter:
sdk: flutter sdk: flutter
flutter_bloc: ^8.0.1 flutter_bloc: ^8.0.1
@ -33,3 +33,6 @@ dev_dependencies:
flutter: flutter:
uses-material-design: true uses-material-design: true
generate: true generate: true
assets:
- assets/images/components/

@ -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