fix: fixed collisions of a ball that only touch path entrance but doesn't get into

pull/40/head
RuiAlonso 4 years ago
parent c049f83eba
commit 99239bf85d

@ -1,6 +1,8 @@
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball/game/game.dart';
enum RampOrientation { up, down }
enum RampType { all, jetpack, sparky }
extension RampTypeX on RampType {
@ -30,6 +32,7 @@ abstract class RampArea extends BodyComponent {
int get maskBits => _maskBits;
Shape get shape;
RampOrientation get orientation;
@override
Body createBody() {
@ -59,23 +62,41 @@ abstract class RampAreaCallback<Area extends RampArea>
Area area,
Contact _,
) {
int maskBits;
if (!ballsInside.contains(ball)) {
ball.body.fixtures.first
..filterData.categoryBits = area.maskBits
..filterData.maskBits = area.maskBits;
maskBits = area.maskBits;
ballsInside.add(ball);
} else {
maskBits = RampType.all.maskBits;
ballsInside.remove(ball);
}
ball.body.fixtures.first
..filterData.categoryBits = maskBits
..filterData.maskBits = maskBits;
}
@override
void end(Ball ball, Area area, Contact ___) {
int maskBits;
if (ballsInside.contains(ball)) {
ball.body.fixtures.first
..filterData.categoryBits = RampType.all.maskBits
..filterData.maskBits = RampType.all.maskBits;
void end(Ball ball, Area area, Contact contact) {
int? maskBits;
ballsInside.remove(ball);
switch (area.orientation) {
case RampOrientation.up:
if (ball.body.position.y > area._position.y) {
maskBits = RampType.all.maskBits;
}
break;
case RampOrientation.down:
if (ball.body.position.y < area._position.y) {
maskBits = RampType.all.maskBits;
}
break;
}
if (maskBits != null) {
ball.body.fixtures.first
..filterData.categoryBits = maskBits
..filterData.maskBits = maskBits;
}
}
}

@ -16,7 +16,7 @@ class JetpackRamp extends PositionComponent with HasGameRef<PinballGame> {
Future<void> onLoad() async {
await add(
Pathway.arc(
color: Color.fromARGB(255, 8, 218, 241),
color: const Color.fromARGB(255, 8, 218, 241),
position: _position,
width: 80,
radius: 200,
@ -30,12 +30,14 @@ class JetpackRamp extends PositionComponent with HasGameRef<PinballGame> {
JetpackRampArea(
position: _position + Vector2(-10.5, 0),
rotation: radians(15),
orientation: RampOrientation.down,
),
);
await add(
JetpackRampArea(
position: _position + Vector2(20.5, 3),
rotation: radians(-5),
orientation: RampOrientation.down,
),
);
@ -47,28 +49,36 @@ class JetpackRampArea extends RampArea {
JetpackRampArea({
required Vector2 position,
double rotation = 0,
int size = 7,
required RampOrientation orientation,
}) : _rotation = rotation,
_size = size,
_orientation = orientation,
super(
position: position,
maskBits: RampType.jetpack.maskBits,
);
final RampOrientation _orientation;
final double _rotation;
final int _size;
final int _size = 7;
@override
RampOrientation get orientation => _orientation;
@override
Shape get shape => PolygonShape()
..set([
Vector2(-_size / 2, -.5)..rotate(_rotation),
Vector2(-_size / 2, .5)..rotate(_rotation),
Vector2(_size / 2, .5)..rotate(_rotation),
Vector2(_size / 2, -.5)..rotate(_rotation),
Vector2(-_size / 2, -.1)..rotate(_rotation),
Vector2(-_size / 2, .1)..rotate(_rotation),
Vector2(_size / 2, .1)..rotate(_rotation),
Vector2(_size / 2, -.1)..rotate(_rotation),
]);
}
class JetpackRampAreaCallback extends RampAreaCallback<JetpackRampArea> {
JetpackRampAreaCallback() : super();
final _ballsInsideJetpack = <Ball>{};
@override
Set get ballsInside => <Ball>{};
Set get ballsInside => _ballsInsideJetpack;
}

@ -27,11 +27,13 @@ class SparkyRamp extends PositionComponent with HasGameRef<PinballGame> {
await add(
SparkyRampArea(
position: _position + Vector2(-19, 6),
orientation: RampOrientation.down,
),
);
await add(
SparkyRampArea(
position: _position + Vector2(33, 6),
orientation: RampOrientation.down,
),
);
@ -43,28 +45,36 @@ class SparkyRampArea extends RampArea {
SparkyRampArea({
required Vector2 position,
double rotation = 0,
int size = 7,
required RampOrientation orientation,
}) : _rotation = rotation,
_size = size,
_orientation = orientation,
super(
position: position,
maskBits: RampType.sparky.maskBits,
);
final RampOrientation _orientation;
final double _rotation;
final int _size;
final int _size = 7;
@override
RampOrientation get orientation => _orientation;
@override
Shape get shape => PolygonShape()
..set([
Vector2(-_size / 2, -.5)..rotate(_rotation),
Vector2(-_size / 2, .5)..rotate(_rotation),
Vector2(_size / 2, .5)..rotate(_rotation),
Vector2(_size / 2, -.5)..rotate(_rotation),
Vector2(-_size / 2, -.1)..rotate(_rotation),
Vector2(-_size / 2, .1)..rotate(_rotation),
Vector2(_size / 2, .1)..rotate(_rotation),
Vector2(_size / 2, -.1)..rotate(_rotation),
]);
}
class SparkyRampAreaCallback extends RampAreaCallback<SparkyRampArea> {
SparkyRampAreaCallback() : super();
final _ballsInsideSparky = <Ball>{};
@override
Set get ballsInside => <Ball>{};
Set get ballsInside => _ballsInsideSparky;
}

Loading…
Cancel
Save