diff --git a/lib/game/components/sling_shot.dart b/lib/game/components/sling_shot.dart index 2e77b69a..2aa9b478 100644 --- a/lib/game/components/sling_shot.dart +++ b/lib/game/components/sling_shot.dart @@ -13,7 +13,9 @@ class SlingShot extends BodyComponent { /// {@macro sling_shot} SlingShot({ required Vector2 position, - }) : _position = position { + required BoardSide side, + }) : _position = position, + _side = side { // TODO(alestiago): Use sprite instead of color when provided. paint = Paint() ..color = const Color(0xFF00FF00) @@ -23,6 +25,13 @@ class SlingShot extends BodyComponent { /// The initial position of the [SlingShot] body. final Vector2 _position; + /// Whether the [SlingShot] is on the left or right side of the board. + /// + /// A [SlingShot] with [BoardSide.left] propels the [Ball] to the right, + /// whereas a [SlingShot] with [BoardSide.right] propells the [Ball] to the + /// left. + final BoardSide _side; + List _createFixtureDefs() { final fixtures = []; @@ -30,17 +39,17 @@ class SlingShot extends BodyComponent { // once a sprite is given. final size = Vector2(10, 10); - const additionalIncrement = 2.5; + // TODO(alestiago): This magic number can be deduced by specifying the + // angle and using polar coordinate system to place the bottom right + // vertex. + // Something as: y = -size.y * math.cos(angle) + const additionalIncrement = 2; final triangleVertices = [ - Vector2(0, 0), - Vector2(0, -size.y), + Vector2(_side.isLeft ? 0 : size.x, 0), + Vector2(_side.isLeft ? 0 : size.x, -size.y), Vector2( - size.x, + _side.isLeft ? size.x : 0, -size.y - additionalIncrement, - // TODO(alestiago): This magic number can be deduced by specifying the - // angle and using polar coordinate system to place the bottom right - // vertex. - // Something as: y = -size.y * math.cos(angle) ), ]; final triangleCentroid = centroid(triangleVertices); @@ -49,8 +58,8 @@ class SlingShot extends BodyComponent { } final triangle = PolygonShape()..set(triangleVertices); - final triangleFixture = FixtureDef(triangle)..friction = 0; - fixtures.add(triangleFixture); + final triangleFixtureDef = FixtureDef(triangle)..friction = 0; + fixtures.add(triangleFixtureDef); final kicker = EdgeShape() ..set( diff --git a/test/game/components/sling_shot_test.dart b/test/game/components/sling_shot_test.dart index d617b3e2..273dbe47 100644 --- a/test/game/components/sling_shot_test.dart +++ b/test/game/components/sling_shot_test.dart @@ -14,7 +14,10 @@ void main() { flameTester.test( 'loads correctly', (game) async { - final slingShot = SlingShot(position: Vector2.zero()); + final slingShot = SlingShot( + position: Vector2.zero(), + side: BoardSide.left, + ); await game.ensureAdd(slingShot); expect(game.contains(slingShot), isTrue); @@ -26,7 +29,10 @@ void main() { 'positions correctly', (game) async { final position = Vector2.all(10); - final slingShot = SlingShot(position: position); + final slingShot = SlingShot( + position: position, + side: BoardSide.left, + ); await game.ensureAdd(slingShot); expect(slingShot.body.position, equals(position)); @@ -36,7 +42,10 @@ void main() { flameTester.test( 'is static', (game) async { - final slingShot = SlingShot(position: Vector2.zero()); + final slingShot = SlingShot( + position: Vector2.zero(), + side: BoardSide.left, + ); await game.ensureAdd(slingShot); expect(slingShot.body.bodyType, equals(BodyType.static)); @@ -48,7 +57,10 @@ void main() { flameTester.test( 'exists', (game) async { - final slingShot = SlingShot(position: Vector2.zero()); + final slingShot = SlingShot( + position: Vector2.zero(), + side: BoardSide.left, + ); await game.ensureAdd(slingShot); expect(slingShot.body.fixtures[0], isA()); @@ -58,7 +70,10 @@ void main() { flameTester.test( 'shape is triangular', (game) async { - final slingShot = SlingShot(position: Vector2.zero()); + final slingShot = SlingShot( + position: Vector2.zero(), + side: BoardSide.left, + ); await game.ensureAdd(slingShot); final fixture = slingShot.body.fixtures[0]; @@ -70,7 +85,10 @@ void main() { flameTester.test( 'has no friction', (game) async { - final slingShot = SlingShot(position: Vector2.zero()); + final slingShot = SlingShot( + position: Vector2.zero(), + side: BoardSide.left, + ); await game.ensureAdd(slingShot); final fixture = slingShot.body.fixtures[0]; @@ -83,7 +101,10 @@ void main() { flameTester.test( 'exists', (game) async { - final slingShot = SlingShot(position: Vector2.zero()); + final slingShot = SlingShot( + position: Vector2.zero(), + side: BoardSide.left, + ); await game.ensureAdd(slingShot); expect(slingShot.body.fixtures[1], isA()); @@ -93,7 +114,10 @@ void main() { flameTester.test( 'shape is edge', (game) async { - final slingShot = SlingShot(position: Vector2.zero()); + final slingShot = SlingShot( + position: Vector2.zero(), + side: BoardSide.left, + ); await game.ensureAdd(slingShot); final fixture = slingShot.body.fixtures[1]; @@ -104,7 +128,10 @@ void main() { flameTester.test( 'has restitution', (game) async { - final slingShot = SlingShot(position: Vector2.zero()); + final slingShot = SlingShot( + position: Vector2.zero(), + side: BoardSide.left, + ); await game.ensureAdd(slingShot); final fixture = slingShot.body.fixtures[1]; @@ -115,7 +142,10 @@ void main() { flameTester.test( 'has no friction', (game) async { - final slingShot = SlingShot(position: Vector2.zero()); + final slingShot = SlingShot( + position: Vector2.zero(), + side: BoardSide.left, + ); await game.ensureAdd(slingShot); final fixture = slingShot.body.fixtures[1];