Skip to content

Commit

Permalink
refactor!: gravity getter and setter (#66)
Browse files Browse the repository at this point in the history
Removed `setGravity` and `getGravity` and replaced them with setter and getter
  • Loading branch information
alestiago authored Nov 3, 2023
1 parent fd9f8c8 commit 5108e30
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
25 changes: 20 additions & 5 deletions packages/forge2d/lib/src/dynamics/world.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,27 @@ class World {
final List<Joint> joints = <Joint>[];

final Vector2 _gravity;

/// The current [World]'s [gravity].
///
/// {@template World.gravity}
/// All [bodies] are affected by [gravity]; unless the [Body] has a specified
/// [Body.gravityOverride]. If so, [Body.gravityOverride] is used instead of
/// [gravity].
///
/// See also:
///
/// * [Body.gravityScale], to multipy [gravity] for a [Body].
/// * [Body.gravityOverride], to change how the world treats the gravity for
/// a [Body].
/// {@endtemplate}
Vector2 get gravity => _gravity;

/// Changes the [World]'s gravity.
///
/// {@macro World.gravity}
set gravity(Vector2 gravity) => this.gravity.setFrom(gravity);

bool _allowSleep = false;

DestroyListener? destroyListener;
Expand Down Expand Up @@ -477,11 +497,6 @@ class World {
/// Gets the quality of the dynamic tree
double getTreeQuality() => contactManager.broadPhase.getTreeQuality();

/// Change the global gravity vector.
void setGravity(Vector2 gravity) {
_gravity.setFrom(gravity);
}

/// Is the world locked (in the middle of a time step).
bool get isLocked => (flags & locked) == locked;

Expand Down
11 changes: 11 additions & 0 deletions packages/forge2d/test/dynamics/world_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ class AnotherRevoluteJoint extends RevoluteJoint {
}

void main() {
group('gravity', () {
test('can change', () {
final world = World();

final newGravity = world.gravity.clone()..x += 1;
world.gravity = newGravity;

expect(world.gravity, equals(newGravity));
});
});

group(
'createJoint',
() {
Expand Down

0 comments on commit 5108e30

Please sign in to comment.