From cb90b53145b311a8f5e410a0936aeabc74156131 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Mon, 29 Jun 2026 13:13:17 +0300 Subject: [PATCH] Refactoring --- src/hex_table.rs | 42 ++++++++++++++++++-------------------- src/plate_automation.rs | 3 +-- src/seed_automation.rs | 10 ++++----- src/subplate_automation.rs | 37 ++++++++++++++------------------- src/table.rs | 3 +-- 5 files changed, 41 insertions(+), 54 deletions(-) diff --git a/src/hex_table.rs b/src/hex_table.rs index c368d30..621a0a2 100644 --- a/src/hex_table.rs +++ b/src/hex_table.rs @@ -6,7 +6,6 @@ use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat}; #[derive(Clone)] pub struct HexTable { - default: T, pub(crate) data: Vec, pub(crate) len: usize, pub(crate) scale: f32, @@ -19,7 +18,6 @@ impl HexTable { let rows = len - 1; let columns = (len as f32 / VERTICAL_OFFSET) as usize; Self { - default: default.clone(), len, scale, data: vec![default; rows * columns], @@ -27,26 +25,26 @@ impl HexTable { } } - pub fn calculate(&self) -> Vec<(f32, f32)> { - let mut vec = Vec::with_capacity(self.len * self.len); - let mut index = 0; - loop { - let x = index % self.len; - let y = index / self.len; - let y_point = y as f32 * VERTICAL_OFFSET; - - if y_point as usize >= self.len { - break; - } - let mut x_point = x as f32; - if y % 2 == 1 { - x_point += 0.5; - } - vec.push((x_point * self.scale, y_point * self.scale)); - index += 1; - } - vec - } + // pub fn calculate(&self) -> Vec<(f32, f32)> { + // let mut vec = Vec::with_capacity(self.len * self.len); + // let mut index = 0; + // loop { + // let x = index % self.len; + // let y = index / self.len; + // let y_point = y as f32 * VERTICAL_OFFSET; + // + // if y_point as usize >= self.len { + // break; + // } + // let mut x_point = x as f32; + // if y % 2 == 1 { + // x_point += 0.5; + // } + // vec.push((x_point * self.scale, y_point * self.scale)); + // index += 1; + // } + // vec + // } pub fn around(&self, x: usize, y: usize) -> Vec<&T> { let x = x as isize; diff --git a/src/plate_automation.rs b/src/plate_automation.rs index 5364eeb..176db6d 100644 --- a/src/plate_automation.rs +++ b/src/plate_automation.rs @@ -1,4 +1,3 @@ -use std::time::Instant; use crate::subplate_automation::{HexMatrixRequest, SubPlateAutomation, ViewRedrawRequest}; use crate::table::{IntoImage, Table}; use crate::SeededRng; @@ -72,7 +71,7 @@ pub struct PlateAutomation { impl PlateAutomation { fn next(&mut self, rng: &mut ChaCha8Rng) { - let time = Instant::now(); + // let time = Instant::now(); let range = 0..self.world.side * self.world.side; let randoms = range.clone().map(|_| rng.random::()).collect::>(); let updated = range.into_par_iter().zip(randoms).map(|(index, random)| { diff --git a/src/seed_automation.rs b/src/seed_automation.rs index a09e348..d880010 100644 --- a/src/seed_automation.rs +++ b/src/seed_automation.rs @@ -1,13 +1,12 @@ -use crate::SeededRng; +use crate::plate_automation::PlateAutomation; use crate::table::{IntoImage, Table}; -use bevy::asset::{Assets, RenderAssetUsages}; -use bevy::image::{BevyDefault, Image}; +use crate::SeededRng; +use bevy::asset::Assets; +use bevy::image::Image; use bevy::input::ButtonInput; use bevy::prelude::{Commands, Component, Entity, KeyCode, Query, Res, ResMut, Sprite}; -use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat}; use rand::RngExt; use rand_chacha::ChaCha8Rng; -use crate::plate_automation::PlateAutomation; pub fn update_automation_view( query: Query<(&Sprite, &SeedAutomation)>, @@ -56,7 +55,6 @@ pub struct SeedAutomation { impl SeedAutomation { fn next(&mut self, rng: &mut ChaCha8Rng) { - let len = (self.world.side * self.world.side) as f32; loop { let x = rng.random_range(16..48); let y = rng.random_range(16..48); diff --git a/src/subplate_automation.rs b/src/subplate_automation.rs index e7c6e97..37b946d 100644 --- a/src/subplate_automation.rs +++ b/src/subplate_automation.rs @@ -8,12 +8,10 @@ use bevy::prelude::{ Commands, Component, Entity, KeyCode, Query, Res, ResMut, Single, Sprite, With, }; -use bevy::tasks::futures_lite::StreamExt; use rand::RngExt; use rand_chacha::ChaCha8Rng; use std::collections::HashMap; use std::time::Instant; -use crate::plate_automation::PlateAutomation; pub fn update_automation_view( query: Single<(&Sprite, &SubPlateAutomation)>, @@ -42,7 +40,7 @@ pub fn update_automation( let random = &mut seeded_rng.0; automation.next(random); - commands.spawn((ViewRedrawRequest)); + commands.spawn(ViewRedrawRequest); } @@ -53,18 +51,18 @@ pub fn update_automation( automation.seed(random); } - commands.spawn((ViewRedrawRequest)); + commands.spawn(ViewRedrawRequest); } if keys.just_pressed(KeyCode::AltRight) { automation.smooth(); - commands.spawn((ViewRedrawRequest)); + commands.spawn(ViewRedrawRequest); } if keys.just_pressed(KeyCode::KeyS) { - automation.calculate_subplates(&mut commands); + automation.calculate_subplates(); } if keys.just_pressed(KeyCode::KeyF) { @@ -153,7 +151,6 @@ impl SubPlateAutomation { let table = &mut hex_query.hex_matrix; let mut table_copy = table.clone(); - let mut edge = (0_usize, 0_usize); for x in 0..table.dimensions.0 { for y in 0..table.dimensions.1 { let color = *table.get_on_square_table(x, y, &automation.world); @@ -178,9 +175,9 @@ impl SubPlateAutomation { } hex_query.hex_matrix = table_copy; - commands.spawn((HexMatrixRedrawRequest)); + commands.spawn(HexMatrixRedrawRequest); } - fn calculate_subplates(&self, commands: &mut Commands) { + fn calculate_subplates(&self) { let mut list: HashMap> = HashMap::new(); self.world .iter() @@ -202,8 +199,8 @@ impl SubPlateAutomation { } pub fn setup_hex_matrix( - mut request_query: Query<(&HexMatrixRequest, Entity)>, - mut view_query: Query<&Sprite, With>, + request_query: Query<(&HexMatrixRequest, Entity)>, + view_query: Query<&Sprite, With>, mut automation_query: Query<&SubPlateAutomation>, mut commands: Commands, mut images: ResMut>, @@ -229,30 +226,26 @@ pub fn setup_hex_matrix( // images.remove(sprite.image.id()); images.insert(sprite.image.id(), image).unwrap(); commands.spawn( - (HexMatrixBuild { + HexMatrixBuild { hex_matrix: hex_table, - }), + }, ); } pub fn update_hex_matrix_view( - mut data_query: Single<&HexMatrixBuild>, - mut request_query: Query>, - mut view_query: Query<&Sprite, With>, + data_query: Single<&HexMatrixBuild>, + request_query: Single>, + view_query: Single<&Sprite, With>, mut commands: Commands, mut images: ResMut>, ) { - if request_query.is_empty() { - return; - } - - let req = request_query.iter_mut().next().unwrap(); + let req = request_query.into_inner(); commands.entity(req).despawn(); println!("Update hex matrix view"); let table = data_query.into_inner(); let image = table.hex_matrix.get_image_data(); - let sprite = view_query.iter().next().unwrap(); + let sprite = view_query.into_inner(); // images.remove(sprite.image.id()); images.insert(sprite.image.id(), image).unwrap(); diff --git a/src/table.rs b/src/table.rs index 4e8f16b..4731208 100644 --- a/src/table.rs +++ b/src/table.rs @@ -1,8 +1,7 @@ -use std::ops::{Index, IndexMut}; use bevy::asset::RenderAssetUsages; -use bevy::image::BevyDefault; use bevy::prelude::*; use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat}; +use std::ops::{Index, IndexMut}; pub struct Table { default: T,