feat: gravity override (#192)

pull/178/head
Allison Ryan 3 years ago committed by GitHub
parent 43ca2beca7
commit 9714bead89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -37,7 +37,7 @@ class ScoreEffectController extends ComponentController<PinballGame>
text: newScore.toString(), text: newScore.toString(),
position: Vector2( position: Vector2(
_noise(), _noise(),
_noise() + (-BoardDimensions.bounds.topCenter.dy + 10), _noise() + (BoardDimensions.bounds.topCenter.dy + 10),
), ),
), ),
); );

@ -1,4 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'dart:math' as math;
import 'dart:ui'; import 'dart:ui';
import 'package:flame/components.dart'; import 'package:flame/components.dart';
@ -107,7 +108,8 @@ class Ball<T extends Forge2DGame> extends BodyComponent<T>
unawaited(gameRef.add(effect)); unawaited(gameRef.add(effect));
} }
_rescale(); _rescaleSize();
_setPositionalGravity();
} }
/// Applies a boost on this [Ball]. /// Applies a boost on this [Ball].
@ -116,19 +118,36 @@ class Ball<T extends Forge2DGame> extends BodyComponent<T>
_boostTimer = _boostDuration; _boostTimer = _boostDuration;
} }
void _rescale() { void _rescaleSize() {
final boardHeight = BoardDimensions.bounds.height; final boardHeight = BoardDimensions.bounds.height;
const maxShrinkAmount = BoardDimensions.perspectiveShrinkFactor; const maxShrinkValue = BoardDimensions.perspectiveShrinkFactor;
final adjustedYPosition = -body.position.y + (boardHeight / 2); final standardizedYPosition = body.position.y + (boardHeight / 2);
final scaleFactor = ((boardHeight - adjustedYPosition) / final scaleFactor = maxShrinkValue +
BoardDimensions.shrinkAdjustedHeight) + ((standardizedYPosition / boardHeight) * (1 - maxShrinkValue));
maxShrinkAmount;
body.fixtures.first.shape.radius = (size.x / 2) * scaleFactor; body.fixtures.first.shape.radius = (size.x / 2) * scaleFactor;
_spriteComponent.scale = Vector2.all(scaleFactor); _spriteComponent.scale = Vector2.all(scaleFactor);
} }
void _setPositionalGravity() {
final defaultGravity = gameRef.world.gravity.y;
final maxXDeviationFromCenter = BoardDimensions.bounds.width / 2;
const maxXGravityPercentage =
(1 - BoardDimensions.perspectiveShrinkFactor) / 2;
final xDeviationFromCenter = body.position.x;
final positionalXForce = ((xDeviationFromCenter / maxXDeviationFromCenter) *
maxXGravityPercentage) *
defaultGravity;
final positionalYForce = math.sqrt(
math.pow(defaultGravity, 2) - math.pow(positionalXForce, 2),
);
body.gravityOverride = Vector2(positionalXForce, positionalYForce);
}
} }
class _BallSpriteComponent extends SpriteComponent with HasGameRef { class _BallSpriteComponent extends SpriteComponent with HasGameRef {

@ -22,8 +22,4 @@ class BoardDimensions {
/// Factor the board shrinks by from the closest point to the farthest. /// Factor the board shrinks by from the closest point to the farthest.
static const perspectiveShrinkFactor = 0.63; static const perspectiveShrinkFactor = 0.63;
/// Board height based on the [perspectiveShrinkFactor].
static final shrinkAdjustedHeight =
(1 / (1 - perspectiveShrinkFactor)) * size.y;
} }

@ -19,9 +19,5 @@ void main() {
test('has perspectiveShrinkFactor', () { test('has perspectiveShrinkFactor', () {
expect(BoardDimensions.perspectiveShrinkFactor, equals(0.63)); expect(BoardDimensions.perspectiveShrinkFactor, equals(0.63));
}); });
test('has shrinkAdjustedHeight', () {
expect(BoardDimensions.shrinkAdjustedHeight, isNotNull);
});
}); });
} }

Loading…
Cancel
Save