changing animation to component

This commit is contained in:
renato.soares
2019-08-09 15:16:28 -03:00
committed by Erick (CptBlackPixel)
parent 51d259038f
commit ccc4677663
2 changed files with 16 additions and 28 deletions

View File

@ -3,6 +3,7 @@ import 'package:flame/game.dart';
import 'package:flame/flame.dart'; import 'package:flame/flame.dart';
import 'package:flame/position.dart'; import 'package:flame/position.dart';
import 'package:flame/animation.dart'; import 'package:flame/animation.dart';
import 'package:flame/components/animation_component.dart';
import 'package:flutter/widgets.dart' hide Animation; import 'package:flutter/widgets.dart' hide Animation;
import 'package:tiled/tiled.dart' show ObjectGroup, TmxObject; import 'package:tiled/tiled.dart' show ObjectGroup, TmxObject;
@ -13,32 +14,22 @@ void main() {
} }
class TiledGame extends BaseGame { class TiledGame extends BaseGame {
TiledComponent tiledMap;
Animation a;
TiledGame() { TiledGame() {
tiledMap = TiledComponent('map.tmx'); final TiledComponent tiledMap = TiledComponent('map.tmx');
add(tiledMap); add(tiledMap);
a = Animation.sequenced('coins.png', 8, textureWidth: 20); __addCoinsInMap(tiledMap);
} }
@override void __addCoinsInMap(TiledComponent tiledMap) async {
void render(Canvas canvas) { final ObjectGroup obj = await tiledMap.getObjectGroupFromLayer("AnimatedCoins");
super.render(canvas);
final ObjectGroup obj = tiledMap.getObjectLayerByName("AnimatedCoins");
if (obj == null) { if (obj == null) {
return; return;
} }
for (TmxObject obj in obj.tmxObjects) { for (TmxObject obj in obj.tmxObjects) {
final Rect rect = final comp = AnimationComponent(20.0, 20.0, Animation.sequenced('coins.png', 8, textureWidth: 20));
Rect.fromLTWH(obj.x.toDouble(), obj.y.toDouble(), 30.0, 30.0); comp.x = obj.x.toDouble();
final position = Position(obj.x.toDouble(), obj.y.toDouble()); comp.y = obj.y.toDouble();
a.getSprite().renderPosition(canvas, position); add(comp);
} }
} }
@override
void update(double t) {
super.update(t);
a.update(t);
}
} }

View File

@ -69,10 +69,9 @@ class TiledComponent extends Component {
final image = images[tile.image.source]; final image = images[tile.image.source];
final rect = tile.computeDrawRect(); final rect = tile.computeDrawRect();
final src = Rect.fromLTWH(rect.left.toDouble(), rect.top.toDouble(), final src =
rect.width.toDouble(), rect.height.toDouble()); Rect.fromLTWH(rect.left.toDouble(), rect.top.toDouble(), rect.width.toDouble(), rect.height.toDouble());
final dst = Rect.fromLTWH(tile.x.toDouble(), tile.y.toDouble(), final dst = Rect.fromLTWH(tile.x.toDouble(), tile.y.toDouble(), rect.width.toDouble(), rect.height.toDouble());
rect.width.toDouble(), rect.height.toDouble());
c.drawImageRect(image, src, dst, paint); c.drawImageRect(image, src, dst, paint);
}); });
@ -81,11 +80,9 @@ class TiledComponent extends Component {
@override @override
void update(double t) {} void update(double t) {}
ObjectGroup getObjectGroupLayerByName(String name) { Future<ObjectGroup> getObjectGroupFromLayer(String name) {
if (!loaded()) { return future.then((onValue) {
return null; return map.objectGroups.firstWhere((objectGroup) => objectGroup.name == name);
} });
return map.objectGroups
.firstWhere((objectGroup) => objectGroup.name == name);
} }
} }