Refactoring
This commit is contained in:
+20
-22
@@ -6,7 +6,6 @@ use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct HexTable<T: Clone> {
|
||||
default: T,
|
||||
pub(crate) data: Vec<T>,
|
||||
pub(crate) len: usize,
|
||||
pub(crate) scale: f32,
|
||||
@@ -19,7 +18,6 @@ impl<T: Clone> HexTable<T> {
|
||||
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<T: Clone> HexTable<T> {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
@@ -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::<f32>()).collect::<Vec<f32>>();
|
||||
let updated = range.into_par_iter().zip(randoms).map(|(index, random)| {
|
||||
|
||||
@@ -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);
|
||||
|
||||
+15
-22
@@ -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<u8, Vec<usize>> = 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<HexMatrixView>>,
|
||||
request_query: Query<(&HexMatrixRequest, Entity)>,
|
||||
view_query: Query<&Sprite, With<HexMatrixView>>,
|
||||
mut automation_query: Query<&SubPlateAutomation>,
|
||||
mut commands: Commands,
|
||||
mut images: ResMut<Assets<Image>>,
|
||||
@@ -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<Entity, With<HexMatrixRedrawRequest>>,
|
||||
mut view_query: Query<&Sprite, With<HexMatrixView>>,
|
||||
data_query: Single<&HexMatrixBuild>,
|
||||
request_query: Single<Entity, With<HexMatrixRedrawRequest>>,
|
||||
view_query: Single<&Sprite, With<HexMatrixView>>,
|
||||
mut commands: Commands,
|
||||
mut images: ResMut<Assets<Image>>,
|
||||
) {
|
||||
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();
|
||||
|
||||
+1
-2
@@ -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<T: Clone> {
|
||||
default: T,
|
||||
|
||||
Reference in New Issue
Block a user