From 28977c7adfd2eaa2b41354511a5c96d59b3b8bf4 Mon Sep 17 00:00:00 2001 From: DevKage Date: Sat, 13 Jul 2024 22:12:12 +0530 Subject: [PATCH] Use named parameters --- packages/benchmark/web/bench2d.dart | 4 +- packages/forge2d/example/web/ball_cage.dart | 20 +++--- packages/forge2d/example/web/blob_test.dart | 2 +- packages/forge2d/example/web/box_test.dart | 11 ++- .../forge2d/example/web/circle_stress.dart | 71 ++++++++----------- packages/forge2d/example/web/domino_test.dart | 16 ++--- .../forge2d/example/web/domino_tower.dart | 36 +++++----- .../example/web/friction_joint_test.dart | 18 ++--- packages/forge2d/example/web/particles.dart | 20 +++--- packages/forge2d/example/web/racer.dart | 8 ++- packages/forge2d/lib/src/dynamics/body.dart | 10 +-- packages/forge2d/test/dynamics/body_test.dart | 15 ++-- .../test/dynamics/joints/joint_test.dart | 15 ++-- 13 files changed, 117 insertions(+), 129 deletions(-) diff --git a/packages/benchmark/web/bench2d.dart b/packages/benchmark/web/bench2d.dart index c5b3dd76..232a92d7 100644 --- a/packages/benchmark/web/bench2d.dart +++ b/packages/benchmark/web/bench2d.dart @@ -64,9 +64,7 @@ class Bench2d { y.setFrom(x); for (var j = i; j < pyramidSize; ++j) { - final bd = BodyDef() - ..type = BodyType.dynamic - ..position.setFrom(y); + final bd = BodyDef(type: BodyType.dynamic, position: y.clone()); world.createBody(bd).createFixtureFromShape(shape, density: 5.0); y.add(deltaY); } diff --git a/packages/forge2d/example/web/ball_cage.dart b/packages/forge2d/example/web/ball_cage.dart index ee43dcd5..faebc9fd 100644 --- a/packages/forge2d/example/web/ball_cage.dart +++ b/packages/forge2d/example/web/ball_cage.dart @@ -27,12 +27,14 @@ class BallCage extends Demo { @override void initialize() { // Define the circle shape. - final circleShape = CircleShape()..radius = wallBallRadius; + final circleShape = CircleShape(radius: wallBallRadius); // Create fixture using the circle shape. - final circleFixtureDef = FixtureDef(circleShape) - ..friction = .9 - ..restitution = 1.0; + final circleFixtureDef = FixtureDef( + circleShape, + friction: .9, + restitution: 1.0, + ); // Create a body def. final circleBodyDef = BodyDef(); @@ -67,12 +69,14 @@ class BallCage extends Demo { } // Create a bouncing ball. - final bouncingCircle = CircleShape()..radius = activeBallRadius; + final bouncingCircle = CircleShape(radius: activeBallRadius); // Create fixture for that ball shape. - final activeFixtureDef = FixtureDef(bouncingCircle) - ..restitution = 1.0 - ..density = 0.05; + final activeFixtureDef = FixtureDef( + bouncingCircle, + restitution: 1.0, + density: 0.05, + ); // Create the active ball body. final activeBodyDef = BodyDef(); diff --git a/packages/forge2d/example/web/blob_test.dart b/packages/forge2d/example/web/blob_test.dart index fa193377..bc267236 100644 --- a/packages/forge2d/example/web/blob_test.dart +++ b/packages/forge2d/example/web/blob_test.dart @@ -53,7 +53,7 @@ class BlobTest extends Demo { bd.type = BodyType.dynamic; final body = world.createBody(bd); - final shape = CircleShape()..radius = bodyRadius; + final shape = CircleShape(radius: bodyRadius); final fixtureDef = FixtureDef(shape) ..density = 1.0 ..filter.groupIndex = -2; diff --git a/packages/forge2d/example/web/box_test.dart b/packages/forge2d/example/web/box_test.dart index 65a412b4..29edfcce 100644 --- a/packages/forge2d/example/web/box_test.dart +++ b/packages/forge2d/example/web/box_test.dart @@ -51,14 +51,13 @@ class BoxTest extends Demo { shape.setAsBox(3.0, 1.5, Vector2.zero(), pi / 2); // Define fixture (links body and shape) - final activeFixtureDef = FixtureDef(shape) - ..restitution = 0.5 - ..density = 0.05; + final activeFixtureDef = FixtureDef(shape, restitution: 0.5, density: 0.05); // Define body - final bodyDef = BodyDef(); - bodyDef.type = BodyType.dynamic; - bodyDef.position = Vector2(0.0, 30.0); + final bodyDef = BodyDef( + type: BodyType.dynamic, + position: Vector2(0.0, 30.0), + ); // Create body and fixture from definitions final fallingBox = world.createBody(bodyDef); diff --git a/packages/forge2d/example/web/circle_stress.dart b/packages/forge2d/example/web/circle_stress.dart index ee6075d0..2c9ea6fc 100644 --- a/packages/forge2d/example/web/circle_stress.dart +++ b/packages/forge2d/example/web/circle_stress.dart @@ -31,17 +31,15 @@ class CircleStress extends Demo { // Ground final shape = PolygonShape(); shape.setAsBoxXY(50.0, 10.0); - final bodyDef = BodyDef() - ..type = BodyType.static - ..position = Vector2(0.0, -10.0); + final bodyDef = BodyDef(position: Vector2(0.0, -10.0)); final body = world.createBody(bodyDef); bodies.add(body); - final fixtureDef = FixtureDef(shape)..friction = 1.0; + final fixtureDef = FixtureDef(shape, friction: 1.0); body.createFixture(fixtureDef); // Walls shape.setAsBoxXY(3.0, 50.0); - final wallDef = BodyDef()..position = Vector2(45.0, 25.0); + final wallDef = BodyDef(position: Vector2(45.0, 25.0)); final rightWall = world.createBody(wallDef); bodies.add(rightWall); rightWall.createFixtureFromShape(shape); @@ -51,10 +49,11 @@ class CircleStress extends Demo { leftWall.createFixtureFromShape(shape); // Corners - final cornerDef = BodyDef(); + final cornerDef = BodyDef( + angle: -pi / 4.0, + position: Vector2(-35.0, 8.0), + ); shape.setAsBoxXY(20.0, 3.0); - cornerDef.angle = -pi / 4.0; - cornerDef.position = Vector2(-35.0, 8.0); var myBod = world.createBody(cornerDef); bodies.add(myBod); myBod.createFixtureFromShape(shape); @@ -66,10 +65,7 @@ class CircleStress extends Demo { // top shape.setAsBoxXY(50.0, 10.0); - final topDef = BodyDef() - ..type = BodyType.static - ..angle = 0.0 - ..position = Vector2(0.0, 75.0); + final topDef = BodyDef(position: Vector2(0.0, 75.0)); final topBody = world.createBody(topDef); bodies.add(topBody); fixtureDef.shape = shape; @@ -85,22 +81,14 @@ class CircleStress extends Demo { ); shape.createChain(vertices); - final fixtureDef = FixtureDef(shape) - ..restitution = 0.0 - ..friction = 0.1; - - final bodyDef = BodyDef() - ..position = Vector2.zero() - ..type = BodyType.static; - + final fixtureDef = FixtureDef(shape, friction: 0.1); + final bodyDef = BodyDef(position: Vector2.zero()); final body = world.createBody(bodyDef)..createFixture(fixtureDef); bodies.add(body); } { - final bd = BodyDef() - ..type = BodyType.dynamic - ..position = Vector2(0.0, 10.0); + final bd = BodyDef(type: BodyType.dynamic, position: Vector2(0.0, 10.0)); const numPieces = 5; const radius = 6.0; final body = world.createBody(bd); @@ -110,14 +98,13 @@ class CircleStress extends Demo { final xPos = radius * cos(2 * pi * (i / numPieces.toDouble())); final yPos = radius * sin(2 * pi * (i / numPieces.toDouble())); - final shape = CircleShape() - ..radius = 1.2 - ..position.setValues(xPos, yPos); - - final fixtureDef = FixtureDef(shape) - ..density = 25.0 - ..friction = .1 - ..restitution = .9; + final shape = CircleShape(radius: 1.2, position: Vector2(xPos, yPos)); + final fixtureDef = FixtureDef( + shape, + density: 25.0, + friction: 0.1, + restitution: 0.9, + ); body.createFixture(fixtureDef); } @@ -139,17 +126,21 @@ class CircleStress extends Demo { for (var j = 0; j < columns; j++) { for (var i = 0; i < loadSize; i++) { - final shape = CircleShape() - ..radius = 1.0 + (i.isEven ? 1.0 : -1.0) * .5 * .75; - final fd2 = FixtureDef(shape) - ..density = shape.radius * 1.5 - ..friction = 0.5 - ..restitution = 0.7; + final shape = CircleShape( + radius: 1.0 + (i.isEven ? 1.0 : -1.0) * .5 * .75, + ); + final fd2 = FixtureDef( + shape, + density: shape.radius * 1.5, + friction: 0.5, + restitution: 0.7, + ); final xPos = -39.0 + 2 * i; final yPos = 50.0 + j; - final bodyDef = BodyDef() - ..type = BodyType.dynamic - ..position = Vector2(xPos, yPos); + final bodyDef = BodyDef( + type: BodyType.dynamic, + position: Vector2(xPos, yPos), + ); final body = world.createBody(bodyDef); bodies.add(body); body.createFixture(fd2); diff --git a/packages/forge2d/example/web/domino_test.dart b/packages/forge2d/example/web/domino_test.dart index 12e052e0..ab831f82 100644 --- a/packages/forge2d/example/web/domino_test.dart +++ b/packages/forge2d/example/web/domino_test.dart @@ -11,10 +11,9 @@ class DominoTest extends Demo { { // Floor final shape = PolygonShape()..setAsBoxXY(50.0, 10.0); - final fixtureDef = FixtureDef(shape)..friction = 0.1; + final fixtureDef = FixtureDef(shape, friction: 0.1); - final bd = BodyDef(); - bd.position = Vector2(0.0, -10.0); + final bd = BodyDef(position: Vector2(0.0, -10.0)); final body = world.createBody(bd); body.createFixture(fixtureDef); bodies.add(body); @@ -24,9 +23,9 @@ class DominoTest extends Demo { // Platforms for (var i = 0; i < 4; i++) { final shape = PolygonShape()..setAsBoxXY(15.0, 0.125); - final fixtureDef = FixtureDef(shape)..friction = 0.1; + final fixtureDef = FixtureDef(shape, friction: 0.1); - final bodyDef = BodyDef()..position = Vector2(0.0, 5.0 + 5 * i); + final bodyDef = BodyDef(position: Vector2(0.0, 5.0 + 5 * i)); final body = world.createBody(bodyDef); body.createFixture(fixtureDef); bodies.add(body); @@ -36,11 +35,8 @@ class DominoTest extends Demo { // Dominoes { final shape = PolygonShape()..setAsBoxXY(0.125, 2.0); - final fixtureDef = FixtureDef(shape) - ..density = 25.0 - ..friction = 0.5; - - final bodyDef = BodyDef()..type = BodyType.dynamic; + final fixtureDef = FixtureDef(shape, density: 25.0, friction: 0.5); + final bodyDef = BodyDef(type: BodyType.dynamic); const numPerRow = 25; diff --git a/packages/forge2d/example/web/domino_tower.dart b/packages/forge2d/example/web/domino_tower.dart index fe70586f..d22dc71b 100644 --- a/packages/forge2d/example/web/domino_tower.dart +++ b/packages/forge2d/example/web/domino_tower.dart @@ -28,13 +28,17 @@ class DominoTower extends Demo { void makeDomino(double x, double y, {required bool horizontal}) { final shape = PolygonShape() ..setAsBoxXY(.5 * dominoWidth, .5 * dominoHeight); - final fixtureDef = FixtureDef(shape) - ..density = dominoDensity - ..friction = dominoFriction - ..restitution = 0.65; - final bodyDef = BodyDef()..type = BodyType.dynamic; - bodyDef.position = Vector2(x, y); - bodyDef.angle = horizontal ? (pi / 2.0) : 0.0; + final fixtureDef = FixtureDef( + shape, + density: dominoDensity, + friction: dominoFriction, + restitution: 0.65, + ); + final bodyDef = BodyDef( + type: BodyType.dynamic, + position: Vector2(x, y), + angle: horizontal ? (pi / 2.0) : 0.0, + ); final body = world.createBody(bodyDef); body.createFixture(fixtureDef); bodies.add(body); @@ -48,8 +52,7 @@ class DominoTower extends Demo { final sd = PolygonShape(); sd.setAsBoxXY(50.0, 10.0); - final bd = BodyDef(); - bd.position = Vector2(0.0, -10.0); + final bd = BodyDef(position: Vector2(0.0, -10.0)); final body = world.createBody(bd); body.createFixtureFromShape(sd); bodies.add(body); @@ -59,15 +62,12 @@ class DominoTower extends Demo { dominoDensity = 10.0; // Make bullet final shape = PolygonShape()..setAsBoxXY(.7, .7); - final fixtureDef = FixtureDef(shape) - ..density = 35.0 - ..shape = shape - ..friction = 0.0 - ..restitution = 0.85; - final bodyDef = BodyDef() - ..type = BodyType.dynamic - ..bullet = true - ..position = Vector2(30.0, 5.00); + final fixtureDef = FixtureDef(shape, density: 35.0, restitution: 0.85); + final bodyDef = BodyDef( + type: BodyType.dynamic, + bullet: true, + position: Vector2(30.0, 5.00), + ); var body = world.createBody(bodyDef); bodies.add(body); body.createFixture(fixtureDef); diff --git a/packages/forge2d/example/web/friction_joint_test.dart b/packages/forge2d/example/web/friction_joint_test.dart index 6f37c8ec..3f67afae 100644 --- a/packages/forge2d/example/web/friction_joint_test.dart +++ b/packages/forge2d/example/web/friction_joint_test.dart @@ -55,16 +55,15 @@ class FrictionJointTest extends Demo { boxShape.setAsBox(3.0, 1.5, Vector2.zero(), pi / 2); // Define fixture (links body and shape) - _boxFixture = FixtureDef(boxShape) - ..restitution = 0.5 - ..density = 0.10; + _boxFixture = FixtureDef(boxShape, restitution: 0.5, density: 0.10); } void _createBox() { // Define body - final bodyDef = BodyDef(); - bodyDef.type = BodyType.dynamic; - bodyDef.position = Vector2(-10.0, 30.0); + final bodyDef = BodyDef( + type: BodyType.dynamic, + position: Vector2(-10.0, 30.0), + ); // Create body and fixture from definitions final fallingBox = world.createBody(bodyDef); @@ -76,9 +75,10 @@ class FrictionJointTest extends Demo { void _createFrictionBox() { // Define body - final bodyDef = BodyDef(); - bodyDef.type = BodyType.dynamic; - bodyDef.position = Vector2(10.0, 30.0); + final bodyDef = BodyDef( + type: BodyType.dynamic, + position: Vector2(10.0, 30.0), + ); // Create body and fixture from definitions final fallingBox = world.createBody(bodyDef); diff --git a/packages/forge2d/example/web/particles.dart b/packages/forge2d/example/web/particles.dart index ee518a39..c9dc22c6 100644 --- a/packages/forge2d/example/web/particles.dart +++ b/packages/forge2d/example/web/particles.dart @@ -26,12 +26,14 @@ class Particles extends Demo { @override void initialize() { // Define the circle shape. - final circleShape = CircleShape()..radius = wallBallRadius; + final circleShape = CircleShape(radius: wallBallRadius); // Create fixture using the circle shape. - final circleFixtureDef = FixtureDef(circleShape) - ..friction = .9 - ..restitution = 1.0; + final circleFixtureDef = FixtureDef( + circleShape, + friction: 0.9, + restitution: 1.0, + ); // Create a body def. final circleBodyDef = BodyDef(); @@ -70,9 +72,11 @@ class Particles extends Demo { bouncingCircle.radius = activeBallRadius; // Create fixture for that ball shape. - final activeFixtureDef = FixtureDef(bouncingCircle) - ..restitution = 1.0 - ..density = 0.05; + final activeFixtureDef = FixtureDef( + bouncingCircle, + restitution: 1.0, + density: 0.05, + ); // Create the active ball body. final activeBodyDef = BodyDef(); @@ -87,7 +91,7 @@ class Particles extends Demo { world.particleSystem.particleRadius = 0.35; world.particleSystem.dampingStrength = 0.2; - final shape = CircleShape()..radius = 5; + final shape = CircleShape(radius: 5); final particleGroup = ParticleGroupDef() ..position.setFrom(fixture.renderCenter) ..destroyAutomatically = true diff --git a/packages/forge2d/example/web/racer.dart b/packages/forge2d/example/web/racer.dart index 461eea81..ac79eb2d 100644 --- a/packages/forge2d/example/web/racer.dart +++ b/packages/forge2d/example/web/racer.dart @@ -76,9 +76,11 @@ class Racer extends Demo implements ContactListener { final shape = PolygonShape() ..setAsBox(27.0, 21.0, Vector2(-30.0, 30.0), radians(20.0)); - final fixtureDef = FixtureDef(shape) - ..isSensor = true - ..userData = GroundArea(0.001, outOfCourse: false); + final fixtureDef = FixtureDef( + shape, + isSensor: true, + userData: GroundArea(0.001, outOfCourse: false), + ); _groundBody.createFixture(fixtureDef); fixtureDef.userData = GroundArea(0.2, outOfCourse: false); diff --git a/packages/forge2d/lib/src/dynamics/body.dart b/packages/forge2d/lib/src/dynamics/body.dart index 125edd92..80917163 100644 --- a/packages/forge2d/lib/src/dynamics/body.dart +++ b/packages/forge2d/lib/src/dynamics/body.dart @@ -177,10 +177,12 @@ class Body { double restitution = 0.0, }) { return createFixture( - FixtureDef(shape) - ..density = density - ..friction = friction - ..restitution = restitution, + FixtureDef( + shape, + density: density, + friction: friction, + restitution: restitution, + ), ); } diff --git a/packages/forge2d/test/dynamics/body_test.dart b/packages/forge2d/test/dynamics/body_test.dart index 1b18e8de..9f5b288b 100644 --- a/packages/forge2d/test/dynamics/body_test.dart +++ b/packages/forge2d/test/dynamics/body_test.dart @@ -27,9 +27,7 @@ void main() { final world = World(gravity); final body = world.createBody( - BodyDef() - ..type = BodyType.dynamic - ..gravityOverride = Vector2.zero(), + BodyDef(type: BodyType.dynamic, gravityOverride: Vector2.zero()), ); final bodyInitialPosition = body.position.clone(); @@ -45,9 +43,7 @@ void main() { 'when gravityOverride is not specfied', () { final gravity = Vector2(10, 10); final world = World(gravity); - - final body = world.createBody(BodyDef()..type = BodyType.dynamic); - + final body = world.createBody(BodyDef(type: BodyType.dynamic)); final bodyInitialPosition = body.position.clone(); world.stepDt(1); @@ -63,9 +59,7 @@ void main() { final world = World(gravity); final body = world.createBody( - BodyDef() - ..type = BodyType.dynamic - ..gravityScale = Vector2.zero(), + BodyDef(type: BodyType.dynamic, gravityScale: Vector2.zero()), ); final bodyInitialPosition = body.position.clone(); @@ -82,8 +76,7 @@ void main() { final gravity = Vector2(10, 10); final world = World(gravity); - final body = world.createBody(BodyDef()..type = BodyType.dynamic); - + final body = world.createBody(BodyDef(type: BodyType.dynamic)); final bodyInitialPosition = body.position.clone(); world.stepDt(1); diff --git a/packages/forge2d/test/dynamics/joints/joint_test.dart b/packages/forge2d/test/dynamics/joints/joint_test.dart index 8a8bbb0c..2bb7bf12 100644 --- a/packages/forge2d/test/dynamics/joints/joint_test.dart +++ b/packages/forge2d/test/dynamics/joints/joint_test.dart @@ -17,14 +17,13 @@ void main() { final bodyDef = BodyDef(); final body1 = world.createBody(bodyDef); final body2 = world.createBody(bodyDef..position = Vector2.all(2)); - final shape = CircleShape() - ..radius = 1.2 - ..position.setValues(10, 10); - - final fixtureDef = FixtureDef(shape) - ..density = 50.0 - ..friction = .1 - ..restitution = .9; + final shape = CircleShape(radius: 1.2, position: Vector2.all(10)); + final fixtureDef = FixtureDef( + shape, + density: 50.0, + friction: 0.1, + restitution: 0.9, + ); body1.createFixture(fixtureDef); body2.createFixture(fixtureDef);