use bevy::prelude::*; use bevy_ecs_tilemap::prelude::*; mod helpers; fn startup(mut commands: Commands, asset_server: Res) { commands.spawn(Camera2dBundle::default()); let texture_handle: Handle = asset_server.load("tiles.png"); let map_size = TilemapSize { x: 2, y: 1 }; // Create a tilemap entity a little early. let tilemap_entity = commands.spawn_empty().id(); let mut tile_storage = TileStorage::empty(map_size); // Spawn the elements of the tilemap. let tile_pos = TilePos { x: 0, y: 0 }; let tile_entity = commands .spawn(TileBundle { position: tile_pos, tilemap_id: TilemapId(tilemap_entity), ..Default::default() }) .id(); tile_storage.set(&tile_pos, tile_entity); let tile_size = TilemapTileSize { x: 16.0, y: 16.0 }; let grid_size = tile_size.into(); let map_type = TilemapType::default(); commands.entity(tilemap_entity).insert(TilemapBundle { grid_size, map_type, size: map_size, storage: tile_storage, texture: TilemapTexture::Single(texture_handle), tile_size, transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0), ..Default::default() }); } fn swap_pos(keyboard_input: Res>, mut query: Query<&mut TilePos>) { if keyboard_input.just_pressed(KeyCode::Space) { for mut pos in query.iter_mut() { if pos.x == 0 { pos.x = 1; } else { pos.x = 0; } } } } fn main() { App::new() .add_plugins( DefaultPlugins .set(WindowPlugin { primary_window: Some(Window { title: String::from("Update tile positions without despawning."), ..Default::default() }), ..default() }) .set(ImagePlugin::default_nearest()), ) .add_plugins(TilemapPlugin) .add_systems(Startup, startup) .add_systems(Update, helpers::camera::movement) .add_systems(Update, swap_pos) .run(); }