feat!: Add CameraComponent to FlameGame (#2740)

This PR adds the `CameraComponent` and a `World` by default to the
FlameGame.
This commit is contained in:
Lukas Klingsbo
2023-09-18 21:06:37 +02:00
committed by GitHub
parent 5be6fc8cae
commit 7c2f400076
89 changed files with 1107 additions and 821 deletions

View File

@ -39,14 +39,12 @@ Press T button to toggle player to collide with other objects.
static const mapSize = 300;
static const bricksCount = 8000;
late final CameraComponent cameraComponent;
late final Player player;
final staticLayer = StaticLayer();
@override
Future<void> onLoad() async {
super.onLoad();
final world = World();
const mapWidth = mapSize * tileSize;
const mapHeight = mapSize * tileSize;
@ -82,12 +80,11 @@ Press T button to toggle player to collide with other objects.
}
staticLayer.reRender();
cameraComponent = CameraComponent.withFixedResolution(
camera = CameraComponent.withFixedResolution(
world: world,
width: 500,
height: 250,
);
addAll([world, cameraComponent]);
player = Player(
position: Vector2.all(mapSize * tileSize / 2),
@ -95,7 +92,7 @@ Press T button to toggle player to collide with other objects.
priority: 2,
);
world.add(player);
cameraComponent.follow(player);
camera.follow(player);
final brick = Brick(
position: player.position.translated(0, -tileSize * 2),
@ -132,7 +129,7 @@ Press T button to toggle player to collide with other objects.
world.add(QuadTreeDebugComponent(collisionDetection));
world.add(LayerComponent(staticLayer));
cameraComponent.viewport.add(FpsTextComponent());
camera.viewport.add(FpsTextComponent());
}
final elapsedMicroseconds = <double>[];
@ -194,9 +191,8 @@ Press T button to toggle player to collide with other objects.
@override
void onScroll(PointerScrollInfo info) {
cameraComponent.viewfinder.zoom += info.scrollDelta.game.y.sign * 0.08;
cameraComponent.viewfinder.zoom =
cameraComponent.viewfinder.zoom.clamp(0.05, 5.0);
camera.viewfinder.zoom += info.scrollDelta.game.y.sign * 0.08;
camera.viewfinder.zoom = camera.viewfinder.zoom.clamp(0.05, 5.0);
}
}