Files
flame/examples/lib/stories/components/keys_example.dart
Luan Nico 06f5a4b4a8 chore: Update cicd.yml file on flame_3d to match main, rollback color changes (#3378)
Update cicd.yml file on flame_3d to match main.

Since we finally updated main, we should require no difference
whatsoever on this file anymore.

This will require downgrading all the color changes, which is fine. This
should never have been a part of flame_3d to begin with.

Files were "untouched" by checking out the exact version as they are in
main right now (will need to rebase flame_3d to main later). I had to
add a couple more files because the files on main had dependencies on
changes that are not yet rebased on flame_3d. These extra diffs should
disappear when I do the final rebase.
2024-12-11 14:26:29 -05:00

119 lines
3.0 KiB
Dart

import 'dart:async';
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
class KeysExampleWidget extends StatefulWidget {
const KeysExampleWidget({super.key});
static const String description = '''
Showcases how component keys can be used to find components
from a flame game instance.
Use the buttons to select or deselect the heroes.
''';
@override
State<KeysExampleWidget> createState() => _KeysExampleWidgetState();
}
class _KeysExampleWidgetState extends State<KeysExampleWidget> {
late final KeysExampleGame game = KeysExampleGame();
void selectHero(ComponentKey key) {
final hero = game.findByKey<SelectableClass>(key);
if (hero != null) {
hero.selected = !hero.selected;
}
}
@override
Widget build(BuildContext context) {
return Stack(
children: [
Positioned.fill(
child: GameWidget(game: game),
),
Positioned(
left: 20,
top: 222,
width: 300,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
onPressed: () {
selectHero(ComponentKey.named('knight'));
},
child: const Text('Knight'),
),
ElevatedButton(
onPressed: () {
selectHero(ComponentKey.named('mage'));
},
child: const Text('Mage'),
),
ElevatedButton(
onPressed: () {
selectHero(ComponentKey.named('ranger'));
},
child: const Text('Ranger'),
),
],
),
),
],
);
}
}
class KeysExampleGame extends FlameGame {
@override
FutureOr<void> onLoad() async {
await super.onLoad();
final knight = await loadSprite('knight.png');
final mage = await loadSprite('mage.png');
final ranger = await loadSprite('ranger.png');
await addAll([
SelectableClass(
key: ComponentKey.named('knight'),
sprite: knight,
size: Vector2.all(100),
position: Vector2(0, 100),
),
SelectableClass(
key: ComponentKey.named('mage'),
sprite: mage,
size: Vector2.all(100),
position: Vector2(120, 100),
),
SelectableClass(
key: ComponentKey.named('ranger'),
sprite: ranger,
size: Vector2.all(100),
position: Vector2(240, 100),
),
]);
}
}
class SelectableClass extends SpriteComponent {
SelectableClass({
super.position,
super.size,
super.key,
super.sprite,
}) : super(paint: Paint()..color = Colors.white.withOpacity(0.5));
bool _selected = false;
bool get selected => _selected;
set selected(bool value) {
_selected = value;
paint = Paint()
..color = value ? Colors.white : Colors.white.withOpacity(0.5);
}
}