Skip to content

Commit

Permalink
Upgrade to flame 1.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ufrshubham committed Oct 13, 2023
1 parent 0c8452c commit f07ba47
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 87 deletions.
4 changes: 2 additions & 2 deletions lib/game/dino.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum DinoAnimationStates {

// This represents the dino character of this game.
class Dino extends SpriteAnimationGroupComponent<DinoAnimationStates>
with CollisionCallbacks, HasGameRef<DinoRun> {
with CollisionCallbacks, HasGameReference<DinoRun> {
// A map of all the animation states and their corresponding animations.
static final _animationMap = {
DinoAnimationStates.idle: SpriteAnimationData.sequenced(
Expand Down Expand Up @@ -161,7 +161,7 @@ class Dino extends SpriteAnimationGroupComponent<DinoAnimationStates>
removeFromParent();
}
anchor = Anchor.bottomLeft;
position = Vector2(32, gameRef.size.y - 22);
position = Vector2(32, game.virtualSize.y - 22);
size = Vector2.all(24);
current = DinoAnimationStates.run;
isHit = false;
Expand Down
25 changes: 17 additions & 8 deletions lib/game/dino_run.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:flame/events.dart';
import 'package:flame/flame.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:hive/hive.dart';
Expand All @@ -16,6 +18,8 @@ import '/widgets/game_over_menu.dart';

// This is the main flame game class.
class DinoRun extends FlameGame with TapDetector, HasCollisionDetection {
DinoRun({super.camera});

// List of all the image assets.
static const _imageAssets = [
'DinoSprites - tard.png',
Expand All @@ -42,9 +46,15 @@ class DinoRun extends FlameGame with TapDetector, HasCollisionDetection {
late PlayerData playerData;
late EnemyManager _enemyManager;

Vector2 get virtualSize => camera.viewport.virtualSize;

// This method get called while flame is preparing this game.
@override
Future<void> onLoad() async {
// Makes the game full screen and landscape only.
await Flame.device.fullScreen();
await Flame.device.setLandscape();

/// Read [PlayerData] and [Settings] from hive.
playerData = await _readPlayerData();
settings = await _readSettings();
Expand All @@ -59,10 +69,8 @@ class DinoRun extends FlameGame with TapDetector, HasCollisionDetection {
// Cache all the images.
await images.loadAll(_imageAssets);

// Set a fixed viewport to avoid manually scaling
// and handling different screen sizes.
// ignore: deprecated_member_use
camera.viewport = FixedResolutionViewport(Vector2(360, 180));
// This makes the camera look at the center of the viewport.
camera.viewfinder.position = camera.viewport.virtualSize * 0.5;

/// Create a [ParallaxComponent] and add it to game.
final parallaxBackground = await loadParallaxComponent(
Expand All @@ -77,9 +85,9 @@ class DinoRun extends FlameGame with TapDetector, HasCollisionDetection {
baseVelocity: Vector2(10, 0),
velocityMultiplierDelta: Vector2(1.4, 0),
);
add(parallaxBackground);

return super.onLoad();
// Add the parallax as the backdrop.
camera.backdrop.add(parallaxBackground);
}

/// This method add the already created [Dino]
Expand All @@ -88,8 +96,8 @@ class DinoRun extends FlameGame with TapDetector, HasCollisionDetection {
_dino = Dino(images.fromCache('DinoSprites - tard.png'), playerData);
_enemyManager = EnemyManager();

add(_dino);
add(_enemyManager);
world.add(_dino);
world.add(_enemyManager);
}

// This method remove all the actors from the game.
Expand Down Expand Up @@ -181,6 +189,7 @@ class DinoRun extends FlameGame with TapDetector, HasCollisionDetection {
case AppLifecycleState.paused:
case AppLifecycleState.detached:
case AppLifecycleState.inactive:
case AppLifecycleState.hidden:
// If game is active, then remove Hud and add PauseMenu
// before pausing the game.
if (overlays.isActive(Hud.id)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/game/enemy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import '/models/enemy_data.dart';

// This represents an enemy in the game world.
class Enemy extends SpriteAnimationComponent
with CollisionCallbacks, HasGameRef<DinoRun> {
with CollisionCallbacks, HasGameReference<DinoRun> {
// The data required for creation of this enemy.
final EnemyData enemyData;

Expand Down Expand Up @@ -46,7 +46,7 @@ class Enemy extends SpriteAnimationComponent
// by 1, if enemy has gone past left end of the screen.
if (position.x < -enemyData.textureSize.x) {
removeFromParent();
gameRef.playerData.currentScore += 1;
game.playerData.currentScore += 1;
}

super.update(dt);
Expand Down
16 changes: 8 additions & 8 deletions lib/game/enemy_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import '/models/enemy_data.dart';

// This class is responsible for spawning random enemies at certain
// interval of time depending upon players current score.
class EnemyManager extends Component with HasGameRef<DinoRun> {
class EnemyManager extends Component with HasGameReference<DinoRun> {
// A list to hold data for all the enemies.
final List<EnemyData> _data = [];

Expand All @@ -32,8 +32,8 @@ class EnemyManager extends Component with HasGameRef<DinoRun> {
// Help in setting all enemies on ground.
enemy.anchor = Anchor.bottomLeft;
enemy.position = Vector2(
gameRef.size.x + 32,
gameRef.size.y - 24,
game.virtualSize.x + 32,
game.virtualSize.y - 24,
);

// If this enemy can fly, set its y position randomly.
Expand All @@ -45,7 +45,7 @@ class EnemyManager extends Component with HasGameRef<DinoRun> {
// Due to the size of our viewport, we can
// use textureSize as size for the components.
enemy.size = enemyData.textureSize;
gameRef.add(enemy);
game.world.add(enemy);
}

@override
Expand All @@ -59,23 +59,23 @@ class EnemyManager extends Component with HasGameRef<DinoRun> {
// As soon as this component is mounted, initilize all the data.
_data.addAll([
EnemyData(
image: gameRef.images.fromCache('AngryPig/Walk (36x30).png'),
image: game.images.fromCache('AngryPig/Walk (36x30).png'),
nFrames: 16,
stepTime: 0.1,
textureSize: Vector2(36, 30),
speedX: 80,
canFly: false,
),
EnemyData(
image: gameRef.images.fromCache('Bat/Flying (46x30).png'),
image: game.images.fromCache('Bat/Flying (46x30).png'),
nFrames: 7,
stepTime: 0.1,
textureSize: Vector2(46, 30),
speedX: 100,
canFly: true,
),
EnemyData(
image: gameRef.images.fromCache('Rino/Run (52x34).png'),
image: game.images.fromCache('Rino/Run (52x34).png'),
nFrames: 6,
stepTime: 0.09,
textureSize: Vector2(52, 34),
Expand All @@ -95,7 +95,7 @@ class EnemyManager extends Component with HasGameRef<DinoRun> {
}

void removeAllEnemies() {
final enemies = gameRef.children.whereType<Enemy>();
final enemies = game.world.children.whereType<Enemy>();
for (var enemy in enemies) {
enemy.removeFromParent();
}
Expand Down
31 changes: 15 additions & 16 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flame/camera.dart';
import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart';
import 'package:flame/game.dart';
import 'package:flame/flame.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

Expand All @@ -14,20 +14,12 @@ import 'widgets/pause_menu.dart';
import 'widgets/settings_menu.dart';
import 'widgets/game_over_menu.dart';

/// This is the single instance of [DinoRun] which
/// will be reused throughout the lifecycle of the game.
DinoRun _dinoRun = DinoRun();

Future<void> main() async {
// Ensures that all bindings are initialized
// before was start calling hive and flame code
// dealing with platform channels.
WidgetsFlutterBinding.ensureInitialized();

// Makes the game full screen and landscape only.
Flame.device.fullScreen();
Flame.device.setLandscape();

// Initializes hive and register the adapters.
await initHive();
runApp(const DinoRunApp());
Expand Down Expand Up @@ -68,7 +60,7 @@ class DinoRunApp extends StatelessWidget {
),
),
home: Scaffold(
body: GameWidget(
body: GameWidget<DinoRun>.controlled(
// This will dislpay a loading bar until [DinoRun] completes
// its onLoad method.
loadingBuilder: (conetxt) => const Center(
Expand All @@ -79,15 +71,22 @@ class DinoRunApp extends StatelessWidget {
),
// Register all the overlays that will be used by this game.
overlayBuilderMap: {
MainMenu.id: (_, DinoRun gameRef) => MainMenu(gameRef),
PauseMenu.id: (_, DinoRun gameRef) => PauseMenu(gameRef),
Hud.id: (_, DinoRun gameRef) => Hud(gameRef),
GameOverMenu.id: (_, DinoRun gameRef) => GameOverMenu(gameRef),
SettingsMenu.id: (_, DinoRun gameRef) => SettingsMenu(gameRef),
MainMenu.id: (_, game) => MainMenu(game),
PauseMenu.id: (_, game) => PauseMenu(game),
Hud.id: (_, game) => Hud(game),
GameOverMenu.id: (_, game) => GameOverMenu(game),
SettingsMenu.id: (_, game) => SettingsMenu(game),
},
// By default MainMenu overlay will be active.
initialActiveOverlays: const [MainMenu.id],
game: _dinoRun,
gameFactory: () => DinoRun(
// Use a fixed resolution camera to avoid manually
// scaling and handling different screen sizes.
camera: CameraComponent.withFixedResolution(
width: 360,
height: 180,
),
),
),
),
);
Expand Down
24 changes: 12 additions & 12 deletions lib/widgets/game_over_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class GameOverMenu extends StatelessWidget {
static const id = 'GameOverMenu';

// Reference to parent game.
final DinoRun gameRef;
final DinoRun game;

const GameOverMenu(this.gameRef, {Key? key}) : super(key: key);
const GameOverMenu(this.game, {Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider.value(
value: gameRef.playerData,
value: game.playerData,
child: Center(
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5),
Expand Down Expand Up @@ -63,11 +63,11 @@ class GameOverMenu extends StatelessWidget {
),
),
onPressed: () {
gameRef.overlays.remove(GameOverMenu.id);
gameRef.overlays.add(Hud.id);
gameRef.resumeEngine();
gameRef.reset();
gameRef.startGamePlay();
game.overlays.remove(GameOverMenu.id);
game.overlays.add(Hud.id);
game.resumeEngine();
game.reset();
game.startGamePlay();
AudioManager.instance.resumeBgm();
},
),
Expand All @@ -79,10 +79,10 @@ class GameOverMenu extends StatelessWidget {
),
),
onPressed: () {
gameRef.overlays.remove(GameOverMenu.id);
gameRef.overlays.add(MainMenu.id);
gameRef.resumeEngine();
gameRef.reset();
game.overlays.remove(GameOverMenu.id);
game.overlays.add(MainMenu.id);
game.resumeEngine();
game.reset();
AudioManager.instance.resumeBgm();
},
),
Expand Down
12 changes: 6 additions & 6 deletions lib/widgets/hud.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class Hud extends StatelessWidget {
static const id = 'Hud';

// Reference to parent game.
final DinoRun gameRef;
final DinoRun game;

const Hud(this.gameRef, {Key? key}) : super(key: key);
const Hud(this.game, {Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider.value(
value: gameRef.playerData,
value: game.playerData,
child: Padding(
padding: const EdgeInsets.only(top: 10.0),
child: Row(
Expand Down Expand Up @@ -52,9 +52,9 @@ class Hud extends StatelessWidget {
),
TextButton(
onPressed: () {
gameRef.overlays.remove(Hud.id);
gameRef.overlays.add(PauseMenu.id);
gameRef.pauseEngine();
game.overlays.remove(Hud.id);
game.overlays.add(PauseMenu.id);
game.pauseEngine();
AudioManager.instance.pauseBgm();
},
child: const Icon(Icons.pause, color: Colors.white),
Expand Down
14 changes: 7 additions & 7 deletions lib/widgets/main_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class MainMenu extends StatelessWidget {
static const id = 'MainMenu';

// Reference to parent game.
final DinoRun gameRef;
final DinoRun game;

const MainMenu(this.gameRef, {Key? key}) : super(key: key);
const MainMenu(this.game, {Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -44,9 +44,9 @@ class MainMenu extends StatelessWidget {
),
ElevatedButton(
onPressed: () {
gameRef.startGamePlay();
gameRef.overlays.remove(MainMenu.id);
gameRef.overlays.add(Hud.id);
game.startGamePlay();
game.overlays.remove(MainMenu.id);
game.overlays.add(Hud.id);
},
child: const Text(
'Play',
Expand All @@ -57,8 +57,8 @@ class MainMenu extends StatelessWidget {
),
ElevatedButton(
onPressed: () {
gameRef.overlays.remove(MainMenu.id);
gameRef.overlays.add(SettingsMenu.id);
game.overlays.remove(MainMenu.id);
game.overlays.add(SettingsMenu.id);
},
child: const Text(
'Settings',
Expand Down
Loading

0 comments on commit f07ba47

Please sign in to comment.