Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 3.1 KiB |
After Width: | Height: | Size: 63 KiB |
After Width: | Height: | Size: 64 KiB |
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 8.6 KiB |
After Width: | Height: | Size: 8.4 KiB |
After Width: | Height: | Size: 11 KiB |
After Width: | Height: | Size: 16 KiB |
@ -0,0 +1,29 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flame/extensions.dart';
|
||||
|
||||
/// {@template board_dimensions}
|
||||
/// Contains various board properties and dimensions for global use.
|
||||
/// {@endtemplate}
|
||||
// TODO(allisonryan0002): consider alternatives for global dimensions.
|
||||
class BoardDimensions {
|
||||
/// Width and height of the board.
|
||||
static final size = Vector2(101.6, 143.8);
|
||||
|
||||
/// [Rect] for easier access to board boundaries.
|
||||
static final bounds = Rect.fromCenter(
|
||||
center: Offset.zero,
|
||||
width: size.x,
|
||||
height: -size.y,
|
||||
);
|
||||
|
||||
/// 3D perspective angle of the board in radians.
|
||||
static final perspectiveAngle = -math.atan(18.6 / bounds.height);
|
||||
|
||||
/// Factor the board shrinks by from the closest point to the farthest.
|
||||
static const perspectiveShrinkFactor = 0.63;
|
||||
|
||||
/// Board height based on the [perspectiveShrinkFactor].
|
||||
static final shrinkAdjustedHeight =
|
||||
(1 / (1 - perspectiveShrinkFactor)) * size.y;
|
||||
}
|
@ -0,0 +1,142 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
/// {@template dash_nest_bumper}
|
||||
/// Bumper with a nest appearance.
|
||||
/// {@endtemplate}
|
||||
abstract class DashNestBumper extends BodyComponent with InitialPosition {
|
||||
/// {@macro dash_nest_bumper}
|
||||
DashNestBumper._({
|
||||
required String activeAssetPath,
|
||||
required String inactiveAssetPath,
|
||||
required SpriteComponent spriteComponent,
|
||||
}) : _activeAssetPath = activeAssetPath,
|
||||
_inactiveAssetPath = inactiveAssetPath,
|
||||
_spriteComponent = spriteComponent;
|
||||
|
||||
final String _activeAssetPath;
|
||||
late final Sprite _activeSprite;
|
||||
final String _inactiveAssetPath;
|
||||
late final Sprite _inactiveSprite;
|
||||
final SpriteComponent _spriteComponent;
|
||||
|
||||
Future<void> _loadSprites() async {
|
||||
// TODO(alestiago): I think ideally we would like to do:
|
||||
// Sprite(path).load so we don't require to store the activeAssetPath and
|
||||
// the inactive assetPath.
|
||||
_inactiveSprite = await gameRef.loadSprite(_inactiveAssetPath);
|
||||
_activeSprite = await gameRef.loadSprite(_activeAssetPath);
|
||||
}
|
||||
|
||||
/// Activates the [DashNestBumper].
|
||||
void activate() {
|
||||
_spriteComponent
|
||||
..sprite = _activeSprite
|
||||
..size = _activeSprite.originalSize / 10;
|
||||
}
|
||||
|
||||
/// Deactivates the [DashNestBumper].
|
||||
void deactivate() {
|
||||
_spriteComponent
|
||||
..sprite = _inactiveSprite
|
||||
..size = _inactiveSprite.originalSize / 10;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
await _loadSprites();
|
||||
|
||||
// TODO(erickzanardo): Look into using onNewState instead.
|
||||
// Currently doing: onNewState(gameRef.read<GameState>()) will throw an
|
||||
// `Exception: build context is not available yet`
|
||||
deactivate();
|
||||
await add(_spriteComponent);
|
||||
}
|
||||
}
|
||||
|
||||
/// {@macro dash_nest_bumper}
|
||||
class BigDashNestBumper extends DashNestBumper {
|
||||
/// {@macro dash_nest_bumper}
|
||||
BigDashNestBumper()
|
||||
: super._(
|
||||
activeAssetPath: Assets.images.dashBumper.main.active.keyName,
|
||||
inactiveAssetPath: Assets.images.dashBumper.main.inactive.keyName,
|
||||
spriteComponent: SpriteComponent(
|
||||
anchor: Anchor.center,
|
||||
),
|
||||
);
|
||||
|
||||
@override
|
||||
Body createBody() {
|
||||
final shape = EllipseShape(
|
||||
center: Vector2.zero(),
|
||||
majorRadius: 4.85,
|
||||
minorRadius: 3.95,
|
||||
)..rotate(math.pi / 2);
|
||||
final fixtureDef = FixtureDef(shape);
|
||||
|
||||
final bodyDef = BodyDef()
|
||||
..position = initialPosition
|
||||
..userData = this;
|
||||
|
||||
return world.createBody(bodyDef)..createFixture(fixtureDef);
|
||||
}
|
||||
}
|
||||
|
||||
/// {@macro dash_nest_bumper}
|
||||
class SmallDashNestBumper extends DashNestBumper {
|
||||
/// {@macro dash_nest_bumper}
|
||||
SmallDashNestBumper._({
|
||||
required String activeAssetPath,
|
||||
required String inactiveAssetPath,
|
||||
required SpriteComponent spriteComponent,
|
||||
}) : super._(
|
||||
activeAssetPath: activeAssetPath,
|
||||
inactiveAssetPath: inactiveAssetPath,
|
||||
spriteComponent: spriteComponent,
|
||||
);
|
||||
|
||||
/// {@macro dash_nest_bumper}
|
||||
SmallDashNestBumper.a()
|
||||
: this._(
|
||||
activeAssetPath: Assets.images.dashBumper.a.active.keyName,
|
||||
inactiveAssetPath: Assets.images.dashBumper.a.inactive.keyName,
|
||||
spriteComponent: SpriteComponent(
|
||||
anchor: Anchor.center,
|
||||
position: Vector2(0.35, -1.2),
|
||||
),
|
||||
);
|
||||
|
||||
/// {@macro dash_nest_bumper}
|
||||
SmallDashNestBumper.b()
|
||||
: this._(
|
||||
activeAssetPath: Assets.images.dashBumper.b.active.keyName,
|
||||
inactiveAssetPath: Assets.images.dashBumper.b.inactive.keyName,
|
||||
spriteComponent: SpriteComponent(
|
||||
anchor: Anchor.center,
|
||||
position: Vector2(0.35, -1.2),
|
||||
),
|
||||
);
|
||||
|
||||
@override
|
||||
Body createBody() {
|
||||
final shape = EllipseShape(
|
||||
center: Vector2.zero(),
|
||||
majorRadius: 3,
|
||||
minorRadius: 2.25,
|
||||
)..rotate(math.pi / 2);
|
||||
final fixtureDef = FixtureDef(shape)
|
||||
..friction = 0
|
||||
..restitution = 4;
|
||||
|
||||
final bodyDef = BodyDef()
|
||||
..position = initialPosition
|
||||
..userData = this;
|
||||
|
||||
return world.createBody(bodyDef)..createFixture(fixtureDef);
|
||||
}
|
||||
}
|
@ -0,0 +1,225 @@
|
||||
// ignore_for_file: comment_references, avoid_renaming_method_parameters
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:pinball_components/gen/assets.gen.dart';
|
||||
import 'package:pinball_components/pinball_components.dart' hide Assets;
|
||||
|
||||
/// {@template dinowalls}
|
||||
/// A [Blueprint] which creates walls for the [ChromeDino].
|
||||
/// {@endtemplate}
|
||||
class DinoWalls extends Forge2DBlueprint {
|
||||
/// {@macro dinowalls}
|
||||
DinoWalls({required this.position});
|
||||
|
||||
/// The [position] where the elements will be created
|
||||
final Vector2 position;
|
||||
|
||||
@override
|
||||
void build(_) {
|
||||
addAll([
|
||||
_DinoTopWall()..initialPosition = position,
|
||||
_DinoBottomWall()..initialPosition = position,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/// {@template dino_top_wall}
|
||||
/// Wall segment located above [ChromeDino].
|
||||
/// {@endtemplate}
|
||||
class _DinoTopWall extends BodyComponent with InitialPosition {
|
||||
///{@macro dino_top_wall}
|
||||
_DinoTopWall() : super(priority: 2);
|
||||
|
||||
List<FixtureDef> _createFixtureDefs() {
|
||||
final fixturesDef = <FixtureDef>[];
|
||||
|
||||
final topStraightShape = EdgeShape()
|
||||
..set(
|
||||
Vector2(29.5, 35.1),
|
||||
Vector2(28.4, 35.1),
|
||||
);
|
||||
final topStraightFixtureDef = FixtureDef(topStraightShape);
|
||||
fixturesDef.add(topStraightFixtureDef);
|
||||
|
||||
final topCurveShape = BezierCurveShape(
|
||||
controlPoints: [
|
||||
topStraightShape.vertex1,
|
||||
Vector2(17.4, 26.38),
|
||||
Vector2(25.5, 20.7),
|
||||
],
|
||||
);
|
||||
fixturesDef.add(FixtureDef(topCurveShape));
|
||||
|
||||
final middleCurveShape = BezierCurveShape(
|
||||
controlPoints: [
|
||||
topCurveShape.vertices.last,
|
||||
Vector2(27.8, 20.1),
|
||||
Vector2(26.8, 19.5),
|
||||
],
|
||||
);
|
||||
fixturesDef.add(FixtureDef(middleCurveShape));
|
||||
|
||||
final bottomCurveShape = BezierCurveShape(
|
||||
controlPoints: [
|
||||
middleCurveShape.vertices.last,
|
||||
Vector2(21.15, 16),
|
||||
Vector2(25.6, 15.2),
|
||||
],
|
||||
);
|
||||
fixturesDef.add(FixtureDef(bottomCurveShape));
|
||||
|
||||
final bottomStraightShape = EdgeShape()
|
||||
..set(
|
||||
bottomCurveShape.vertices.last,
|
||||
Vector2(31, 14.5),
|
||||
);
|
||||
final bottomStraightFixtureDef = FixtureDef(bottomStraightShape);
|
||||
fixturesDef.add(bottomStraightFixtureDef);
|
||||
|
||||
return fixturesDef;
|
||||
}
|
||||
|
||||
@override
|
||||
Body createBody() {
|
||||
renderBody = false;
|
||||
|
||||
final bodyDef = BodyDef()
|
||||
..userData = this
|
||||
..position = initialPosition
|
||||
..type = BodyType.static;
|
||||
|
||||
final body = world.createBody(bodyDef);
|
||||
_createFixtureDefs().forEach(
|
||||
(fixture) => body.createFixture(
|
||||
fixture
|
||||
..restitution = 0.1
|
||||
..friction = 0,
|
||||
),
|
||||
);
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
await _loadSprite();
|
||||
}
|
||||
|
||||
Future<void> _loadSprite() async {
|
||||
final sprite = await gameRef.loadSprite(
|
||||
Assets.images.dino.dinoLandTop.keyName,
|
||||
);
|
||||
final spriteComponent = SpriteComponent(
|
||||
sprite: sprite,
|
||||
size: Vector2(10.6, 27.7),
|
||||
anchor: Anchor.center,
|
||||
position: Vector2(27, -28.2),
|
||||
);
|
||||
|
||||
await add(spriteComponent);
|
||||
}
|
||||
}
|
||||
|
||||
/// {@template dino_bottom_wall}
|
||||
/// Wall segment located below [ChromeDino].
|
||||
/// {@endtemplate}
|
||||
class _DinoBottomWall extends BodyComponent with InitialPosition {
|
||||
///{@macro dino_top_wall}
|
||||
_DinoBottomWall() : super(priority: 2);
|
||||
|
||||
List<FixtureDef> _createFixtureDefs() {
|
||||
final fixturesDef = <FixtureDef>[];
|
||||
|
||||
final topStraightControlPoints = [
|
||||
Vector2(32.4, 8.3),
|
||||
Vector2(25, 7.7),
|
||||
];
|
||||
final topStraightShape = EdgeShape()
|
||||
..set(
|
||||
topStraightControlPoints.first,
|
||||
topStraightControlPoints.last,
|
||||
);
|
||||
final topStraightFixtureDef = FixtureDef(topStraightShape);
|
||||
fixturesDef.add(topStraightFixtureDef);
|
||||
|
||||
final topLeftCurveControlPoints = [
|
||||
topStraightControlPoints.last,
|
||||
Vector2(21.8, 7),
|
||||
Vector2(29.5, -13.8),
|
||||
];
|
||||
final topLeftCurveShape = BezierCurveShape(
|
||||
controlPoints: topLeftCurveControlPoints,
|
||||
);
|
||||
fixturesDef.add(FixtureDef(topLeftCurveShape));
|
||||
|
||||
final bottomLeftStraightControlPoints = [
|
||||
topLeftCurveControlPoints.last,
|
||||
Vector2(31.8, -44.1),
|
||||
];
|
||||
final bottomLeftStraightShape = EdgeShape()
|
||||
..set(
|
||||
bottomLeftStraightControlPoints.first,
|
||||
bottomLeftStraightControlPoints.last,
|
||||
);
|
||||
final bottomLeftStraightFixtureDef = FixtureDef(bottomLeftStraightShape);
|
||||
fixturesDef.add(bottomLeftStraightFixtureDef);
|
||||
|
||||
final bottomStraightControlPoints = [
|
||||
bottomLeftStraightControlPoints.last,
|
||||
Vector2(37.8, -44.1),
|
||||
];
|
||||
final bottomStraightShape = EdgeShape()
|
||||
..set(
|
||||
bottomStraightControlPoints.first,
|
||||
bottomStraightControlPoints.last,
|
||||
);
|
||||
final bottomStraightFixtureDef = FixtureDef(bottomStraightShape);
|
||||
fixturesDef.add(bottomStraightFixtureDef);
|
||||
|
||||
return fixturesDef;
|
||||
}
|
||||
|
||||
@override
|
||||
Body createBody() {
|
||||
renderBody = false;
|
||||
|
||||
final bodyDef = BodyDef()
|
||||
..userData = this
|
||||
..position = initialPosition
|
||||
..type = BodyType.static;
|
||||
|
||||
final body = world.createBody(bodyDef);
|
||||
_createFixtureDefs().forEach(
|
||||
(fixture) => body.createFixture(
|
||||
fixture
|
||||
..restitution = 0.1
|
||||
..friction = 0,
|
||||
),
|
||||
);
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
await _loadSprite();
|
||||
}
|
||||
|
||||
Future<void> _loadSprite() async {
|
||||
final sprite = await gameRef.loadSprite(
|
||||
Assets.images.dino.dinoLandBottom.keyName,
|
||||
);
|
||||
final spriteComponent = SpriteComponent(
|
||||
sprite: sprite,
|
||||
size: Vector2(15.6, 54.8),
|
||||
anchor: Anchor.center,
|
||||
)..position = Vector2(31.7, 18);
|
||||
|
||||
await add(spriteComponent);
|
||||
}
|
||||
}
|
@ -1 +1,2 @@
|
||||
export 'blueprint.dart';
|
||||
export 'priority.dart';
|
||||
|
@ -0,0 +1,39 @@
|
||||
import 'dart:math' as math;
|
||||
import 'package:flame/components.dart';
|
||||
|
||||
/// Helper methods to change the [priority] of a [Component].
|
||||
extension ComponentPriorityX on Component {
|
||||
static const _lowestPriority = 0;
|
||||
|
||||
/// Changes the priority to a specific one.
|
||||
void sendTo(int destinationPriority) {
|
||||
if (priority != destinationPriority) {
|
||||
priority = math.max(destinationPriority, _lowestPriority);
|
||||
reorderChildren();
|
||||
}
|
||||
}
|
||||
|
||||
/// Changes the priority to the lowest possible.
|
||||
void sendToBack() {
|
||||
if (priority != _lowestPriority) {
|
||||
priority = _lowestPriority;
|
||||
reorderChildren();
|
||||
}
|
||||
}
|
||||
|
||||
/// Decreases the priority to be lower than another [Component].
|
||||
void showBehindOf(Component other) {
|
||||
if (priority >= other.priority) {
|
||||
priority = math.max(other.priority - 1, _lowestPriority);
|
||||
reorderChildren();
|
||||
}
|
||||
}
|
||||
|
||||
/// Increases the priority to be higher than another [Component].
|
||||
void showInFrontOf(Component other) {
|
||||
if (priority <= other.priority) {
|
||||
priority = other.priority + 1;
|
||||
reorderChildren();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
import 'package:dashbook/dashbook.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:sandbox/common/common.dart';
|
||||
import 'package:sandbox/stories/baseboard/basic.dart';
|
||||
|
||||
void addBaseboardStories(Dashbook dashbook) {
|
||||
dashbook.storiesOf('Baseboard').add(
|
||||
'Basic',
|
||||
(context) => GameWidget(
|
||||
game: BasicBaseboardGame(),
|
||||
),
|
||||
codeLink: buildSourceLink('baseboard/basic.dart'),
|
||||
info: BasicBaseboardGame.info,
|
||||
);
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
import 'package:sandbox/common/common.dart';
|
||||
|
||||
class BasicBaseboardGame extends BasicGame {
|
||||
static const info = '''
|
||||
Basic example of how a Baseboard works.
|
||||
''';
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
|
||||
final center = screenToWorld(camera.viewport.canvasSize! / 2);
|
||||
|
||||
final leftBaseboard = Baseboard(side: BoardSide.left)
|
||||
..initialPosition = center - Vector2(25, 0);
|
||||
final rightBaseboard = Baseboard(side: BoardSide.right)
|
||||
..initialPosition = center + Vector2(25, 0);
|
||||
|
||||
await addAll([
|
||||
leftBaseboard,
|
||||
rightBaseboard,
|
||||
]);
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
export 'ball/ball.dart';
|
||||
export 'baseboard/baseboard.dart';
|
||||
export 'effects/effects.dart';
|
||||
export 'flipper/flipper.dart';
|
||||
export 'layer/layer.dart';
|
||||
|
@ -0,0 +1,27 @@
|
||||
import 'package:flame/extensions.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
void main() {
|
||||
group('BoardDimensions', () {
|
||||
test('has size', () {
|
||||
expect(BoardDimensions.size, equals(Vector2(101.6, 143.8)));
|
||||
});
|
||||
|
||||
test('has bounds', () {
|
||||
expect(BoardDimensions.bounds, isNotNull);
|
||||
});
|
||||
|
||||
test('has perspectiveAngle', () {
|
||||
expect(BoardDimensions.perspectiveAngle, isNotNull);
|
||||
});
|
||||
|
||||
test('has perspectiveShrinkFactor', () {
|
||||
expect(BoardDimensions.perspectiveShrinkFactor, equals(0.63));
|
||||
});
|
||||
|
||||
test('has shrinkAdjustedHeight', () {
|
||||
expect(BoardDimensions.shrinkAdjustedHeight, isNotNull);
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,116 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
import '../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final flameTester = FlameTester(TestGame.new);
|
||||
|
||||
group('BigDashNestBumper', () {
|
||||
flameTester.test('loads correctly', (game) async {
|
||||
final bumper = BigDashNestBumper();
|
||||
await game.ensureAdd(bumper);
|
||||
expect(game.contains(bumper), isTrue);
|
||||
});
|
||||
|
||||
flameTester.test('activate returns normally', (game) async {
|
||||
final bumper = BigDashNestBumper();
|
||||
await game.ensureAdd(bumper);
|
||||
|
||||
expect(bumper.activate, returnsNormally);
|
||||
});
|
||||
|
||||
flameTester.test('deactivate returns normally', (game) async {
|
||||
final bumper = BigDashNestBumper();
|
||||
await game.ensureAdd(bumper);
|
||||
|
||||
expect(bumper.deactivate, returnsNormally);
|
||||
});
|
||||
|
||||
flameTester.test('changes sprite', (game) async {
|
||||
final bumper = BigDashNestBumper();
|
||||
await game.ensureAdd(bumper);
|
||||
|
||||
final spriteComponent = bumper.firstChild<SpriteComponent>()!;
|
||||
|
||||
final deactivatedSprite = spriteComponent.sprite;
|
||||
bumper.activate();
|
||||
expect(
|
||||
spriteComponent.sprite,
|
||||
isNot(equals(deactivatedSprite)),
|
||||
);
|
||||
|
||||
final activatedSprite = spriteComponent.sprite;
|
||||
bumper.deactivate();
|
||||
expect(
|
||||
spriteComponent.sprite,
|
||||
isNot(equals(activatedSprite)),
|
||||
);
|
||||
|
||||
expect(
|
||||
activatedSprite,
|
||||
isNot(equals(deactivatedSprite)),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('SmallDashNestBumper', () {
|
||||
flameTester.test('"a" loads correctly', (game) async {
|
||||
final bumper = SmallDashNestBumper.a();
|
||||
await game.ensureAdd(bumper);
|
||||
|
||||
expect(game.contains(bumper), isTrue);
|
||||
});
|
||||
|
||||
flameTester.test('"b" loads correctly', (game) async {
|
||||
final bumper = SmallDashNestBumper.b();
|
||||
await game.ensureAdd(bumper);
|
||||
expect(game.contains(bumper), isTrue);
|
||||
});
|
||||
|
||||
flameTester.test('activate returns normally', (game) async {
|
||||
final bumper = SmallDashNestBumper.a();
|
||||
await game.ensureAdd(bumper);
|
||||
|
||||
expect(bumper.activate, returnsNormally);
|
||||
});
|
||||
|
||||
flameTester.test('deactivate returns normally', (game) async {
|
||||
final bumper = SmallDashNestBumper.a();
|
||||
await game.ensureAdd(bumper);
|
||||
|
||||
expect(bumper.deactivate, returnsNormally);
|
||||
});
|
||||
|
||||
flameTester.test('changes sprite', (game) async {
|
||||
final bumper = SmallDashNestBumper.a();
|
||||
await game.ensureAdd(bumper);
|
||||
|
||||
final spriteComponent = bumper.firstChild<SpriteComponent>()!;
|
||||
|
||||
final deactivatedSprite = spriteComponent.sprite;
|
||||
bumper.activate();
|
||||
expect(
|
||||
spriteComponent.sprite,
|
||||
isNot(equals(deactivatedSprite)),
|
||||
);
|
||||
|
||||
final activatedSprite = spriteComponent.sprite;
|
||||
bumper.deactivate();
|
||||
expect(
|
||||
spriteComponent.sprite,
|
||||
isNot(equals(activatedSprite)),
|
||||
);
|
||||
|
||||
expect(
|
||||
activatedSprite,
|
||||
isNot(equals(deactivatedSprite)),
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
import '../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
group('DinoWalls', () {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final flameTester = FlameTester(TestGame.new);
|
||||
|
||||
flameTester.test(
|
||||
'loads correctly',
|
||||
(game) async {
|
||||
final dinoWalls = DinoWalls(position: Vector2.zero());
|
||||
await game.addFromBlueprint(dinoWalls);
|
||||
await game.ready();
|
||||
|
||||
for (final wall in dinoWalls.components) {
|
||||
expect(game.contains(wall), isTrue);
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
After Width: | Height: | Size: 38 KiB |
After Width: | Height: | Size: 44 KiB |
@ -0,0 +1,221 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:pinball_components/src/flame/priority.dart';
|
||||
|
||||
import '../../helpers/helpers.dart';
|
||||
|
||||
class TestBodyComponent extends BodyComponent {
|
||||
@override
|
||||
Body createBody() {
|
||||
final fixtureDef = FixtureDef(CircleShape());
|
||||
return world.createBody(BodyDef())..createFixture(fixtureDef);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
final flameTester = FlameTester(Forge2DGame.new);
|
||||
|
||||
group('ComponentPriorityX', () {
|
||||
group('sendTo', () {
|
||||
flameTester.test(
|
||||
'changes the priority correctly to other level',
|
||||
(game) async {
|
||||
const newPriority = 5;
|
||||
final component = TestBodyComponent()..priority = 4;
|
||||
|
||||
component.sendTo(newPriority);
|
||||
|
||||
expect(component.priority, equals(newPriority));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'calls reorderChildren if the new priority is different',
|
||||
(game) async {
|
||||
const newPriority = 5;
|
||||
final component = MockComponent();
|
||||
when(() => component.priority).thenReturn(4);
|
||||
|
||||
component.sendTo(newPriority);
|
||||
|
||||
verify(component.reorderChildren).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
"doesn't call reorderChildren if the priority is the same",
|
||||
(game) async {
|
||||
const newPriority = 5;
|
||||
final component = MockComponent();
|
||||
when(() => component.priority).thenReturn(newPriority);
|
||||
|
||||
component.sendTo(newPriority);
|
||||
|
||||
verifyNever(component.reorderChildren);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('sendToBack', () {
|
||||
flameTester.test(
|
||||
'changes the priority correctly to board level',
|
||||
(game) async {
|
||||
final component = TestBodyComponent()..priority = 4;
|
||||
|
||||
component.sendToBack();
|
||||
|
||||
expect(component.priority, equals(0));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'calls reorderChildren if the priority is greater than lowest level',
|
||||
(game) async {
|
||||
final component = MockComponent();
|
||||
when(() => component.priority).thenReturn(4);
|
||||
|
||||
component.sendToBack();
|
||||
|
||||
verify(component.reorderChildren).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
"doesn't call reorderChildren if the priority is the lowest level",
|
||||
(game) async {
|
||||
final component = MockComponent();
|
||||
when(() => component.priority).thenReturn(0);
|
||||
|
||||
component.sendToBack();
|
||||
|
||||
verifyNever(component.reorderChildren);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('showBehindOf', () {
|
||||
flameTester.test(
|
||||
'changes the priority if it is greater than other component',
|
||||
(game) async {
|
||||
const startPriority = 2;
|
||||
final component = TestBodyComponent()..priority = startPriority;
|
||||
final otherComponent = TestBodyComponent()
|
||||
..priority = startPriority - 1;
|
||||
|
||||
component.showBehindOf(otherComponent);
|
||||
|
||||
expect(component.priority, equals(otherComponent.priority - 1));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
"doesn't change the priority if it is lower than other component",
|
||||
(game) async {
|
||||
const startPriority = 2;
|
||||
final component = TestBodyComponent()..priority = startPriority;
|
||||
final otherComponent = TestBodyComponent()
|
||||
..priority = startPriority + 1;
|
||||
|
||||
component.showBehindOf(otherComponent);
|
||||
|
||||
expect(component.priority, equals(startPriority));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'calls reorderChildren if the priority is greater than other component',
|
||||
(game) async {
|
||||
const startPriority = 2;
|
||||
final component = MockComponent();
|
||||
final otherComponent = MockComponent();
|
||||
when(() => component.priority).thenReturn(startPriority);
|
||||
when(() => otherComponent.priority).thenReturn(startPriority - 1);
|
||||
|
||||
component.showBehindOf(otherComponent);
|
||||
|
||||
verify(component.reorderChildren).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
"doesn't call reorderChildren if the priority is lower than other "
|
||||
'component',
|
||||
(game) async {
|
||||
const startPriority = 2;
|
||||
final component = MockComponent();
|
||||
final otherComponent = MockComponent();
|
||||
when(() => component.priority).thenReturn(startPriority);
|
||||
when(() => otherComponent.priority).thenReturn(startPriority + 1);
|
||||
|
||||
component.showBehindOf(otherComponent);
|
||||
|
||||
verifyNever(component.reorderChildren);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('showInFrontOf', () {
|
||||
flameTester.test(
|
||||
'changes the priority if it is lower than other component',
|
||||
(game) async {
|
||||
const startPriority = 2;
|
||||
final component = TestBodyComponent()..priority = startPriority;
|
||||
final otherComponent = TestBodyComponent()
|
||||
..priority = startPriority + 1;
|
||||
|
||||
component.showInFrontOf(otherComponent);
|
||||
|
||||
expect(component.priority, equals(otherComponent.priority + 1));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
"doesn't change the priority if it is greater than other component",
|
||||
(game) async {
|
||||
const startPriority = 2;
|
||||
final component = TestBodyComponent()..priority = startPriority;
|
||||
final otherComponent = TestBodyComponent()
|
||||
..priority = startPriority - 1;
|
||||
|
||||
component.showInFrontOf(otherComponent);
|
||||
|
||||
expect(component.priority, equals(startPriority));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'calls reorderChildren if the priority is lower than other component',
|
||||
(game) async {
|
||||
const startPriority = 2;
|
||||
final component = MockComponent();
|
||||
final otherComponent = MockComponent();
|
||||
when(() => component.priority).thenReturn(startPriority);
|
||||
when(() => otherComponent.priority).thenReturn(startPriority + 1);
|
||||
|
||||
component.showInFrontOf(otherComponent);
|
||||
|
||||
verify(component.reorderChildren).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
"doesn't call reorderChildren if the priority is greater than other "
|
||||
'component',
|
||||
(game) async {
|
||||
const startPriority = 2;
|
||||
final component = MockComponent();
|
||||
final otherComponent = MockComponent();
|
||||
when(() => component.priority).thenReturn(startPriority);
|
||||
when(() => otherComponent.priority).thenReturn(startPriority - 1);
|
||||
|
||||
component.showInFrontOf(otherComponent);
|
||||
|
||||
verifyNever(component.reorderChildren);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|