From b204d067fd27a933c3cd896ab03e6d2a3a600e80 Mon Sep 17 00:00:00 2001 From: alestiago Date: Tue, 22 Mar 2022 11:52:06 +0000 Subject: [PATCH] feat: potioned and clean --- lib/game/components/board.dart | 3 +- lib/game/components/chrome_dino.dart | 47 +++++++++++++--------------- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/lib/game/components/board.dart b/lib/game/components/board.dart index af07c534..3dfdd314 100644 --- a/lib/game/components/board.dart +++ b/lib/game/components/board.dart @@ -27,7 +27,8 @@ class Board extends Component { ), ); - final dino = ChromeDino()..initialPosition = _size / 2 + Vector2(0, -10); + // TODO(alestiago): adjust positioning once aspect ratio is fixed. + final dino = ChromeDino()..initialPosition = _size / 2 + Vector2(15, 5); await addAll([ bottomGroup, diff --git a/lib/game/components/chrome_dino.dart b/lib/game/components/chrome_dino.dart index 1da3505e..7418e3da 100644 --- a/lib/game/components/chrome_dino.dart +++ b/lib/game/components/chrome_dino.dart @@ -22,18 +22,20 @@ class ChromeDino extends BodyComponent with InitialPosition { /// Anchors the [ChromeDino] to the [RevoluteJoint] that controls its arc /// motion. Future _anchorToJoint() async { - final anchor = ChromeDinoAnchor(chromeDino: this); + final anchor = _ChromeDinoAnchor(chromeDino: this); await add(anchor); - final jointDef = ChromeDinoAnchorRevoluteJointDef( + final jointDef = _ChromeDinoAnchorRevoluteJointDef( chromeDino: this, anchor: anchor, ); final joint = world.createJoint(jointDef) as RevoluteJoint; - ChromeDinoAnchorRevoluteJointDef.spin(joint, removed); + _ChromeDinoAnchorRevoluteJointDef.swivel(joint, removed); } + // TODO(alestiago): Remove once the following is added to Flame. + // ignore: public_member_api_docs Completer removed = Completer(); @override @@ -42,22 +44,16 @@ class ChromeDino extends BodyComponent with InitialPosition { removed.complete(true); } - @override - void update(double dt) { - super.update(dt); - // print(body.angle); - } - @override Future onLoad() async { await super.onLoad(); - await _anchorToJoint(); } List _createFixtureDefs() { final fixtureDefs = []; + // TODO(alestiago): Subject to change when sprites are added. final box = PolygonShape()..setAsBoxXY(size.x / 2, size.y / 2); final fixtureDef = FixtureDef(box) ..shape = box @@ -111,9 +107,9 @@ class ChromeDino extends BodyComponent with InitialPosition { /// {@template flipper_anchor} /// [JointAnchor] positioned at the end of a [ChromeDino]. /// {@endtemplate} -class ChromeDinoAnchor extends JointAnchor { +class _ChromeDinoAnchor extends JointAnchor { /// {@macro flipper_anchor} - ChromeDinoAnchor({ + _ChromeDinoAnchor({ required ChromeDino chromeDino, }) { initialPosition = Vector2( @@ -124,13 +120,13 @@ class ChromeDinoAnchor extends JointAnchor { } /// {@template chrome_dino_anchor_revolute_joint_def} -/// Hinges a [ChromeDino] to a [ChromeDinoAnchor]. +/// Hinges a [ChromeDino] to a [_ChromeDinoAnchor]. /// {@endtemplate} -class ChromeDinoAnchorRevoluteJointDef extends RevoluteJointDef { +class _ChromeDinoAnchorRevoluteJointDef extends RevoluteJointDef { /// {@macro chrome_dino_anchor_revolute_joint_def} - ChromeDinoAnchorRevoluteJointDef({ + _ChromeDinoAnchorRevoluteJointDef({ required ChromeDino chromeDino, - required ChromeDinoAnchor anchor, + required _ChromeDinoAnchor anchor, }) { initialize( chromeDino.body, @@ -138,26 +134,27 @@ class ChromeDinoAnchorRevoluteJointDef extends RevoluteJointDef { anchor.body.position, ); enableLimit = true; - const halfAngle = _sweepingAngle / 2; - lowerAngle = -halfAngle; - upperAngle = halfAngle; + const angle = math.pi / 3.5; + lowerAngle = -angle / 2; + upperAngle = angle / 2; enableMotor = true; + // TODO(alestiago): Tune this values. maxMotorTorque = 999; motorSpeed = 999; } - // TODO(alestiago): Refactor. - static void spin(RevoluteJoint joint, Completer completer) { + /// Sweeps the [ChromeDino] up and down repeatedly. + // TODO(alestiago): consider refactor once the issue is solved: + // https://github.com/flame-engine/forge2d/issues/36 + static void swivel(RevoluteJoint joint, Completer completer) { Future.doWhile(() async { if (completer.isCompleted) return false; - await Future.delayed(Duration(milliseconds: 1000)); + // TODO(alestiago): Tune this values. + await Future.delayed(const Duration(milliseconds: 1000)); joint.setMotorSpeed(-joint.motorSpeed); return true; }); } - - /// The total angle of the arc motion. - static const _sweepingAngle = math.pi / 3.5; }