mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 20:13:50 +08:00
Merge branch 'v1.0.0' into erick.removing-deprecated-stuff
This commit is contained in:
@ -10,6 +10,8 @@
|
|||||||
- Use isRelative on effects
|
- Use isRelative on effects
|
||||||
- Use Vector2 for position and size on PositionComponent
|
- Use Vector2 for position and size on PositionComponent
|
||||||
- Removing all deprecated methods
|
- Removing all deprecated methods
|
||||||
|
- Rename `resize` method on components to `onGameResize`
|
||||||
|
- Make `Resizable` have a `gameSize` property instead of `size`
|
||||||
|
|
||||||
## [next]
|
## [next]
|
||||||
- Fix spriteAsWidget deprecation message
|
- Fix spriteAsWidget deprecation message
|
||||||
|
|||||||
2
FAQ.md
2
FAQ.md
@ -44,7 +44,7 @@ If you want a more full-fledged game, please check [BGUG](https://github.com/fir
|
|||||||
|
|
||||||
`Game` is the most barebones interface that Flame exposes. If you crete a Game, you will need to implement a lot of stuff. Flame will hook you up on the game loop, so you will get to implement `render` and `update` yourself. From scratch. If you wanna use the component system, you can, but you don't need to. You do everything yourself.
|
`Game` is the most barebones interface that Flame exposes. If you crete a Game, you will need to implement a lot of stuff. Flame will hook you up on the game loop, so you will get to implement `render` and `update` yourself. From scratch. If you wanna use the component system, you can, but you don't need to. You do everything yourself.
|
||||||
|
|
||||||
`BaseGame` extends `Game` and adds lots of functionality on top of that. Just drop your components, it works! They are rendered, updated, resized, automatically. You might still want to override some of `BaseGame`'s methods to add custom functionality, but you will probably be calling the super method to let `BaseGame` do its work.
|
`BaseGame` extends `Game` and adds lots of functionality on top of that. Just drop your components, it works! They are rendered and updated, automatically. You might still want to override some of `BaseGame`'s methods to add custom functionality, but you will probably be calling the super method to let `BaseGame` do its work.
|
||||||
|
|
||||||
## How does the Camera work?
|
## How does the Camera work?
|
||||||
|
|
||||||
|
|||||||
@ -27,7 +27,7 @@ class AndroidComponent extends SpriteComponent with Resizable {
|
|||||||
@override
|
@override
|
||||||
void update(double dt) {
|
void update(double dt) {
|
||||||
super.update(dt);
|
super.update(dt);
|
||||||
if (size == null) {
|
if (gameSize == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,14 +36,14 @@ class AndroidComponent extends SpriteComponent with Resizable {
|
|||||||
final rect = toRect();
|
final rect = toRect();
|
||||||
|
|
||||||
if ((x <= 0 && xDirection == -1) ||
|
if ((x <= 0 && xDirection == -1) ||
|
||||||
(rect.right >= size.x && xDirection == 1)) {
|
(rect.right >= gameSize.x && xDirection == 1)) {
|
||||||
xDirection = xDirection * -1;
|
xDirection = xDirection * -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
y += yDirection * SPEED * dt;
|
y += yDirection * SPEED * dt;
|
||||||
|
|
||||||
if ((y <= 0 && yDirection == -1) ||
|
if ((y <= 0 && yDirection == -1) ||
|
||||||
(rect.bottom >= size.y && yDirection == 1)) {
|
(rect.bottom >= gameSize.y && yDirection == 1)) {
|
||||||
yDirection = yDirection * -1;
|
yDirection = yDirection * -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -43,14 +43,14 @@ class Player extends Component implements JoystickListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void resize(Vector2 size) {
|
void onGameResize(Vector2 size) {
|
||||||
_rect = Rect.fromLTWH(
|
_rect = Rect.fromLTWH(
|
||||||
(size.x / 2) - 25,
|
(size.x / 2) - 25,
|
||||||
(size.y / 2) - 25,
|
(size.y / 2) - 25,
|
||||||
50,
|
50,
|
||||||
50,
|
50,
|
||||||
);
|
);
|
||||||
super.resize(size);
|
super.onGameResize(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@ -20,11 +20,11 @@ abstract class Component {
|
|||||||
/// Renders this component on the provided Canvas [c].
|
/// Renders this component on the provided Canvas [c].
|
||||||
void render(Canvas c);
|
void render(Canvas c);
|
||||||
|
|
||||||
/// This is a hook called by [BaseGame] to let this component know that the screen (or flame draw area) has been update.
|
/// It receives the new game size.
|
||||||
|
/// Executed right after the component is attached to a game and right before [onMount] is called
|
||||||
///
|
///
|
||||||
/// It receives the new size.
|
/// Use [Resizable] to save the gameSize in a component.
|
||||||
/// You can use the [Resizable] mixin if you want an implementation of this hook that keeps track of the current size.
|
void onGameResize(Vector2 gameSize) {}
|
||||||
void resize(Vector2 size) {}
|
|
||||||
|
|
||||||
/// Whether this component has been loaded yet. If not loaded, [BaseGame] will not try to render it.
|
/// Whether this component has been loaded yet. If not loaded, [BaseGame] will not try to render it.
|
||||||
///
|
///
|
||||||
@ -53,7 +53,7 @@ abstract class Component {
|
|||||||
/// Called when the component has been added and prepared by the game instance.
|
/// Called when the component has been added and prepared by the game instance.
|
||||||
///
|
///
|
||||||
/// This can be used to make initializations on your component as, when this method is called,
|
/// This can be used to make initializations on your component as, when this method is called,
|
||||||
/// things like resize (and other mixins) are already set and usable.
|
/// things like [onGameResize] are already set and usable.
|
||||||
void onMount() {}
|
void onMount() {}
|
||||||
|
|
||||||
/// Called right before the component is destroyed and removed from the game
|
/// Called right before the component is destroyed and removed from the game
|
||||||
|
|||||||
@ -70,10 +70,10 @@ class JoystickComponent extends JoystickController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void resize(Vector2 size) {
|
void onGameResize(Vector2 size) {
|
||||||
directional?.initialize(size, this);
|
directional?.initialize(size, this);
|
||||||
actions?.forEach((action) => action.initialize(size, this));
|
actions?.forEach((action) => action.initialize(size, this));
|
||||||
super.resize(size);
|
super.onGameResize(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@ -1,21 +1,18 @@
|
|||||||
|
import 'package:meta/meta.dart';
|
||||||
import '../../extensions/vector2.dart';
|
import '../../extensions/vector2.dart';
|
||||||
|
import '../component.dart';
|
||||||
|
|
||||||
/// Useful mixin to add to your components if you want to hold a reference to the current screen size.
|
/// A [Component] mixin to make your component keep track of the size of the game viewport.
|
||||||
///
|
mixin Resizable on Component {
|
||||||
/// This mixin implements the resize method in order to hold an updated reference to the current screen [size].
|
|
||||||
/// Also, it updates its [children], if any.
|
|
||||||
class Resizable {
|
|
||||||
/// This is the current updated screen size.
|
/// This is the current updated screen size.
|
||||||
Vector2 size;
|
Vector2 gameSize;
|
||||||
|
|
||||||
/// Implementation provided by this mixin to the resize hook.
|
/// Implementation provided by this mixin to the resize hook.
|
||||||
void resize(Vector2 size) {
|
/// This is a hook called by [BaseGame] to let this component know that the screen (or flame draw area) has been update.
|
||||||
this.size = size;
|
@override
|
||||||
resizableChildren().forEach((e) => e?.resize(size));
|
@mustCallSuper
|
||||||
|
void onGameResize(Vector2 gameSize) {
|
||||||
|
super.onGameResize(gameSize);
|
||||||
|
this.gameSize = gameSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Overwrite this to add children to this [Resizable].
|
|
||||||
///
|
|
||||||
/// If a [Resizable] has children, its children as resized as well.
|
|
||||||
Iterable<Resizable> resizableChildren() => [];
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -179,8 +179,8 @@ class ParallaxComponent extends PositionComponent {
|
|||||||
|
|
||||||
@mustCallSuper
|
@mustCallSuper
|
||||||
@override
|
@override
|
||||||
void resize(Vector2 size) {
|
void onGameResize(Vector2 size) {
|
||||||
super.resize(size);
|
super.onGameResize(size);
|
||||||
_layers.forEach((layer) => layer.resize(size));
|
_layers.forEach((layer) => layer.resize(size));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -184,9 +184,9 @@ abstract class PositionComponent extends Component {
|
|||||||
|
|
||||||
@mustCallSuper
|
@mustCallSuper
|
||||||
@override
|
@override
|
||||||
void resize(Vector2 size) {
|
void onGameResize(Vector2 gameSize) {
|
||||||
super.resize(size);
|
super.onGameResize(gameSize);
|
||||||
_children.forEach((child) => child.resize(size));
|
_children.forEach((child) => child.onGameResize(gameSize));
|
||||||
}
|
}
|
||||||
|
|
||||||
@mustCallSuper
|
@mustCallSuper
|
||||||
|
|||||||
@ -148,7 +148,7 @@ class TextBoxComponent extends PositionComponent with Resizable {
|
|||||||
|
|
||||||
Future<Image> _redrawCache() {
|
Future<Image> _redrawCache() {
|
||||||
final PictureRecorder recorder = PictureRecorder();
|
final PictureRecorder recorder = PictureRecorder();
|
||||||
final Canvas c = Canvas(recorder, size.toRect());
|
final Canvas c = Canvas(recorder, gameSize.toRect());
|
||||||
_fullRender(c);
|
_fullRender(c);
|
||||||
return recorder.endRecording().toImage(width.toInt(), height.toInt());
|
return recorder.endRecording().toImage(width.toInt(), height.toInt());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,7 +30,7 @@ class BaseGame extends Game with FPSCounter {
|
|||||||
/// Components to be removed on the next update
|
/// Components to be removed on the next update
|
||||||
final List<Component> _removeLater = [];
|
final List<Component> _removeLater = [];
|
||||||
|
|
||||||
/// Current screen size, updated every resize via the [resize] method hook
|
/// Current game viewport size, updated every resize via the [resize] method hook
|
||||||
Vector2 size;
|
Vector2 size;
|
||||||
|
|
||||||
/// Camera position; every non-HUD component is translated so that the camera position is the top-left corner of the screen.
|
/// Camera position; every non-HUD component is translated so that the camera position is the top-left corner of the screen.
|
||||||
@ -59,7 +59,7 @@ class BaseGame extends Game with FPSCounter {
|
|||||||
|
|
||||||
// first time resize
|
// first time resize
|
||||||
if (size != null) {
|
if (size != null) {
|
||||||
c.resize(size);
|
c.onGameResize(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
c.onMount();
|
c.onMount();
|
||||||
@ -140,7 +140,7 @@ class BaseGame extends Game with FPSCounter {
|
|||||||
@mustCallSuper
|
@mustCallSuper
|
||||||
void resize(Vector2 size) {
|
void resize(Vector2 size) {
|
||||||
this.size = size;
|
this.size = size;
|
||||||
components.forEach((c) => c.resize(size));
|
components.forEach((c) => c.onGameResize(size));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns whether this [Game] is in debug mode or not.
|
/// Returns whether this [Game] is in debug mode or not.
|
||||||
|
|||||||
@ -43,7 +43,7 @@ void main() {
|
|||||||
game.add(wrapper);
|
game.add(wrapper);
|
||||||
game.onTapDown(1, TapDownDetails(globalPosition: const Offset(0.0, 0.0)));
|
game.onTapDown(1, TapDownDetails(globalPosition: const Offset(0.0, 0.0)));
|
||||||
|
|
||||||
expect(child.size, size);
|
expect(child.gameSize, size);
|
||||||
expect(child.tapped, true);
|
expect(child.tapped, true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -7,12 +7,10 @@ import 'package:flame/components/mixins/resizable.dart';
|
|||||||
|
|
||||||
class MyComponent extends PositionComponent with Resizable {
|
class MyComponent extends PositionComponent with Resizable {
|
||||||
String name;
|
String name;
|
||||||
List<MyComponent> myChildren;
|
|
||||||
|
|
||||||
MyComponent(this.name, {this.myChildren = const []});
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Iterable<Resizable> resizableChildren() => myChildren;
|
Vector2 size = Vector2(2.0, 2.0);
|
||||||
|
|
||||||
|
MyComponent(this.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyGame extends BaseGame {}
|
class MyGame extends BaseGame {}
|
||||||
@ -21,27 +19,26 @@ Vector2 size = Vector2(1.0, 1.0);
|
|||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
group('resizable test', () {
|
group('resizable test', () {
|
||||||
test('propagate resize to children', () {
|
|
||||||
final MyComponent a = MyComponent('a');
|
|
||||||
final MyComponent b = MyComponent('b', myChildren: [a]);
|
|
||||||
b.resize(size);
|
|
||||||
expect(a.size, size);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('game calls resize on add', () {
|
test('game calls resize on add', () {
|
||||||
final MyComponent a = MyComponent('a');
|
final MyComponent a = MyComponent('a');
|
||||||
final MyGame game = MyGame();
|
final MyGame game = MyGame();
|
||||||
game.resize(size);
|
game.resize(size);
|
||||||
game.add(a);
|
game.add(a);
|
||||||
expect(a.size, size);
|
expect(a.gameSize, size);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('game calls resize after added', () {
|
test('game calls resize after added', () {
|
||||||
final MyComponent a = MyComponent('a');
|
final MyComponent a = MyComponent('a');
|
||||||
final MyGame game = MyGame();
|
final MyGame game = MyGame();
|
||||||
game.add(a);
|
game.add(a);
|
||||||
game.resize(size);
|
game.resize(size);
|
||||||
expect(a.size, size);
|
expect(a.gameSize, size);
|
||||||
|
});
|
||||||
|
test('game calls doesnt change component size', () {
|
||||||
|
final MyComponent a = MyComponent('a');
|
||||||
|
final MyGame game = MyGame();
|
||||||
|
game.add(a);
|
||||||
|
game.resize(size);
|
||||||
|
expect(a.size, isNot(size));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user