Refactoring
This commit is contained in:
+20
-22
@@ -6,7 +6,6 @@ use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat};
|
|||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct HexTable<T: Clone> {
|
pub struct HexTable<T: Clone> {
|
||||||
default: T,
|
|
||||||
pub(crate) data: Vec<T>,
|
pub(crate) data: Vec<T>,
|
||||||
pub(crate) len: usize,
|
pub(crate) len: usize,
|
||||||
pub(crate) scale: f32,
|
pub(crate) scale: f32,
|
||||||
@@ -19,7 +18,6 @@ impl<T: Clone> HexTable<T> {
|
|||||||
let rows = len - 1;
|
let rows = len - 1;
|
||||||
let columns = (len as f32 / VERTICAL_OFFSET) as usize;
|
let columns = (len as f32 / VERTICAL_OFFSET) as usize;
|
||||||
Self {
|
Self {
|
||||||
default: default.clone(),
|
|
||||||
len,
|
len,
|
||||||
scale,
|
scale,
|
||||||
data: vec![default; rows * columns],
|
data: vec![default; rows * columns],
|
||||||
@@ -27,26 +25,26 @@ impl<T: Clone> HexTable<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn calculate(&self) -> Vec<(f32, f32)> {
|
// pub fn calculate(&self) -> Vec<(f32, f32)> {
|
||||||
let mut vec = Vec::with_capacity(self.len * self.len);
|
// let mut vec = Vec::with_capacity(self.len * self.len);
|
||||||
let mut index = 0;
|
// let mut index = 0;
|
||||||
loop {
|
// loop {
|
||||||
let x = index % self.len;
|
// let x = index % self.len;
|
||||||
let y = index / self.len;
|
// let y = index / self.len;
|
||||||
let y_point = y as f32 * VERTICAL_OFFSET;
|
// let y_point = y as f32 * VERTICAL_OFFSET;
|
||||||
|
//
|
||||||
if y_point as usize >= self.len {
|
// if y_point as usize >= self.len {
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
let mut x_point = x as f32;
|
// let mut x_point = x as f32;
|
||||||
if y % 2 == 1 {
|
// if y % 2 == 1 {
|
||||||
x_point += 0.5;
|
// x_point += 0.5;
|
||||||
}
|
// }
|
||||||
vec.push((x_point * self.scale, y_point * self.scale));
|
// vec.push((x_point * self.scale, y_point * self.scale));
|
||||||
index += 1;
|
// index += 1;
|
||||||
}
|
// }
|
||||||
vec
|
// vec
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub fn around(&self, x: usize, y: usize) -> Vec<&T> {
|
pub fn around(&self, x: usize, y: usize) -> Vec<&T> {
|
||||||
let x = x as isize;
|
let x = x as isize;
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
use std::time::Instant;
|
|
||||||
use crate::subplate_automation::{HexMatrixRequest, SubPlateAutomation, ViewRedrawRequest};
|
use crate::subplate_automation::{HexMatrixRequest, SubPlateAutomation, ViewRedrawRequest};
|
||||||
use crate::table::{IntoImage, Table};
|
use crate::table::{IntoImage, Table};
|
||||||
use crate::SeededRng;
|
use crate::SeededRng;
|
||||||
@@ -72,7 +71,7 @@ pub struct PlateAutomation {
|
|||||||
|
|
||||||
impl PlateAutomation {
|
impl PlateAutomation {
|
||||||
fn next(&mut self, rng: &mut ChaCha8Rng) {
|
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 range = 0..self.world.side * self.world.side;
|
||||||
let randoms = range.clone().map(|_| rng.random::<f32>()).collect::<Vec<f32>>();
|
let randoms = range.clone().map(|_| rng.random::<f32>()).collect::<Vec<f32>>();
|
||||||
let updated = range.into_par_iter().zip(randoms).map(|(index, random)| {
|
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 crate::table::{IntoImage, Table};
|
||||||
use bevy::asset::{Assets, RenderAssetUsages};
|
use crate::SeededRng;
|
||||||
use bevy::image::{BevyDefault, Image};
|
use bevy::asset::Assets;
|
||||||
|
use bevy::image::Image;
|
||||||
use bevy::input::ButtonInput;
|
use bevy::input::ButtonInput;
|
||||||
use bevy::prelude::{Commands, Component, Entity, KeyCode, Query, Res, ResMut, Sprite};
|
use bevy::prelude::{Commands, Component, Entity, KeyCode, Query, Res, ResMut, Sprite};
|
||||||
use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat};
|
|
||||||
use rand::RngExt;
|
use rand::RngExt;
|
||||||
use rand_chacha::ChaCha8Rng;
|
use rand_chacha::ChaCha8Rng;
|
||||||
use crate::plate_automation::PlateAutomation;
|
|
||||||
|
|
||||||
pub fn update_automation_view(
|
pub fn update_automation_view(
|
||||||
query: Query<(&Sprite, &SeedAutomation)>,
|
query: Query<(&Sprite, &SeedAutomation)>,
|
||||||
@@ -56,7 +55,6 @@ pub struct SeedAutomation {
|
|||||||
|
|
||||||
impl SeedAutomation {
|
impl SeedAutomation {
|
||||||
fn next(&mut self, rng: &mut ChaCha8Rng) {
|
fn next(&mut self, rng: &mut ChaCha8Rng) {
|
||||||
let len = (self.world.side * self.world.side) as f32;
|
|
||||||
loop {
|
loop {
|
||||||
let x = rng.random_range(16..48);
|
let x = rng.random_range(16..48);
|
||||||
let y = 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,
|
Commands, Component, Entity, KeyCode, Query, Res, ResMut, Single, Sprite,
|
||||||
With,
|
With,
|
||||||
};
|
};
|
||||||
use bevy::tasks::futures_lite::StreamExt;
|
|
||||||
use rand::RngExt;
|
use rand::RngExt;
|
||||||
use rand_chacha::ChaCha8Rng;
|
use rand_chacha::ChaCha8Rng;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use crate::plate_automation::PlateAutomation;
|
|
||||||
|
|
||||||
pub fn update_automation_view(
|
pub fn update_automation_view(
|
||||||
query: Single<(&Sprite, &SubPlateAutomation)>,
|
query: Single<(&Sprite, &SubPlateAutomation)>,
|
||||||
@@ -42,7 +40,7 @@ pub fn update_automation(
|
|||||||
let random = &mut seeded_rng.0;
|
let random = &mut seeded_rng.0;
|
||||||
|
|
||||||
automation.next(random);
|
automation.next(random);
|
||||||
commands.spawn((ViewRedrawRequest));
|
commands.spawn(ViewRedrawRequest);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,18 +51,18 @@ pub fn update_automation(
|
|||||||
automation.seed(random);
|
automation.seed(random);
|
||||||
}
|
}
|
||||||
|
|
||||||
commands.spawn((ViewRedrawRequest));
|
commands.spawn(ViewRedrawRequest);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if keys.just_pressed(KeyCode::AltRight) {
|
if keys.just_pressed(KeyCode::AltRight) {
|
||||||
automation.smooth();
|
automation.smooth();
|
||||||
commands.spawn((ViewRedrawRequest));
|
commands.spawn(ViewRedrawRequest);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if keys.just_pressed(KeyCode::KeyS) {
|
if keys.just_pressed(KeyCode::KeyS) {
|
||||||
automation.calculate_subplates(&mut commands);
|
automation.calculate_subplates();
|
||||||
}
|
}
|
||||||
|
|
||||||
if keys.just_pressed(KeyCode::KeyF) {
|
if keys.just_pressed(KeyCode::KeyF) {
|
||||||
@@ -153,7 +151,6 @@ impl SubPlateAutomation {
|
|||||||
let table = &mut hex_query.hex_matrix;
|
let table = &mut hex_query.hex_matrix;
|
||||||
let mut table_copy = table.clone();
|
let mut table_copy = table.clone();
|
||||||
|
|
||||||
let mut edge = (0_usize, 0_usize);
|
|
||||||
for x in 0..table.dimensions.0 {
|
for x in 0..table.dimensions.0 {
|
||||||
for y in 0..table.dimensions.1 {
|
for y in 0..table.dimensions.1 {
|
||||||
let color = *table.get_on_square_table(x, y, &automation.world);
|
let color = *table.get_on_square_table(x, y, &automation.world);
|
||||||
@@ -178,9 +175,9 @@ impl SubPlateAutomation {
|
|||||||
}
|
}
|
||||||
hex_query.hex_matrix = table_copy;
|
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();
|
let mut list: HashMap<u8, Vec<usize>> = HashMap::new();
|
||||||
self.world
|
self.world
|
||||||
.iter()
|
.iter()
|
||||||
@@ -202,8 +199,8 @@ impl SubPlateAutomation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn setup_hex_matrix(
|
pub fn setup_hex_matrix(
|
||||||
mut request_query: Query<(&HexMatrixRequest, Entity)>,
|
request_query: Query<(&HexMatrixRequest, Entity)>,
|
||||||
mut view_query: Query<&Sprite, With<HexMatrixView>>,
|
view_query: Query<&Sprite, With<HexMatrixView>>,
|
||||||
mut automation_query: Query<&SubPlateAutomation>,
|
mut automation_query: Query<&SubPlateAutomation>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut images: ResMut<Assets<Image>>,
|
mut images: ResMut<Assets<Image>>,
|
||||||
@@ -229,30 +226,26 @@ pub fn setup_hex_matrix(
|
|||||||
// images.remove(sprite.image.id());
|
// images.remove(sprite.image.id());
|
||||||
images.insert(sprite.image.id(), image).unwrap();
|
images.insert(sprite.image.id(), image).unwrap();
|
||||||
commands.spawn(
|
commands.spawn(
|
||||||
(HexMatrixBuild {
|
HexMatrixBuild {
|
||||||
hex_matrix: hex_table,
|
hex_matrix: hex_table,
|
||||||
}),
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_hex_matrix_view(
|
pub fn update_hex_matrix_view(
|
||||||
mut data_query: Single<&HexMatrixBuild>,
|
data_query: Single<&HexMatrixBuild>,
|
||||||
mut request_query: Query<Entity, With<HexMatrixRedrawRequest>>,
|
request_query: Single<Entity, With<HexMatrixRedrawRequest>>,
|
||||||
mut view_query: Query<&Sprite, With<HexMatrixView>>,
|
view_query: Single<&Sprite, With<HexMatrixView>>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut images: ResMut<Assets<Image>>,
|
mut images: ResMut<Assets<Image>>,
|
||||||
) {
|
) {
|
||||||
if request_query.is_empty() {
|
let req = request_query.into_inner();
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let req = request_query.iter_mut().next().unwrap();
|
|
||||||
commands.entity(req).despawn();
|
commands.entity(req).despawn();
|
||||||
println!("Update hex matrix view");
|
println!("Update hex matrix view");
|
||||||
let table = data_query.into_inner();
|
let table = data_query.into_inner();
|
||||||
let image = table.hex_matrix.get_image_data();
|
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.remove(sprite.image.id());
|
||||||
images.insert(sprite.image.id(), image).unwrap();
|
images.insert(sprite.image.id(), image).unwrap();
|
||||||
|
|||||||
+1
-2
@@ -1,8 +1,7 @@
|
|||||||
use std::ops::{Index, IndexMut};
|
|
||||||
use bevy::asset::RenderAssetUsages;
|
use bevy::asset::RenderAssetUsages;
|
||||||
use bevy::image::BevyDefault;
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat};
|
use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat};
|
||||||
|
use std::ops::{Index, IndexMut};
|
||||||
|
|
||||||
pub struct Table<T: Clone> {
|
pub struct Table<T: Clone> {
|
||||||
default: T,
|
default: T,
|
||||||
|
|||||||
Reference in New Issue
Block a user