Skip to content

Commit

Permalink
feat!: Make world nullable in CameraComponent (#2615)
Browse files Browse the repository at this point in the history
`CameraComponent` can now stare at nothingness because its world
reference can be null now.

### Migration instructions

`CameraComponent.world` is now nullable. While accessing it, make sure
to perform null checks if it can be null in your case. Otherwise, if you
are sure that the world is non-null perform unconditional using
`CameraComponent.world!`.
  • Loading branch information
ufrshubham committed Jul 19, 2023
1 parent 832c051 commit 14f5163
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 9 additions & 7 deletions packages/flame/lib/src/camera/camera_component.dart
Expand Up @@ -41,7 +41,7 @@ import 'package:vector_math/vector_math_64.dart';
/// That is, they will be affected both by the viewport and the viewfinder.
class CameraComponent extends Component {
CameraComponent({
required this.world,
this.world,
Viewport? viewport,
Viewfinder? viewfinder,
List<Component>? hudComponents,
Expand All @@ -58,9 +58,9 @@ class CameraComponent extends Component {
/// initially set up to show world coordinates (0, 0) at the center of the
/// viewport.
factory CameraComponent.withFixedResolution({
required World world,
required double width,
required double height,
World? world,
List<Component>? hudComponents,
}) {
return CameraComponent(
Expand Down Expand Up @@ -97,7 +97,7 @@ class CameraComponent extends Component {
///
/// The [world] component is generally mounted externally to the camera, and
/// this variable is a mere reference to it.
World world;
World? world;

/// The axis-aligned bounding rectangle of a [world] region which is currently
/// visible through the viewport.
Expand Down Expand Up @@ -140,13 +140,14 @@ class CameraComponent extends Component {
viewport.position.y - viewport.anchor.y * viewport.size.y,
);
// Render the world through the viewport
if (world.isMounted && currentCameras.length < maxCamerasDepth) {
if ((world?.isMounted ?? false) &&
currentCameras.length < maxCamerasDepth) {
canvas.save();
viewport.clip(canvas);
try {
currentCameras.add(this);
canvas.transform(viewfinder.transform.transformMatrix.storage);
world.renderFromCamera(canvas);
world!.renderFromCamera(canvas);
viewfinder.renderTree(canvas);
} finally {
currentCameras.removeLast();
Expand All @@ -167,11 +168,12 @@ class CameraComponent extends Component {
point.x - viewport.position.x + viewport.anchor.x * viewport.size.x,
point.y - viewport.position.y + viewport.anchor.y * viewport.size.y,
);
if (world.isMounted && currentCameras.length < maxCamerasDepth) {
if ((world?.isMounted ?? false) &&
currentCameras.length < maxCamerasDepth) {
if (viewport.containsLocalPoint(viewportPoint)) {
currentCameras.add(this);
final worldPoint = viewfinder.transform.globalToLocal(viewportPoint);
yield* world.componentsAtPoint(worldPoint, nestedPoints);
yield* world!.componentsAtPoint(worldPoint, nestedPoints);
yield* viewfinder.componentsAtPoint(worldPoint, nestedPoints);
currentCameras.removeLast();
}
Expand Down
Expand Up @@ -12,7 +12,7 @@ void main() {
world: World(),
viewport: FixedSizeViewport(300, 100),
);
game.addAll([camera.world, camera]);
game.addAll([camera.world!, camera]);
await game.ready();

expect(camera.viewport, isA<FixedSizeViewport>());
Expand All @@ -29,7 +29,7 @@ void main() {
world: World(),
viewport: FixedSizeViewport(400, 100),
);
game.addAll([camera.world, camera]);
game.addAll([camera.world!, camera]);
await game.ready();

final viewport = camera.viewport;
Expand Down

0 comments on commit 14f5163

Please sign in to comment.