20个不稳定版本 (5个破坏性更新)
0.6.0 | 2024年7月7日 |
---|---|
0.5.1 | 2024年5月19日 |
0.4.3 | 2023年12月19日 |
0.4.0 | 2023年11月6日 |
0.1.2 | 2023年1月23日 |
#129 in 游戏开发
每月366次下载
42KB
922 代码行
Bevy Slippy Tiles
一个辅助Bevy插件,用于处理下载符合OpenStreetMap规范的滑块瓦片。
DownloadSlippyTilesEvent
可以触发以请求一个或多个滑块瓦片下载。
SlippyTileDownloadedEvent
在成功检索到请求的滑块瓦片时触发。文件路径存储在事件中,可以使用资产加载器使用。
示例
以下是从这个crate中的示例代码片段。此应用程序将加载一个滑块瓦片及其在指定经纬度周围8个瓦片。
运行方式: cargo run --example simple
// ...
const LATITUDE: f64 = 45.4111;
const LONGITUDE: f64 = -75.6980;
fn main() {
App::new()
// Our slippy tiles settings and plugin
.insert_resource(SlippyTilesSettings::new(
"https://tile.openstreetmap.org", // Tile server endpoint.
"tiles/", // assets/ folder storing the slippy tile downloads.
))
.add_plugins(DefaultPlugins)
.add_plugins(SlippyTilesPlugin);
// ...
}
// ...
fn request_slippy_tiles(mut download_slippy_tile_events: EventWriter<DownloadSlippyTilesEvent>) {
// ...
let slippy_tile_event = DownloadSlippyTilesEvent {
tile_size: TileSize::Normal, // Size of tiles - Normal = 256px, Large = 512px (not all tile servers).
zoom_level: ZoomLevel::L18, // Map zoom level (L0 = entire world, L19 = closest zoom level).
coordinates: Coordinates::from_latitude_longitude(LATITUDE, LONGITUDE),
radius: Radius(1), // Request one layer of surrounding tiles (2 = two layers of surrounding tiles - 25 total, 3 = three layers of surrounding tiles - 49 total, etc).
use_cache: true, // Don't make request if already requested previously, or if file already exists in tiles directory.
};
download_slippy_tile_events.send(slippy_tile_event);
}
fn display_slippy_tiles(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut slippy_tile_downloaded_events: EventReader<SlippyTileDownloadedEvent>,
) {
for slippy_tile_downloaded_event in slippy_tile_downloaded_events.read() {
let zoom_level = slippy_tile_downloaded_event.zoom_level;
// ..
let tile_pixels = slippy_tile_downloaded_event.tile_size.to_pixels() as f32;
let transform_x = (current_x as f32 - center_x as f32) * tile_pixels;
let transform_y = (center_y as f32 - current_y as f32) * tile_pixels;
// Add our slippy tile to the screen.
commands.spawn(SpriteBundle {
texture: asset_server.load(slippy_tile_downloaded_event.path.clone()),
transform: Transform::from_xyz(transform_x, transform_y, 0.0),
..Default::default()
});
}
}
Bevy兼容性
bevy | bevy_slippy_tiles |
---|---|
0.14 | 0.6 |
0.13 | 0.5 |
0.12 | 0.4 |
0.11 | 0.3 |
0.10 | 0.2 |
0.9 | 0.1.3 |
依赖
~23MB
~416K SLoC