This commit is contained in:
Luan Nico
2020-09-14 23:34:51 -04:00
parent adb4cc2336
commit 86ca1990cc
5 changed files with 37 additions and 15 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 273 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -24,11 +24,7 @@ class Selector extends SpriteComponent {
bool show = false;
Selector(double s)
: super.fromSprite(
s,
s,
Sprite('tiles.png', x: 64, y: 0, width: 32, height: 32),
);
: super.fromSprite(s, s, Sprite('selector.png', width: 32, height: 32));
@override
void render(Canvas canvas) {
@ -50,7 +46,7 @@ class MyGame extends BaseGame with MouseMovementDetector {
void init() async {
final tileset = await IsometricTileset.load('tiles.png', 32);
final layer0 = [
[-1, 1, 1, 1, 0, 0],
[3, 1, 1, 1, 0, 0],
[-1, 1, 2, 1, 0, 0],
[-1, 0, 1, 1, 0, 0],
[-1, 1, 1, 1, 0, 0],
@ -91,13 +87,24 @@ class MyGame extends BaseGame with MouseMovementDetector {
add(selector = Selector(s.toDouble()));
}
@override
void render(Canvas canvas) {
super.render(canvas);
canvas.drawRect(
const Rect.fromLTWH(x - 1, y - 1, 3, 3),
Paint()..color = const Color(0xFFFF00FF),
);
}
@override
void onMouseMove(PointerHoverEvent event) {
if (base == null || selector == null) {
return; // loading
}
final screenPosition = Position.fromOffset(event.position);
final blockCoords = base.getBlock(screenPosition);
final blockPosition =
base.cartToIso(Position.fromInts(blockCoords.x * s, blockCoords.y * s));
selector.setByPosition(blockPosition.add(topLeft));
selector.show = true;
final block = base.getBlock(screenPosition);
selector.show = base.containsBlock(block);
selector.setByPosition(base.getBlockPosition(block).add(topLeft));
}
}

View File

@ -19,3 +19,4 @@ dev_dependencies:
flutter:
assets:
- assets/images/tiles.png
- assets/images/selector.png

View File

@ -54,7 +54,6 @@ class IsometricTileMapComponent extends PositionComponent {
void render(Canvas c) {
prepareCanvas(c);
final s = effectiveTileSize.toDouble() / 2;
final size = Position.fromInts(effectiveTileSize, effectiveTileSize);
matrix.asMap().forEach((i, line) {
line.asMap().forEach((j, element) {
@ -63,12 +62,21 @@ class IsometricTileMapComponent extends PositionComponent {
}
final sprite = tileset.getTile(element);
final p = cartToIso(Position(j * s, i * s));
final p = getBlockPositionInts(j, i);
sprite.renderRect(c, Position.rectFrom(p, size));
});
});
}
Position getBlockPosition(Block block) {
return getBlockPositionInts(block.x, block.y);
}
Position getBlockPositionInts(int i, int j) {
final s = effectiveTileSize.toDouble() / 2;
return cartToIso(Position(i * s, j * s)).minus(Position(s, 0));
}
Position isoToCart(Position p) {
final x = (2 * p.y + p.x) / 2;
final y = (2 * p.y - p.x) / 2;
@ -82,11 +90,17 @@ class IsometricTileMapComponent extends PositionComponent {
}
Block getBlock(Position p) {
final s = effectiveTileSize.toDouble();
final s = effectiveTileSize.toDouble() / 2;
final cart = isoToCart(p.clone().minus(toPosition()));
print(cart);
final px = cart.x ~/ s;
final py = cart.y ~/ s;
return Block(px, py);
}
bool containsBlock(Block block) {
return block.x >= 0 &&
block.x < matrix.length &&
block.y >= 0 &&
block.y < matrix[block.x].length;
}
}