refactor: moved sensor

feat/spaceship-ramp-logic
RuiAlonso 2 years ago
parent 8c40c98f46
commit 7349d04217

@ -15,14 +15,6 @@ class SpaceshipRamp extends Blueprint {
SpaceshipRamp() SpaceshipRamp()
: super( : super(
components: [ components: [
// TODO(ruimiguel): once blueprint contactcallback works well, add
// this sensors here.
/*
SpaceshipRampSensor(type: SpaceshipRampSensorType.door)
..initialPosition = Vector2(1.7, -20),
SpaceshipRampSensor(type: SpaceshipRampSensorType.inside)
..initialPosition = Vector2(1.7, -21.5),
*/
_SpaceshipRampOpening( _SpaceshipRampOpening(
outsidePriority: RenderPriority.ballOnBoard, outsidePriority: RenderPriority.ballOnBoard,
rotation: math.pi, rotation: math.pi,
@ -396,34 +388,39 @@ enum SpaceshipRampSensorType {
inside, inside,
} }
/// {@template spaceship_ramp_sensor} /// {@template spaceship_ramp_opening}
/// Small sensor body used to detect when a ball has entered the /// [LayerSensor] with [Layer.spaceshipEntranceRamp] to filter [Ball] collisions
/// [SpaceshipRamp]. /// inside [_SpaceshipRampBackground].
/// {@endtemplate} /// {@endtemplate}
class SpaceshipRampSensor extends BodyComponent with InitialPosition, Layered { class SpaceshipRampSensor extends BodyComponent with InitialPosition, Layered {
/// {@macro spaceship_ramp_sensor} /// {@macro spaceship_ramp_opening}
SpaceshipRampSensor({required this.type}) { SpaceshipRampSensor({required this.type}) : super() {
layer = Layer.spaceshipEntranceRamp; layer = Layer.spaceshipEntranceRamp;
renderBody = false; renderBody = false;
} }
final SpaceshipRampSensorType type; final SpaceshipRampSensorType type;
static final Vector2 _size = Vector2(_SpaceshipRampBackground.width / 3, .1);
@override @override
Body createBody() { Body createBody() {
final shape = PolygonShape() final shape = PolygonShape()
..setAsBox( ..setAsBox(
2, _size.x,
.1, _size.y,
initialPosition, initialPosition,
-5 * math.pi / 180, -5 * math.pi / 180,
); );
final fixtureDef = FixtureDef(shape)..isSensor = true; final fixtureDef = FixtureDef(
shape,
final bodyDef = BodyDef() isSensor: true,
..position = initialPosition );
..userData = this; final bodyDef = BodyDef(
position: initialPosition,
userData: this,
);
return world.createBody(bodyDef)..createFixture(fixtureDef); return world.createBody(bodyDef)..createFixture(fixtureDef);
} }

Loading…
Cancel
Save