diff --git a/CHANGELOG.md b/CHANGELOG.md index ed6a74c44..05fb0a786 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# CHANGELOG + +## 1.19.1 + - Bump AudioPlayers version to allow for web support + - Adding Game#pauseEngine and Game#resumeEngine methods + - Removing FlameBinding since it isn't used and clashes with newest flutter + ## 0.19.0 - Fixing component lifecycle calls on BaseGame#addLater - Fixing Component#onDestroy, which was been called multiple times sometimes diff --git a/LICENSE.md b/LICENSE similarity index 100% rename from LICENSE.md rename to LICENSE diff --git a/doc/README.md b/doc/README.md index bae1471f0..2dad7aafd 100644 --- a/doc/README.md +++ b/doc/README.md @@ -23,7 +23,7 @@ Check out the [awesome flame repository](https://github.com/flame-engine/awesome ```yaml dependencies: - flame: ^0.19.0 + flame: ^0.19.1 ``` And start using it! diff --git a/example/lib/generated_plugin_registrant.dart b/example/lib/generated_plugin_registrant.dart new file mode 100644 index 000000000..2fe34b2c2 --- /dev/null +++ b/example/lib/generated_plugin_registrant.dart @@ -0,0 +1,15 @@ +// +// Generated file. Do not edit. +// + +// ignore: unused_import +import 'dart:ui'; + +import 'package:audioplayers/audioplayers_web.dart'; + +import 'package:flutter_web_plugins/flutter_web_plugins.dart'; + +void registerPlugins(PluginRegistry registry) { + AudioplayersPlugin.registerWith(registry.registrarFor(AudioplayersPlugin)); + registry.registerMessageHandler(); +} diff --git a/example/lib/main.dart b/example/lib/main.dart index 03b85057b..824d11f5c 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -2,13 +2,18 @@ import 'dart:math' as math; import 'dart:ui'; import 'package:flame/anchor.dart'; +import 'package:flame/gestures.dart'; import 'package:flame/components/component.dart'; import 'package:flame/components/mixins/has_game_ref.dart'; import 'package:flame/game.dart'; import 'package:flame/palette.dart'; import 'package:flutter/material.dart'; -void main() => runApp(MyGame().widget); +void main() { + final game = MyGame(); + + runApp(game.widget); +} class Palette { static const PaletteEntry white = BasicPalette.white; @@ -47,10 +52,22 @@ class Square extends PositionComponent with HasGameRef { } } -class MyGame extends BaseGame { +class MyGame extends BaseGame with TapDetector { final double squareSize = 128; + bool running = true; MyGame() { add(Square()); } + + @override + void onTap() { + if (running) { + pauseEngine(); + } else { + resumeEngine(); + } + + running = !running; + } } diff --git a/lib/flame.dart b/lib/flame.dart index 6b67541f5..619fb73d7 100644 --- a/lib/flame.dart +++ b/lib/flame.dart @@ -54,23 +54,7 @@ class Flame { _bundle = bundle; } - /// TODO verify if this is still needed (I don't think so) static void initializeWidget() { WidgetsFlutterBinding.ensureInitialized(); } } - -/// This class never needs to be used. -/// -/// It only exists here in order for [BindingBase] to setup Flutter services. -/// TODO: this could possibly be private, verify if it'd work. -class FlameBiding extends BindingBase with GestureBinding, ServicesBinding { - static FlameBiding instance; - - static FlameBiding ensureInitialized() { - if (FlameBiding.instance == null) { - FlameBiding(); - } - return FlameBiding.instance; - } -} diff --git a/lib/game/game.dart b/lib/game/game.dart index e2f1670af..32f656022 100644 --- a/lib/game/game.dart +++ b/lib/game/game.dart @@ -89,6 +89,18 @@ abstract class Game { RawKeyboard.instance.removeListener(_handleKeyEvent); } } + + /// Flag to tell the game loop if it should start running upon creation + bool runOnCreation = true; + + /// Pauses the engine game loop execution + void pauseEngine() => _pauseEngineFn?.call(); + + /// Resumes the engine game loop execution + void resumeEngine() => _resumeEngineFn?.call(); + + VoidCallback _pauseEngineFn; + VoidCallback _resumeEngineFn; } class OverlayWidget { @@ -334,6 +346,7 @@ class GameRenderBox extends RenderBox with WidgetsBindingObserver { Game game; int _frameCallbackId; + bool _running = false; Duration previous = Duration.zero; @@ -353,7 +366,19 @@ class GameRenderBox extends RenderBox with WidgetsBindingObserver { super.attach(owner); game.onAttach(); - _scheduleTick(); + game._pauseEngineFn = () { + if (_running) { + previous = Duration.zero; + _unscheduleTick(); + } + }; + + game._resumeEngineFn = () { + if (!_running) _scheduleTick(); + }; + + if (game.runOnCreation) _scheduleTick(); + _bindLifecycleListener(); } @@ -366,11 +391,15 @@ class GameRenderBox extends RenderBox with WidgetsBindingObserver { } void _scheduleTick() { + _running = true; _frameCallbackId = SchedulerBinding.instance.scheduleFrameCallback(_tick); } void _unscheduleTick() { - SchedulerBinding.instance.cancelFrameCallbackWithId(_frameCallbackId); + _running = false; + if (_frameCallbackId != null) { + SchedulerBinding.instance.cancelFrameCallbackWithId(_frameCallbackId); + } } void _tick(Duration timestamp) { diff --git a/pubspec.yaml b/pubspec.yaml index 119edc1f3..94913b7ad 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,12 +1,12 @@ name: flame description: A minimalist Flutter game engine, provides a nice set of somewhat independent modules you can choose from. -version: 0.19.0 +version: 0.19.1 homepage: https://github.com/flame-engine/flame dependencies: flutter: sdk: flutter - audioplayers: ^0.15.0 + audioplayers: ^0.15.1 ordered_set: ^2.0.0 path_provider: ^1.6.0 box2d_flame: ^0.4.5