diff options
author | Tom Barrett <tom@tombarrett.xyz> | 2022-07-31 14:00:56 +0200 |
---|---|---|
committer | Tom Barrett <tom@tombarrett.xyz> | 2022-07-31 14:00:56 +0200 |
commit | cd606702b4cc73119e8472ff2b006a3f13687d9c (patch) | |
tree | 7d153cea6a2bee02bb05e654a988ba679d602334 /src | |
parent | 9b28e3da4c9f92230d56648b07ad8a8c6a0ffadd (diff) |
functional parity with master, although a bug with the textures
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 53 |
1 files changed, 32 insertions, 21 deletions
diff --git a/src/main.rs b/src/main.rs index d0fb782..0c7f92e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,17 @@ use bevy::math::Vec3; use bevy::prelude::*; +use bevy::render::texture::ImageSettings; use gems::cell::{self, AnimationTimer, Cell, Occupant}; use gems::constants; use rand::{thread_rng, Rng}; use std::time::Duration; -fn star_spawning(mut commands: Commands, time: Res<Time>, mut q: Query<&mut AnimationTimer>) { - for mut timer in q.iter_mut() { +fn star_spawning( + mut commands: Commands, + time: Res<Time>, + mut query: Query<(&mut AnimationTimer, &Handle<TextureAtlas>)>, +) { + for (mut timer, texture_atlas_handle) in &mut query { if !timer.repeating() { timer.tick(time.delta()); if timer.just_finished() { @@ -15,20 +20,19 @@ fn star_spawning(mut commands: Commands, time: Res<Time>, mut q: Query<&mut Anim commands .spawn_bundle(SpriteSheetBundle { sprite: TextureAtlasSprite::new(constants::TILESHEET_STAR1), + texture_atlas: texture_atlas_handle.clone(), transform: Transform { translation: Vec3::new( - 0.0, - 0.0, - //thread_rng().gen_range(-300.0..300.0), - //thread_rng().gen_range(-200.0..300.0), - 1.15, + thread_rng().gen_range(-300.0..300.0), + thread_rng().gen_range(-200.0..300.0), + 0.1, ), - //scale: Vec3::splat(thread_rng().gen_range(1.0..3.0)), + scale: Vec3::splat(thread_rng().gen_range(1.0..3.0)), ..Default::default() }, ..Default::default() }) - .insert(AnimationTimer::from_seconds(0.25, true)); + .insert(AnimationTimer::from_seconds(0.15, true)); } } } @@ -51,7 +55,7 @@ pub fn setup( ); let atlas_handle = texture_atlases.add(atlas); - commands.spawn_bundle(OrthographicCameraBundle::new_2d()); + commands.spawn_bundle(Camera2dBundle::default()); commands.spawn_bundle(SpriteBundle { texture: background, transform: Transform { @@ -64,7 +68,7 @@ pub fn setup( commands.spawn_bundle(SpriteBundle { texture: title, transform: Transform { - translation: Vec3::new(240.0, 200.0, 0.1), + translation: Vec3::new(240.0, 200.0, 0.2), scale: Vec3::splat(2.0), ..Default::default() }, @@ -74,13 +78,16 @@ pub fn setup( sprite: TextureAtlasSprite::new(constants::TILESHEET_COSMONAUT1), texture_atlas: atlas_handle.clone(), transform: Transform { - translation: Vec3::new(225.0, -200.0, 0.1), + translation: Vec3::new(225.0, -200.0, 0.2), scale: Vec3::splat(constants::TILE_SCALE), ..Default::default() }, ..Default::default() }); - commands.spawn_bundle((AnimationTimer::from_seconds(1.0, false),)); + commands.spawn_bundle(( + AnimationTimer::from_seconds(1.0, false), + atlas_handle.clone(), + )); commands.spawn_bundle(SpriteSheetBundle { sprite: TextureAtlasSprite::new(constants::TILESHEET_COSMONAUT2), texture_atlas: atlas_handle.clone(), @@ -88,7 +95,7 @@ pub fn setup( translation: Vec3::new( 225.0, -200.0 + (-constants::TILE_SIZE) * constants::TILE_SCALE, - 0.1, + 0.2, ), scale: Vec3::splat(constants::TILE_SCALE), ..Default::default() @@ -106,7 +113,7 @@ pub fn setup( translation: Vec3::new( ((i as f32) * constants::TILE_SIZE * constants::TILE_SCALE) - 330.0, ((j as f32) * constants::TILE_SIZE * constants::TILE_SCALE) - 160.0, - 0.1, + 0.2, ), scale: Vec3::splat(constants::TILE_SCALE), ..Default::default() @@ -163,7 +170,7 @@ fn animation( fn cosmonaut_detect( mut commands: Commands, windows: Res<Windows>, - mut q: Query<(&Transform, &TextureAtlasSprite)>, + mut q: Query<(&Transform, &TextureAtlasSprite, &Handle<TextureAtlas>)>, ) { if let Some(mut cursor_position) = windows .get_primary() @@ -173,7 +180,7 @@ fn cosmonaut_detect( (constants::WINDOW_WIDTH / 2.0) - constants::TILE_SIZE * constants::TILE_SCALE * 0.5; cursor_position.y -= (constants::WINDOW_HEIGHT / 2.0) - constants::TILE_SIZE * constants::TILE_SCALE * 0.5; - for (transform, sprite) in q.iter_mut() { + for (transform, sprite, texture_atlas_handle) in q.iter_mut() { if transform.translation.x < cursor_position.x && transform.translation.x + constants::TILE_SIZE * constants::TILE_SCALE > cursor_position.x @@ -185,6 +192,7 @@ fn cosmonaut_detect( commands .spawn_bundle(SpriteSheetBundle { sprite: TextureAtlasSprite::new(constants::TILESHEET_VISOR1), + texture_atlas: texture_atlas_handle.clone(), transform: *transform, ..Default::default() }) @@ -242,10 +250,12 @@ fn mouse( } if let Some(selected) = cells.clone().iter_mut().find(|c| c.selected) { if let Some(hovered) = cells.iter_mut().find(|c| c.hovered) { - if (selected.x == hovered.x + 1 && selected.y == hovered.y) - || (selected.x == hovered.x.overflowing_sub(1).0 && selected.y == hovered.y) - || (selected.y == hovered.y + 1 && selected.x == hovered.x) - || (selected.y == hovered.y.overflowing_sub(1).0 && selected.x == hovered.x) + if ((selected.x == hovered.x + 1 + || selected.x == hovered.x.overflowing_sub(1).0) + && selected.y == hovered.y) + || ((selected.y == hovered.y + 1 + || selected.y == hovered.y.overflowing_sub(1).0) + && selected.x == hovered.x) { std::mem::swap(&mut selected.occupant, &mut hovered.occupant); for (mut cell, _, mut sprite) in q.iter_mut() { @@ -269,6 +279,7 @@ fn mouse( pub fn main() { App::new() + .insert_resource(ImageSettings::default_nearest()) .insert_resource(WindowDescriptor { title: "gems".to_string(), width: constants::WINDOW_WIDTH, |