use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
math::{uvec2, vec2, vec3},
prelude::*,
sprite::Mesh2dHandle,
window::PresentMode,
};
use bevy_fast_tilemap::prelude::*;
#[path = "common/mouse_controls_camera.rs"]
mod mouse_controls_camera;
use mouse_controls_camera::MouseControlsCameraPlugin;
fn main() {
App::new()
.add_plugins((
DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: String::from("Fast Tilemap example"),
resolution: (1820., 920.).into(),
present_mode: PresentMode::Immediate,
..default()
}),
..default()
}),
LogDiagnosticsPlugin::default(),
FrameTimeDiagnosticsPlugin::default(),
MouseControlsCameraPlugin::default(),
FastTileMapPlugin::default(),
))
.add_systems(Startup, startup)
.run();
}
fn startup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<Map>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
commands.spawn(Camera2dBundle::default());
let map = Map::builder(
uvec2(51, 51),
asset_server.load("pixel_tiles_16.png"),
vec2(16., 16.),
)
.build_and_initialize(|m| {
for y in 0..m.size().y {
for x in 0..m.size().y {
m.set(x, y, ((x + y) % 4 + 1) as u32);
}
}
});
let mesh = Mesh2dHandle(meshes.add(Mesh::from(Circle::new(300.0))));
commands
.spawn(MapBundleUnmanaged::new(map, materials.as_mut()))
.insert(mesh.clone());
let map = Map::builder(
uvec2(51, 51),
asset_server.load("pixel_tiles_16.png"),
vec2(16., 16.),
)
.build_and_initialize(initialize_layer1);
let mut bundle = MapBundleManaged::new(map, materials.as_mut());
bundle.transform = Transform::default().with_translation(vec3(0., 0., 1.));
commands.spawn(bundle).insert(mesh);
}
fn initialize_layer1(m: &mut MapIndexerMut) {
let k = 10;
let y_min = m.size().y / 2 - k;
let x_min = m.size().x / 2 - k;
let y_max = m.size().y / 2 + k + 1;
let x_max = m.size().x / 2 + k + 1;
for y in y_min..y_max {
for x in x_min..x_max {
m.set(x, y, 11u32);
} } }