1
0
mirror of https://github.com/flutter/samples.git synced 2026-03-27 14:51:45 +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:
Filip Hracek
2022-05-10 15:08:43 +02:00
committed by GitHub
parent 5143bcf302
commit daa024a829
208 changed files with 8993 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}

View File

@@ -0,0 +1,57 @@
// 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 'dart:async';
import 'package:flutter/foundation.dart';
import 'persistence/player_progress_persistence.dart';
/// Encapsulates the player's progress.
class PlayerProgress extends ChangeNotifier {
static const maxHighestScoresPerPlayer = 10;
final PlayerProgressPersistence _store;
int _highestLevelReached = 0;
/// Creates an instance of [PlayerProgress] backed by an injected
/// persistence [store].
PlayerProgress(PlayerProgressPersistence store) : _store = store;
/// The highest level that the player has reached so far.
int get highestLevelReached => _highestLevelReached;
/// Fetches the latest data from the backing persistence store.
Future<void> getLatestFromStore() async {
final level = await _store.getHighestLevelReached();
if (level > _highestLevelReached) {
_highestLevelReached = level;
notifyListeners();
} else if (level < _highestLevelReached) {
await _store.saveHighestLevelReached(_highestLevelReached);
}
}
/// Resets the player's progress so it's like if they just started
/// playing the game for the first time.
void reset() {
_highestLevelReached = 0;
notifyListeners();
_store.saveHighestLevelReached(_highestLevelReached);
}
/// Registers [level] as reached.
///
/// If this is higher than [highestLevelReached], it will update that
/// value and save it to the injected persistence store.
void setLevelReached(int level) {
if (level > _highestLevelReached) {
_highestLevelReached = level;
notifyListeners();
unawaited(_store.saveHighestLevelReached(level));
}
}
}