feat: made Pathway use InitialPosition

pull/50/head
alestiago 4 years ago
parent de2b73247a
commit 04daf13271

@ -2,20 +2,19 @@ import 'package:flame/extensions.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:geometry/geometry.dart'; import 'package:geometry/geometry.dart';
import 'package:pinball/game/game.dart';
/// {@template pathway} /// {@template pathway}
/// [Pathway] creates lines of various shapes. /// [Pathway] creates lines of various shapes.
/// ///
/// [BodyComponent]s such as a Ball can collide and move along a [Pathway]. /// [BodyComponent]s such as a Ball can collide and move along a [Pathway].
/// {@endtemplate} /// {@endtemplate}
class Pathway extends BodyComponent { class Pathway extends BodyComponent with InitialPosition {
Pathway._({ Pathway._({
// TODO(ruialonso): remove color when assets added. // TODO(ruialonso): remove color when assets added.
Color? color, Color? color,
required Vector2 position,
required List<List<Vector2>> paths, required List<List<Vector2>> paths,
}) : _position = position, }) : _paths = paths {
_paths = paths {
paint = Paint() paint = Paint()
..color = color ?? const Color.fromARGB(0, 0, 0, 0) ..color = color ?? const Color.fromARGB(0, 0, 0, 0)
..style = PaintingStyle.stroke; ..style = PaintingStyle.stroke;
@ -23,14 +22,12 @@ class Pathway extends BodyComponent {
/// Creates a uniform unidirectional (straight) [Pathway]. /// Creates a uniform unidirectional (straight) [Pathway].
/// ///
/// Does so with two [ChainShape] separated by a [width]. Placed /// Does so with two [ChainShape] separated by a [width]. Can
/// at a [position] between [start] and [end] points. Can
/// be rotated by a given [rotation] in radians. /// be rotated by a given [rotation] in radians.
/// ///
/// If [singleWall] is true, just one [ChainShape] is created. /// If [singleWall] is true, just one [ChainShape] is created.
factory Pathway.straight({ factory Pathway.straight({
Color? color, Color? color,
required Vector2 position,
required Vector2 start, required Vector2 start,
required Vector2 end, required Vector2 end,
required double width, required double width,
@ -56,28 +53,25 @@ class Pathway extends BodyComponent {
return Pathway._( return Pathway._(
color: color, color: color,
position: position,
paths: paths, paths: paths,
); );
} }
/// Creates an arc [Pathway]. /// Creates an arc [Pathway].
/// ///
/// The [angle], in radians, specifies the size of the arc. For example, 2*pi /// The [angle], in radians, specifies the size of the arc. For example, two
/// returns a complete circumference and minor angles a semi circumference. /// pi returns a complete circumference.
///
/// The center of the arc is placed at [position].
/// ///
/// Does so with two [ChainShape] separated by a [width]. Which can be /// Does so with two [ChainShape] separated by a [width]. Which can be
/// rotated by a given [rotation] in radians. /// rotated by a given [rotation] in radians.
/// ///
/// The outer radius is specified by [radius], whilst the inner one is /// 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. /// If [singleWall] is true, just one [ChainShape] is created.
factory Pathway.arc({ factory Pathway.arc({
Color? color, Color? color,
required Vector2 position, required Vector2 center,
required double width, required double width,
required double radius, required double radius,
required double angle, required double angle,
@ -88,7 +82,7 @@ class Pathway extends BodyComponent {
// TODO(ruialonso): Refactor repetitive logic // TODO(ruialonso): Refactor repetitive logic
final outerWall = calculateArc( final outerWall = calculateArc(
center: position, center: center,
radius: radius, radius: radius,
angle: angle, angle: angle,
offsetAngle: rotation, offsetAngle: rotation,
@ -97,7 +91,7 @@ class Pathway extends BodyComponent {
if (!singleWall) { if (!singleWall) {
final innerWall = calculateArc( final innerWall = calculateArc(
center: position, center: center,
radius: radius - width, radius: radius - width,
angle: angle, angle: angle,
offsetAngle: rotation, offsetAngle: rotation,
@ -107,7 +101,6 @@ class Pathway extends BodyComponent {
return Pathway._( return Pathway._(
color: color, color: color,
position: position,
paths: paths, paths: paths,
); );
} }
@ -123,7 +116,6 @@ class Pathway extends BodyComponent {
/// If [singleWall] is true, just one [ChainShape] is created. /// If [singleWall] is true, just one [ChainShape] is created.
factory Pathway.bezierCurve({ factory Pathway.bezierCurve({
Color? color, Color? color,
required Vector2 position,
required List<Vector2> controlPoints, required List<Vector2> controlPoints,
required double width, required double width,
double rotation = 0, double rotation = 0,
@ -148,19 +140,17 @@ class Pathway extends BodyComponent {
return Pathway._( return Pathway._(
color: color, color: color,
position: position,
paths: paths, paths: paths,
); );
} }
final Vector2 _position;
final List<List<Vector2>> _paths; final List<List<Vector2>> _paths;
@override @override
Body createBody() { Body createBody() {
final bodyDef = BodyDef() final bodyDef = BodyDef()
..type = BodyType.static ..type = BodyType.static
..position = _position; ..position = initialPosition;
final body = world.createBody(bodyDef); final body = world.createBody(bodyDef);
for (final path in _paths) { for (final path in _paths) {

@ -20,11 +20,10 @@ void main() {
(game) async { (game) async {
await game.ready(); await game.ready();
final pathway = Pathway.straight( final pathway = Pathway.straight(
position: Vector2.zero(),
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
width: width, width: width,
); )..initialPosition = Vector2.zero();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(game.contains(pathway), isTrue); expect(game.contains(pathway), isTrue);
@ -44,11 +43,10 @@ void main() {
final pathway = Pathway.straight( final pathway = Pathway.straight(
color: defaultColor, color: defaultColor,
position: Vector2.zero(),
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
width: width, width: width,
); )..initialPosition = Vector2.zero();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(game.contains(pathway), isTrue); expect(game.contains(pathway), isTrue);
@ -63,11 +61,10 @@ void main() {
(game) async { (game) async {
await game.ready(); await game.ready();
final pathway = Pathway.straight( final pathway = Pathway.straight(
position: Vector2.zero(),
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
width: width, width: width,
); )..initialPosition = Vector2.zero();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(game.contains(pathway), isTrue); expect(game.contains(pathway), isTrue);
@ -81,15 +78,14 @@ void main() {
await game.ready(); await game.ready();
final position = Vector2.all(10); final position = Vector2.all(10);
final pathway = Pathway.straight( final pathway = Pathway.straight(
position: position,
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
width: width, width: width,
); )..initialPosition = position;
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
game.contains(pathway); game.contains(pathway);
expect(pathway.body.position, position); expect(pathway.body.position, equals(position));
}, },
); );
@ -98,11 +94,10 @@ void main() {
(game) async { (game) async {
await game.ready(); await game.ready();
final pathway = Pathway.straight( final pathway = Pathway.straight(
position: Vector2.zero(),
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
width: width, width: width,
); )..initialPosition = Vector2.zero();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(pathway.body.bodyType, equals(BodyType.static)); expect(pathway.body.bodyType, equals(BodyType.static));
@ -116,12 +111,11 @@ void main() {
(game) async { (game) async {
await game.ready(); await game.ready();
final pathway = Pathway.straight( final pathway = Pathway.straight(
position: Vector2.zero(),
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
width: width, width: width,
singleWall: true, singleWall: true,
); )..initialPosition = Vector2.zero();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(pathway.body.fixtures.length, 1); expect(pathway.body.fixtures.length, 1);
@ -136,11 +130,10 @@ void main() {
(game) async { (game) async {
await game.ready(); await game.ready();
final pathway = Pathway.straight( final pathway = Pathway.straight(
position: Vector2.zero(),
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
width: width, width: width,
); )..initialPosition = Vector2.zero();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(pathway.body.fixtures.length, 2); expect(pathway.body.fixtures.length, 2);
@ -159,11 +152,11 @@ void main() {
(game) async { (game) async {
await game.ready(); await game.ready();
final pathway = Pathway.arc( final pathway = Pathway.arc(
position: Vector2.zero(), center: Vector2.zero(),
width: width, width: width,
radius: 100, radius: 100,
angle: math.pi / 2, angle: math.pi / 2,
); )..initialPosition = Vector2.zero();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(game.contains(pathway), isTrue); expect(game.contains(pathway), isTrue);
@ -177,11 +170,11 @@ void main() {
await game.ready(); await game.ready();
final position = Vector2.all(10); final position = Vector2.all(10);
final pathway = Pathway.arc( final pathway = Pathway.arc(
position: position, center: position,
width: width, width: width,
radius: 100, radius: 100,
angle: math.pi / 2, angle: math.pi / 2,
); )..initialPosition = position;
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
game.contains(pathway); game.contains(pathway);
@ -194,11 +187,11 @@ void main() {
(game) async { (game) async {
await game.ready(); await game.ready();
final pathway = Pathway.arc( final pathway = Pathway.arc(
position: Vector2.zero(), center: Vector2.zero(),
width: width, width: width,
radius: 100, radius: 100,
angle: math.pi / 2, angle: math.pi / 2,
); )..initialPosition = Vector2.zero();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(pathway.body.bodyType, equals(BodyType.static)); expect(pathway.body.bodyType, equals(BodyType.static));
@ -220,10 +213,9 @@ void main() {
(game) async { (game) async {
await game.ready(); await game.ready();
final pathway = Pathway.bezierCurve( final pathway = Pathway.bezierCurve(
position: Vector2.zero(),
controlPoints: controlPoints, controlPoints: controlPoints,
width: width, width: width,
); )..initialPosition = Vector2.zero();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(game.contains(pathway), isTrue); expect(game.contains(pathway), isTrue);
@ -237,10 +229,9 @@ void main() {
await game.ready(); await game.ready();
final position = Vector2.all(10); final position = Vector2.all(10);
final pathway = Pathway.bezierCurve( final pathway = Pathway.bezierCurve(
position: position,
controlPoints: controlPoints, controlPoints: controlPoints,
width: width, width: width,
); )..initialPosition = position;
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
game.contains(pathway); game.contains(pathway);
@ -253,10 +244,9 @@ void main() {
(game) async { (game) async {
await game.ready(); await game.ready();
final pathway = Pathway.bezierCurve( final pathway = Pathway.bezierCurve(
position: Vector2.zero(),
controlPoints: controlPoints, controlPoints: controlPoints,
width: width, width: width,
); )..initialPosition = Vector2.zero();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(pathway.body.bodyType, equals(BodyType.static)); expect(pathway.body.bodyType, equals(BodyType.static));

Loading…
Cancel
Save