diff options
author | Tom Barrett <tom@tombarrett.xyz> | 2024-09-08 18:44:03 +0200 |
---|---|---|
committer | Tom Barrett <tom@tombarrett.xyz> | 2024-09-08 18:44:03 +0200 |
commit | 4b672ebdb61669dbd2c14fe1220dec0fb1f90f0d (patch) | |
tree | 9dd15a428f5bc97e19e8e2b730a6004ab1506c2a /src | |
parent | 0375faa9670737191d0c276f3b5daf83a6cfc8e8 (diff) |
trying out bevy 13bevy_13
Diffstat (limited to 'src')
-rw-r--r-- | src/cell.rs | 8 | ||||
-rw-r--r-- | src/main.rs | 95 |
2 files changed, 66 insertions, 37 deletions
diff --git a/src/cell.rs b/src/cell.rs index 6571cd5..d865c97 100644 --- a/src/cell.rs +++ b/src/cell.rs @@ -102,13 +102,13 @@ impl Cell { } } - pub fn set_occupant(&mut self, occupant: Occupant, sprite: &mut TextureAtlasSprite) { + pub fn set_occupant(&mut self, occupant: Occupant, sprite: &mut TextureAtlas) { self.occupant = occupant; sprite.index = self.occupant.to_index(); } } -pub fn insert(mut q: Query<(&mut Cell, &mut TextureAtlasSprite)>) { +pub fn insert(mut q: Query<(&mut Cell, &mut TextureAtlas)>) { for (mut cell, mut sprite) in q.iter_mut() { if cell.occupant == Occupant::None && cell.y == constants::GRID_SIZE - 1 { cell.set_occupant(rand::random(), &mut sprite); @@ -129,7 +129,7 @@ pub fn start_explosion( } } -pub fn check(mut q: Query<(&mut Cell, &mut TextureAtlasSprite)>) { +pub fn check(mut q: Query<(&mut Cell, &mut TextureAtlas)>) { let mut cells = [[Cell::default(); constants::GRID_SIZE]; constants::GRID_SIZE]; for (cell, _) in q.iter_mut() { cells[cell.x][cell.y] = *cell; @@ -185,7 +185,7 @@ pub fn check(mut q: Query<(&mut Cell, &mut TextureAtlasSprite)>) { } } -pub fn falling(mut q: Query<(&mut Cell, &mut TextureAtlasSprite)>) { +pub fn falling(mut q: Query<(&mut Cell, &mut TextureAtlas)>) { let mut have_gems = Vec::new(); for (cell, _sprite) in q.iter_mut() { if cell.occupant != Occupant::None { diff --git a/src/main.rs b/src/main.rs index 136bb87..7eee0ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,9 +9,13 @@ use std::time::Duration; fn star_spawning( mut commands: Commands, time: Res<Time>, - mut query: Query<(&mut AnimationTimer, &Handle<TextureAtlas>)>, + mut query: Query<( + &mut AnimationTimer, + &Handle<TextureAtlasLayout>, + &Handle<Image>, + )>, ) { - for (mut timer, texture_atlas_handle) in &mut query { + for (mut timer, layout, texture) in &mut query { if !timer.repeating() { timer.tick(time.delta()); if timer.just_finished() { @@ -19,8 +23,11 @@ fn star_spawning( timer.reset(); commands .spawn(SpriteSheetBundle { - sprite: TextureAtlasSprite::new(constants::TILESHEET_STAR1), - texture_atlas: texture_atlas_handle.clone(), + texture: texture.clone(), + atlas: TextureAtlas { + layout: layout.clone(), + index: constants::TILESHEET_STAR1, + }, transform: Transform { translation: Vec3::new( thread_rng().gen_range(-300.0..300.0), @@ -41,21 +48,20 @@ fn star_spawning( pub fn setup( mut commands: Commands, asset_server: Res<AssetServer>, - mut texture_atlases: ResMut<Assets<TextureAtlas>>, + mut texture_atlases: ResMut<Assets<TextureAtlasLayout>>, ) { let background = asset_server.load("background.png"); let tileset = asset_server.load("tileset.png"); let title = asset_server.load("title.png"); - let atlas = TextureAtlas::from_grid( - tileset, + let layout = TextureAtlasLayout::from_grid( Vec2::new(constants::TILE_SIZE, constants::TILE_SIZE), 5, 5, None, None, ); - let atlas_handle = texture_atlases.add(atlas); + let layout_handle = texture_atlases.add(layout); commands.spawn(Camera2dBundle::default()); commands.spawn(SpriteBundle { @@ -77,8 +83,11 @@ pub fn setup( ..Default::default() }); commands.spawn(SpriteSheetBundle { - sprite: TextureAtlasSprite::new(constants::TILESHEET_COSMONAUT1), - texture_atlas: atlas_handle.clone(), + texture: tileset.clone(), + atlas: TextureAtlas { + layout: layout_handle.clone(), + index: constants::TILESHEET_COSMONAUT1, + }, transform: Transform { translation: Vec3::new(225.0, -200.0, 0.2), scale: Vec3::splat(constants::TILE_SCALE), @@ -88,11 +97,15 @@ pub fn setup( }); commands.spawn(( AnimationTimer::from_seconds(1.0, TimerMode::Once), - atlas_handle.clone(), + layout_handle.clone(), + tileset.clone(), )); commands.spawn(SpriteSheetBundle { - sprite: TextureAtlasSprite::new(constants::TILESHEET_COSMONAUT2), - texture_atlas: atlas_handle.clone(), + texture: tileset.clone(), + atlas: TextureAtlas { + layout: layout_handle.clone(), + index: constants::TILESHEET_COSMONAUT2, + }, transform: Transform { translation: Vec3::new( 225.0, @@ -109,7 +122,11 @@ pub fn setup( for j in 0..constants::GRID_SIZE { commands .spawn(SpriteSheetBundle { - texture_atlas: atlas_handle.clone(), + texture: tileset.clone(), + atlas: TextureAtlas { + layout: layout_handle.clone(), + index: Occupant::None as usize, + }, transform: Transform { translation: Vec3::new( ((i as f32) * constants::TILE_SIZE * constants::TILE_SCALE) - 330.0, @@ -133,13 +150,13 @@ fn animation( Entity, Option<&mut Cell>, &mut AnimationTimer, - &mut TextureAtlasSprite, + &mut TextureAtlas, )>, ) { - for (entity, cell, mut timer, mut sprite) in q.iter_mut() { + for (entity, cell, mut timer, mut atlas) in q.iter_mut() { timer.tick(time.delta()); if timer.finished() { - let index = match sprite.index { + let index = match atlas.index { constants::TILESHEET_EXPLOSION1 => Some(constants::TILESHEET_EXPLOSION2), constants::TILESHEET_EXPLOSION2 => Some(constants::TILESHEET_EXPLOSION3), constants::TILESHEET_EXPLOSION3 => Some(constants::TILESHEET_EXPLOSION4), @@ -156,9 +173,9 @@ fn animation( }; if let Some(index) = index { - sprite.index = index; + atlas.index = index; } else if let Some(mut cell) = cell { - cell.set_occupant(Occupant::None, &mut sprite); + cell.set_occupant(Occupant::None, &mut atlas); commands.entity(entity).remove::<AnimationTimer>(); } else { commands.entity(entity).despawn(); @@ -169,7 +186,12 @@ fn animation( fn cosmonaut_detect( mut commands: Commands, - mut q: Query<(&Transform, &TextureAtlasSprite, &Handle<TextureAtlas>)>, + mut q: Query<( + &Transform, + &TextureAtlas, + &Handle<TextureAtlasLayout>, + //&Handle<Image>, + )>, mut windows: Query<&Window, With<PrimaryWindow>>, ) { let window = windows.single_mut(); @@ -179,32 +201,39 @@ fn cosmonaut_detect( cursor_position.y -= (constants::WINDOW_HEIGHT / 2.0) + constants::TILE_SIZE * constants::TILE_SCALE * 0.5; cursor_position.y *= -1.0; - for (transform, sprite, texture_atlas_handle) in q.iter_mut() { + println!("here"); + for (transform, atlas, layout) in q.iter_mut() { if transform.translation.x < cursor_position.x && transform.translation.x + constants::TILE_SIZE * constants::TILE_SCALE > cursor_position.x && transform.translation.y < cursor_position.y && transform.translation.y + constants::TILE_SIZE * constants::TILE_SCALE > cursor_position.y - && sprite.index == constants::TILESHEET_COSMONAUT1 + && atlas.index == constants::TILESHEET_COSMONAUT1 { + println!("{:?}\n{:?}", atlas, layout); + /* commands .spawn(SpriteSheetBundle { - sprite: TextureAtlasSprite::new(constants::TILESHEET_VISOR1), - texture_atlas: texture_atlas_handle.clone(), + //texture: texture.clone(), + atlas: TextureAtlas { + layout: layout.clone(), + index: constants::TILESHEET_VISOR1, + }, transform: *transform, ..Default::default() }) .insert(AnimationTimer::from_seconds(0.1, TimerMode::Repeating)); + */ } } } } fn mouse( - mut q: Query<(&mut Cell, &mut Transform, &mut TextureAtlasSprite)>, + mut q: Query<(&mut Cell, &mut Transform, &mut TextureAtlas, &mut Sprite)>, mut windows: Query<&Window, With<PrimaryWindow>>, - mouse_button_input: Res<Input<MouseButton>>, + mouse_button_input: Res<ButtonInput<MouseButton>>, ) { let window = windows.single_mut(); if let Some(mut cursor_position) = window.cursor_position() { @@ -214,7 +243,7 @@ fn mouse( (constants::WINDOW_HEIGHT / 2.0) + constants::TILE_SIZE * constants::TILE_SCALE * 0.5; cursor_position.y *= -1.0; - for (cell, _, mut sprite) in q.iter_mut() { + for (cell, _, _, mut sprite) in q.iter_mut() { if cell.selected { sprite.color.set_a(0.2); } else if cell.hovered { @@ -224,7 +253,7 @@ fn mouse( } } - for (mut cell, transform, _) in q.iter_mut() { + for (mut cell, transform, _, _) in q.iter_mut() { if transform.translation.x < cursor_position.x && transform.translation.x + constants::TILE_SIZE * constants::TILE_SCALE > cursor_position.x @@ -244,7 +273,7 @@ fn mouse( if mouse_button_input.just_released(MouseButton::Left) { let mut cells: Vec<Cell> = Vec::new(); - for (cell, _, _) in q.iter_mut() { + for (cell, _, _, _) in q.iter_mut() { cells.push(*cell); } if let Some(selected) = cells.clone().iter_mut().find(|c| c.selected) { @@ -256,19 +285,19 @@ fn mouse( && selected.x == hovered.x) { std::mem::swap(&mut selected.occupant, &mut hovered.occupant); - for (mut cell, _, mut sprite) in q.iter_mut() { + for (mut cell, _, mut atlas, _) in q.iter_mut() { if cell.x == selected.x && cell.y == selected.y { cell.occupant = selected.occupant; - sprite.index = cell.occupant.to_index(); + atlas.index = cell.occupant.to_index(); } else if cell.x == hovered.x && cell.y == hovered.y { cell.occupant = hovered.occupant; - sprite.index = cell.occupant.to_index(); + atlas.index = cell.occupant.to_index(); } } } } } - for (mut cell, _, _) in q.iter_mut() { + for (mut cell, _, _, _) in q.iter_mut() { cell.selected = false; } } |