Skip to content

Commit

Permalink
HasWorldReference -> HasWorld
Browse files Browse the repository at this point in the history
  • Loading branch information
spydon committed Sep 18, 2023
1 parent ed15c46 commit 37ba71a
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 15 deletions.
4 changes: 2 additions & 2 deletions doc/flame/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ scheduled for addition.
### Access to the World from a Component

When a component requires access to the `World` that it is attached to one can
use the `HasWorldReference` mixin.
use the `HasWorld` mixin.

Example:

```dart
class MyComponent extends Component with HasWorldReference<MyWorld>,
class MyComponent extends Component with HasWorld<MyWorld>,
TapCallbacks {
@override
void onTapDown(TapDownEvent info) {
Expand Down
1 change: 1 addition & 0 deletions packages/flame/lib/components.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export 'src/components/mixins/has_game_ref.dart' show HasGameRef;
export 'src/components/mixins/has_paint.dart';
export 'src/components/mixins/has_time_scale.dart';
export 'src/components/mixins/has_visibility.dart';
export 'src/components/mixins/has_world.dart';
export 'src/components/mixins/hoverable.dart';
export 'src/components/mixins/keyboard_handler.dart';
export 'src/components/mixins/notifier.dart';
Expand Down
1 change: 0 additions & 1 deletion packages/flame/lib/experimental.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ export 'src/experimental/geometry/shapes/rounded_rectangle.dart'
show RoundedRectangle;
export 'src/experimental/geometry/shapes/shape.dart' show Shape;
export 'src/experimental/has_game_reference.dart' show HasGameReference;
export 'src/experimental/has_world_reference.dart' show HasWorldReference;
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import 'package:flame/camera.dart';
import 'package:flame/src/components/core/component.dart';
import 'package:meta/meta.dart';

/// [HasWorldReference] mixin provides the [world] property, which is the cached
/// accessor for the world instance that this component belongs to.
/// [HasWorld] mixin provides the [world] property, which is the cached accessor
/// for the world instance that this component belongs to.
///
/// The type [T] on the mixin is the type of your world class. This type will be
/// the type of the [world] reference, and the mixin will check at runtime that
/// the actual type matches the expectation.
mixin HasWorldReference<T extends World> on Component {
mixin HasWorld<T extends World> on Component {
T? _world;

/// Reference to the [World] instance that this component belongs to.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flame/components.dart';
import 'package:flame/experimental.dart';
import 'package:flame/game.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
Expand All @@ -13,9 +12,9 @@ class _WorldReferenceWorld extends World {
}

void main() {
group('HasWorldReference', () {
group('HasWorld', () {
testWithGame(
'component with default HasWorldReference',
'component with default HasWorld',
() => FlameGame(world: _WorldReferenceWorld()),
(game) async {
final component1 = _Component<World>();
Expand All @@ -27,7 +26,7 @@ void main() {
);

testWithGame<_MyGame>(
'component with typed HasWorldReference',
'component with typed HasWorld',
_MyGame.new,
(game) async {
final component = _Component<_WorldReferenceWorld>();
Expand Down Expand Up @@ -89,20 +88,18 @@ void main() {
});
}

class _Component<T extends World> extends Component with HasWorldReference<T> {}
class _Component<T extends World> extends Component with HasWorld<T> {}

class _MyGame extends FlameGame {
_MyGame() : super(world: _WorldReferenceWorld());
}

class _FooComponent extends Component
with HasWorldReference<_WorldReferenceWorld> {
class _FooComponent extends Component with HasWorld<_WorldReferenceWorld> {
void foo() {
world.foo();
}
}

class _BarComponent extends Component
with HasWorldReference<_WorldReferenceWorld> {}
class _BarComponent extends Component with HasWorld<_WorldReferenceWorld> {}

class MockWorld extends Mock implements _WorldReferenceWorld {}

0 comments on commit 37ba71a

Please sign in to comment.