mirror of
https://github.com/flutter/samples.git
synced 2026-04-27 09:58:50 +00:00
Add game_template (#1180)
Adds a template / sample for games built in Flutter, with all the bells and whistles, like ads, in-app purchases, audio, main menu, settings, and so on. Co-authored-by: Parker Lougheed Co-authored-by: Shams Zakhour
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
// Copyright 2022, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'player_progress_persistence.dart';
|
||||
|
||||
/// An implementation of [PlayerProgressPersistence] that uses
|
||||
/// `package:shared_preferences`.
|
||||
class LocalStoragePlayerProgressPersistence extends PlayerProgressPersistence {
|
||||
final Future<SharedPreferences> instanceFuture =
|
||||
SharedPreferences.getInstance();
|
||||
|
||||
@override
|
||||
Future<int> getHighestLevelReached() async {
|
||||
final prefs = await instanceFuture;
|
||||
return prefs.getInt('highestLevelReached') ?? 0;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> saveHighestLevelReached(int level) async {
|
||||
final prefs = await instanceFuture;
|
||||
await prefs.setInt('highestLevelReached', level);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2022, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'player_progress_persistence.dart';
|
||||
|
||||
/// An in-memory implementation of [PlayerProgressPersistence].
|
||||
/// Useful for testing.
|
||||
class MemoryOnlyPlayerProgressPersistence implements PlayerProgressPersistence {
|
||||
int level = 0;
|
||||
|
||||
@override
|
||||
Future<int> getHighestLevelReached() async {
|
||||
await Future<void>.delayed(const Duration(milliseconds: 500));
|
||||
return level;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> saveHighestLevelReached(int level) async {
|
||||
await Future<void>.delayed(const Duration(milliseconds: 500));
|
||||
this.level = level;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// Copyright 2022, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
/// An interface of persistence stores for the player's progress.
|
||||
///
|
||||
/// Implementations can range from simple in-memory storage through
|
||||
/// local preferences to cloud saves.
|
||||
abstract class PlayerProgressPersistence {
|
||||
Future<int> getHighestLevelReached();
|
||||
|
||||
Future<void> saveHighestLevelReached(int level);
|
||||
}
|
||||
Reference in New Issue
Block a user