mirror of
https://github.com/flame-engine/flame.git
synced 2025-10-30 08:27:36 +08:00
Unify examples structure (#1118)
* Animations, CameraAndViewport, CollisionDetection and Components unified * Added descriptions to effects * Rename input games * Unify input stories * Add info to parallax section * Added descriptions to the rendering examples * Add descriptions to the sprites directory * Fix utils and rendering section * Add descriptions to the widgets section * Delete directory that rebase brought back * Unify game names * Added some styleguide docs for examples * Fix analyze issues * All files should have _example as suffix * Made the FollowComponentExample a bit easier to understand * Change priority of ember
This commit is contained in:
101
examples/lib/stories/rendering/isometric_tile_map_example.dart
Normal file
101
examples/lib/stories/rendering/isometric_tile_map_example.dart
Normal file
@ -0,0 +1,101 @@
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame/extensions.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/input.dart';
|
||||
import 'package:flame/sprite.dart';
|
||||
import 'package:flutter/material.dart' hide Image;
|
||||
|
||||
class IsometricTileMapExample extends FlameGame with MouseMovementDetector {
|
||||
static const String description = '''
|
||||
Shows an example of how to use the `IsometricTileMapComponent`.\n\n
|
||||
Move the mouse over the board to see a selector appearing on the tiles.
|
||||
''';
|
||||
|
||||
final topLeft = Vector2.all(500);
|
||||
|
||||
static const scale = 2.0;
|
||||
static const srcTileSize = 32.0;
|
||||
static const destTileSize = scale * srcTileSize;
|
||||
|
||||
static const halfSize = true;
|
||||
static const tileHeight = scale * (halfSize ? 8.0 : 16.0);
|
||||
static const suffix = halfSize ? '-short' : '';
|
||||
|
||||
final originColor = Paint()..color = const Color(0xFFFF00FF);
|
||||
final originColor2 = Paint()..color = const Color(0xFFAA55FF);
|
||||
|
||||
late IsometricTileMapComponent base;
|
||||
late Selector selector;
|
||||
|
||||
IsometricTileMapExample();
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
final tilesetImage = await images.load('tile_maps/tiles$suffix.png');
|
||||
final tileset = SpriteSheet(
|
||||
image: tilesetImage,
|
||||
srcSize: Vector2.all(srcTileSize),
|
||||
);
|
||||
final matrix = [
|
||||
[3, 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(
|
||||
base = IsometricTileMapComponent(
|
||||
tileset,
|
||||
matrix,
|
||||
destTileSize: Vector2.all(destTileSize),
|
||||
tileHeight: tileHeight,
|
||||
position: topLeft,
|
||||
),
|
||||
);
|
||||
|
||||
final selectorImage = await images.load('tile_maps/selector$suffix.png');
|
||||
add(selector = Selector(destTileSize, selectorImage));
|
||||
}
|
||||
|
||||
@override
|
||||
void render(Canvas canvas) {
|
||||
super.render(canvas);
|
||||
canvas.renderPoint(topLeft, size: 5, paint: originColor);
|
||||
canvas.renderPoint(
|
||||
topLeft.clone()..y -= tileHeight,
|
||||
size: 5,
|
||||
paint: originColor2,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void onMouseMove(PointerHoverInfo info) {
|
||||
final screenPosition = info.eventPosition.game;
|
||||
final block = base.getBlock(screenPosition);
|
||||
selector.show = base.containsBlock(block);
|
||||
selector.position.setFrom(topLeft + base.getBlockRenderPosition(block));
|
||||
}
|
||||
}
|
||||
|
||||
class Selector extends SpriteComponent {
|
||||
bool show = true;
|
||||
|
||||
Selector(double s, Image image)
|
||||
: super(
|
||||
sprite: Sprite(image, srcSize: Vector2.all(32.0)),
|
||||
size: Vector2.all(s),
|
||||
);
|
||||
|
||||
@override
|
||||
void render(Canvas canvas) {
|
||||
if (!show) {
|
||||
return;
|
||||
}
|
||||
|
||||
super.render(canvas);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user