refactor: improve layer and ramp to allow connection between different layers outside from board

pull/65/head
RuiAlonso 4 years ago
parent 1af0c47328
commit cca948ee0b

@ -55,6 +55,9 @@ enum Layer {
/// Collide only with Launcher group elements. /// Collide only with Launcher group elements.
launcher, launcher,
/// Collide only with Spaceship group elements.
spaceship,
} }
/// {@template layer_mask_bits} /// {@template layer_mask_bits}
@ -81,6 +84,8 @@ extension LayerMaskBits on Layer {
return 0x0002; return 0x0002;
case Layer.launcher: case Layer.launcher:
return 0x0005; return 0x0005;
case Layer.spaceship:
return 0x000A;
} }
} }
} }

@ -27,15 +27,21 @@ abstract class RampOpening extends BodyComponent with InitialPosition, Layered {
/// {@macro ramp_opening} /// {@macro ramp_opening}
RampOpening({ RampOpening({
required Layer pathwayLayer, required Layer pathwayLayer,
Layer? outsideLayer,
required this.orientation, required this.orientation,
}) : _pathwayLayer = pathwayLayer { }) : _pathwayLayer = pathwayLayer,
_outsideLayer = outsideLayer ?? Layer.board {
layer = Layer.board; layer = Layer.board;
} }
final Layer _pathwayLayer; final Layer _pathwayLayer;
final Layer _outsideLayer;
/// Mask of category bits for collision inside [Pathway]. /// Mask of category bits for collision inside [Pathway].
Layer get pathwayLayer => _pathwayLayer; Layer get pathwayLayer => _pathwayLayer;
/// Mask of category bits for collision outside [Pathway].
Layer get outsideLayer => _outsideLayer;
/// The [Shape] of the [RampOpening]. /// The [Shape] of the [RampOpening].
Shape get shape; Shape get shape;
@ -85,7 +91,7 @@ class RampOpeningBallContactCallback<Opening extends RampOpening>
@override @override
void end(Ball ball, Opening opening, Contact _) { void end(Ball ball, Opening opening, Contact _) {
if (!_ballsInside.contains(ball)) { if (!_ballsInside.contains(ball)) {
ball.layer = Layer.board; ball.layer = opening.outsideLayer;
} else { } else {
// TODO(ruimiguel): change this code. Check what happens with ball that // TODO(ruimiguel): change this code. Check what happens with ball that
// slightly touch Opening and goes out again. With InitialPosition change // slightly touch Opening and goes out again. With InitialPosition change
@ -97,7 +103,7 @@ class RampOpeningBallContactCallback<Opening extends RampOpening>
ball.body.linearVelocity.y > 0); ball.body.linearVelocity.y > 0);
if (isBallOutsideOpening) { if (isBallOutsideOpening) {
ball.layer = Layer.board; ball.layer = opening.outsideLayer;
_ballsInside.remove(ball); _ballsInside.remove(ball);
} }
} }

Loading…
Cancel
Save