feat: added maskbits to ball for collisions

pull/40/head
RuiAlonso 4 years ago
parent 81dddc50f3
commit 70f2af6ff0

@ -7,12 +7,15 @@ class Ball extends PositionBodyComponent<PinballGame, SpriteComponent>
with BlocComponent<GameBloc, GameState> { with BlocComponent<GameBloc, GameState> {
Ball({ Ball({
required Vector2 position, required Vector2 position,
int? maskBits,
}) : _position = position, }) : _position = position,
_maskBits = maskBits ?? Filter().maskBits,
super(size: ballSize); super(size: ballSize);
static final ballSize = Vector2.all(2); static final ballSize = Vector2.all(2);
final Vector2 _position; final Vector2 _position;
final int _maskBits;
static const spritePath = 'components/ball.png'; static const spritePath = 'components/ball.png';
@ -34,7 +37,12 @@ class Ball extends PositionBodyComponent<PinballGame, SpriteComponent>
..position = _position ..position = _position
..type = BodyType.dynamic; ..type = BodyType.dynamic;
return world.createBody(bodyDef)..createFixture(fixtureDef); final body = world.createBody(bodyDef);
body.createFixture(fixtureDef)
..filterData.categoryBits = _maskBits
..filterData.maskBits = _maskBits;
return body;
} }
void lost() { void lost() {
@ -42,8 +50,8 @@ class Ball extends PositionBodyComponent<PinballGame, SpriteComponent>
final bloc = gameRef.read<GameBloc>()..add(const BallLost()); final bloc = gameRef.read<GameBloc>()..add(const BallLost());
final shouldBallRespwan = !bloc.state.isLastBall; final shouldBallRespawn = !bloc.state.isLastBall;
if (shouldBallRespwan) { if (shouldBallRespawn) {
gameRef.spawnBall(); gameRef.spawnBall();
} }
} }

Loading…
Cancel
Save