Files
Lukas Klingsbo ef7427941c Possibility to change priority and reorder the component list (#793)
* Possibility to change component priority

* Fix formatting

* Posibility to change priority

* Docs for priority change

* Priority example

* Add section explaining priority

* Update doc/game.md

Co-authored-by: Erick <erickzanardoo@gmail.com>

* Update examples/lib/stories/components/components.dart

Co-authored-by: Erick <erickzanardoo@gmail.com>

* No null priorities in the super call chain

* Possibility to wait for all children to be loaded

* Test for addChildren

* Introduce parent to Component

* Check whether parent is BaseComponent

Co-authored-by: Erick <erickzanardoo@gmail.com>
2021-05-19 19:07:54 +02:00

58 lines
1.3 KiB
Dart

import 'dart:math';
import 'dart:ui';
import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flame/gestures.dart';
import 'package:flame/palette.dart';
class Square extends PositionComponent with HasGameRef<Priority>, Tapable {
late final Paint paint;
Square(Vector2 position) {
this.position.setFrom(position);
size.setValues(100, 100);
paint = _randomPaint();
}
@override
bool onTapDown(TapDownInfo event) {
final topComponent = gameRef.components.last;
if (topComponent != this) {
gameRef.changePriority(this, topComponent.priority + 1);
}
return false;
}
@override
void render(Canvas canvas) {
super.render(canvas);
canvas.drawRect(size.toRect(), paint);
}
static Paint _randomPaint() {
final rng = Random();
final color = Color.fromRGBO(
rng.nextInt(256),
rng.nextInt(256),
rng.nextInt(256),
0.9,
);
return PaletteEntry(color).paint();
}
}
class Priority extends BaseGame with HasTapableComponents {
@override
Future<void> onLoad() async {
final squares = [
Square(Vector2(100, 100)),
Square(Vector2(160, 100)),
Square(Vector2(170, 150)),
Square(Vector2(110, 150)),
];
addAll(squares);
}
}