Skip to content

Commit

Permalink
chore: apply feedback to upgrade flame 1.9.1 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcossevilla authored Sep 25, 2023
1 parent f84fc8d commit 4956a08
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 37 deletions.
21 changes: 12 additions & 9 deletions examples/standard_platformer/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@ 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;

@override
Future<void> onLoad() async {
await super.onLoad();
await loadWorldAndMap(
tileSize: 16,
tiledMapPath: 'map.tmx',
tileCameraWidth: 32,
tileCameraHeight: 16,
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions packages/leap/lib/src/entities/physical_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ class PhysicalEntity<TGame extends LeapGame> 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;
Expand Down
4 changes: 2 additions & 2 deletions packages/leap/lib/src/input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -87,7 +87,7 @@ class SimpleTapInput extends PositionComponent

@override
Future<void> onLoad() async {
size = gameRef.leapWorld.map.size;
size = (gameRef.world as LeapWorld).map.size;
return super.onLoad();
}

Expand Down
44 changes: 22 additions & 22 deletions packages/leap/lib/src/leap_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,54 @@ 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
void lifecycleStateChange(AppLifecycleState state) {
super.lifecycleStateChange(state);
final oldAppState = appState;
appState = state;
for (final c in children.query<AppLifecycleAware>()) {
c.appLifecycleStateChanged(oldAppState, state);
for (final child in children.query<AppLifecycleAware>()) {
child.appLifecycleStateChanged(oldAppState, state);
}
}

/// Tile size (width and height) in pixels.
double get tileSize => leapWorld.tileSize;

/// All the physical entities in the world.
Iterable<PhysicalEntity> get physicals => leapWorld.physicals;
Iterable<PhysicalEntity> 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<void> 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);
}
}
2 changes: 1 addition & 1 deletion packages/leap/lib/src/leap_world.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class LeapWorld extends World with HasGameRef<LeapGame> {
/// Maximum velocity of physical components per-second.
late double maxVelocity;

LeapMap get map => gameRef.map;
LeapMap get map => gameRef.leapMap;

@override
Future<void> onLoad() async {
Expand Down

0 comments on commit 4956a08

Please sign in to comment.