diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cell.rs | 79 | ||||
-rw-r--r-- | src/constants.rs | 50 | ||||
-rw-r--r-- | src/cosmonaut.rs | 110 | ||||
-rw-r--r-- | src/game.rs | 275 | ||||
-rw-r--r-- | src/main.rs | 81 |
5 files changed, 120 insertions, 475 deletions
diff --git a/src/cell.rs b/src/cell.rs index e4be4ac..6a818c5 100644 --- a/src/cell.rs +++ b/src/cell.rs @@ -1,10 +1,48 @@ use crate::constants; use bevy::prelude::*; +use bevy::utils::Duration; use rand::{ distributions::{Distribution, Standard}, Rng, }; +#[derive(Component)] +pub struct AnimationTimer { + timer: Timer, +} + +impl AnimationTimer { + pub fn from_seconds(duration: f32, repeating: bool) -> AnimationTimer { + AnimationTimer { + timer: Timer::from_seconds(duration, repeating), + } + } + + pub fn finished(&self) -> bool { + self.timer.finished() + } + + pub fn tick(&mut self, delta: Duration) -> &Timer { + self.timer.tick(delta) + } + + pub fn just_finished(&self) -> bool { + self.timer.just_finished() + } + + pub fn reset(&mut self) { + self.timer.reset(); + } + + pub fn set_duration(&mut self, duration: Duration) { + self.timer.set_duration(duration); + } + + pub fn repeating(&self) -> bool { + self.timer.repeating() + } +} + #[derive(Debug, PartialEq, Clone, Copy)] pub enum Occupant { None, @@ -23,7 +61,7 @@ impl Default for Occupant { } impl Occupant { - pub fn to_index(&self) -> u32 { + pub fn to_index(&self) -> usize { match self { Occupant::Green => constants::TILESHEET_GREEN, Occupant::Yellow => constants::TILESHEET_YELLOW, @@ -49,7 +87,7 @@ impl Distribution<Occupant> for Standard { } } -#[derive(Debug, Clone, Default, Copy)] +#[derive(Debug, Clone, Default, Copy, Component)] pub struct Cell { pub x: usize, pub y: usize, @@ -73,37 +111,40 @@ impl Cell { &mut self, occupant: Occupant, sprite: &mut TextureAtlasSprite, - visible: &mut Visible, + visibility: &mut Visibility, ) { self.occupant = occupant; sprite.index = self.occupant.to_index(); if self.occupant == Occupant::None { - visible.is_visible = false; + visibility.is_visible = false; } else { - visible.is_visible = true; + visibility.is_visible = true; } } } -pub fn insert(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visible)>) { - for (mut cell, mut sprite, mut visible) in q.iter_mut() { +pub fn insert(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visibility)>) { + for (mut cell, mut sprite, mut visibility) in q.iter_mut() { if cell.occupant == Occupant::None && cell.y == constants::GRID_SIZE - 1 { - cell.set_occupant(rand::random(), &mut sprite, &mut visible); + cell.set_occupant(rand::random(), &mut sprite, &mut visibility); } } } -pub fn start_explosion(mut commands: Commands, mut q: Query<(Entity, &Cell), Without<Timer>>) { +pub fn start_explosion( + mut commands: Commands, + mut q: Query<(Entity, &Cell), Without<AnimationTimer>>, +) { for (entity, cell) in q.iter_mut() { if cell.occupant == Occupant::Explosion { commands .entity(entity) - .insert(Timer::from_seconds(0.1, true)); + .insert(AnimationTimer::from_seconds(0.1, true)); } } } -pub fn check(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visible)>) { +pub fn check(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visibility)>) { let mut cells = [[Cell::default(); constants::GRID_SIZE]; constants::GRID_SIZE]; for (cell, _, _) in q.iter_mut() { cells[cell.x][cell.y] = *cell; @@ -150,39 +191,39 @@ pub fn check(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visible)>) { for c in connected.iter() { for (i, j) in c.iter() { - for (mut cell, mut sprite, mut visible) in q.iter_mut() { + for (mut cell, mut sprite, mut visibility) in q.iter_mut() { if &cell.x == i && &cell.y == j && cell.occupant != Occupant::Explosion { - cell.set_occupant(Occupant::Explosion, &mut sprite, &mut visible); + cell.set_occupant(Occupant::Explosion, &mut sprite, &mut visibility); } } } } } -pub fn falling(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visible)>) { +pub fn falling(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visibility)>) { let mut have_gems = Vec::new(); - for (cell, _sprite, _visible) in q.iter_mut() { + for (cell, _sprite, _visibility) in q.iter_mut() { if cell.occupant != Occupant::None { have_gems.push(*cell); } } let mut moved_gems = Vec::new(); - for (mut cell, mut sprite, mut visible) in q.iter_mut() { + for (mut cell, mut sprite, mut visibility) in q.iter_mut() { if cell.occupant == Occupant::None { if let Some(c) = have_gems .iter() .find(|&c| (c.x, c.y) == (cell.x, cell.y + 1)) { - cell.set_occupant(c.occupant, &mut sprite, &mut visible); + cell.set_occupant(c.occupant, &mut sprite, &mut visibility); moved_gems.push(c); } } } - for (mut cell, mut sprite, mut visible) in q.iter_mut() { + for (mut cell, mut sprite, mut visibility) in q.iter_mut() { if moved_gems.iter().any(|c| (c.x, c.y) == (cell.x, cell.y)) { - cell.set_occupant(Occupant::None, &mut sprite, &mut visible); + cell.set_occupant(Occupant::None, &mut sprite, &mut visibility); } } } diff --git a/src/constants.rs b/src/constants.rs index 86bbe5e..af5dc06 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -4,32 +4,32 @@ pub const WINDOW_HEIGHT: f32 = 600.0; pub const WINDOW_WIDTH: f32 = 800.0; pub const GRID_SIZE: usize = 8; -pub const TILESHEET_EXPLOSION1: u32 = 0; -pub const TILESHEET_EXPLOSION2: u32 = 1; -pub const TILESHEET_EXPLOSION3: u32 = 2; -pub const TILESHEET_EXPLOSION4: u32 = 3; -pub const TILESHEET_EXPLOSION5: u32 = 4; +pub const TILESHEET_EXPLOSION1: usize = 0; +pub const TILESHEET_EXPLOSION2: usize = 1; +pub const TILESHEET_EXPLOSION3: usize = 2; +pub const TILESHEET_EXPLOSION4: usize = 3; +pub const TILESHEET_EXPLOSION5: usize = 4; -pub const TILESHEET_GREEN: u32 = 5; -pub const TILESHEET_YELLOW: u32 = 6; -pub const TILESHEET_BLUE: u32 = 7; -pub const TILESHEET_RED: u32 = 8; -pub const TILESHEET_PURPLE: u32 = 9; +pub const TILESHEET_GREEN: usize = 5; +pub const TILESHEET_YELLOW: usize = 6; +pub const TILESHEET_BLUE: usize = 7; +pub const TILESHEET_RED: usize = 8; +pub const TILESHEET_PURPLE: usize = 9; -pub const TILESHEET_STAR1: u32 = 10; -pub const TILESHEET_STAR2: u32 = 11; -pub const TILESHEET_STAR3: u32 = 12; -//pub const TILESHEET_X: u32 = 13; -//pub const TILESHEET_X: u32 = 14; +pub const TILESHEET_STAR1: usize = 10; +pub const TILESHEET_STAR2: usize = 11; +pub const TILESHEET_STAR3: usize = 12; +//pub const TILESHEET_X: usize = 13; +//pub const TILESHEET_X: usize = 14; -pub const TILESHEET_COSMONAUT1: u32 = 15; -pub const TILESHEET_VISOR1: u32 = 16; -pub const TILESHEET_VISOR2: u32 = 17; -//pub const TILESHEET_X: u32 = 18; -//pub const TILESHEET_X: u32 = 19; +pub const TILESHEET_COSMONAUT1: usize = 15; +pub const TILESHEET_VISOR1: usize = 16; +pub const TILESHEET_VISOR2: usize = 17; +//pub const TILESHEET_X: usize = 18; +//pub const TILESHEET_X: usize = 19; -pub const TILESHEET_COSMONAUT2: u32 = 20; -pub const TILESHEET_VISOR3: u32 = 21; -pub const TILESHEET_VISOR4: u32 = 22; -//pub const TILESHEET_X: u32 = 23; -//pub const TILESHEET_X: u32 = 24; +pub const TILESHEET_COSMONAUT2: usize = 20; +pub const TILESHEET_VISOR3: usize = 21; +pub const TILESHEET_VISOR4: usize = 22; +//pub const TILESHEET_X: usize = 23; +//pub const TILESHEET_X: usize = 24; diff --git a/src/cosmonaut.rs b/src/cosmonaut.rs deleted file mode 100644 index 90a7c07..0000000 --- a/src/cosmonaut.rs +++ /dev/null @@ -1,110 +0,0 @@ -use crate::constants; -use ggez::graphics::{self, DrawParam, FilterMode, Image, Rect}; -use ggez::mint::{Point2, Vector2}; -use ggez::{Context, GameResult}; -use std::time::Instant; - -#[derive(Clone, Copy)] -enum CosmonautFrames { - None, - One, - Two, - Three, - Four, -} - -impl CosmonautFrames { - pub fn next(&mut self) { - *self = match self { - CosmonautFrames::None => CosmonautFrames::One, - CosmonautFrames::One => CosmonautFrames::Two, - CosmonautFrames::Two => CosmonautFrames::Three, - CosmonautFrames::Three => CosmonautFrames::Four, - CosmonautFrames::Four => CosmonautFrames::None, - }; - } -} - -pub struct Cosmonaut { - destination: Point2<f32>, - image: Image, - frame: CosmonautFrames, - timer: Instant, - scale: Vector2<f32>, -} - -impl Cosmonaut { - pub fn new(context: &mut Context) -> GameResult<Cosmonaut> { - let mut image = Image::new(context, "/cosmonaut.png")?; - image.set_filter(FilterMode::Nearest); - - Ok(Cosmonaut { - image, - destination: Point2 { x: 600.0, y: 350.0 }, - frame: CosmonautFrames::None, - timer: Instant::now(), - scale: Vector2 { - x: constants::TILE_SCALE * 2.0, - y: constants::TILE_SCALE * 2.0, - }, - }) - } - - pub fn draw(&self, context: &mut Context) -> GameResult { - graphics::draw( - context, - &self.image, - DrawParam::default() - .dest(self.destination) - .scale(self.scale) - .src(Rect::new(0.0, 0.0, 1.0 / 3.0, 1.0)), - )?; - let source = match self.frame { - CosmonautFrames::None => None, - CosmonautFrames::One => Some(Rect::new(1.0 / 3.0, 0.0, 1.0 / 3.0, 1.0 / 2.0)), - CosmonautFrames::Two => Some(Rect::new(2.0 / 3.0, 0.0, 1.0, 1.0 / 2.0)), - CosmonautFrames::Three => Some(Rect::new(1.0 / 3.0, 1.0 / 2.0, 1.0 / 3.0, 1.0 / 2.0)), - CosmonautFrames::Four => Some(Rect::new(2.0 / 3.0, 1.0 / 2.0, 1.0, 1.0)), - }; - if let Some(source) = source { - graphics::draw( - context, - &self.image, - DrawParam::default() - .dest(self.destination) - .src(source) - .scale(self.scale), - )?; - } - - Ok(()) - } - - pub fn update(&mut self) { - match self.frame { - CosmonautFrames::None => (), - _ => { - if self.timer.elapsed().as_millis() > 50 { - self.frame.next(); - self.timer = Instant::now(); - } - } - } - } - - pub fn start(&mut self) { - if let CosmonautFrames::None = self.frame { - if self.timer.elapsed().as_secs() > 5 { - self.timer = Instant::now(); - self.frame.next() - } - } - } - - pub fn contains(&self, position: Point2<f32>) -> bool { - position.x > self.destination.x - && position.y > self.destination.y - && position.x < self.destination.x + constants::TILE_WIDTH * constants::TILE_SCALE * 2.0 - && position.y < self.destination.y + constants::TILE_WIDTH * constants::TILE_SCALE * 2.0 - } -} diff --git a/src/game.rs b/src/game.rs deleted file mode 100644 index d2b88e8..0000000 --- a/src/game.rs +++ /dev/null @@ -1,275 +0,0 @@ -use ggez::event::EventHandler; -use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image, WrapMode}; -use ggez::input::mouse; -use ggez::mint::{Point2, Vector2}; -use ggez::{Context, GameResult}; -use std::time::Instant; - -use crate::cell::{Cell, Occupant}; -use crate::constants; -use crate::cosmonaut::Cosmonaut; - -pub struct Game { - selected: Option<(usize, usize)>, - spritebatch: SpriteBatch, - grid: Vec<Vec<Cell>>, - background: Image, - cosmonaut: Cosmonaut, -} - -impl Game { - pub fn new(context: &mut Context) -> GameResult<Game> { - let mut background = Image::new(context, "/background.png")?; - background.set_filter(FilterMode::Nearest); - - let mut image = Image::new(context, "/tileset.png")?; - image.set_filter(FilterMode::Nearest); - image.set_wrap(WrapMode::Mirror, WrapMode::Mirror); - - let mut grid = Vec::new(); - let mut y = 0.0; - - for _ in 0..constants::COLUMNS { - let mut column = Vec::new(); - for j in 0..constants::ROWS { - column.push(Cell::new(Point2 { - x: (j as f32) - * (constants::TILE_WIDTH * constants::TILE_SCALE + constants::BORDER_SIZE) - + constants::SHIFT_X, - y: y + constants::SHIFT_Y, - })); - } - y += constants::TILE_HEIGHT * constants::TILE_SCALE + constants::BORDER_SIZE; - grid.push(column); - } - - Ok(Game { - grid, - selected: None, - cosmonaut: Cosmonaut::new(context)?, - spritebatch: SpriteBatch::new(image), - background, - }) - } - - fn update_explosions(&mut self) { - let mut last = Occupant::None; - let mut connected = Vec::new(); - for i in 0..constants::COLUMNS { - let mut c = Vec::new(); - for j in 0..constants::ROWS { - if self.grid[i][j].occupant == last && last != Occupant::None { - c.push((i, j)); - c.push((i, j - 1)); - } else { - connected.push(c.clone()); - c.clear(); - } - last = self.grid[i][j].occupant; - } - connected.push(c); - last = Occupant::None; - } - - for c in connected.iter() { - if c.len() > 3 { - for (i, j) in c.iter() { - self.grid[*i][*j].occupant = Occupant::Explosion { - frame: 0, - timer: Instant::now(), - }; - } - } - } - - connected.clear(); - for i in 0..constants::COLUMNS { - let mut c = Vec::new(); - for j in 0..constants::ROWS { - if self.grid[j][i].occupant == last && last != Occupant::None { - c.push((j, i)); - c.push((j - 1, i)); - } else { - connected.push(c.clone()); - c.clear(); - } - last = self.grid[j][i].occupant; - } - connected.push(c); - last = Occupant::None; - } - - for c in connected.iter() { - if c.len() > 3 { - for (j, i) in c.iter() { - self.grid[*j][*i].occupant = Occupant::Explosion { - frame: 0, - timer: Instant::now(), - }; - } - } - } - } - - fn update_dropping(&mut self) { - for i in 1..constants::COLUMNS { - for j in 0..constants::ROWS { - if self.grid[i][j].occupant == Occupant::None - && self.grid[i - 1][j].occupant != Occupant::None - { - self.grid[i][j].occupant = self.grid[i - 1][j].occupant; - self.grid[i - 1][j].occupant = Occupant::None; - } - } - } - } - - fn update_feeding(&mut self) { - for i in 0..constants::COLUMNS { - if self.grid[0][i].occupant == Occupant::None { - self.grid[0][i].occupant = rand::random(); - } - } - } - - fn update_frames(&mut self) { - for row in self.grid.iter_mut() { - for cell in row.iter_mut() { - let mut done = false; - if let Occupant::Explosion { - ref mut frame, - ref mut timer, - } = cell.occupant - { - if timer.elapsed().as_millis() > 500 { - if *frame < 4 { - *frame += 1; - *timer = Instant::now(); - } else { - done = true; - } - } - } - if done { - cell.occupant = Occupant::None; - } - } - } - } -} - -impl EventHandler for Game { - fn update(&mut self, context: &mut Context) -> GameResult { - for row in self.grid.iter_mut() { - for cell in row.iter_mut() { - cell.hover_off(); - } - } - - let position = mouse::position(context); - - if self.cosmonaut.contains(position) { - self.cosmonaut.start(); - } - - self.cosmonaut.update(); - - for row in self.grid.iter_mut() { - for cell in row.iter_mut() { - if cell.contains(position) { - cell.hover_on(); - } - } - } - - self.update_explosions(); - self.update_frames(); - self.update_dropping(); - self.update_feeding(); - - Ok(()) - } - - fn mouse_button_down_event( - &mut self, - _context: &mut Context, - button: mouse::MouseButton, - x: f32, - y: f32, - ) { - if button == mouse::MouseButton::Left { - let position = Point2 { x, y }; - for (i, row) in self.grid.iter_mut().enumerate() { - for (j, cell) in row.iter_mut().enumerate() { - if cell.contains(position) && cell.moveable() { - self.selected = Some((i, j)); - cell.clicked_on(); - } - } - } - } - } - - fn mouse_button_up_event( - &mut self, - _context: &mut Context, - button: mouse::MouseButton, - x: f32, - y: f32, - ) { - if button == mouse::MouseButton::Left { - for row in self.grid.iter_mut() { - for cell in row.iter_mut() { - cell.clicked_off(); - } - } - - if let Some(selected) = self.selected { - let position = Point2 { x, y }; - let mut swap = None; - for (i, row) in self.grid.iter_mut().enumerate() { - for (j, cell) in row.iter_mut().enumerate() { - if cell.contains(position) - && cell.moveable() - && (((i + 1 == selected.0) && (j == selected.1)) - || ((i.overflowing_sub(1).0 == selected.0) && (j == selected.1)) - || ((i == selected.0) && (j + 1 == selected.1)) - || ((i == selected.0) && (j.overflowing_sub(1)).0 == selected.1)) - { - swap = Some((i, j)); - } - } - } - if let Some((i, j)) = swap { - let clone = self.grid[i][j].occupant; - self.grid[i][j].occupant = self.grid[selected.0][selected.1].occupant; - self.grid[selected.0][selected.1].occupant = clone; - self.selected = None; - } - } - } - } - - fn draw(&mut self, context: &mut Context) -> GameResult { - graphics::clear(context, [0.0, 0.0, 0.0, 1.0].into()); - graphics::draw( - context, - &self.background, - DrawParam::default().scale(Vector2 { x: 3.35, y: 3.35 }), - )?; - self.cosmonaut.draw(context)?; - - for row in self.grid.iter() { - for cell in row.iter() { - cell.draw(context, &mut self.spritebatch)?; - } - } - - graphics::draw(context, &self.spritebatch, DrawParam::default())?; - - self.spritebatch.clear(); - - graphics::present(context)?; - Ok(()) - } -} diff --git a/src/main.rs b/src/main.rs index c69d791..d0fb782 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ use bevy::math::Vec3; use bevy::prelude::*; -use gems::cell::{self, Cell, Occupant}; +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 Timer>) { +fn star_spawning(mut commands: Commands, time: Res<Time>, mut q: Query<&mut AnimationTimer>) { for mut timer in q.iter_mut() { if !timer.repeating() { timer.tick(time.delta()); @@ -17,16 +17,18 @@ fn star_spawning(mut commands: Commands, time: Res<Time>, mut q: Query<&mut Time sprite: TextureAtlasSprite::new(constants::TILESHEET_STAR1), transform: Transform { translation: Vec3::new( - thread_rng().gen_range(-300..300) as f32, - thread_rng().gen_range(-200..300) as f32, 0.0, + 0.0, + //thread_rng().gen_range(-300.0..300.0), + //thread_rng().gen_range(-200.0..300.0), + 1.15, ), - scale: Vec3::splat(thread_rng().gen_range(1..3) as f32), + //scale: Vec3::splat(thread_rng().gen_range(1.0..3.0)), ..Default::default() }, ..Default::default() }) - .insert(Timer::from_seconds(0.25, true)); + .insert(AnimationTimer::from_seconds(0.25, true)); } } } @@ -35,7 +37,6 @@ fn star_spawning(mut commands: Commands, time: Res<Time>, mut q: Query<&mut Time pub fn setup( mut commands: Commands, asset_server: Res<AssetServer>, - mut materials: ResMut<Assets<ColorMaterial>>, mut texture_atlases: ResMut<Assets<TextureAtlas>>, ) { let background = asset_server.load("background.png"); @@ -52,7 +53,7 @@ pub fn setup( commands.spawn_bundle(OrthographicCameraBundle::new_2d()); commands.spawn_bundle(SpriteBundle { - material: materials.add(background.into()), + texture: background, transform: Transform { translation: Vec3::new(50.0, 0.0, 0.0), scale: Vec3::splat(constants::TILE_SCALE), @@ -61,9 +62,9 @@ pub fn setup( ..Default::default() }); commands.spawn_bundle(SpriteBundle { - material: materials.add(title.into()), + texture: title, transform: Transform { - translation: Vec3::new(240.0, 200.0, 0.0), + translation: Vec3::new(240.0, 200.0, 0.1), scale: Vec3::splat(2.0), ..Default::default() }, @@ -73,13 +74,13 @@ pub fn setup( sprite: TextureAtlasSprite::new(constants::TILESHEET_COSMONAUT1), texture_atlas: atlas_handle.clone(), transform: Transform { - translation: Vec3::new(225.0, -200.0, 0.0), + translation: Vec3::new(225.0, -200.0, 0.1), scale: Vec3::splat(constants::TILE_SCALE), ..Default::default() }, ..Default::default() }); - commands.spawn_bundle((Timer::from_seconds(1.0, false),)); + commands.spawn_bundle((AnimationTimer::from_seconds(1.0, false),)); commands.spawn_bundle(SpriteSheetBundle { sprite: TextureAtlasSprite::new(constants::TILESHEET_COSMONAUT2), texture_atlas: atlas_handle.clone(), @@ -87,7 +88,7 @@ pub fn setup( translation: Vec3::new( 225.0, -200.0 + (-constants::TILE_SIZE) * constants::TILE_SCALE, - 0.0, + 0.1, ), scale: Vec3::splat(constants::TILE_SCALE), ..Default::default() @@ -100,15 +101,12 @@ pub fn setup( commands .spawn_bundle(SpriteSheetBundle { texture_atlas: atlas_handle.clone(), - visible: Visible { - is_visible: false, - is_transparent: true, - }, + visibility: Visibility { is_visible: false }, transform: Transform { 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, - 1.0, + 0.1, ), scale: Vec3::splat(constants::TILE_SCALE), ..Default::default() @@ -126,12 +124,12 @@ fn animation( mut q: Query<( Entity, Option<&mut Cell>, - &mut Timer, + &mut AnimationTimer, &mut TextureAtlasSprite, - &mut Visible, + &mut Visibility, )>, ) { - for (entity, cell, mut timer, mut sprite, mut visible) in q.iter_mut() { + for (entity, cell, mut timer, mut sprite, mut visibility) in q.iter_mut() { timer.tick(time.delta()); if timer.finished() { let index = match sprite.index { @@ -153,8 +151,8 @@ fn animation( if let Some(index) = index { sprite.index = index; } else if let Some(mut cell) = cell { - cell.set_occupant(Occupant::None, &mut sprite, &mut visible); - commands.entity(entity).remove::<Timer>(); + cell.set_occupant(Occupant::None, &mut sprite, &mut visibility); + commands.entity(entity).remove::<AnimationTimer>(); } else { commands.entity(entity).despawn(); } @@ -190,7 +188,7 @@ fn cosmonaut_detect( transform: *transform, ..Default::default() }) - .insert(Timer::from_seconds(0.1, true)); + .insert(AnimationTimer::from_seconds(0.1, true)); } } } @@ -242,16 +240,14 @@ fn mouse( for (cell, _, _) in q.iter_mut() { cells.push(*cell); } - if let Some(mut selected) = cells.clone().iter_mut().find(|c| c.selected) { - if let Some(mut hovered) = cells.iter_mut().find(|c| c.hovered) { + 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) { - let tmp = selected.occupant; - selected.occupant = hovered.occupant; - hovered.occupant = tmp; + std::mem::swap(&mut selected.occupant, &mut hovered.occupant); for (mut cell, _, mut sprite) in q.iter_mut() { if cell.x == selected.x && cell.y == selected.y { cell.occupant = selected.occupant; @@ -271,23 +267,8 @@ fn mouse( } } -pub struct GemsPlugin; -impl Plugin for GemsPlugin { - fn build(&self, app: &mut AppBuilder) { - app.add_startup_system(setup.system()); - app.add_system(cell::insert.system()); - app.add_system(cell::falling.system()); - app.add_system(cell::check.system()); - app.add_system(cell::start_explosion.system()); - app.add_system(mouse.system()); - app.add_system(cosmonaut_detect.system()); - app.add_system(animation.system()); - app.add_system(star_spawning.system()); - } -} - pub fn main() { - App::build() + App::new() .insert_resource(WindowDescriptor { title: "gems".to_string(), width: constants::WINDOW_WIDTH, @@ -296,6 +277,14 @@ pub fn main() { ..Default::default() }) .add_plugins(DefaultPlugins) - .add_plugin(GemsPlugin) + .add_startup_system(setup) + .add_system(cell::insert) + .add_system(cell::falling) + .add_system(cell::check) + .add_system(cell::start_explosion) + .add_system(mouse) + .add_system(cosmonaut_detect) + .add_system(animation) + .add_system(star_spawning) .run(); } |