From 200c3da505ceba77e427448b8f1086c658823c1c Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Wed, 22 Jan 2020 20:57:31 -0300 Subject: [PATCH] Improving FlareComponent API and updating FlareFlutter dependency --- doc/examples/flare/.gitignore | 2 + doc/examples/flare/README.md | 4 +- doc/examples/flare/lib/main_component.dart | 74 ++++++++++++++++++++++ lib/components/flare_component.dart | 20 +++++- pubspec.yaml | 2 +- 5 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 doc/examples/flare/lib/main_component.dart diff --git a/doc/examples/flare/.gitignore b/doc/examples/flare/.gitignore index 07488ba61..7e56423a9 100644 --- a/doc/examples/flare/.gitignore +++ b/doc/examples/flare/.gitignore @@ -68,3 +68,5 @@ !**/ios/**/default.pbxuser !**/ios/**/default.perspectivev3 !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages + +.flutter-plugins-dependencies diff --git a/doc/examples/flare/README.md b/doc/examples/flare/README.md index 0140a3ffb..97ff3f306 100644 --- a/doc/examples/flare/README.md +++ b/doc/examples/flare/README.md @@ -1,5 +1,7 @@ # flare -Example game application showcasing how to use Flare animations on Flame. +Example game application showcasing how to use Flare animations on Flame. Flare animation used for the example: https://www.2dimensions.com/a/kautuk/files/flare/bob-minion/preview + +The `main.dart` is a more generic example, test most of the features as `main_component.dart` is a specific example for the `FlareComponent` class. diff --git a/doc/examples/flare/lib/main_component.dart b/doc/examples/flare/lib/main_component.dart new file mode 100644 index 000000000..fb47accd2 --- /dev/null +++ b/doc/examples/flare/lib/main_component.dart @@ -0,0 +1,74 @@ +import 'dart:ui'; +import 'package:flame/gestures.dart'; +import 'package:flame/flame.dart'; +import 'package:flame/game.dart'; +import 'package:flame/components/flare_component.dart'; +import 'package:flame/text_config.dart'; +import 'package:flame/position.dart'; + +import 'package:flutter/material.dart'; + +void main() { + WidgetsFlutterBinding.ensureInitialized(); + + final game = MyGame(); + runApp(game.widget); +} + +class MyGame extends BaseGame with TapDetector, DoubleTapDetector { + final TextConfig fpsTextConfig = + const TextConfig(color: const Color(0xFFFFFFFF)); + + final paint = Paint()..color = const Color(0xFFE5E5E5E5); + final List _animations = ["Stand", "Wave", "Jump", "Dance"]; + int _currentAnimation = 0; + FlareComponent flareComponent; + + bool loaded = false; + + MyGame() { + _start(); + } + + @override + bool debugMode() => true; + + @override + void onTap() { + cycleAnimation(); + } + + @override + void onDoubleTap() { + flareComponent.width += 10; + flareComponent.height += 10; + + flareComponent.x -= 10; + flareComponent.y -= 10; + } + + void cycleAnimation() { + if (_currentAnimation == 3) { + _currentAnimation = 0; + } else { + _currentAnimation++; + } + flareComponent.updateAnimation(_animations[_currentAnimation]); + } + + void _start() async { + flareComponent = FlareComponent("assets/Bob_Minion.flr", "Stand", 306, 228); + flareComponent.x = 50; + flareComponent.y = 240; + add(flareComponent); + } + + @override + void render(Canvas canvas) { + super.render(canvas); + + if (debugMode()) { + fpsTextConfig.render(canvas, fps(120).toString(), Position(0, 10)); + } + } +} diff --git a/lib/components/flare_component.dart b/lib/components/flare_component.dart index 792a8027a..76778fc9a 100644 --- a/lib/components/flare_component.dart +++ b/lib/components/flare_component.dart @@ -20,6 +20,12 @@ class FlareComponent extends PositionComponent { }); } + void updateAnimation(String animation) { + if (loaded()) { + _flareAnimation.updateAnimation(animation); + } + } + @override bool loaded() => _flareAnimation != null; @@ -31,8 +37,20 @@ class FlareComponent extends PositionComponent { @override void update(double dt) { - if (_flareAnimation != null) { + if (loaded()) { _flareAnimation.update(dt); } } + + @override + set width(_width) { + super.width = _width; + if (loaded()) _flareAnimation.width = width; + } + + @override + set height(_height) { + super.height = _height; + if (loaded()) _flareAnimation.height = height; + } } diff --git a/pubspec.yaml b/pubspec.yaml index f69f33867..b5baae88e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -14,7 +14,7 @@ dependencies: tiled: ^0.2.1 convert: ^2.0.1 flutter_svg: ^0.15.0 - flare_flutter: ^1.5.4 + flare_flutter: ^2.0.1 dev_dependencies: flutter_test: