Isometric rendering

This commit is contained in:
Luan Nico
2020-09-06 14:05:03 -04:00
parent 7b8b489d6e
commit fd9d69e7d5
2 changed files with 25 additions and 5 deletions

View File

@ -17,9 +17,17 @@ class MyGame extends BaseGame {
void init() async {
final tileset = await IsometricTileset.load('tiles.png', 32);
final matrix = [
[-1, 1, 0, 0],
[1, 1, 0, 2],
[-1, 1, 1, 1, 0, 0],
[-1, 1, 2, 1, 0, 0],
[-1, 0, 1, 1, 0, 0],
[-1, 1, 1, 1, 0, 0],
[1, 1, 1, 1, 0, 2],
[1, 3, 3, 3, 0, 2],
];
add(IsometricTileMapComponent(tileset, matrix));
add(
IsometricTileMapComponent(tileset, matrix)
..x = 100
..y = 100,
);
}
}

View File

@ -46,7 +46,7 @@ class IsometricTileMapComponent extends PositionComponent {
void render(Canvas c) {
prepareCanvas(c);
final s = effectiveTileSize.toDouble();
final s = effectiveTileSize.toDouble() / 2;
matrix.asMap().forEach((i, line) {
line.asMap().forEach((j, element) {
if (element == -1) {
@ -54,8 +54,20 @@ class IsometricTileMapComponent extends PositionComponent {
}
final sprite = tileset.getTile(element);
sprite.renderPosition(c, Position(j * s, i * s));
sprite.renderPosition(c, cartToIso(Position(j * s, i * s)));
});
});
}
Position isoToCart(Position p) {
final x = (2 * p.y + p.x) / 2;
final y = (2 * p.y - p.x) / 2;
return Position(x, y);
}
Position cartToIso(Position p) {
final x = p.x - p.y;
final y = (p.x + p.y) / 2;
return Position(x, y);
}
}