Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
feat: remove TiledComponent usage (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago authored Mar 11, 2024
1 parent 1347bdc commit 2556677
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 138 deletions.
Binary file added packages/trashy_road/assets/images/maps/map1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/trashy_road/assets/images/maps/map2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 18 additions & 39 deletions packages/trashy_road/lib/gen/assets.gen.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<void> 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<TrashyRoadGame>,
FlameBlocReader<GameBloc, GameState> {
late final Sprite _sprite;

late MapBounds bounds;
static final Map<String, String> _floorImages = UnmodifiableMapView({
'map1': Assets.images.maps.map1.path,
'map2': Assets.images.maps.map2.path,
});

@override
FutureOr<void> onLoad() async {
Future<void> 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<ObjectGroup>(name);
final objectGroup = layerByName(name) as ObjectGroup?;
if (objectGroup == null) {
throw ArgumentError.value(
name,
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class VehicleSpawningBehavior extends Behavior<RoadLane>
final world = ancestors().whereType<TrashyRoadWorld>().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));
Expand All @@ -48,8 +48,8 @@ class VehicleSpawningBehavior extends Behavior<RoadLane>

@override
void update(double dt) {
final world = ancestors().whereType<TrashyRoadWorld>().first;
final bounds = world.bounds;
final bounds = game.bounds;
if (bounds == null) return;

final vehicles = parent.children.whereType<Vehicle>();
for (final vehicle in vehicles) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
71 changes: 19 additions & 52 deletions packages/trashy_road/lib/src/game/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -55,7 +54,7 @@ class TrashyRoadGame extends FlameGame

late final Player _player;

TrashyRoadWorld? _trashyRoadWorld;
MapBounds? bounds;

@override
Color backgroundColor() {
Expand All @@ -73,37 +72,38 @@ class TrashyRoadGame extends FlameGame
FutureOr<void> 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<TrashyRoadWorld>();

final trashyRoadWorld = TrashyRoadWorld(tileMap: tiledMap);
final blocProvider = FlameBlocProvider<GameBloc, GameState>(
create: () => _gameBloc,
children: [
ZCanvasComponent(
children: [
trashyRoadWorld,
],
children: [trashyRoadWorld],
),
],
);

world.add(blocProvider);
await world.add(blocProvider);

_player = trashyRoadWorld.tiled.children.whereType<Player>().first;
camera.follow(_player);
_updateBounds();
// TODO(alestiago): Refactor this and properly await loading.
unawaited(
trashyRoadWorld.loaded.then((_) {
_player = trashyRoadWorld.children.whereType<Player>().first;
camera.follow(_player);
_updateBounds();
}),
);
}

void _updateBounds() {
final worldBounds = _trashyRoadWorld?.bounds;
final worldBounds = bounds;
if (worldBounds == null) return;

final viewportHalf = camera.viewport.size / 2;
Expand Down Expand Up @@ -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,
);
}
}
Loading

0 comments on commit 2556677

Please sign in to comment.