use bevy::{prelude::*, sprite::SpriteBundle, DefaultPlugins}; use bevy_cells::prelude::*; fn main() { App::new() .add_plugins(DefaultPlugins) .add_plugins(CellsPlugin) .add_systems(Startup, spawn) .add_systems(Update, move_character) .add_systems(PostUpdate, sync_cell_transforms) .run(); } #[derive(Component)] struct Block; #[derive(Component)] struct Character; struct GameLayer; impl CellMapLabel for GameLayer { const CHUNK_SIZE: usize = 16; } fn spawn(mut commands: Commands, asset_server: Res) { let block = asset_server.load("block.png"); let character = asset_server.load("character.png"); commands.spawn(Camera2dBundle::default()); let mut cell_commands = commands.cells::(); let sprite_bundle = SpriteBundle { texture: block, ..Default::default() }; // spawn a 10 * 10 room cell_commands.spawn_cell_batch( CoordIterator::new([-5, 5], [5, 5]) .chain(CoordIterator::new([-5, -5], [5, -5])) .chain(CoordIterator::new([5, -4], [5, 4])) .chain(CoordIterator::new([-5, -4], [-5, 4])), move |_| (Block, sprite_bundle.clone()), ); // spawn a player cell_commands.spawn_cell( [0, 0], ( Character, SpriteBundle { texture: character, ..Default::default() }, ), ); } fn move_character( keyboard_input: Res>, mut commands: Commands, character: CellQuery>, walls: CellQuery>, ) { let mut cell_commands = commands.cells::(); let mut x = if keyboard_input.just_pressed(KeyCode::A) { -1 } else { 0 }; x += if keyboard_input.just_pressed(KeyCode::D) { 1 } else { 0 }; let mut y = if keyboard_input.just_pressed(KeyCode::W) { 1 } else { 0 }; y -= if keyboard_input.just_pressed(KeyCode::S) { 1 } else { 0 }; let char_c = character.get_single().unwrap(); let new_coord = [char_c[0] + x, char_c[1] + y]; if walls.get_at(new_coord).is_none() { cell_commands.move_cell(**char_c, new_coord); } } fn sync_cell_transforms( mut cells: CellQuery>, ) { for (cell_c, mut transform) in cells.iter_mut() { transform.translation.x = cell_c[0] as f32 * 16.0; transform.translation.y = cell_c[1] as f32 * 16.0; } }