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

Commit

Permalink
feat: preload slices (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago authored Mar 11, 2024
1 parent f665fa3 commit d92bb70
Showing 1 changed file with 42 additions and 26 deletions.
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;
}
}

0 comments on commit d92bb70

Please sign in to comment.