Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Loadable mixin no longer declares onMount and onRemove #1243

Merged
merged 11 commits into from
Dec 27, 2021
24 changes: 21 additions & 3 deletions packages/flame/lib/src/components/component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import 'cache/value_cache.dart';
/// called automatically once the component is added to the component tree in
/// your game (with `game.add`).
class Component with Loadable {
Component({int? priority}) : _priority = priority ?? 0;

/// What coordinate system this component should respect (i.e. should it
/// observe camera, viewport, or use the raw canvas).
///
Expand Down Expand Up @@ -115,7 +117,23 @@ class Component with Loadable {
return _debugTextPaintCache.value!;
}

Component({int? priority}) : _priority = priority ?? 0;
//#region Component lifecycle methods

/// Called after the component has finished running its [onLoad] method and
/// when the component is added to its new parent.
///
/// Whenever [onMount] returns something, the parent will wait for the future
/// to be resolved before adding it. If `null` is returned, the class is
/// added right away.
///
/// Example:
/// ```dart
/// @override
/// void onMount() {
/// position = parent!.size / 2;
/// }
/// ```
void onMount() {}

/// This method is called periodically by the game engine to request that your
/// component updates itself.
Expand Down Expand Up @@ -153,6 +171,8 @@ class Component with Loadable {
}
}

//#endregion

void renderDebugMode(Canvas canvas) {}

@protected
Expand Down Expand Up @@ -202,10 +222,8 @@ class Component with Loadable {
}

/// Called right before the component is removed from the game.
@override
@mustCallSuper
void onRemove() {
super.onRemove();
_children?.forEach((child) => child.onRemove());
isPrepared = false;
isMounted = false;
Expand Down
6 changes: 6 additions & 0 deletions packages/flame/lib/src/game/mixins/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ mixin Game on Loadable {
/// Use for calculating the FPS.
void onTimingsCallback(List<FrameTiming> timings) {}

void onMount() {}
st-pasha marked this conversation as resolved.
Show resolved Hide resolved

/// Marks game as attached to any Flutter widget tree.
///
/// Should not be called manually.
Expand Down Expand Up @@ -126,6 +128,10 @@ mixin Game on Loadable {
onDetach();
}

/// Called when the game is about to be removed from the Flutter widget tree,
/// but before it is actually removed.
void onRemove() {}

/// Called after the game has left the widget tree.
/// This can be overridden to add logic that requires the game
/// not be on the flutter widget tree anymore.
Expand Down
26 changes: 0 additions & 26 deletions packages/flame/lib/src/game/mixins/loadable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import '../../../game.dart';
/// What it provides in practice is a cache for [onLoad], so that a
/// component/class can be certain that [onLoad] only runs once which then gives
/// the possibility to do late initializations in [onLoad].
///
/// It also provides empty implementations of [onMount] and [onRemove] which are
/// called when the implementing class/component is added or removed from a
/// parent/widget, in that respective order.
mixin Loadable {
/// This receives the new bounding size from its parent, which could be for
/// example a [GameWidget] or a `Component`.
Expand Down Expand Up @@ -41,26 +37,4 @@ mixin Loadable {
@internal
@nonVirtual
late Future<void>? onLoadCache = onLoad();

/// Called after the component has successfully run [onLoad] and before the
/// component is added to its new parent.
///
/// Whenever [onMount] returns something, the parent will wait for the
/// [Future] to be resolved before adding it.
/// If `null` is returned, the class is added right away.
///
/// This can be overwritten to add custom logic to the component's mounting.
///
/// Example:
/// ```dart
/// @override
/// void onMount() {
/// position = parent!.size / 2;
/// }
/// ```
void onMount() {}

/// Called when the class is removed from its parent.
/// The parent could be for example a [GameWidget] or a `Component`.
void onRemove() {}
}
1 change: 1 addition & 0 deletions packages/flame_bloc/test/flame_bloc_game_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ void main() {
game.add(component);

expect(component.state, equals(InventoryState.sword));
game.remove(component);
st-pasha marked this conversation as resolved.
Show resolved Hide resolved
},
);

Expand Down