Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add option for a custom image and asset loader #2569

Merged
merged 2 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/flame_tiled/lib/src/flame_tsx_provider.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'package:flame/flame.dart';
import 'package:flutter/services.dart';
import 'package:tiled/tiled.dart';
import 'package:xml/xml.dart';

/// A implementation of [TsxProvider] use by RenderableTileMap.
///
/// It uses [Flame.bundle] and has a built-in cache for the file read.
/// It uses [Flame.bundle] or a custom asset bundle
/// and has a built-in cache for the file read.
CodeDoctorDE marked this conversation as resolved.
Show resolved Hide resolved
class FlameTsxProvider implements TsxProvider {
/// Parsed data for this tsx file.
final String data;
Expand Down Expand Up @@ -34,8 +36,11 @@ class FlameTsxProvider implements TsxProvider {
/// Parses a file returning a [FlameTsxProvider].
///
/// NOTE: this method looks for files under the path "assets/tiles/".
static Future<FlameTsxProvider> parse(String key) async {
final data = await Flame.bundle.loadString('assets/tiles/$key');
static Future<FlameTsxProvider> parse(
String key, [
AssetBundle? bundle,
]) async {
final data = await (bundle ?? Flame.bundle).loadString('assets/tiles/$key');
return FlameTsxProvider._(data, key);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flame/cache.dart';
import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/flame.dart';
Expand Down Expand Up @@ -68,13 +69,14 @@ class FlameImageLayer extends RenderableLayer<ImageLayer> {
required CameraComponent? camera,
required TiledMap map,
required Vector2 destTileSize,
Images? images,
}) async {
return FlameImageLayer(
layer: layer,
parent: parent,
map: map,
destTileSize: destTileSize,
image: await Flame.images.load(layer.image.source!),
image: await (images ?? Flame.images).load(layer.image.source!),
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flame/cache.dart';
import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame_tiled/src/renderable_layers/group_layer.dart';
Expand Down Expand Up @@ -35,6 +36,7 @@ abstract class RenderableLayer<T extends Layer> {
required Map<Tile, TileFrames> animationFrames,
required TiledAtlas atlas,
bool? ignoreFlip,
Images? images,
}) async {
if (layer is TileLayer) {
return FlameTileLayer.load(
Expand All @@ -53,6 +55,7 @@ abstract class RenderableLayer<T extends Layer> {
camera: camera,
map: map,
destTileSize: destTileSize,
images: images,
);
} else if (layer is ObjectGroup) {
return ObjectLayer.load(
Expand Down
18 changes: 17 additions & 1 deletion packages/flame_tiled/lib/src/renderable_tile_map.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:flame/cache.dart';
import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/flame.dart';
Expand All @@ -13,6 +14,7 @@ import 'package:flame_tiled/src/tile_animation.dart';
import 'package:flame_tiled/src/tile_atlas.dart';
import 'package:flame_tiled/src/tile_stack.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/services.dart';
import 'package:tiled/tiled.dart';

/// {@template _renderable_tiled_map}
Expand Down Expand Up @@ -201,13 +203,17 @@ class RenderableTiledMap {
Vector2 destTileSize, {
CameraComponent? camera,
bool? ignoreFlip,
Images? images,
AssetBundle? bundle,
}) async {
final contents = await Flame.bundle.loadString('assets/tiles/$fileName');
return fromString(
contents,
destTileSize,
camera: camera,
ignoreFlip: ignoreFlip,
images: images,
bundle: bundle,
);
}

Expand All @@ -219,16 +225,20 @@ class RenderableTiledMap {
Vector2 destTileSize, {
CameraComponent? camera,
bool? ignoreFlip,
Images? images,
AssetBundle? bundle,
}) async {
final map = await TiledMap.fromString(
contents,
FlameTsxProvider.parse,
(key) => FlameTsxProvider.parse(key, bundle),
);
return fromTiledMap(
map,
destTileSize,
camera: camera,
ignoreFlip: ignoreFlip,
images: images,
bundle: bundle,
);
}

Expand All @@ -240,6 +250,8 @@ class RenderableTiledMap {
Vector2 destTileSize, {
CameraComponent? camera,
bool? ignoreFlip,
Images? images,
AssetBundle? bundle,
}) async {
// We're not going to load animation frames that are never referenced; but
// we do supply the common cache for all layers in this map, and maintain
Expand All @@ -259,6 +271,7 @@ class RenderableTiledMap {
animationFrames,
atlas: await TiledAtlas.fromTiledMap(map),
ignoreFlip: ignoreFlip,
images: images,
);

return RenderableTiledMap(
Expand All @@ -279,6 +292,7 @@ class RenderableTiledMap {
Map<Tile, TileFrames> animationFrames, {
required TiledAtlas atlas,
bool? ignoreFlip,
Images? images,
}) async {
final visibleLayers = layers.where((layer) => layer.visible);

Expand All @@ -292,6 +306,7 @@ class RenderableTiledMap {
animationFrames: animationFrames,
atlas: atlas,
ignoreFlip: ignoreFlip,
images: images,
);

if (layer is Group && renderableLayer is GroupLayer) {
Expand All @@ -304,6 +319,7 @@ class RenderableTiledMap {
animationFrames,
atlas: atlas,
ignoreFlip: ignoreFlip,
images: images,
);
}

Expand Down