From d2c59a4d07efa3ec4418d7e82352a1cdf38e1518 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Fri, 19 Mar 2021 19:21:11 +0100 Subject: branch trying out bevy --- src/main.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index a1fee19..30df8fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,8 @@ -use ggez::event::{self}; -use ggez::{ContextBuilder, GameResult}; +use bevy::prelude::App; -use gems::game::Game; +struct Gem; +struct Position { x: f32, y: f32} -pub fn main() -> GameResult { - let (ref mut context, ref mut event_loop) = ContextBuilder::new("gems", "jw&tb") - .add_resource_path("./resources") - .build()?; - let game = &mut Game::new(context)?; - event::run(context, event_loop, game) +pub fn main() { + App::build().run(); } -- cgit v1.2.3 From e131d553d2526edb7cffbe9f4ba48d9c6ae4e44f Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Sat, 20 Mar 2021 11:17:52 +0100 Subject: draw background --- src/main.rs | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 86 insertions(+), 4 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 30df8fb..c4e40f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,90 @@ -use bevy::prelude::App; +use bevy::math::Vec3; +use bevy::prelude::*; +use gems::constants; -struct Gem; -struct Position { x: f32, y: f32} +enum Occupant { + None, + Green, +} + +struct Cell { + x: u8, + y: u8, + occupant: Occupant, +} + +impl Cell { + pub fn new(x: u8, y: u8) -> Cell { + Cell { + x, + y, + occupant: Occupant::None, + } + } +} + +struct Position { + x: f32, + y: f32, +} + +pub fn add_cells(commands: &mut Commands) { + for i in 0..8 { + for j in 0..8 { + commands.spawn(( + Cell::new(i, j), + Position { + x: (i as f32 * constants::TILE_HEIGHT), + y: (j as f32 * constants::TILE_WIDTH), + }, + )); + } + } +} + +pub fn add_background( + commands: &mut Commands, + asset_server: Res, + mut materials: ResMut>, +) { + let texture_handle = asset_server.load("background.png"); + commands + .spawn(Camera2dBundle { + transform: Transform { + translation: Vec3{ + x: -40.0, + y: 0.0, + z: 0.0, + }, + scale: Vec3::splat(0.3), + ..Default::default() + }, + ..Default::default() + }) + .spawn(SpriteBundle { + material: materials.add(texture_handle.into()), + ..Default::default() + }); +} + +pub struct GemsPlugin; +impl Plugin for GemsPlugin { + fn build(&self, app: &mut AppBuilder) { + app.add_startup_system(add_background.system()); + app.add_startup_system(add_cells.system()); + } +} pub fn main() { - App::build().run(); + App::build() + .add_resource(WindowDescriptor { + title: "gems".to_string(), + width: 800.0, + height: 600.0, + resizable: false, + ..Default::default() + }) + .add_plugins(DefaultPlugins) + .add_plugin(GemsPlugin) + .run(); } -- cgit v1.2.3 From ec95776771d061c21af17686491181c78b7955c9 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Sat, 20 Mar 2021 12:35:38 +0100 Subject: draw grid --- src/main.rs | 89 +++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 60 insertions(+), 29 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index c4e40f9..acecaad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,55 +23,86 @@ impl Cell { } } -struct Position { - x: f32, - y: f32, -} - -pub fn add_cells(commands: &mut Commands) { - for i in 0..8 { - for j in 0..8 { - commands.spawn(( - Cell::new(i, j), - Position { - x: (i as f32 * constants::TILE_HEIGHT), - y: (j as f32 * constants::TILE_WIDTH), - }, - )); - } - } -} - -pub fn add_background( +pub fn setup( commands: &mut Commands, asset_server: Res, mut materials: ResMut>, + mut texture_atlases: ResMut>, ) { - let texture_handle = asset_server.load("background.png"); + let background = asset_server.load("background.png"); + let tileset = asset_server.load("tileset.png"); + + let atlas = TextureAtlas::from_grid(tileset, Vec2::new(16.0, 16.0), 4, 4); + let atlas_handle = texture_atlases.add(atlas); + commands - .spawn(Camera2dBundle { + .spawn(Camera2dBundle::default()) + .spawn(SpriteBundle { + material: materials.add(background.into()), transform: Transform { - translation: Vec3{ - x: -40.0, + translation: Vec3 { + x: 50.0, y: 0.0, z: 0.0, }, - scale: Vec3::splat(0.3), + scale: Vec3::splat(3.5), ..Default::default() }, ..Default::default() }) - .spawn(SpriteBundle { - material: materials.add(texture_handle.into()), + .spawn(SpriteSheetBundle { + sprite: TextureAtlasSprite::new(8), + texture_atlas: atlas_handle.clone(), + transform: Transform { + translation: Vec3 { + x: 225.0, + y: -200.0, + z: 0.0, + }, + scale: Vec3::splat(3.5), + ..Default::default() + }, + ..Default::default() + }) + .spawn(SpriteSheetBundle { + sprite: TextureAtlasSprite::new(12), + texture_atlas: atlas_handle.clone(), + transform: Transform { + translation: Vec3 { + x: 225.0, + y: -200.0 + (-16.0) * 3.5, + z: 0.0, + }, + scale: Vec3::splat(3.5), + ..Default::default() + }, ..Default::default() }); + + for i in 0..8 { + for j in 0..8 { + commands.spawn(SpriteSheetBundle { + sprite: TextureAtlasSprite::new(1), + texture_atlas: atlas_handle.clone(), + transform: Transform { + translation: Vec3 { + x: ((i as f32) * 16.0 * 3.5) - 320.0, + y: ((j as f32) * 16.0 * 3.5) - 170.0, + z: 0.0, + }, + scale: Vec3::splat(3.5), + ..Default::default() + }, + ..Default::default() + }); + } + } } pub struct GemsPlugin; impl Plugin for GemsPlugin { fn build(&self, app: &mut AppBuilder) { - app.add_startup_system(add_background.system()); - app.add_startup_system(add_cells.system()); + app.add_startup_system(setup.system()); } } -- cgit v1.2.3 From baad8b54bdff624b648af7bcf977f14711ff59bc Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Sat, 20 Mar 2021 14:00:10 +0100 Subject: populate top row with new gems --- src/main.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 59 insertions(+), 13 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index acecaad..f3d41b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,44 @@ use bevy::math::Vec3; use bevy::prelude::*; -use gems::constants; +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; +#[derive(Debug, PartialEq)] enum Occupant { None, Green, + Yellow, + Red, + Diamond, } +impl Occupant { + pub fn to_index(&self) -> u32 { + match self { + Occupant::Green => 0, + Occupant::Yellow => 1, + Occupant::Red => 2, + Occupant::Diamond => 3, + Occupant::None => 13, + } + } +} + +impl Distribution for Standard { + fn sample(&self, rng: &mut R) -> Occupant { + match rng.gen_range(0..=3) { + 0 => Occupant::Green, + 1 => Occupant::Yellow, + 2 => Occupant::Diamond, + 3 => Occupant::Red, + _ => Occupant::None, + } + } +} + +#[derive(Debug)] struct Cell { x: u8, y: u8, @@ -23,6 +55,17 @@ impl Cell { } } +fn cell_falling_system(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite)>) { + for (mut cell, mut sprite) in cell_query.iter_mut() { + if cell.occupant == Occupant::None { + if cell.y == 7 { + cell.occupant = rand::random(); + sprite.index = cell.occupant.to_index(); + } + } + } +} + pub fn setup( commands: &mut Commands, asset_server: Res, @@ -81,20 +124,22 @@ pub fn setup( for i in 0..8 { for j in 0..8 { - commands.spawn(SpriteSheetBundle { - sprite: TextureAtlasSprite::new(1), - texture_atlas: atlas_handle.clone(), - transform: Transform { - translation: Vec3 { - x: ((i as f32) * 16.0 * 3.5) - 320.0, - y: ((j as f32) * 16.0 * 3.5) - 170.0, - z: 0.0, + commands + .spawn(SpriteSheetBundle { + texture_atlas: atlas_handle.clone(), + sprite: TextureAtlasSprite::new(11), + transform: Transform { + translation: Vec3 { + x: ((i as f32) * 16.0 * 3.5) - 320.0, + y: ((j as f32) * 16.0 * 3.5) - 160.0, + z: 0.0, + }, + scale: Vec3::splat(3.5), + ..Default::default() }, - scale: Vec3::splat(3.5), ..Default::default() - }, - ..Default::default() - }); + }) + .with(Cell::new(i, j)); } } } @@ -103,6 +148,7 @@ pub struct GemsPlugin; impl Plugin for GemsPlugin { fn build(&self, app: &mut AppBuilder) { app.add_startup_system(setup.system()); + app.add_system(cell_falling_system.system()); } } -- cgit v1.2.3 From 4fe231fcc252f19e3fa5e50d369571f1caf6ad33 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Sat, 20 Mar 2021 15:35:18 +0100 Subject: gems now fall --- src/main.rs | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index f3d41b0..56f2ea9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use rand::{ Rng, }; -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] enum Occupant { None, Green, @@ -38,7 +38,7 @@ impl Distribution for Standard { } } -#[derive(Debug)] +#[derive(Debug, Clone)] struct Cell { x: u8, y: u8, @@ -55,7 +55,7 @@ impl Cell { } } -fn cell_falling_system(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite)>) { +fn cell_insert_system(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite)>) { for (mut cell, mut sprite) in cell_query.iter_mut() { if cell.occupant == Occupant::None { if cell.y == 7 { @@ -66,6 +66,36 @@ fn cell_falling_system(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite } } +fn cell_falling_system(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite)>) { + let mut have_gems = Vec::new(); + for (cell, _sprite) in cell_query.iter_mut() { + if cell.occupant != Occupant::None { + have_gems.push(cell.clone()); + } + } + + let mut moved_gems = Vec::new(); + for (mut cell, mut sprite) in cell_query.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.occupant = c.occupant.clone(); + sprite.index = cell.occupant.to_index(); + moved_gems.push(c.clone()); + } + } + } + + for (mut cell, mut sprite) in cell_query.iter_mut() { + if moved_gems.iter().any(|c| (c.x, c.y) == (cell.x, cell.y)) { + cell.occupant = Occupant::None; + sprite.index = cell.occupant.to_index(); + } + } +} + pub fn setup( commands: &mut Commands, asset_server: Res, @@ -148,6 +178,7 @@ 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.system()); app.add_system(cell_falling_system.system()); } } -- cgit v1.2.3 From c657649cdc1a4159f2c76852b32fc563d1279509 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Sat, 20 Mar 2021 16:11:32 +0100 Subject: added check system --- src/main.rs | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 92 insertions(+), 14 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 56f2ea9..c81203f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,25 @@ use bevy::math::Vec3; use bevy::prelude::*; +use gems::constants; use rand::{ distributions::{Distribution, Standard}, Rng, }; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Copy)] enum Occupant { None, Green, Yellow, Red, Diamond, + Explosion, +} + +impl Default for Occupant { + fn default() -> Occupant { + Occupant::None + } } impl Occupant { @@ -21,6 +29,7 @@ impl Occupant { Occupant::Yellow => 1, Occupant::Red => 2, Occupant::Diamond => 3, + Occupant::Explosion => 4, Occupant::None => 13, } } @@ -38,15 +47,15 @@ impl Distribution for Standard { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default, Copy)] struct Cell { - x: u8, - y: u8, + x: usize, + y: usize, occupant: Occupant, } impl Cell { - pub fn new(x: u8, y: u8) -> Cell { + pub fn new(x: usize, y: usize) -> Cell { Cell { x, y, @@ -57,10 +66,78 @@ impl Cell { fn cell_insert_system(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite)>) { for (mut cell, mut sprite) in cell_query.iter_mut() { - if cell.occupant == Occupant::None { - if cell.y == 7 { - cell.occupant = rand::random(); - sprite.index = cell.occupant.to_index(); + if cell.occupant == Occupant::None && cell.y == constants::ROWS - 1 { + cell.occupant = rand::random(); + sprite.index = cell.occupant.to_index(); + } + } +} + +fn cell_check_system(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite)>) { + let mut cells = [[Cell::default(); constants::COLUMNS]; constants::ROWS]; + for (cell, _) in cell_query.iter_mut() { + cells[cell.x][cell.y] = *cell; + } + + let mut last = Occupant::None; + let mut connected = Vec::new(); + + for (i, _) in cells.iter().enumerate() { + let mut c = Vec::new(); + for j in 0..constants::ROWS { + if cells[i][j].occupant == last && last != Occupant::None { + c.push((i, j)); + c.push((i, j - 1)); + } else { + connected.push(c.clone()); + c.clear(); + } + last = cells[i][j].occupant; + } + connected.push(c); + last = Occupant::None; + } + + for c in connected.iter() { + if c.len() > 3 { + for (i, j) in c.iter() { + for (mut cell, mut sprite) in cell_query.iter_mut() { + if &cell.x == i && &cell.y == j { + cell.occupant = Occupant::Explosion; + sprite.index = cell.occupant.to_index(); + } + } + } + } + } + + connected.clear(); + + for (i, row) in cells.iter().enumerate() { + let mut c = Vec::new(); + for (j, _) in row.iter().enumerate() { + if cells[j][i].occupant == last && last != Occupant::None { + c.push((j, i)); + c.push((j - 1, i)); + } else { + connected.push(c.clone()); + c.clear(); + } + last = cells[j][i].occupant; + } + connected.push(c); + last = Occupant::None; + } + + for c in connected.iter() { + if c.len() > 3 { + for (i, j) in c.iter() { + for (mut cell, mut sprite) in cell_query.iter_mut() { + if &cell.x == i && &cell.y == j { + cell.occupant = Occupant::Explosion; + sprite.index = cell.occupant.to_index(); + } + } } } } @@ -70,7 +147,7 @@ fn cell_falling_system(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite let mut have_gems = Vec::new(); for (cell, _sprite) in cell_query.iter_mut() { if cell.occupant != Occupant::None { - have_gems.push(cell.clone()); + have_gems.push(*cell); } } @@ -81,9 +158,9 @@ fn cell_falling_system(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite .iter() .find(|&c| (c.x, c.y) == (cell.x, cell.y + 1)) { - cell.occupant = c.occupant.clone(); + cell.occupant = c.occupant; sprite.index = cell.occupant.to_index(); - moved_gems.push(c.clone()); + moved_gems.push(c); } } } @@ -152,8 +229,8 @@ pub fn setup( ..Default::default() }); - for i in 0..8 { - for j in 0..8 { + for i in 0..constants::COLUMNS { + for j in 0..constants::ROWS { commands .spawn(SpriteSheetBundle { texture_atlas: atlas_handle.clone(), @@ -180,6 +257,7 @@ impl Plugin for GemsPlugin { app.add_startup_system(setup.system()); app.add_system(cell_insert_system.system()); app.add_system(cell_falling_system.system()); + app.add_system(cell_check_system.system()); } } -- cgit v1.2.3 From c24bf56c3d69b826078307ddebb129a7be99ad97 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Sat, 20 Mar 2021 17:22:49 +0100 Subject: explosion animation system --- src/main.rs | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index c81203f..4df4fa7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,6 +73,36 @@ fn cell_insert_system(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite) } } +fn explosion_animation_system( + time: Res