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:flame_forge2d/flame_forge2d.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
enum RampOrientation { up, down }
enum RampType { all, jetpack, sparky } enum RampType { all, jetpack, sparky }
extension RampTypeX on RampType { extension RampTypeX on RampType {
@ -30,6 +32,7 @@ abstract class RampArea extends BodyComponent {
int get maskBits => _maskBits; int get maskBits => _maskBits;
Shape get shape; Shape get shape;
RampOrientation get orientation;
@override @override
Body createBody() { Body createBody() {
@ -59,23 +62,41 @@ abstract class RampAreaCallback<Area extends RampArea>
Area area, Area area,
Contact _, Contact _,
) { ) {
int maskBits;
if (!ballsInside.contains(ball)) { if (!ballsInside.contains(ball)) {
ball.body.fixtures.first maskBits = area.maskBits;
..filterData.categoryBits = area.maskBits
..filterData.maskBits = area.maskBits;
ballsInside.add(ball); ballsInside.add(ball);
} else {
maskBits = RampType.all.maskBits;
ballsInside.remove(ball);
} }
ball.body.fixtures.first
..filterData.categoryBits = maskBits
..filterData.maskBits = maskBits;
} }
@override @override
void end(Ball ball, Area area, Contact ___) { void end(Ball ball, Area area, Contact contact) {
int maskBits; int? maskBits;
if (ballsInside.contains(ball)) {
ball.body.fixtures.first
..filterData.categoryBits = RampType.all.maskBits
..filterData.maskBits = RampType.all.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 { Future<void> onLoad() async {
await add( await add(
Pathway.arc( Pathway.arc(
color: Color.fromARGB(255, 8, 218, 241), color: const Color.fromARGB(255, 8, 218, 241),
position: _position, position: _position,
width: 80, width: 80,
radius: 200, radius: 200,
@ -30,12 +30,14 @@ class JetpackRamp extends PositionComponent with HasGameRef<PinballGame> {
JetpackRampArea( JetpackRampArea(
position: _position + Vector2(-10.5, 0), position: _position + Vector2(-10.5, 0),
rotation: radians(15), rotation: radians(15),
orientation: RampOrientation.down,
), ),
); );
await add( await add(
JetpackRampArea( JetpackRampArea(
position: _position + Vector2(20.5, 3), position: _position + Vector2(20.5, 3),
rotation: radians(-5), rotation: radians(-5),
orientation: RampOrientation.down,
), ),
); );
@ -47,28 +49,36 @@ class JetpackRampArea extends RampArea {
JetpackRampArea({ JetpackRampArea({
required Vector2 position, required Vector2 position,
double rotation = 0, double rotation = 0,
int size = 7, required RampOrientation orientation,
}) : _rotation = rotation, }) : _rotation = rotation,
_size = size, _orientation = orientation,
super( super(
position: position, position: position,
maskBits: RampType.jetpack.maskBits, maskBits: RampType.jetpack.maskBits,
); );
final RampOrientation _orientation;
final double _rotation; final double _rotation;
final int _size; final int _size = 7;
@override
RampOrientation get orientation => _orientation;
@override @override
Shape get shape => PolygonShape() Shape get shape => PolygonShape()
..set([ ..set([
Vector2(-_size / 2, -.5)..rotate(_rotation), Vector2(-_size / 2, -.1)..rotate(_rotation),
Vector2(-_size / 2, .5)..rotate(_rotation), Vector2(-_size / 2, .1)..rotate(_rotation),
Vector2(_size / 2, .5)..rotate(_rotation), Vector2(_size / 2, .1)..rotate(_rotation),
Vector2(_size / 2, -.5)..rotate(_rotation), Vector2(_size / 2, -.1)..rotate(_rotation),
]); ]);
} }
class JetpackRampAreaCallback extends RampAreaCallback<JetpackRampArea> { class JetpackRampAreaCallback extends RampAreaCallback<JetpackRampArea> {
JetpackRampAreaCallback() : super();
final _ballsInsideJetpack = <Ball>{};
@override @override
Set get ballsInside => <Ball>{}; Set get ballsInside => _ballsInsideJetpack;
} }

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

Loading…
Cancel
Save