Add chunk constructor

This commit is contained in:
Jarrod Doyle 2024-01-29 15:15:46 +00:00
parent 508f7d7ad8
commit a57b457f49
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 20 additions and 16 deletions

View File

@ -46,23 +46,16 @@ fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
let image_handle = images.add(image); let image_handle = images.add(image);
commands commands.spawn(Chunk::new(256, 256)).insert(SpriteBundle {
.spawn(Chunk { sprite: Sprite {
width: 256, flip_y: true,
height: 256,
cells: vec![Elements::Air; 256 * 256],
dirty_rect: DirtyRect::default(),
})
.insert(SpriteBundle {
sprite: Sprite {
flip_y: true,
..default()
},
texture: image_handle,
transform: Transform::from_translation(Vec3::new(256., 0., 0.))
.with_scale(Vec3::new(2., 2., 0.)),
..default() ..default()
}); },
texture: image_handle,
transform: Transform::from_translation(Vec3::new(256., 0., 0.))
.with_scale(Vec3::new(2., 2., 0.)),
..default()
});
} }
#[derive(Component)] #[derive(Component)]
@ -73,6 +66,17 @@ pub struct Chunk {
dirty_rect: DirtyRect, dirty_rect: DirtyRect,
} }
impl Chunk {
pub fn new(width: usize, height: usize) -> Self {
Self {
width,
height,
cells: vec![Elements::Air; width * height],
dirty_rect: DirtyRect::default(),
}
}
}
pub fn place_sand_system(mut chunk: Query<&mut Chunk>) { pub fn place_sand_system(mut chunk: Query<&mut Chunk>) {
// We know for now there's only one chunk // We know for now there's only one chunk
let chunk = chunk.get_single_mut(); let chunk = chunk.get_single_mut();