refactor: changed name of Path to Pathway

pull/14/head
RuiAlonso 4 years ago
parent 20c8153363
commit 7e43e0042e

@ -1,6 +1,6 @@
export 'anchor.dart'; export 'anchor.dart';
export 'ball.dart'; export 'ball.dart';
export 'path.dart'; export 'pathway.dart';
export 'plunger.dart'; export 'plunger.dart';
export 'score_points.dart'; export 'score_points.dart';
export 'wall.dart'; export 'wall.dart';

@ -3,12 +3,12 @@ import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:maths/maths.dart'; import 'package:maths/maths.dart';
/// {@template path} /// {@template pathway}
/// [Path] creates different shapes that sets the pathways that ball can follow /// [Pathway] creates different shapes that sets the pathwayways that ball
/// or collide to like walls. /// can follow or collide to like walls.
/// {@endtemplate} /// {@endtemplate}
class Path extends BodyComponent { class Pathway extends BodyComponent {
Path._({ Pathway._({
Color? color, Color? color,
required Vector2 position, required Vector2 position,
required List<List<Vector2>> paths, required List<List<Vector2>> paths,
@ -19,18 +19,19 @@ class Path extends BodyComponent {
..style = PaintingStyle.stroke; ..style = PaintingStyle.stroke;
} }
/// {@macro path} /// {@macro pathway}
/// [Path.straight] creates a straight path for the ball given a [position] /// [Pathway.straight] creates a straight pathway for the ball given
/// for the body, between a [start] and [end] points. /// a [position] for the body, between a [start] and [end] points.
/// It creates two [ChainShape] separated by a [pathWidth]. If [singleWall] /// It creates two [ChainShape] separated by a [pathwayWidth]. If [singleWall]
/// is true, just one [ChainShape] is created (like a wall instead of a path) /// is true, just one [ChainShape] is created
/// The path could be rotated by [rotation] in degrees. /// (like a wall instead of a pathway)
factory Path.straight({ /// The pathway could be rotated by [rotation] in degrees.
factory Pathway.straight({
Color? color, Color? color,
required Vector2 position, required Vector2 position,
required Vector2 start, required Vector2 start,
required Vector2 end, required Vector2 end,
required double pathWidth, required double pathwayWidth,
double rotation = 0, double rotation = 0,
bool singleWall = false, bool singleWall = false,
}) { }) {
@ -43,32 +44,32 @@ class Path extends BodyComponent {
if (!singleWall) { if (!singleWall) {
final wall2 = [ final wall2 = [
start + Vector2(pathWidth, 0), start + Vector2(pathwayWidth, 0),
end + Vector2(pathWidth, 0), end + Vector2(pathwayWidth, 0),
]; ];
paths.add(wall2.map((e) => e..rotate(radians(rotation))).toList()); paths.add(wall2.map((e) => e..rotate(radians(rotation))).toList());
} }
return Path._( return Pathway._(
color: color, color: color,
position: position, position: position,
paths: paths, paths: paths,
); );
} }
/// {@macro path} /// {@macro pathway}
/// [Path.straight] creates an arc path for the ball given a [position] /// [Pathway.straight] creates an arc pathway for the ball given a [position]
/// for the body, a [radius] for the circumference and an [angle] to specify /// for the body, a [radius] for the circumference and an [angle] to specify
/// the size of the semi circumference. /// the size of the semi circumference.
/// It creates two [ChainShape] separated by a [pathWidth], like a circular /// It creates two [ChainShape] separated by a [pathwayWidth], like a circular
/// crown. The specified [radius] is for the outer arc, the inner one will /// crown. The specified [radius] is for the outer arc, the inner one will
/// have a radius of radius-pathWidth. /// have a radius of radius-pathwayWidth.
/// If [singleWall] is true, just one [ChainShape] is created. /// If [singleWall] is true, just one [ChainShape] is created.
/// The path could be rotated by [rotation] in degrees. /// The pathway could be rotated by [rotation] in degrees.
factory Path.arc({ factory Pathway.arc({
Color? color, Color? color,
required Vector2 position, required Vector2 position,
required double pathWidth, required double pathwayWidth,
required double radius, required double radius,
required double angle, required double angle,
double rotation = 0, double rotation = 0,
@ -85,7 +86,7 @@ class Path extends BodyComponent {
paths.add(wall1); paths.add(wall1);
if (!singleWall) { if (!singleWall) {
final minRadius = radius - pathWidth; final minRadius = radius - pathwayWidth;
final wall2 = calculateArc( final wall2 = calculateArc(
center: position, center: position,
@ -96,26 +97,27 @@ class Path extends BodyComponent {
paths.add(wall2); paths.add(wall2);
} }
return Path._( return Pathway._(
color: color, color: color,
position: position, position: position,
paths: paths, paths: paths,
); );
} }
/// {@macro path} /// {@macro pathway}
/// [Path.straight] creates a bezier curve path for the ball given a /// [Pathway.straight] creates a bezier curve pathway for the ball given a
/// [position] for the body, with control point specified by [controlPoints]. /// [position] for the body, with control point specified by [controlPoints].
/// First and last points set the beginning and end of the curve, all the /// First and last points set the beginning and end of the curve, all the
/// inner points between them set the bezier curve final shape. /// inner points between them set the bezier curve final shape.
/// It creates two [ChainShape] separated by a [pathWidth]. If [singleWall] /// It creates two [ChainShape] separated by a [pathwayWidth]. If [singleWall]
/// is true, just one [ChainShape] is created (like a wall instead of a path) /// is true, just one [ChainShape] is created
/// The path could be rotated by [rotation] in degrees. /// (like a wall instead of a pathway)
factory Path.bezierCurve({ /// The pathway could be rotated by [rotation] in degrees.
factory Pathway.bezierCurve({
Color? color, Color? color,
required Vector2 position, required Vector2 position,
required List<Vector2> controlPoints, required List<Vector2> controlPoints,
required double pathWidth, required double pathwayWidth,
double rotation = 0, double rotation = 0,
bool singleWall = false, bool singleWall = false,
}) { }) {
@ -128,13 +130,13 @@ class Path extends BodyComponent {
if (!singleWall) { if (!singleWall) {
wall2 = calculateBezierCurve( wall2 = calculateBezierCurve(
controlPoints: controlPoints controlPoints: controlPoints
.map((e) => e + Vector2(pathWidth, -pathWidth)) .map((e) => e + Vector2(pathwayWidth, -pathwayWidth))
.toList(), .toList(),
); );
paths.add(wall2.map((e) => e..rotate(radians(rotation))).toList()); paths.add(wall2.map((e) => e..rotate(radians(rotation))).toList());
} }
return Path._( return Pathway._(
color: color, color: color,
position: position, position: position,
paths: paths, paths: paths,

@ -9,25 +9,25 @@ void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(PinballGame.new); final flameTester = FlameTester(PinballGame.new);
group('Path', () { group('Pathway', () {
const pathWidth = 50.0; const pathwayWidth = 50.0;
group('straight', () { group('straight', () {
group('color', () { group('color', () {
flameTester.test( flameTester.test(
'has transparent color by default if not specified', 'has transparent color by default if not specified',
(game) async { (game) async {
final path = Path.straight( final pathway = Pathway.straight(
position: Vector2.zero(), position: Vector2.zero(),
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
pathWidth: pathWidth, pathwayWidth: pathwayWidth,
); );
await game.ensureAdd(path); await game.ensureAdd(pathway);
expect(game.contains(path), isTrue); expect(game.contains(pathway), isTrue);
expect(path.paint, isNotNull); expect(pathway.paint, isNotNull);
expect( expect(
path.paint.color, pathway.paint.color,
equals(Color.fromARGB(0, 0, 0, 0)), equals(Color.fromARGB(0, 0, 0, 0)),
); );
}, },
@ -37,17 +37,17 @@ void main() {
(game) async { (game) async {
const defaultColor = Colors.blue; const defaultColor = Colors.blue;
final path = Path.straight( final pathway = Pathway.straight(
color: defaultColor, color: defaultColor,
position: Vector2.zero(), position: Vector2.zero(),
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
pathWidth: pathWidth, pathwayWidth: pathwayWidth,
); );
await game.ensureAdd(path); await game.ensureAdd(pathway);
expect(game.contains(path), isTrue); expect(game.contains(pathway), isTrue);
expect(path.paint, isNotNull); expect(pathway.paint, isNotNull);
expect(path.paint.color.value, equals(defaultColor.value)); expect(pathway.paint.color.value, equals(defaultColor.value));
}, },
); );
}); });
@ -55,14 +55,14 @@ void main() {
flameTester.test( flameTester.test(
'loads correctly', 'loads correctly',
(game) async { (game) async {
final path = Path.straight( final pathway = Pathway.straight(
position: Vector2.zero(), position: Vector2.zero(),
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
pathWidth: pathWidth, pathwayWidth: pathwayWidth,
); );
await game.ensureAdd(path); await game.ensureAdd(pathway);
expect(game.contains(path), isTrue); expect(game.contains(pathway), isTrue);
}, },
); );
@ -71,31 +71,31 @@ void main() {
'positions correctly', 'positions correctly',
(game) async { (game) async {
final position = Vector2.all(10); final position = Vector2.all(10);
final path = Path.straight( final pathway = Pathway.straight(
position: position, position: position,
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
pathWidth: pathWidth, pathwayWidth: pathwayWidth,
); );
await game.ensureAdd(path); await game.ensureAdd(pathway);
game.contains(path); game.contains(pathway);
expect(path.body.position, position); expect(pathway.body.position, position);
}, },
); );
flameTester.test( flameTester.test(
'is static', 'is static',
(game) async { (game) async {
final path = Path.straight( final pathway = Pathway.straight(
position: Vector2.zero(), position: Vector2.zero(),
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
pathWidth: pathWidth, pathwayWidth: pathwayWidth,
); );
await game.ensureAdd(path); await game.ensureAdd(pathway);
expect(path.body.bodyType, equals(BodyType.static)); expect(pathway.body.bodyType, equals(BodyType.static));
}, },
); );
}); });
@ -104,17 +104,17 @@ void main() {
flameTester.test( flameTester.test(
'exists only one ChainShape if just one wall', 'exists only one ChainShape if just one wall',
(game) async { (game) async {
final path = Path.straight( final pathway = Pathway.straight(
position: Vector2.zero(), position: Vector2.zero(),
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
pathWidth: pathWidth, pathwayWidth: pathwayWidth,
singleWall: true, singleWall: true,
); );
await game.ensureAdd(path); await game.ensureAdd(pathway);
expect(path.body.fixtures.length, 1); expect(pathway.body.fixtures.length, 1);
final fixture = path.body.fixtures[0]; final fixture = pathway.body.fixtures[0];
expect(fixture, isA<Fixture>()); expect(fixture, isA<Fixture>());
expect(fixture.shape.shapeType, equals(ShapeType.chain)); expect(fixture.shape.shapeType, equals(ShapeType.chain));
}, },
@ -123,16 +123,16 @@ void main() {
flameTester.test( flameTester.test(
'exists two ChainShape if there is by default two walls', 'exists two ChainShape if there is by default two walls',
(game) async { (game) async {
final path = Path.straight( final pathway = Pathway.straight(
position: Vector2.zero(), position: Vector2.zero(),
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
pathWidth: pathWidth, pathwayWidth: pathwayWidth,
); );
await game.ensureAdd(path); await game.ensureAdd(pathway);
expect(path.body.fixtures.length, 2); expect(pathway.body.fixtures.length, 2);
for (final fixture in path.body.fixtures) { for (final fixture in pathway.body.fixtures) {
expect(fixture, isA<Fixture>()); expect(fixture, isA<Fixture>());
expect(fixture.shape.shapeType, equals(ShapeType.chain)); expect(fixture.shape.shapeType, equals(ShapeType.chain));
} }
@ -145,14 +145,14 @@ void main() {
flameTester.test( flameTester.test(
'loads correctly', 'loads correctly',
(game) async { (game) async {
final path = Path.arc( final pathway = Pathway.arc(
position: Vector2.zero(), position: Vector2.zero(),
pathWidth: pathWidth, pathwayWidth: pathwayWidth,
radius: 100, radius: 100,
angle: 90, angle: 90,
); );
await game.ensureAdd(path); await game.ensureAdd(pathway);
expect(game.contains(path), isTrue); expect(game.contains(pathway), isTrue);
}, },
); );
@ -161,31 +161,31 @@ void main() {
'positions correctly', 'positions correctly',
(game) async { (game) async {
final position = Vector2.all(10); final position = Vector2.all(10);
final path = Path.arc( final pathway = Pathway.arc(
position: position, position: position,
pathWidth: pathWidth, pathwayWidth: pathwayWidth,
radius: 100, radius: 100,
angle: 90, angle: 90,
); );
await game.ensureAdd(path); await game.ensureAdd(pathway);
game.contains(path); game.contains(pathway);
expect(path.body.position, position); expect(pathway.body.position, position);
}, },
); );
flameTester.test( flameTester.test(
'is static', 'is static',
(game) async { (game) async {
final path = Path.arc( final pathway = Pathway.arc(
position: Vector2.zero(), position: Vector2.zero(),
pathWidth: pathWidth, pathwayWidth: pathwayWidth,
radius: 100, radius: 100,
angle: 90, angle: 90,
); );
await game.ensureAdd(path); await game.ensureAdd(pathway);
expect(path.body.bodyType, equals(BodyType.static)); expect(pathway.body.bodyType, equals(BodyType.static));
}, },
); );
}); });
@ -202,13 +202,13 @@ void main() {
flameTester.test( flameTester.test(
'loads correctly', 'loads correctly',
(game) async { (game) async {
final path = Path.bezierCurve( final pathway = Pathway.bezierCurve(
position: Vector2.zero(), position: Vector2.zero(),
controlPoints: controlPoints, controlPoints: controlPoints,
pathWidth: pathWidth, pathwayWidth: pathwayWidth,
); );
await game.ensureAdd(path); await game.ensureAdd(pathway);
expect(game.contains(path), isTrue); expect(game.contains(pathway), isTrue);
}, },
); );
@ -217,29 +217,29 @@ void main() {
'positions correctly', 'positions correctly',
(game) async { (game) async {
final position = Vector2.all(10); final position = Vector2.all(10);
final path = Path.bezierCurve( final pathway = Pathway.bezierCurve(
position: position, position: position,
controlPoints: controlPoints, controlPoints: controlPoints,
pathWidth: pathWidth, pathwayWidth: pathwayWidth,
); );
await game.ensureAdd(path); await game.ensureAdd(pathway);
game.contains(path); game.contains(pathway);
expect(path.body.position, position); expect(pathway.body.position, position);
}, },
); );
flameTester.test( flameTester.test(
'is static', 'is static',
(game) async { (game) async {
final path = Path.bezierCurve( final pathway = Pathway.bezierCurve(
position: Vector2.zero(), position: Vector2.zero(),
controlPoints: controlPoints, controlPoints: controlPoints,
pathWidth: pathWidth, pathwayWidth: pathwayWidth,
); );
await game.ensureAdd(path); await game.ensureAdd(pathway);
expect(path.body.bodyType, equals(BodyType.static)); expect(pathway.body.bodyType, equals(BodyType.static));
}, },
); );
}); });

Loading…
Cancel
Save