Adding method to remove components from the list

This commit is contained in:
Erick Zanardo
2020-06-21 10:18:04 -03:00
parent 943cb44900
commit cd5ba3b906
6 changed files with 44 additions and 24 deletions

View File

@ -1,6 +1,7 @@
# CHANGELOG # CHANGELOG
## [next] ## [next]
- Adding BaseGame#remove
## 0.22.1 ## 0.22.1
- Fix Box2DComponent render priority - Fix Box2DComponent render priority

View File

@ -45,6 +45,8 @@ A very simple `BaseGame` implementation example can be seen below:
} }
``` ```
To remove components from the list on a `BaseGame` the `remove` can be used.
## Flutter Widgets and Game instances ## Flutter Widgets and Game instances
Since a Flame game is a widget itself, it is quite easy to use Flutter widgets and Flame game together. But to make it even easier, Flame provides a `mixin` called `HasWidgetsOverlay` which will enable any Flutter widget to be show on top of your game instance, this makes it very easy to create things like a pause menu, or an inventory screen for example. Since a Flame game is a widget itself, it is quite easy to use Flutter widgets and Flame game together. But to make it even easier, Flame provides a `mixin` called `HasWidgetsOverlay` which will enable any Flutter widget to be show on top of your game instance, this makes it very easy to create things like a pause menu, or an inventory screen for example.

2
example/.gitignore vendored
View File

@ -74,3 +74,5 @@ build/
macos macos
test test
web
lib/generated_plugin_registrant.dart

View File

@ -1,15 +0,0 @@
//
// 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();
}

View File

@ -24,12 +24,6 @@ class Palette {
class Square extends PositionComponent with HasGameRef<MyGame> { class Square extends PositionComponent with HasGameRef<MyGame> {
static const SPEED = 0.25; static const SPEED = 0.25;
@override
void resize(Size size) {
x = size.width / 2;
y = size.height / 2;
}
@override @override
void render(Canvas c) { void render(Canvas c) {
prepareCanvas(c); prepareCanvas(c);
@ -53,16 +47,41 @@ class Square extends PositionComponent with HasGameRef<MyGame> {
} }
} }
class MyGame extends BaseGame with TapDetector { class MyGame extends BaseGame with DoubleTapDetector, TapDetector {
final double squareSize = 128; final double squareSize = 128;
bool running = true; bool running = true;
MyGame() { MyGame() {
add(Square()); add(Square()
..x = 100
..y = 100);
} }
@override @override
void onTap() { void onTapUp(details) {
final touchArea = Rect.fromCenter(
center: details.localPosition,
width: 20,
height: 20,
);
bool handled = false;
components.forEach((c) {
if (c is PositionComponent && c.toRect().overlaps(touchArea)) {
handled = true;
remove(c);
}
});
if (!handled) {
addLater(Square()
..x = touchArea.left
..y = touchArea.top);
}
}
@override
void onDoubleTap() {
if (running) { if (running) {
pauseEngine(); pauseEngine();
} else { } else {

View File

@ -27,6 +27,9 @@ class BaseGame extends Game {
/// Components added by the [addLater] method /// Components added by the [addLater] method
final List<Component> _addLater = []; final List<Component> _addLater = [];
/// Components to be removed on the next update
final List<Component> _removeLater = [];
/// Current screen size, updated every resize via the [resize] method hook /// Current screen size, updated every resize via the [resize] method hook
Size size; Size size;
@ -86,6 +89,11 @@ class BaseGame extends Game {
_addLater.add(c); _addLater.add(c);
} }
/// Removes a component from the components list
void remove(Component c) {
_removeLater.add(c);
}
/// This implementation of render basically calls [renderComponent] for every component, making sure the canvas is reset for each one. /// This implementation of render basically calls [renderComponent] for every component, making sure the canvas is reset for each one.
/// ///
/// You can override it further to add more custom behaviour. /// You can override it further to add more custom behaviour.
@ -119,6 +127,9 @@ class BaseGame extends Game {
/// You can override it further to add more custom behaviour. /// You can override it further to add more custom behaviour.
@override @override
void update(double t) { void update(double t) {
_removeLater.forEach((c) => components.remove(c));
_removeLater.clear();
components.addAll(_addLater); components.addAll(_addLater);
_addLater.clear(); _addLater.clear();