mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 20:13:50 +08:00
Isometric rendering
This commit is contained in:
@ -17,9 +17,17 @@ class MyGame extends BaseGame {
|
|||||||
void init() async {
|
void init() async {
|
||||||
final tileset = await IsometricTileset.load('tiles.png', 32);
|
final tileset = await IsometricTileset.load('tiles.png', 32);
|
||||||
final matrix = [
|
final matrix = [
|
||||||
[-1, 1, 0, 0],
|
[-1, 1, 1, 1, 0, 0],
|
||||||
[1, 1, 0, 2],
|
[-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,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -46,7 +46,7 @@ class IsometricTileMapComponent extends PositionComponent {
|
|||||||
void render(Canvas c) {
|
void render(Canvas c) {
|
||||||
prepareCanvas(c);
|
prepareCanvas(c);
|
||||||
|
|
||||||
final s = effectiveTileSize.toDouble();
|
final s = effectiveTileSize.toDouble() / 2;
|
||||||
matrix.asMap().forEach((i, line) {
|
matrix.asMap().forEach((i, line) {
|
||||||
line.asMap().forEach((j, element) {
|
line.asMap().forEach((j, element) {
|
||||||
if (element == -1) {
|
if (element == -1) {
|
||||||
@ -54,8 +54,20 @@ class IsometricTileMapComponent extends PositionComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final sprite = tileset.getTile(element);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user