diff --git a/examples/standard_platformer/lib/main.dart b/examples/standard_platformer/lib/main.dart index 1abd957..7998fc1 100644 --- a/examples/standard_platformer/lib/main.dart +++ b/examples/standard_platformer/lib/main.dart @@ -11,13 +11,17 @@ import 'package:leap_standard_platformer/welcome_dialog.dart'; void main() { runApp( GameWidget( - game: ExamplePlatformerLeapGame(), + game: ExamplePlatformerLeapGame( + tileSize: 16, + ), ), ); } class ExamplePlatformerLeapGame extends LeapGame with TapCallbacks, HasKeyboardHandlerComponents { + ExamplePlatformerLeapGame({required super.tileSize}); + late final Player player; late final SimpleCombinedInput input; @@ -25,7 +29,6 @@ class ExamplePlatformerLeapGame extends LeapGame Future onLoad() async { await super.onLoad(); await loadWorldAndMap( - tileSize: 16, tiledMapPath: 'map.tmx', tileCameraWidth: 32, tileCameraHeight: 16, @@ -35,24 +38,24 @@ class ExamplePlatformerLeapGame extends LeapGame add(input); player = Player(); - cameraComponent.world?.add(player); - cameraComponent.follow(player); + camera.world!.add(player); + camera.follow(player); if (!FlameAudio.bgm.isPlaying) { FlameAudio.bgm.play('village_music.mp3'); } - cameraComponent.viewport.add(Hud()); - cameraComponent.viewport.add( + camera.viewport.add(Hud()); + camera.viewport.add( WelcomeDialog( position: Vector2( - cameraComponent.viewport.size.x * 0.5, - cameraComponent.viewport.size.y * 0.9, + camera.viewport.size.x * 0.5, + camera.viewport.size.y * 0.9, ), ), ); - await Coin.loadAllInMap(map); + await Coin.loadAllInMap(leapMap); } @override diff --git a/packages/leap/lib/src/entities/physical_entity.dart b/packages/leap/lib/src/entities/physical_entity.dart index 41c5c01..03e37ec 100644 --- a/packages/leap/lib/src/entities/physical_entity.dart +++ b/packages/leap/lib/src/entities/physical_entity.dart @@ -60,10 +60,10 @@ class PhysicalEntity extends PositionedEntity ), ); - /// NOTE: Can only be accessed after component tree has been to the [LeapGame] - LeapMap get map => gameRef.map; + /// Can only be accessed after component tree has been to the [LeapGame]. + LeapMap get map => gameRef.leapMap; - LeapWorld get world => gameRef.leapWorld; + LeapWorld get world => gameRef.world as LeapWorld; /// Tile size (width and height) in pixels double get tileSize => gameRef.tileSize; diff --git a/packages/leap/lib/src/input.dart b/packages/leap/lib/src/input.dart index 07b9cf5..63e0428 100644 --- a/packages/leap/lib/src/input.dart +++ b/packages/leap/lib/src/input.dart @@ -3,7 +3,7 @@ import 'dart:ui'; import 'package:flame/components.dart'; import 'package:flame/events.dart'; import 'package:flutter/services.dart'; -import 'package:leap/src/leap_game.dart'; +import 'package:leap/leap.dart'; import 'package:leap/src/mixins/mixins.dart'; /// Combines touch screen and keyboard input into one API. @@ -87,7 +87,7 @@ class SimpleTapInput extends PositionComponent @override Future onLoad() async { - size = gameRef.leapWorld.map.size; + size = (gameRef.world as LeapWorld).map.size; return super.onLoad(); } diff --git a/packages/leap/lib/src/leap_game.dart b/packages/leap/lib/src/leap_game.dart index 2bd50ec..b95ddd6 100644 --- a/packages/leap/lib/src/leap_game.dart +++ b/packages/leap/lib/src/leap_game.dart @@ -9,11 +9,15 @@ import 'package:leap/src/mixins/mixins.dart'; /// A [FlameGame] with all the Leap built-ins. class LeapGame extends FlameGame with HasTrackedComponents { - LeapGame() : appState = AppLifecycleState.resumed; + LeapGame({ + required this.tileSize, + this.appState = AppLifecycleState.resumed, + }) : super(world: LeapWorld(tileSize: tileSize)); + + late final LeapMap leapMap; + + final double tileSize; - late final LeapMap map; - late final LeapWorld leapWorld; - late final CameraComponent cameraComponent; AppLifecycleState appState; @override @@ -21,42 +25,38 @@ class LeapGame extends FlameGame with HasTrackedComponents { super.lifecycleStateChange(state); final oldAppState = appState; appState = state; - for (final c in children.query()) { - c.appLifecycleStateChanged(oldAppState, state); + for (final child in children.query()) { + child.appLifecycleStateChanged(oldAppState, state); } } - /// Tile size (width and height) in pixels. - double get tileSize => leapWorld.tileSize; - /// All the physical entities in the world. - Iterable get physicals => leapWorld.physicals; + Iterable get physicals => (world as LeapWorld).physicals; - /// Initializes and loads the [leapWorld] and [map] components with a Tiled map, - /// the map file is loaded from "assets/tiled/[tiledMapPath]", and should - /// use tile size [tileSize]. + /// Initializes and loads the [world] and [leapMap] components + /// with a Tiled map. + /// + /// The map file should be loaded from "assets/tiled/[tiledMapPath]", + /// and use tile size [tileSize]. Future loadWorldAndMap({ required String tiledMapPath, - required double tileSize, required int tileCameraWidth, required int tileCameraHeight, }) async { - final flameWorld = World(); - await add(flameWorld); + await add(world); // Default the camera size to the bounds of the Tiled map. - cameraComponent = CameraComponent.withFixedResolution( + camera = CameraComponent.withFixedResolution( width: tileSize * tileCameraWidth, height: tileSize * tileCameraHeight, - world: flameWorld, + world: world, ); - await add(cameraComponent); + await add(camera); // These two classes reference each other, so the order matters here to // load properly. - leapWorld = LeapWorld(tileSize: tileSize); - map = await LeapMap.load(tiledMapPath, tileSize); + leapMap = await LeapMap.load(tiledMapPath, tileSize); - await flameWorld.addAll([map, leapWorld]); + await world.add(leapMap); } } diff --git a/packages/leap/lib/src/leap_world.dart b/packages/leap/lib/src/leap_world.dart index 539b8fb..0bf46a0 100644 --- a/packages/leap/lib/src/leap_world.dart +++ b/packages/leap/lib/src/leap_world.dart @@ -24,7 +24,7 @@ class LeapWorld extends World with HasGameRef { /// Maximum velocity of physical components per-second. late double maxVelocity; - LeapMap get map => gameRef.map; + LeapMap get map => gameRef.leapMap; @override Future onLoad() async {