From 04daf1327124f800e2bda0ca06b843af3a3a9552 Mon Sep 17 00:00:00 2001 From: alestiago Date: Wed, 16 Mar 2022 12:47:35 +0000 Subject: [PATCH] feat: made Pathway use InitialPosition --- lib/game/components/pathway.dart | 32 +++++++------------ test/game/components/pathway_test.dart | 44 ++++++++++---------------- 2 files changed, 28 insertions(+), 48 deletions(-) diff --git a/lib/game/components/pathway.dart b/lib/game/components/pathway.dart index d41ce3da..7174c9cd 100644 --- a/lib/game/components/pathway.dart +++ b/lib/game/components/pathway.dart @@ -2,20 +2,19 @@ import 'package:flame/extensions.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flutter/material.dart'; import 'package:geometry/geometry.dart'; +import 'package:pinball/game/game.dart'; /// {@template pathway} /// [Pathway] creates lines of various shapes. /// /// [BodyComponent]s such as a Ball can collide and move along a [Pathway]. /// {@endtemplate} -class Pathway extends BodyComponent { +class Pathway extends BodyComponent with InitialPosition { Pathway._({ // TODO(ruialonso): remove color when assets added. Color? color, - required Vector2 position, required List> paths, - }) : _position = position, - _paths = paths { + }) : _paths = paths { paint = Paint() ..color = color ?? const Color.fromARGB(0, 0, 0, 0) ..style = PaintingStyle.stroke; @@ -23,14 +22,12 @@ class Pathway extends BodyComponent { /// Creates a uniform unidirectional (straight) [Pathway]. /// - /// Does so with two [ChainShape] separated by a [width]. Placed - /// at a [position] between [start] and [end] points. Can + /// Does so with two [ChainShape] separated by a [width]. Can /// be rotated by a given [rotation] in radians. /// /// If [singleWall] is true, just one [ChainShape] is created. factory Pathway.straight({ Color? color, - required Vector2 position, required Vector2 start, required Vector2 end, required double width, @@ -56,28 +53,25 @@ class Pathway extends BodyComponent { return Pathway._( color: color, - position: position, paths: paths, ); } /// Creates an arc [Pathway]. /// - /// The [angle], in radians, specifies the size of the arc. For example, 2*pi - /// returns a complete circumference and minor angles a semi circumference. - /// - /// The center of the arc is placed at [position]. + /// The [angle], in radians, specifies the size of the arc. For example, two + /// pi returns a complete circumference. /// /// Does so with two [ChainShape] separated by a [width]. Which can be /// rotated by a given [rotation] in radians. /// /// The outer radius is specified by [radius], whilst the inner one is - /// equivalent to [radius] - [width]. + /// equivalent to the [radius] minus the [width]. /// /// If [singleWall] is true, just one [ChainShape] is created. factory Pathway.arc({ Color? color, - required Vector2 position, + required Vector2 center, required double width, required double radius, required double angle, @@ -88,7 +82,7 @@ class Pathway extends BodyComponent { // TODO(ruialonso): Refactor repetitive logic final outerWall = calculateArc( - center: position, + center: center, radius: radius, angle: angle, offsetAngle: rotation, @@ -97,7 +91,7 @@ class Pathway extends BodyComponent { if (!singleWall) { final innerWall = calculateArc( - center: position, + center: center, radius: radius - width, angle: angle, offsetAngle: rotation, @@ -107,7 +101,6 @@ class Pathway extends BodyComponent { return Pathway._( color: color, - position: position, paths: paths, ); } @@ -123,7 +116,6 @@ class Pathway extends BodyComponent { /// If [singleWall] is true, just one [ChainShape] is created. factory Pathway.bezierCurve({ Color? color, - required Vector2 position, required List controlPoints, required double width, double rotation = 0, @@ -148,19 +140,17 @@ class Pathway extends BodyComponent { return Pathway._( color: color, - position: position, paths: paths, ); } - final Vector2 _position; final List> _paths; @override Body createBody() { final bodyDef = BodyDef() ..type = BodyType.static - ..position = _position; + ..position = initialPosition; final body = world.createBody(bodyDef); for (final path in _paths) { diff --git a/test/game/components/pathway_test.dart b/test/game/components/pathway_test.dart index 80c968d8..dff87ba2 100644 --- a/test/game/components/pathway_test.dart +++ b/test/game/components/pathway_test.dart @@ -20,11 +20,10 @@ void main() { (game) async { await game.ready(); final pathway = Pathway.straight( - position: Vector2.zero(), start: Vector2(10, 10), end: Vector2(20, 20), width: width, - ); + )..initialPosition = Vector2.zero(); await game.ensureAdd(pathway); expect(game.contains(pathway), isTrue); @@ -44,11 +43,10 @@ void main() { final pathway = Pathway.straight( color: defaultColor, - position: Vector2.zero(), start: Vector2(10, 10), end: Vector2(20, 20), width: width, - ); + )..initialPosition = Vector2.zero(); await game.ensureAdd(pathway); expect(game.contains(pathway), isTrue); @@ -63,11 +61,10 @@ void main() { (game) async { await game.ready(); final pathway = Pathway.straight( - position: Vector2.zero(), start: Vector2(10, 10), end: Vector2(20, 20), width: width, - ); + )..initialPosition = Vector2.zero(); await game.ensureAdd(pathway); expect(game.contains(pathway), isTrue); @@ -81,15 +78,14 @@ void main() { await game.ready(); final position = Vector2.all(10); final pathway = Pathway.straight( - position: position, start: Vector2(10, 10), end: Vector2(20, 20), width: width, - ); + )..initialPosition = position; await game.ensureAdd(pathway); game.contains(pathway); - expect(pathway.body.position, position); + expect(pathway.body.position, equals(position)); }, ); @@ -98,11 +94,10 @@ void main() { (game) async { await game.ready(); final pathway = Pathway.straight( - position: Vector2.zero(), start: Vector2(10, 10), end: Vector2(20, 20), width: width, - ); + )..initialPosition = Vector2.zero(); await game.ensureAdd(pathway); expect(pathway.body.bodyType, equals(BodyType.static)); @@ -116,12 +111,11 @@ void main() { (game) async { await game.ready(); final pathway = Pathway.straight( - position: Vector2.zero(), start: Vector2(10, 10), end: Vector2(20, 20), width: width, singleWall: true, - ); + )..initialPosition = Vector2.zero(); await game.ensureAdd(pathway); expect(pathway.body.fixtures.length, 1); @@ -136,11 +130,10 @@ void main() { (game) async { await game.ready(); final pathway = Pathway.straight( - position: Vector2.zero(), start: Vector2(10, 10), end: Vector2(20, 20), width: width, - ); + )..initialPosition = Vector2.zero(); await game.ensureAdd(pathway); expect(pathway.body.fixtures.length, 2); @@ -159,11 +152,11 @@ void main() { (game) async { await game.ready(); final pathway = Pathway.arc( - position: Vector2.zero(), + center: Vector2.zero(), width: width, radius: 100, angle: math.pi / 2, - ); + )..initialPosition = Vector2.zero(); await game.ensureAdd(pathway); expect(game.contains(pathway), isTrue); @@ -177,11 +170,11 @@ void main() { await game.ready(); final position = Vector2.all(10); final pathway = Pathway.arc( - position: position, + center: position, width: width, radius: 100, angle: math.pi / 2, - ); + )..initialPosition = position; await game.ensureAdd(pathway); game.contains(pathway); @@ -194,11 +187,11 @@ void main() { (game) async { await game.ready(); final pathway = Pathway.arc( - position: Vector2.zero(), + center: Vector2.zero(), width: width, radius: 100, angle: math.pi / 2, - ); + )..initialPosition = Vector2.zero(); await game.ensureAdd(pathway); expect(pathway.body.bodyType, equals(BodyType.static)); @@ -220,10 +213,9 @@ void main() { (game) async { await game.ready(); final pathway = Pathway.bezierCurve( - position: Vector2.zero(), controlPoints: controlPoints, width: width, - ); + )..initialPosition = Vector2.zero(); await game.ensureAdd(pathway); expect(game.contains(pathway), isTrue); @@ -237,10 +229,9 @@ void main() { await game.ready(); final position = Vector2.all(10); final pathway = Pathway.bezierCurve( - position: position, controlPoints: controlPoints, width: width, - ); + )..initialPosition = position; await game.ensureAdd(pathway); game.contains(pathway); @@ -253,10 +244,9 @@ void main() { (game) async { await game.ready(); final pathway = Pathway.bezierCurve( - position: Vector2.zero(), controlPoints: controlPoints, width: width, - ); + )..initialPosition = Vector2.zero(); await game.ensureAdd(pathway); expect(pathway.body.bodyType, equals(BodyType.static));