Creating AssetsCache class

This commit is contained in:
Erick Zanardo
2019-03-09 15:55:48 -03:00
parent 1562e26e28
commit 3a852ced2e
4 changed files with 32 additions and 3 deletions

View File

@ -16,7 +16,7 @@ class MyGame extends BaseGame {
final animation = await FlameAnimation.Animation.fromAsepriteData(
"chopper.png",
"./assets/chopper.json"
"chopper.json"
);
final animationComponent = AnimationComponent(200, 200, animation);

View File

@ -1,6 +1,6 @@
import 'dart:convert';
import 'package:flutter/services.dart' show rootBundle;
import 'flame.dart';
import 'sprite.dart';
/// Represents a single animation frame.
@ -110,7 +110,7 @@ class Animation {
/// [imagePath]: Source of the spritesheet animation
/// [dataPath]: Animation's exported data in json format
static Future<Animation> fromAsepriteData(String imagePath, String dataPath) async {
String content = await rootBundle.loadString(dataPath);
String content = await Flame.assets.readFile(dataPath);
Map<String, dynamic> json = jsonDecode(content);
Map<String, dynamic> jsonFrames = json["frames"];

25
lib/assets_cache.dart Normal file
View File

@ -0,0 +1,25 @@
import 'package:flutter/services.dart' show rootBundle;
class AssetsCache {
Map<String, String> textFiles = {};
void clear(String file) {
textFiles.remove(file);
}
void clearCache() {
textFiles.clear();
}
Future<String> readFile(String fileName) async {
if (!textFiles.containsKey(fileName)) {
textFiles[fileName] = await _readFile(fileName);
}
return textFiles[fileName];
}
Future<String> _readFile(String fileName) async {
return await rootBundle.loadString("assets/" + fileName);
}
}

View File

@ -7,6 +7,7 @@ import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'images.dart';
import 'assets_cache.dart';
import 'util.dart';
/// This class holds static references to some useful objects to use in your game.
@ -27,6 +28,9 @@ class Flame {
/// Access a shared instance of the [Util] class.
static Util util = new Util();
/// Access a shard instance of [AssetsCache] class.
static AssetsCache assets = new AssetsCache();
static Future<void> init(
{AssetBundle bundle,
bool fullScreen = true,