i have been fooling around with Flame and Tiled for a few days and i have read the docs, but i cant figure out how to get tile data from the TiledComponent ?
Hope someone can help :)
I Would also like to know how to add new objects to the ObjectGroup layer. Like adding monsters etc. to the map.
import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame/flame.dart';
import 'package:flame/game.dart';
import 'package:flame_tiled/flame_tiled.dart';
import 'package:flutter/widgets.dart' hide Animation, Image;
import 'package:flame/input.dart';
import 'package:flame/events.dart';
// Main
void main() {
runApp(GameWidget(game: TiledGame()));
}
// Tile and map
Size tile = const Size(128, 64);
Size map = const Size(30, 30);
// TiledGame
class TiledGame extends FlameGame with PanDetector, TapDetector {
late TiledComponent mapComponent;
TiledGame() : super(
camera: CameraComponent.withFixedResolution(
width: map.width * tile.width,
height: map.height * tile.height,
),
);
@override
Future<void> onLoad() async {
debugMode = true;
// Map
mapComponent = await TiledComponent.load(
'test.tmx',
Vector2(128, 64),
prefix: 'assets/maps/'
);
world.add(mapComponent);
// Camera
camera.viewfinder.anchor = Anchor.center;
camera.moveTo(
Vector2(
(map.width * tile.width) * 0.5,
(map.height * tile.height) * 0.5
)
);
}
@override
void onTapDown(TapDownInfo info) {
// int x = ??
// int y = ??
// print(mapComponent.tileMap.getTileData(layerId: 0, x: x, y: y));
}
@override
void onPanUpdate(DragUpdateInfo info) {
double offset = 3.3;
Vector2 pos = Vector2((info.delta.global.x*offset)*-1, (info.delta.global.y*offset)*-1);
camera.moveBy(pos);
}
}
You can get hold of any of the layers from the tilemap via the
RenderableTiledMapof theTiledComponent. For example, if your tilemap contains an object layer called 'Trigger', you can read it like this:Here is the mapping from
Tiled's layer types totiled.dart's layer class types:This information can also be found on Flame's official docs here: https://docs.flame-engine.org/latest/bridge_packages/flame_tiled/layers.html#layers
As for the second part of your question:
You can take a look at this code for reference: https://github.com/ufrshubham/ski_master/blob/afbaa158cdf9f35a0d0575726ef8abe079f3d868/lib/game/routes/gameplay.dart#L88-L112