Skip to content

Commit 839e53a

Browse files
authored
refactor!: isEnabled getter and setter (#65)
Removed setEnabled and isEnabled. Also updated documentation for the new getter and setter. // Before: contact.setEnabled(true); contact.isEnabled(); // true // After: contact.isEnabled = true; contact.isEnabled; // true
1 parent c1aba67 commit 839e53a

3 files changed

Lines changed: 92 additions & 12 deletions

File tree

packages/forge2d/lib/src/dynamics/contacts/contact.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,19 +161,21 @@ abstract class Contact {
161161
this.indexB == indexA);
162162
}
163163

164-
/// Enable/disable this contact. This can be used inside the pre-solve contact
165-
/// listener. The contact is only disabled for the current time step
166-
/// (or sub-step in continuous collisions).
167-
void setEnabled(bool enable) {
168-
if (enable) {
164+
/// Enable or disable this contact.
165+
///
166+
/// This can be used inside [ContactListener.preSolve]. The contact is
167+
/// only disabled for the current time step (or sub-step in continuous
168+
/// collisions).
169+
set isEnabled(bool value) {
170+
if (value) {
169171
flags |= enabledFlag;
170172
} else {
171173
flags &= ~enabledFlag;
172174
}
173175
}
174176

175-
/// Has this contact been disabled?
176-
bool isEnabled() => (flags & enabledFlag) == enabledFlag;
177+
/// Whether this contact is enabled.
178+
bool get isEnabled => (flags & enabledFlag) == enabledFlag;
177179

178180
void resetFriction() {
179181
_friction = Contact.mixFriction(fixtureA.friction, fixtureB.friction);

packages/forge2d/lib/src/dynamics/world.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ class World {
565565
}
566566

567567
// Is this contact solid and touching?
568-
if (contact.isEnabled() == false || contact.isTouching() == false) {
568+
if (contact.isEnabled == false || contact.isTouching() == false) {
569569
continue;
570570
}
571571

@@ -681,7 +681,7 @@ class World {
681681

682682
for (final contact in contactManager.contacts) {
683683
// Is this contact disabled?
684-
if (contact.isEnabled() == false) {
684+
if (contact.isEnabled == false) {
685685
continue;
686686
}
687687

@@ -798,9 +798,9 @@ class World {
798798
++minContact.toiCount;
799799

800800
// Is the contact solid?
801-
if (minContact.isEnabled() == false || minContact.isTouching() == false) {
801+
if (minContact.isEnabled == false || minContact.isTouching() == false) {
802802
// Restore the sweeps.
803-
minContact.setEnabled(false);
803+
minContact.isEnabled = false;
804804
bodyA.sweep.set(_backup1);
805805
bodyB.sweep.set(_backup2);
806806
bodyA.synchronizeTransform();
@@ -855,7 +855,7 @@ class World {
855855
contact.update(contactManager.contactListener);
856856

857857
// Was the contact disabled by the user?
858-
if (contact.isEnabled() == false) {
858+
if (contact.isEnabled == false) {
859859
other.sweep.set(_backup1);
860860
other.synchronizeTransform();
861861
continue;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import 'package:forge2d/forge2d.dart';
2+
import 'package:mocktail/mocktail.dart';
3+
import 'package:test/test.dart';
4+
5+
class _MockFixture extends Mock implements Fixture {}
6+
7+
class _TestContact extends Contact {
8+
_TestContact(
9+
super.fixtureA,
10+
super.indexA,
11+
super.fixtureB,
12+
super.indexB,
13+
);
14+
15+
@override
16+
void evaluate(_, __, ___) => throw UnimplementedError();
17+
}
18+
19+
void main() {
20+
group('Contact', () {
21+
late Fixture fixtureA;
22+
late int indexA;
23+
late Fixture fixtureB;
24+
late int indexB;
25+
26+
setUp(() {
27+
fixtureA = _MockFixture();
28+
when(() => fixtureA.friction).thenReturn(0);
29+
when(() => fixtureA.restitution).thenReturn(0);
30+
31+
fixtureB = _MockFixture();
32+
when(() => fixtureB.friction).thenReturn(0);
33+
when(() => fixtureB.restitution).thenReturn(0);
34+
35+
indexA = 0;
36+
indexB = 0;
37+
});
38+
39+
test('can be instantiated', () {
40+
expect(
41+
_TestContact(
42+
fixtureA,
43+
indexA,
44+
fixtureB,
45+
indexB,
46+
),
47+
isA<Contact>(),
48+
);
49+
});
50+
51+
group('isEnabled', () {
52+
test('true by default', () {
53+
final contact = _TestContact(
54+
fixtureA,
55+
indexA,
56+
fixtureB,
57+
indexB,
58+
);
59+
60+
expect(contact.isEnabled, isTrue);
61+
});
62+
63+
test('can change', () {
64+
final contact = _TestContact(
65+
fixtureA,
66+
indexA,
67+
fixtureB,
68+
indexB,
69+
);
70+
71+
final newIsEnabled = !contact.isEnabled;
72+
contact.isEnabled = newIsEnabled;
73+
74+
expect(contact.isEnabled, equals(newIsEnabled));
75+
});
76+
});
77+
});
78+
}

0 commit comments

Comments
 (0)