mirror of https://github.com/flutter/pinball.git
refactor: priority layer (#83)
* refactor: removed findNested extensions (#77) * feat: added new Elevation for manage priority of BodyComponents * refactor: changed ball with Elevated * refactor: changed spaceship with Elevated * test: tests for Elevated * test: fix tests * chore: removed unused * test: fixed tests * refactor: changed Elevated mixin to Priority extension * test: fixed priority test changes * chore: ignore for file * test: fixed tests for PriorityX * chore: unused import * chore: removed unecessary ignore * refactor: removed unnecessary comma Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * refactor: fixed grammatical error in test named Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * refactor: moved priority set to constructors * chore: tests names * doc: doc priority * refactor: moved priority to flame dir * feat: added priority to rampopening * fix: fixed priority changes * test: fixed tests for spaceship priority changes * fix: fixed tests for priority * chore: renamed pathwayLayer and pathwayPriority to inside * Update packages/pinball_components/lib/src/components/ramp_opening.dart Co-authored-by: Alejandro Santiago <dev@alestiago.com> Co-authored-by: Alejandro Santiago <dev@alestiago.com> Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com>pull/133/head
parent
90588d95c4
commit
8b86656412
@ -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,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);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in new issue