Skip to content

Commit

Permalink
Query cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescarterbell committed Apr 14, 2024
1 parent 9f50a33 commit 8729849
Show file tree
Hide file tree
Showing 12 changed files with 335 additions and 312 deletions.
8 changes: 3 additions & 5 deletions crates/bevy_tiles/examples/basic_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ fn spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
let character = asset_server.load("character.png");

commands.spawn(Camera2dBundle::default());
let mut map_id = commands.spawn_map::<GameLayer, 2>(16, GameLayer).id();
let mut map = commands.spawn_map::<2>(16, GameLayer);

let sprite_bundle = SpriteBundle {
texture: block,
..Default::default()
};

// spawn a 10 * 10 room
commands.spawn_tile_batch(
map_id,
map.spawn_tile_batch(
CoordIterator::new([-5, 5], [5, 5])
.chain(CoordIterator::new([-5, -5], [5, -5]))
.chain(CoordIterator::new([5, -4], [5, 4]))
Expand All @@ -43,8 +42,7 @@ fn spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
);

// spawn a player
commands.spawn_tile(
map_id,
map.spawn_tile(
[0, 0],
(
Character,
Expand Down
26 changes: 12 additions & 14 deletions crates/bevy_tiles/examples/basic_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ struct Block;
#[derive(Component)]
struct Character;

#[derive(Component)]
struct GameLayer;

impl TileMapLabel for GameLayer {
const CHUNK_SIZE: usize = 16;
}

fn spawn(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let cube = meshes.add(Mesh::from(shape::Cube { size: 1.0 }));
let cube = meshes.add(Mesh::from(Cuboid {
half_size: Vec3::ONE,
}));

let color_block = materials.add(StandardMaterial {
base_color: Color::BLUE,
Expand All @@ -57,7 +56,7 @@ fn spawn(
..Default::default()
};

let mut tile_commands = commands.tiles::<GameLayer, 3>();
let mut tile_commands = commands.spawn_map::<3>(16, GameLayer);

// spawn a 10 * 10 room
tile_commands.spawn_tile_batch(
Expand Down Expand Up @@ -108,10 +107,12 @@ fn spawn(
fn move_character(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut commands: Commands,
character: TileMapQuery<GameLayer, &TileCoord<3>, With<Character>, 3>,
walls: TileMapQuery<GameLayer, (), With<Block>, 3>,
map: Query<Entity, With<GameLayer>>,
character: Query<&TileCoord<3>, With<Character>>,
walls_maps: TileMapQuery<(), With<Block>, 3>,
) {
let mut tile_commands = commands.tiles::<GameLayer, 3>();
let map_id = map.single();
let walls = walls_maps.get_map(map_id).unwrap();

let mut x = if keyboard_input.just_pressed(KeyCode::KeyA) {
-1
Expand Down Expand Up @@ -150,14 +151,11 @@ fn move_character(
let new_coord = [char_c[0] + x, char_c[1] + y, char_c[2] + z];

if walls.get_at(new_coord).is_none() {
tile_commands.move_tile(**char_c, new_coord);
commands.move_tile(map_id, **char_c, new_coord);
}
}

fn sync_tile_transforms(
// Important, you have to put the 3 in all these places at the moment!!!
mut tiles: TileMapQuery<GameLayer, (&TileCoord<3>, &mut Transform), Changed<TileCoord<3>>, 3>,
) {
fn sync_tile_transforms(mut tiles: Query<(&TileCoord<3>, &mut Transform), Changed<TileCoord<3>>>) {
for (tile_c, mut transform) in tiles.iter_mut() {
transform.translation.x = tile_c[0] as f32;
transform.translation.y = tile_c[1] as f32;
Expand Down
16 changes: 5 additions & 11 deletions crates/bevy_tiles/examples/logo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,24 @@ fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(TilesPlugin)
.add_systems(Startup, spawn)
.add_systems(Update, sync_tile_transforms)
.add_systems(Startup, (spawn, sync_tile_transforms).chain())
.run();
}

#[derive(Component)]
struct Block;

#[derive(Component)]
struct GameLayer;

impl TileMapLabel for GameLayer {
const CHUNK_SIZE: usize = 16;
}

fn spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
let block = asset_server.load("block.png");

commands.spawn(Camera2dBundle {
transform: Transform::from_translation(Vec3::new(480.0, 32.0, 0.0)),
..Default::default()
});
let mut tile_commands = commands.tiles::<GameLayer, 2>();
let mut map = commands.spawn_map::<2>(16, GameLayer);

let sprite_bundle = SpriteBundle {
texture: block,
Expand All @@ -51,14 +47,12 @@ eeeee eeee e e e e eeee8 eeeee e eeee eeeee
});

// spawn a 10 * 10 room
tile_commands.spawn_tile_batch(logo.collect::<Vec<[isize; 2]>>(), move |_| {
map.spawn_tile_batch(logo.collect::<Vec<[isize; 2]>>(), move |_| {
(Block, sprite_bundle.clone())
});
}

fn sync_tile_transforms(
mut tiles: TileMapQuery<GameLayer, (&TileCoord, &mut Transform), Changed<TileCoord>>,
) {
fn sync_tile_transforms(mut tiles: Query<(&TileCoord, &mut Transform), Changed<TileCoord>>) {
for (tile_c, mut transform) in tiles.iter_mut() {
transform.translation.x = tile_c[0] as f32 * 16.0;
transform.translation.y = tile_c[1] as f32 * 16.0;
Expand Down
30 changes: 16 additions & 14 deletions crates/bevy_tiles/examples/spatial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,9 @@ impl DerefMut for Damage {
}
}

#[derive(Component)]
struct GameLayer;

impl TileMapLabel for GameLayer {
const CHUNK_SIZE: usize = 16;
}

fn spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
let block = asset_server.load("block.png");

Expand All @@ -57,26 +54,33 @@ fn spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
},
..Default::default()
});
let mut tile_commands = commands.tiles::<GameLayer, 2>();
let mut tile_commands = commands.spawn_map::<2>(32, GameLayer);

let sprite_bundle = SpriteBundle {
texture: block,
..Default::default()
};

tile_commands.spawn_tile_batch(CoordIterator::new([-250, -250], [250, 250]), move |_| {
(Block, sprite_bundle.clone())
})
let size = 200;

tile_commands.spawn_tile_batch(
CoordIterator::new([-size, -size], [size, size]),
move |_| (Block, sprite_bundle.clone()),
);
}

fn add_damage(
mut commands: Commands,
mut blocks: TileMapQuery<(Entity, Option<&mut Damage>), With<Block>>,
mut block_maps: TileMapQuery<(Entity, Option<&mut Damage>), With<Block>>,
map: Query<(Entity, &TileMap), With<GameLayer>>,
windows: Query<&Window, With<PrimaryWindow>>,
camera: Query<(&Camera, &GlobalTransform)>,
buttons: Res<ButtonInput<MouseButton>>,
) {
let (map_id, map) = map.single();
let (cam, cam_t) = camera.single();
let mut blocks = block_maps.get_map_mut(map_id).unwrap();

let cursor_pos = windows
.single()
.cursor_position()
Expand Down Expand Up @@ -104,8 +108,8 @@ fn add_damage(
.then_some(cursor_pos)
.flatten()
{
let chunk_c = calculate_chunk_coordinate(damage_pos, GameLayer::CHUNK_SIZE);
commands.tiles::<GameLayer, 2>().despawn_chunk(chunk_c);
let chunk_c = calculate_chunk_coordinate(damage_pos, map.get_chunk_size());
commands.tile_map::<2>(map_id).despawn_chunk(chunk_c);
}
}

Expand All @@ -129,9 +133,7 @@ fn check_damage(
}
}

fn sync_tile_transforms(
mut tiles: TileMapQuery<GameLayer, (&TileCoord, &mut Transform), Changed<TileCoord>>,
) {
fn sync_tile_transforms(mut tiles: Query<(&TileCoord, &mut Transform), Changed<TileCoord>>) {
for (tile_c, mut transform) in tiles.iter_mut() {
transform.translation.x = tile_c[0] as f32 * 16.0;
transform.translation.y = tile_c[1] as f32 * 16.0;
Expand Down
Loading

0 comments on commit 8729849

Please sign in to comment.