Merge pull request #433 from flame-engine/luan.composed-v2

Composed refactor part 1: Fixes, lint & nits on examples
This commit is contained in:
Luan Nico
2020-08-03 11:40:12 -04:00
committed by GitHub
12 changed files with 80 additions and 48 deletions

View File

@@ -8,26 +8,38 @@ import 'package:flutter/material.dart';
void main() async { void main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
await Flame.images.loadAll(['creature.png', 'chopper.png']);
final Size size = await Flame.util.initialDimensions(); final Size size = await Flame.util.initialDimensions();
final game = MyGame(size); final game = MyGame(size);
runApp(game.widget); runApp(game.widget);
} }
class MyGame extends BaseGame with TapDetector { class MyGame extends BaseGame with TapDetector {
final animation = flame_animation.Animation.sequenced('chopper.png', 4, final animation = flame_animation.Animation.sequenced(
textureWidth: 48, textureHeight: 48, stepTime: 0.15, loop: true); 'chopper.png',
4,
textureWidth: 48,
textureHeight: 48,
stepTime: 0.15,
loop: true,
);
void addAnimation(double x, double y) { void addAnimation(double x, double y) {
const textureWidth = 291.0; const textureWidth = 291.0;
const textureHeight = 178.0; const textureHeight = 178.0;
final animationComponent = AnimationComponent.sequenced( final animationComponent = AnimationComponent.sequenced(
291, 178, 'creature.png', 18, 291,
amountPerRow: 10, 178,
textureWidth: textureWidth, 'creature.png',
textureHeight: textureHeight, 18,
stepTime: 0.15, amountPerRow: 10,
loop: true, textureWidth: textureWidth,
destroyOnFinish: true); textureHeight: textureHeight,
stepTime: 0.15,
loop: false,
destroyOnFinish: true,
);
animationComponent.x = x - textureWidth / 2; animationComponent.x = x - textureWidth / 2;
animationComponent.y = y - textureHeight / 2; animationComponent.y = y - textureHeight / 2;
@@ -42,14 +54,15 @@ class MyGame extends BaseGame with TapDetector {
MyGame(Size screenSize) { MyGame(Size screenSize) {
size = screenSize; size = screenSize;
final animationComponent = AnimationComponent(100, 100, animation); const s = 100.0;
animationComponent.x = size.width / 2 - 100; final animationComponent = AnimationComponent(s, s, animation);
animationComponent.y = 100; animationComponent.x = size.width / 2 - s;
animationComponent.y = s;
final reversedAnimationComponent = final reversedAnimationComponent =
AnimationComponent(100, 100, animation.reversed()); AnimationComponent(s, s, animation.reversed());
reversedAnimationComponent.x = size.width / 2; reversedAnimationComponent.x = size.width / 2;
reversedAnimationComponent.y = 100; reversedAnimationComponent.y = s;
add(animationComponent); add(animationComponent);
add(reversedAnimationComponent); add(reversedAnimationComponent);

View File

@@ -18,7 +18,9 @@ class MyGame extends BaseGame {
void _start() async { void _start() async {
final animation = await flame_animation.Animation.fromAsepriteData( final animation = await flame_animation.Animation.fromAsepriteData(
'chopper.png', 'chopper.json'); 'chopper.png',
'chopper.json',
);
final animationComponent = AnimationComponent(200, 200, animation); final animationComponent = AnimationComponent(200, 200, animation);
animationComponent.x = (size.width / 2) - 100; animationComponent.x = (size.width / 2) - 100;

View File

@@ -21,16 +21,17 @@ TextConfig regular = TextConfig(color: BasicPalette.white.color);
AudioPool pool = AudioPool('laser.mp3'); AudioPool pool = AudioPool('laser.mp3');
class MyGame extends BaseGame with TapDetector { class MyGame extends BaseGame with TapDetector {
static final black = BasicPalette.black.paint;
MyGame(Size screenSize) { MyGame(Size screenSize) {
size = screenSize; size = screenSize;
} }
@override @override
void render(Canvas canvas) { void render(Canvas canvas) {
canvas.drawRect(Rect.fromLTWH(0.0, 0.0, size.width, size.height), canvas.drawRect(Rect.fromLTWH(0.0, 0.0, size.width, size.height), black);
BasicPalette.black.paint); final p = Position.fromSize(size).div(2);
regular.render(canvas, 'hit me!', Position.fromSize(size).div(2), regular.render(canvas, 'hit me!', p, anchor: Anchor.center);
anchor: Anchor.center);
super.render(canvas); super.render(canvas);
} }

View File

@@ -19,7 +19,7 @@ class MyGame extends BaseGame with TapDetector {
final TextConfig fpsTextConfig = TextConfig(color: const Color(0xFFFFFFFF)); final TextConfig fpsTextConfig = TextConfig(color: const Color(0xFFFFFFFF));
final paint = Paint()..color = const Color(0xFFE5E5E5E5); final paint = Paint()..color = const Color(0xFFE5E5E5E5);
final List<String> _animations = ["Stand", "Wave", "Jump", "Dance"]; final List<String> _animations = ['Stand', 'Wave', 'Jump', 'Dance'];
int _currentAnimation = 0; int _currentAnimation = 0;
FlareAnimation flareAnimation; FlareAnimation flareAnimation;
@@ -48,26 +48,26 @@ class MyGame extends BaseGame with TapDetector {
} }
void _start() async { void _start() async {
flareAnimation = await FlareAnimation.load("assets/Bob_Minion.flr"); flareAnimation = await FlareAnimation.load('assets/Bob_Minion.flr');
flareAnimation.updateAnimation("Stand"); flareAnimation.updateAnimation('Stand');
flareAnimation.width = 306; flareAnimation.width = 306;
flareAnimation.height = 228; flareAnimation.height = 228;
final flareAnimation2 = final flareAnimation2 =
FlareComponent("assets/Bob_Minion.flr", "Wave", 306, 228); FlareComponent('assets/Bob_Minion.flr', 'Wave', 306, 228);
flareAnimation2.x = 50; flareAnimation2.x = 50;
flareAnimation2.y = 240; flareAnimation2.y = 240;
add(flareAnimation2); add(flareAnimation2);
final flareAnimation3 = final flareAnimation3 =
FlareComponent("assets/Bob_Minion.flr", "Jump", 306, 228); FlareComponent('assets/Bob_Minion.flr', 'Jump', 306, 228);
flareAnimation3.x = 50; flareAnimation3.x = 50;
flareAnimation3.y = 400; flareAnimation3.y = 400;
add(flareAnimation3); add(flareAnimation3);
final flareAnimation4 = final flareAnimation4 =
FlareComponent("assets/Bob_Minion.flr", "Dance", 306, 228); FlareComponent('assets/Bob_Minion.flr', 'Dance', 306, 228);
flareAnimation4.x = 50; flareAnimation4.x = 50;
flareAnimation4.y = 550; flareAnimation4.y = 550;
add(flareAnimation4); add(flareAnimation4);

View File

@@ -3,7 +3,7 @@ import 'package:flame/game.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class MyGame extends BaseGame { class MyGame extends Game {
static final Paint paint = Paint()..color = const Color(0xFFFFFFFF); static final Paint paint = Paint()..color = const Color(0xFFFFFFFF);
var movingLeft = false; var movingLeft = false;
@@ -24,19 +24,19 @@ class MyGame extends BaseGame {
final keyLabel = e.data.logicalKey.keyLabel; final keyLabel = e.data.logicalKey.keyLabel;
if (keyLabel == "a") { if (keyLabel == 'a') {
movingLeft = isKeyDown; movingLeft = isKeyDown;
} }
if (keyLabel == "d") { if (keyLabel == 'd') {
movingRight = isKeyDown; movingRight = isKeyDown;
} }
if (keyLabel == "w") { if (keyLabel == 'w') {
movingUp = isKeyDown; movingUp = isKeyDown;
} }
if (keyLabel == "s") { if (keyLabel == 's') {
movingDown = isKeyDown; movingDown = isKeyDown;
} }
}); });

View File

@@ -29,9 +29,9 @@ class MyGame extends Game with KeyboardEvents {
@override @override
void onKeyEvent(e) { void onKeyEvent(e) {
final bool isKeyDown = e is RawKeyDownEvent; final bool isKeyDown = e is RawKeyDownEvent;
if (e.data.keyLabel == "a") { if (e.data.keyLabel == 'a') {
_dir = isKeyDown ? -1 : 0; _dir = isKeyDown ? -1 : 0;
} else if (e.data.keyLabel == "d") { } else if (e.data.keyLabel == 'd') {
_dir = isKeyDown ? 1 : 0; _dir = isKeyDown ? 1 : 0;
} }
} }

View File

@@ -12,15 +12,18 @@ void main() async {
class MyGame extends BaseGame { class MyGame extends BaseGame {
MyGame() { MyGame() {
final images = [ final images = [
ParallaxImage("bg.png"), ParallaxImage('bg.png'),
ParallaxImage("mountain-far.png"), ParallaxImage('mountain-far.png'),
ParallaxImage("mountains.png"), ParallaxImage('mountains.png'),
ParallaxImage("trees.png"), ParallaxImage('trees.png'),
ParallaxImage("foreground-trees.png"), ParallaxImage('foreground-trees.png'),
]; ];
final parallaxComponent = ParallaxComponent(images, final parallaxComponent = ParallaxComponent(
baseSpeed: const Offset(20, 0), layerDelta: const Offset(30, 0)); images,
baseSpeed: const Offset(20, 0),
layerDelta: const Offset(30, 0),
);
add(parallaxComponent); add(parallaxComponent);
} }

View File

@@ -12,8 +12,13 @@ void main() async {
} }
class MyGame extends BaseGame { class MyGame extends BaseGame {
final animation = flame_animation.Animation.sequenced('chopper.png', 4, final animation = flame_animation.Animation.sequenced(
textureWidth: 48, textureHeight: 48, stepTime: 0.15); 'chopper.png',
4,
textureWidth: 48,
textureHeight: 48,
stepTime: 0.15,
);
AnimationComponent buildAnimation() { AnimationComponent buildAnimation() {
final ac = AnimationComponent(100, 100, animation); final ac = AnimationComponent(100, 100, animation);

View File

@@ -35,7 +35,6 @@ class Ball extends PositionComponent {
} }
forward = !forward; forward = !forward;
print('boin');
Flame.audio.play('boin.mp3', volume: 1.0); Flame.audio.play('boin.mp3', volume: 1.0);
} }
} }

View File

@@ -22,7 +22,7 @@ class TiledGame extends BaseGame {
void _addCoinsInMap(TiledComponent tiledMap) async { void _addCoinsInMap(TiledComponent tiledMap) async {
final ObjectGroup objGroup = final ObjectGroup objGroup =
await tiledMap.getObjectGroupFromLayer("AnimatedCoins"); await tiledMap.getObjectGroupFromLayer('AnimatedCoins');
if (objGroup == null) { if (objGroup == null) {
return; return;
} }

View File

@@ -11,6 +11,7 @@ dependencies:
sdk: flutter sdk: flutter
flame: flame:
path: ../../../ path: ../../../
tiled: 0.6.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:

View File

@@ -1,15 +1,19 @@
import 'dart:ui'; import 'dart:ui';
import 'component.dart'; import 'component.dart';
import 'package:flame/animation.dart'; import '../animation.dart';
class AnimationComponent extends PositionComponent { class AnimationComponent extends PositionComponent {
Animation animation; Animation animation;
Paint overridePaint; Paint overridePaint;
bool destroyOnFinish = false; bool destroyOnFinish = false;
AnimationComponent(double width, double height, this.animation, AnimationComponent(
{this.destroyOnFinish = false}) { double width,
double height,
this.animation, {
this.destroyOnFinish = false,
}) {
this.width = width; this.width = width;
this.height = height; this.height = height;
} }
@@ -54,8 +58,12 @@ class AnimationComponent extends PositionComponent {
@override @override
void render(Canvas canvas) { void render(Canvas canvas) {
prepareCanvas(canvas); prepareCanvas(canvas);
animation.getSprite().render(canvas, animation.getSprite().render(
width: width, height: height, overridePaint: overridePaint); canvas,
width: width,
height: height,
overridePaint: overridePaint,
);
} }
@override @override