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

feat!: Make world nullable in CameraComponent #2615

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions packages/flame/lib/src/camera/camera_component.dart
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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