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 'ball.dart';
export 'path.dart';
export 'pathway.dart';
export 'plunger.dart';
export 'score_points.dart';
export 'wall.dart';

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

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