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

feat: preload slices #310

Merged
merged 1 commit into from
Mar 11, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:audioplayers/audioplayers.dart';
import 'package:bloc/bloc.dart';
import 'package:collection/collection.dart';
import 'package:equatable/equatable.dart';
import 'package:flame/cache.dart';
import 'package:flutter/widgets.dart';
Expand Down Expand Up @@ -29,36 +30,31 @@ class PreloadCubit extends Cubit<PreloadState> {

/// Load items sequentially allows display of what is being loaded
Future<void> loadSequentially() async {
final displayImages =
Assets.images.display.values.whereType<AssetGenImage>().toList();
final spritePaths =
Assets.images.sprites.values.map((image) => image.path).toList();

final phases = [
PreloadPhase(
'audio',
() => audio.loadAll(Assets.audio.values),
...PreloadPhase.sliced(
name: 'audio',
items: Assets.audio.values,
start: audio.loadAll,
),
PreloadPhase(
'images',
() => imageProviderCache.loadAll(
[
Assets.images.display.pauseIcon,
Assets.images.display.paperBackground,
Assets.images.display.replayIcon,
Assets.images.display.playIcon,
Assets.images.display.menuIcon,
Assets.images.display.nextIcon,
],
),
...PreloadPhase.sliced(
name: 'images',
items: displayImages,
start: imageProviderCache.loadAll,
),
PreloadPhase(
'game images',
() => images.loadAll([]),
...PreloadPhase.sliced(
name: 'sprites',
items: spritePaths,
start: images.loadAll,
),
PreloadPhase(
'maps',
() => tiled.loadAll(
[
Assets.tiles.map1,
Assets.tiles.map2,
],
),
...PreloadPhase.sliced(
name: 'maps',
items: [Assets.tiles.map1, Assets.tiles.map2],
start: tiled.loadAll,
),
];

Expand All @@ -81,4 +77,24 @@ class PreloadPhase {

final String label;
final ValueGetter<Future<void>> start;

static Iterable<PreloadPhase> sliced<T>({
required String name,
required Iterable<T> items,
required Future<void> Function(List<T> items) start,
}) {
const sliceSize = 15;
final slices = items.slices(sliceSize).toList();
final phases = <PreloadPhase>[];
for (var phaseIndex = 0; phaseIndex < slices.length; phaseIndex++) {
final phase = slices[phaseIndex];
phases.add(
PreloadPhase(
'$name ${phaseIndex + 1} of ${slices.length}',
() => start(phase),
),
);
}
return phases;
}
}
Loading