diff --git a/packages/trashy_road/assets/images/maps/map1.png b/packages/trashy_road/assets/images/maps/map1.png new file mode 100644 index 00000000..9317e431 Binary files /dev/null and b/packages/trashy_road/assets/images/maps/map1.png differ diff --git a/packages/trashy_road/assets/images/maps/map2.png b/packages/trashy_road/assets/images/maps/map2.png new file mode 100644 index 00000000..0edc73eb Binary files /dev/null and b/packages/trashy_road/assets/images/maps/map2.png differ diff --git a/packages/trashy_road/lib/gen/assets.gen.dart b/packages/trashy_road/lib/gen/assets.gen.dart index de0828ce..6bf0f880 100644 --- a/packages/trashy_road/lib/gen/assets.gen.dart +++ b/packages/trashy_road/lib/gen/assets.gen.dart @@ -60,6 +60,9 @@ class $AssetsAudioGen { /// File path: assets/audio/step1.mp3 String get step1 => 'assets/audio/step1.mp3'; + /// File path: assets/audio/steps.mp3 + String get steps => 'assets/audio/steps.mp3'; + /// File path: assets/audio/trash_collected.mp3 String get trashCollected => 'assets/audio/trash_collected.mp3'; @@ -83,6 +86,7 @@ class $AssetsAudioGen { ratingStars3, runningTime, step1, + steps, trashCollected, wrongBin ]; @@ -92,8 +96,8 @@ class $AssetsImagesGen { const $AssetsImagesGen(); $AssetsImagesDisplayGen get display => const $AssetsImagesDisplayGen(); + $AssetsImagesMapsGen get maps => const $AssetsImagesMapsGen(); $AssetsImagesSpritesGen get sprites => const $AssetsImagesSpritesGen(); - $AssetsImagesTiledGen get tiled => const $AssetsImagesTiledGen(); } class $AssetsRiveGen { @@ -342,6 +346,19 @@ class $AssetsImagesDisplayGen { ]; } +class $AssetsImagesMapsGen { + const $AssetsImagesMapsGen(); + + /// File path: assets/images/maps/map1.png + AssetGenImage get map1 => const AssetGenImage('assets/images/maps/map1.png'); + + /// File path: assets/images/maps/map2.png + AssetGenImage get map2 => const AssetGenImage('assets/images/maps/map2.png'); + + /// List of all assets + List get values => [map1, map2]; +} + class $AssetsImagesSpritesGen { const $AssetsImagesSpritesGen(); @@ -794,44 +811,6 @@ class $AssetsImagesSpritesGen { ]; } -class $AssetsImagesTiledGen { - const $AssetsImagesTiledGen(); - - /// File path: assets/images/tiled/organic_park_trash_can.png - AssetGenImage get organicParkTrashCan => - const AssetGenImage('assets/images/tiled/organic_park_trash_can.png'); - - /// File path: assets/images/tiled/organic_trash_can.png - AssetGenImage get organicTrashCan => - const AssetGenImage('assets/images/tiled/organic_trash_can.png'); - - /// File path: assets/images/tiled/paper_park_trash_can.png - AssetGenImage get paperParkTrashCan => - const AssetGenImage('assets/images/tiled/paper_park_trash_can.png'); - - /// File path: assets/images/tiled/paper_trash_can.png - AssetGenImage get paperTrashCan => - const AssetGenImage('assets/images/tiled/paper_trash_can.png'); - - /// File path: assets/images/tiled/plastic_park_trash_can.png - AssetGenImage get plasticParkTrashCan => - const AssetGenImage('assets/images/tiled/plastic_park_trash_can.png'); - - /// File path: assets/images/tiled/plastic_trash_can.png - AssetGenImage get plasticTrashCan => - const AssetGenImage('assets/images/tiled/plastic_trash_can.png'); - - /// List of all assets - List get values => [ - organicParkTrashCan, - organicTrashCan, - paperParkTrashCan, - paperTrashCan, - plasticParkTrashCan, - plasticTrashCan - ]; -} - class Assets { Assets._(); diff --git a/packages/trashy_road/lib/src/game/components/trashy_road_world/trashy_road_world.dart b/packages/trashy_road/lib/src/game/components/trashy_road_world/trashy_road_world.dart index 542bed6d..f3966f44 100644 --- a/packages/trashy_road/lib/src/game/components/trashy_road_world/trashy_road_world.dart +++ b/packages/trashy_road/lib/src/game/components/trashy_road_world/trashy_road_world.dart @@ -1,7 +1,11 @@ import 'dart:async'; +import 'dart:collection'; +import 'dart:ui'; import 'package:flame/components.dart'; -import 'package:flame_tiled/flame_tiled.dart'; +import 'package:flame_bloc/flame_bloc.dart'; +import 'package:tiled/tiled.dart'; +import 'package:trashy_road/gen/assets.gen.dart'; import 'package:trashy_road/src/game/game.dart'; /// The different layers in the Tiled map. @@ -18,55 +22,79 @@ enum _TiledLayer { final String name; } -class TrashyRoadWorld extends Component { - TrashyRoadWorld.create({required this.tiled}) { - final borderLayer = - tiled.tileMap.getObjectGroup(_TiledLayer.borderLayer.name); - tiled.addAll(borderLayer.objects.map(MapEdge.fromTiledObject)); +class TrashyRoadWorld extends PositionComponent { + TrashyRoadWorld({ + required this.tileMap, + }); - final trashLayer = - tiled.tileMap.getObjectGroup(_TiledLayer.trashLayer.name); - tiled.addAll(trashLayer.objects.map(Trash.fromTiledObject)); + final TiledMap tileMap; + + @override + FutureOr onLoad() async { + await super.onLoad(); + + final borderLayer = tileMap.getObjectGroup(_TiledLayer.borderLayer.name); + await addAll(borderLayer.objects.map(MapEdge.fromTiledObject)); + + final trashLayer = tileMap.getObjectGroup(_TiledLayer.trashLayer.name); + await addAll(trashLayer.objects.map(Trash.fromTiledObject)); final coreItemsLayer = - tiled.tileMap.getObjectGroup(_TiledLayer.coreItemsLayer.name); + tileMap.getObjectGroup(_TiledLayer.coreItemsLayer.name); + final playerObjects = coreItemsLayer.objects.where((object) => object.type == 'player'); - tiled.addAll(playerObjects.map(Player.fromTiledObject)); + + await addAll(playerObjects.map(Player.fromTiledObject)); final trashCanObjects = coreItemsLayer.objects.where((object) => object.type == 'trash_can'); - tiled.addAll(trashCanObjects.map(TrashCan.fromTiledObject)); + await addAll(trashCanObjects.map(TrashCan.fromTiledObject)); - final roadLaneLayer = - tiled.tileMap.getObjectGroup(_TiledLayer.roadLayer.name); - tiled.addAll(roadLaneLayer.objects.map(RoadLane.fromTiledObject)); + final roadLaneLayer = tileMap.getObjectGroup(_TiledLayer.roadLayer.name); + await addAll(roadLaneLayer.objects.map(RoadLane.fromTiledObject)); - final obstaclesLayer = - tiled.tileMap.getObjectGroup(_TiledLayer.obstacles.name); - tiled.addAll(obstaclesLayer.objects.map(Obstacle.fromTiledObject)); + final obstaclesLayer = tileMap.getObjectGroup(_TiledLayer.obstacles.name); + await addAll(obstaclesLayer.objects.map(Obstacle.fromTiledObject)); - bounds = MapBounds.fromLTWH( - tiled.topLeftPosition.x, - tiled.topLeftPosition.y, - tiled.width, - tiled.height, - ); + await add(_TiledFloor()); + + size = Vector2(tileMap.width.toDouble(), tileMap.height.toDouble()); } +} - final TiledComponent tiled; +class _TiledFloor extends Component + with + HasGameReference, + FlameBlocReader { + late final Sprite _sprite; - late MapBounds bounds; + static final Map _floorImages = UnmodifiableMapView({ + 'map1': Assets.images.maps.map1.path, + 'map2': Assets.images.maps.map2.path, + }); @override - FutureOr onLoad() async { + Future onLoad() async { await super.onLoad(); - add(tiled); + + final mapIdentifier = bloc.state.identifier; + assert( + _floorImages.containsKey(mapIdentifier), + 'No floor image found for map identifier "$mapIdentifier"', + ); + final mapPath = _floorImages[mapIdentifier]!; + _sprite = await Sprite.load(mapPath, images: game.images); + } + + @override + void render(Canvas canvas) { + _sprite.render(canvas); } } -extension on RenderableTiledMap { +extension on TiledMap { ObjectGroup getObjectGroup(String name) { - final objectGroup = getLayer(name); + final objectGroup = layerByName(name) as ObjectGroup?; if (objectGroup == null) { throw ArgumentError.value( name, diff --git a/packages/trashy_road/lib/src/game/entities/obstacle/obstacle.dart b/packages/trashy_road/lib/src/game/entities/obstacle/obstacle.dart index 73b95972..0aae35cb 100644 --- a/packages/trashy_road/lib/src/game/entities/obstacle/obstacle.dart +++ b/packages/trashy_road/lib/src/game/entities/obstacle/obstacle.dart @@ -1,7 +1,7 @@ import 'package:flame/collisions.dart'; import 'package:flame/components.dart'; import 'package:flame_behaviors/flame_behaviors.dart'; -import 'package:flame_tiled/flame_tiled.dart'; +import 'package:tiled/tiled.dart'; import 'package:trashy_road/game_settings.dart'; import 'package:trashy_road/gen/assets.gen.dart'; import 'package:trashy_road/src/game/game.dart'; diff --git a/packages/trashy_road/lib/src/game/entities/player/player.dart b/packages/trashy_road/lib/src/game/entities/player/player.dart index f96ac6bf..8f54bdcd 100644 --- a/packages/trashy_road/lib/src/game/entities/player/player.dart +++ b/packages/trashy_road/lib/src/game/entities/player/player.dart @@ -4,8 +4,8 @@ import 'dart:ui' as ui; import 'package:flame/collisions.dart'; import 'package:flame/components.dart'; import 'package:flame_behaviors/flame_behaviors.dart'; -import 'package:flame_tiled/flame_tiled.dart'; import 'package:flutter/widgets.dart'; +import 'package:tiled/tiled.dart'; import 'package:trashy_road/game_settings.dart'; import 'package:trashy_road/gen/assets.gen.dart'; import 'package:trashy_road/src/game/game.dart'; diff --git a/packages/trashy_road/lib/src/game/entities/road_lane/behavior/vehicle_spawning_behavior.dart b/packages/trashy_road/lib/src/game/entities/road_lane/behavior/vehicle_spawning_behavior.dart index cbad1a96..bf07b54d 100644 --- a/packages/trashy_road/lib/src/game/entities/road_lane/behavior/vehicle_spawning_behavior.dart +++ b/packages/trashy_road/lib/src/game/entities/road_lane/behavior/vehicle_spawning_behavior.dart @@ -31,7 +31,7 @@ class VehicleSpawningBehavior extends Behavior final world = ancestors().whereType().first; for (var i = 0; i < parent.traffic; i++) { - var startPosition = (i / parent.traffic) * world.tiled.size.x; + var startPosition = (i / parent.traffic) * world.size.x; startPosition *= _minTrafficVariation + (game.random.nextDouble() * (1 - _minTrafficVariation)); @@ -48,8 +48,8 @@ class VehicleSpawningBehavior extends Behavior @override void update(double dt) { - final world = ancestors().whereType().first; - final bounds = world.bounds; + final bounds = game.bounds; + if (bounds == null) return; final vehicles = parent.children.whereType(); for (final vehicle in vehicles) { diff --git a/packages/trashy_road/lib/src/game/entities/road_lane/road_lane.dart b/packages/trashy_road/lib/src/game/entities/road_lane/road_lane.dart index 44eaff7c..55199b7b 100644 --- a/packages/trashy_road/lib/src/game/entities/road_lane/road_lane.dart +++ b/packages/trashy_road/lib/src/game/entities/road_lane/road_lane.dart @@ -1,6 +1,6 @@ import 'package:flame/components.dart'; import 'package:flame_behaviors/flame_behaviors.dart'; -import 'package:flame_tiled/flame_tiled.dart'; +import 'package:tiled/tiled.dart'; import 'package:trashy_road/game_settings.dart'; import 'package:trashy_road/src/game/game.dart'; diff --git a/packages/trashy_road/lib/src/game/entities/trash_can/trash_can.dart b/packages/trashy_road/lib/src/game/entities/trash_can/trash_can.dart index ed79626b..056a8328 100644 --- a/packages/trashy_road/lib/src/game/entities/trash_can/trash_can.dart +++ b/packages/trashy_road/lib/src/game/entities/trash_can/trash_can.dart @@ -3,8 +3,8 @@ import 'dart:async'; import 'package:flame/collisions.dart'; import 'package:flame/components.dart'; import 'package:flame_behaviors/flame_behaviors.dart'; -import 'package:flame_tiled/flame_tiled.dart'; import 'package:meta/meta.dart'; +import 'package:tiled/tiled.dart'; import 'package:trashy_road/game_settings.dart'; import 'package:trashy_road/gen/assets.gen.dart'; import 'package:trashy_road/src/game/game.dart'; diff --git a/packages/trashy_road/lib/src/game/game.dart b/packages/trashy_road/lib/src/game/game.dart index e678932e..e0cbdb6f 100644 --- a/packages/trashy_road/lib/src/game/game.dart +++ b/packages/trashy_road/lib/src/game/game.dart @@ -8,7 +8,6 @@ import 'package:flame/events.dart'; import 'package:flame/experimental.dart'; import 'package:flame/game.dart'; import 'package:flame_bloc/flame_bloc.dart'; -import 'package:flame_tiled/flame_tiled.dart'; import 'package:flutter/material.dart'; import 'package:trashy_road/game_settings.dart'; import 'package:trashy_road/src/audio/audio.dart'; @@ -55,7 +54,7 @@ class TrashyRoadGame extends FlameGame late final Player _player; - TrashyRoadWorld? _trashyRoadWorld; + MapBounds? bounds; @override Color backgroundColor() { @@ -73,37 +72,38 @@ class TrashyRoadGame extends FlameGame FutureOr onLoad() async { await super.onLoad(); - final tiledMap = _gameBloc.state.map..transformTileImagePaths(); - final renderableTiledMap = await RenderableTiledMap.fromTiledMap( - tiledMap, - GameSettings.gridDimensions, - images: images, + final tiledMap = _gameBloc.state.map; + bounds = MapBounds.fromLTWH( + 0, + 0, + tiledMap.width.toDouble() * GameSettings.gridDimensions.x, + tiledMap.height.toDouble() * GameSettings.gridDimensions.y, ); - final tiled = TiledComponent(renderableTiledMap); - final trashyRoadWorld = - _trashyRoadWorld = TrashyRoadWorld.create(tiled: tiled); - children.register(); + final trashyRoadWorld = TrashyRoadWorld(tileMap: tiledMap); final blocProvider = FlameBlocProvider( create: () => _gameBloc, children: [ ZCanvasComponent( - children: [ - trashyRoadWorld, - ], + children: [trashyRoadWorld], ), ], ); - world.add(blocProvider); + await world.add(blocProvider); - _player = trashyRoadWorld.tiled.children.whereType().first; - camera.follow(_player); - _updateBounds(); + // TODO(alestiago): Refactor this and properly await loading. + unawaited( + trashyRoadWorld.loaded.then((_) { + _player = trashyRoadWorld.children.whereType().first; + camera.follow(_player); + _updateBounds(); + }), + ); } void _updateBounds() { - final worldBounds = _trashyRoadWorld?.bounds; + final worldBounds = bounds; if (worldBounds == null) return; final viewportHalf = camera.viewport.size / 2; @@ -154,36 +154,3 @@ class TrashyRoadGame extends FlameGame camera.update(dt); } } - -extension on TiledMap { - void transformTileImagePaths() { - for (final tileset in tilesets) { - for (final tile in tileset.tiles) { - final tileImage = tile.image; - if (tileImage == null) continue; - final newTileImage = tileImage.copyWith( - source: tileImage.source?.replaceFirst('..', 'assets'), - ); - tile.image = newTileImage; - } - } - } -} - -extension on TiledImage { - TiledImage copyWith({ - String? source, - String? format, - int? width, - int? height, - String? trans, - }) { - return TiledImage( - source: source ?? this.source, - format: format ?? this.format, - width: width ?? this.width, - height: height ?? this.height, - trans: trans ?? this.trans, - ); - } -} diff --git a/packages/trashy_road/lib/src/game/view/game_page.dart b/packages/trashy_road/lib/src/game/view/game_page.dart index 1105a628..397a5ed8 100644 --- a/packages/trashy_road/lib/src/game/view/game_page.dart +++ b/packages/trashy_road/lib/src/game/view/game_page.dart @@ -1,9 +1,8 @@ import 'dart:async'; -import 'package:basura/basura.dart'; -import 'package:flame_tiled/flame_tiled.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:tiled/tiled.dart'; import 'package:trashy_road/gen/gen.dart'; import 'package:trashy_road/src/game/game.dart'; import 'package:trashy_road/src/loading/loading.dart'; @@ -25,13 +24,20 @@ class GamePage extends StatelessWidget { required String identifier, required TiledMap tiledMap, }) { - return BasuraBlackEaseInOut( - settings: RouteSettings(name: identifier), - builder: (_) => GamePage( + return PageRouteBuilder( + pageBuilder: (context, _, __) => GamePage( identifier: identifier, map: tiledMap, ), ); + + // return BasuraBlackEaseInOut( + // settings: RouteSettings(name: identifier), + // builder: (_) => GamePage( + // identifier: identifier, + // map: tiledMap, + // ), + // ); } /// The identifier of the game. diff --git a/packages/trashy_road/macos/Podfile.lock b/packages/trashy_road/macos/Podfile.lock index 6d50016d..b8b751b5 100644 --- a/packages/trashy_road/macos/Podfile.lock +++ b/packages/trashy_road/macos/Podfile.lock @@ -32,4 +32,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 236401fc2c932af29a9fcf0e97baeeb2d750d367 -COCOAPODS: 1.15.0 +COCOAPODS: 1.14.2 diff --git a/packages/trashy_road/pubspec.yaml b/packages/trashy_road/pubspec.yaml index 9648bca8..afd1fbec 100644 --- a/packages/trashy_road/pubspec.yaml +++ b/packages/trashy_road/pubspec.yaml @@ -57,7 +57,6 @@ flutter: - assets/audio/ - assets/rive/ - assets/tiles/ + - assets/images/maps/ - assets/images/display/ - assets/images/sprites/ - # tiled required beacuse flame_tiled loads these (even though we don't use them directly). - - assets/images/tiled/ diff --git a/packages/trashy_road/test/src/game/entities/player/behaviors/player_moving_behavior_test.dart b/packages/trashy_road/test/src/game/entities/player/behaviors/player_moving_behavior_test.dart index f1d061cd..6d515fbb 100644 --- a/packages/trashy_road/test/src/game/entities/player/behaviors/player_moving_behavior_test.dart +++ b/packages/trashy_road/test/src/game/entities/player/behaviors/player_moving_behavior_test.dart @@ -3,9 +3,9 @@ import 'dart:async'; import 'package:flame/game.dart'; import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_test/flame_test.dart'; -import 'package:flame_tiled/flame_tiled.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; +import 'package:tiled/tiled.dart'; import 'package:trashy_road/src/game/game.dart'; class _MockTiledMap extends Mock implements TiledMap {} diff --git a/packages/trashy_road/test/src/game/entities/player/behaviors/player_moving_keyboard_behavior_test.dart b/packages/trashy_road/test/src/game/entities/player/behaviors/player_moving_keyboard_behavior_test.dart index b7cdb139..b512e1a4 100644 --- a/packages/trashy_road/test/src/game/entities/player/behaviors/player_moving_keyboard_behavior_test.dart +++ b/packages/trashy_road/test/src/game/entities/player/behaviors/player_moving_keyboard_behavior_test.dart @@ -3,10 +3,10 @@ import 'dart:async'; import 'package:flame/game.dart'; import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_test/flame_test.dart'; -import 'package:flame_tiled/flame_tiled.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; +import 'package:tiled/tiled.dart'; import 'package:trashy_road/src/game/game.dart'; class _MockTiledMap extends Mock implements TiledMap {}