refactor: cleaned calcualteSpeed method

pull/15/head
alestiago 4 years ago
parent 37d1327446
commit f54f86c36c

@ -17,7 +17,7 @@ class Flipper extends BodyComponent {
bool isMirrored = false, bool isMirrored = false,
}) : _position = position, }) : _position = position,
_isMirrored = isMirrored, _isMirrored = isMirrored,
_speed = _calculateRequiredSpeed() { _speed = _calculateSpeed() {
// TODO(alestiago): Use sprite instead of color when provided. // TODO(alestiago): Use sprite instead of color when provided.
paint = Paint() paint = Paint()
..color = const Color(0xFF00FF00) ..color = const Color(0xFF00FF00)
@ -40,20 +40,25 @@ class Flipper extends BodyComponent {
isMirrored: false, isMirrored: false,
); );
// TODO(alestiago): Use width and height.
static final size = Vector2(12, 2.8); static final size = Vector2(12, 2.8);
/// The total duration of a full flipper's arc motion. /// The total duration of a full flipper's arc motion.
/// ///
/// A full flipper's arc motion is from the lowest position (resting point) to /// A full flipper's arc motion is from the resting position to the highest
/// the highest position. /// position.
static const _sweepingAnimationDuration = Duration(milliseconds: 100); static const _sweepingAnimationDuration = Duration(milliseconds: 100);
static double _calculateRequiredSpeed() { /// The total amount of speed required to move the [Flipper] from the resting
/// position to the highest position.
static double _calculateSpeed() {
// TODO(alestiago): test correctness. // TODO(alestiago): test correctness.
const angle = FlipperAnchorRevoluteJointDef._sweepingAngle / 2; const angle = FlipperAnchorRevoluteJointDef._sweepingAngle / 2;
final sweepingDistance = (size.x * math.sin(angle)) * 2; final sweepingDistance = (size.x * math.sin(angle)) * 2;
final seconds = _sweepingAnimationDuration.inMicroseconds / final seconds = _sweepingAnimationDuration.inMicroseconds /
Duration.microsecondsPerSecond; Duration.microsecondsPerSecond;
return sweepingDistance / seconds; return sweepingDistance / seconds;
} }
@ -125,8 +130,8 @@ class Flipper extends BodyComponent {
]; ];
final trapezium = PolygonShape()..set(trapeziumVertices); final trapezium = PolygonShape()..set(trapeziumVertices);
final trapeziumFixtureDef = FixtureDef(trapezium) final trapeziumFixtureDef = FixtureDef(trapezium)
..density = 50.0 ..density = 50.0 // TODO(alestiago): Use a proper density.
..friction = .1; ..friction = .1; // TODO(alestiago): Use a proper friction.
fixtures.add(trapeziumFixtureDef); fixtures.add(trapeziumFixtureDef);
return fixtures; return fixtures;

Loading…
Cancel
Save