mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-03 12:28:03 +08:00
selector
This commit is contained in:
BIN
doc/examples/isometric/assets/images/selector.png
Normal file
BIN
doc/examples/isometric/assets/images/selector.png
Normal file
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 |
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,3 +19,4 @@ dev_dependencies:
|
||||
flutter:
|
||||
assets:
|
||||
- assets/images/tiles.png
|
||||
- assets/images/selector.png
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user