From 99239bf85dec00f5f8085bad682a9e3e8430d14c Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Fri, 11 Mar 2022 10:54:29 +0100 Subject: [PATCH] fix: fixed collisions of a ball that only touch path entrance but doesn't get into --- lib/game/components/crossing_ramp.dart | 41 +++++++++++++++++++------- lib/game/components/jetpack_ramp.dart | 28 ++++++++++++------ lib/game/components/sparky_ramp.dart | 26 +++++++++++----- 3 files changed, 68 insertions(+), 27 deletions(-) diff --git a/lib/game/components/crossing_ramp.dart b/lib/game/components/crossing_ramp.dart index 637b13f3..6baa2015 100644 --- a/lib/game/components/crossing_ramp.dart +++ b/lib/game/components/crossing_ramp.dart @@ -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 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; } } } diff --git a/lib/game/components/jetpack_ramp.dart b/lib/game/components/jetpack_ramp.dart index 25e3f929..304d3901 100644 --- a/lib/game/components/jetpack_ramp.dart +++ b/lib/game/components/jetpack_ramp.dart @@ -16,7 +16,7 @@ class JetpackRamp extends PositionComponent with HasGameRef { Future 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 { 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 { + JetpackRampAreaCallback() : super(); + + final _ballsInsideJetpack = {}; + @override - Set get ballsInside => {}; + Set get ballsInside => _ballsInsideJetpack; } diff --git a/lib/game/components/sparky_ramp.dart b/lib/game/components/sparky_ramp.dart index 5ed718c2..d23f7561 100644 --- a/lib/game/components/sparky_ramp.dart +++ b/lib/game/components/sparky_ramp.dart @@ -27,11 +27,13 @@ class SparkyRamp extends PositionComponent with HasGameRef { 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 { + SparkyRampAreaCallback() : super(); + + final _ballsInsideSparky = {}; + @override - Set get ballsInside => {}; + Set get ballsInside => _ballsInsideSparky; }