From 93408306eaf59241dc95d82e9d94a2e0b1daa07f Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Mon, 29 Jun 2026 13:07:49 +0300 Subject: [PATCH] Collision base --- src/collision_world.rs | 29 +++++++++++++++++++++++++++++ src/hex_table.rs | 3 +-- src/main.rs | 31 ++++++++++++++----------------- src/plate_automation.rs | 30 +++++++++++++++--------------- src/seed_automation.rs | 2 +- src/subplate_automation.rs | 8 ++++---- 6 files changed, 64 insertions(+), 39 deletions(-) create mode 100644 src/collision_world.rs diff --git a/src/collision_world.rs b/src/collision_world.rs new file mode 100644 index 0000000..4b7ab39 --- /dev/null +++ b/src/collision_world.rs @@ -0,0 +1,29 @@ +use bevy::prelude::*; + +#[derive(Clone)] +pub struct CollisionWorld { + bodies: Vec> +} + +#[derive(Clone)] +pub struct CollisionBody { + value: T, + offset: Vec2, + border: Vec, + forces: Vec, + resistance: f32, +} + +impl CollisionWorld { + pub fn new(parts: Vec<(Vec, T)>) -> CollisionWorld { + Self{ + bodies: parts.iter().map(|(border, id)| {CollisionBody{ + value: id.clone(), + offset: Vec2::ZERO, + forces: vec![Vec2::ZERO; border.len()], + border: border.clone(), + resistance: 1.0, + }}).collect(), + } + } +} \ No newline at end of file diff --git a/src/hex_table.rs b/src/hex_table.rs index bac6eeb..c368d30 100644 --- a/src/hex_table.rs +++ b/src/hex_table.rs @@ -1,9 +1,8 @@ use crate::table::{IntoImage, Table}; use bevy::asset::RenderAssetUsages; -use bevy::image::{BevyDefault, Image}; +use bevy::image::Image; use bevy::prelude::Color; use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat}; -use std::slice::Iter; #[derive(Clone)] pub struct HexTable { diff --git a/src/main.rs b/src/main.rs index b347b41..c6243a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,20 @@ +extern crate bevy; mod hex_table; mod plate_automation; mod seed_automation; mod subplate_automation; mod table; +mod collision_world; use crate::subplate_automation::HexMatrixView; -use crate::table::{IntoImage, Table}; -use bevy::asset::io::embedded::GetAssetServer; +use crate::table::{Table}; use bevy::image::TextureFormatPixelInfo; -use bevy::input::keyboard::keyboard_input_system; use bevy::prelude::*; -use bevy::window::{ExitCondition, WindowResolution}; -use rand::{Rng, RngExt}; -use rand_chacha::ChaCha8Rng; +use bevy::window::WindowResolution; use rand_chacha::rand_core::SeedableRng; +use rand_chacha::ChaCha8Rng; use seed_automation::SeedAutomation; -extern crate bevy; const RECTANGLE_SIDE: f32 = 1500.0; const WINDOW_SIDE: u32 = 1700; @@ -83,7 +81,7 @@ fn setup(mut commands: Commands, mut images: ResMut>) { )); } -fn moving_system(mut hex: Query<&mut Transform, With>, keys: Res>) { +fn moving_system(hex: Query<&mut Transform, With>, keys: Res>) { for mut transform in hex { if keys.pressed(KeyCode::ArrowDown) { transform.translation.y += 3.0; @@ -126,59 +124,58 @@ pub struct Moving; mod tests { use crate::hex_table::HexTable; use crate::table::Table; - use bevy::render::render_resource::encase::private::RuntimeSizedArray; #[test] fn around_1() { - let mut table = Table::new(false, 16); + let table = Table::new(false, 16); let around = table.around(0, 0); assert_eq!(around.len(), 2); } #[test] fn around_2() { - let mut table = Table::new(false, 16); + let table = Table::new(false, 16); let around = table.around(15, 15); assert_eq!(around.len(), 2); } #[test] fn around_3() { - let mut table = Table::new(false, 16); + let table = Table::new(false, 16); let around = table.around(5, 5); assert_eq!(around.len(), 4); } #[test] fn around_1_line() { - let mut table = Table::new(false, 16); + let table = Table::new(false, 16); let around = table.around_line(0); assert_eq!(around.len(), 2); } #[test] fn around_2_line() { - let mut table = Table::new(false, 16); + let table = Table::new(false, 16); let around = table.around_line(16 * 16 - 1); assert_eq!(around.len(), 2); } #[test] fn around_3_line() { - let mut table = Table::new(false, 16); + let table = Table::new(false, 16); let around = table.around_line(24); assert_eq!(around.len(), 4); } #[test] fn hex_table_1() { - let mut table = HexTable::new(10, false, 4.0); + let table = HexTable::new(10, false, 4.0); let coord = table.get_offset_of(9, 0); assert_eq!(coord, (36.0, 0.0)); } #[test] fn hex_table_around_1() { - let mut table = HexTable::new(10, false, 4.0); + let table = HexTable::new(10, false, 4.0); let around = table.around(9, 0); assert_eq!(around.len(), 3); } diff --git a/src/plate_automation.rs b/src/plate_automation.rs index e72dc53..5364eeb 100644 --- a/src/plate_automation.rs +++ b/src/plate_automation.rs @@ -1,18 +1,16 @@ -use rayon::iter::IndexedParallelIterator; use std::time::Instant; -use crate::SeededRng; +use crate::subplate_automation::{HexMatrixRequest, SubPlateAutomation, ViewRedrawRequest}; 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::{Color, Commands, Component, Entity, KeyCode, Query, Res, ResMut, Single, Sprite}; -use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat}; +use bevy::prelude::{Commands, Component, Entity, KeyCode, Query, Res, ResMut, Single, Sprite}; use rand::RngExt; use rand_chacha::ChaCha8Rng; -use rayon::prelude::IntoParallelIterator; -use crate::seed_automation::SeedAutomation; -use crate::subplate_automation::{HexMatrixRequest, SubPlateAutomation, ViewRedrawRequest}; +use rayon::iter::IndexedParallelIterator; use rayon::iter::ParallelIterator; +use rayon::prelude::IntoParallelIterator; pub fn update_automation_view( query: Single<(&Sprite, &PlateAutomation)>, @@ -24,7 +22,7 @@ pub fn update_automation_view( let (sprite, automation) = query.into_inner(); let image = automation.world.get_image_data(); - images.remove(sprite.image.id()); + // images.remove(sprite.image.id()); images.insert(sprite.image.id(), image).unwrap(); commands.entity(request.into_inner().1).despawn(); @@ -45,13 +43,13 @@ pub fn update_automation( let random = &mut seeded_rng.0; automation.next(random); - commands.spawn((ViewRedrawRequest)); + commands.spawn(ViewRedrawRequest); } if keys.just_pressed(KeyCode::AltLeft) { automation.world.grow(); - commands.spawn((ViewRedrawRequest)); + commands.spawn(ViewRedrawRequest); println!("Map size {}", automation.world.side); } @@ -63,7 +61,7 @@ pub fn update_automation( commands.entity(entity).remove::().insert(SubPlateAutomation{ world: new_table }); - commands.spawn((HexMatrixRequest)); + commands.spawn(HexMatrixRequest); } } @@ -76,7 +74,7 @@ impl PlateAutomation { fn next(&mut self, rng: &mut ChaCha8Rng) { let time = Instant::now(); let range = 0..self.world.side * self.world.side; - let randoms = range.clone().map(|x| rng.random::()).collect::>(); + let randoms = range.clone().map(|_| rng.random::()).collect::>(); let updated = range.into_par_iter().zip(randoms).map(|(index, random)| { if *self.world.get(index) { return true; @@ -88,7 +86,7 @@ impl PlateAutomation { return false; } - let chance = (around as f32 * 0.25); + let chance = (around as f32 * 0.25).powi(2); if random > chance { return false; } @@ -98,5 +96,7 @@ impl PlateAutomation { self.world.data = updated; + // println!("Done in {} mcs", time.elapsed().as_micros()); + } } diff --git a/src/seed_automation.rs b/src/seed_automation.rs index 80b08f5..a09e348 100644 --- a/src/seed_automation.rs +++ b/src/seed_automation.rs @@ -21,7 +21,7 @@ pub fn update_automation_view( let image = automation.world.get_image_data(); - images.remove(sprite.image.id()); + // images.remove(sprite.image.id()); images.insert(sprite.image.id(), image).unwrap(); } diff --git a/src/subplate_automation.rs b/src/subplate_automation.rs index 3e139d8..e7c6e97 100644 --- a/src/subplate_automation.rs +++ b/src/subplate_automation.rs @@ -24,7 +24,7 @@ pub fn update_automation_view( let (sprite, automation) = query.into_inner(); let image = automation.world.get_image_data(); - images.remove(sprite.image.id()); + // images.remove(sprite.image.id()); images.insert(sprite.image.id(), image).unwrap(); commands.entity(request.into_inner().1).despawn(); @@ -60,7 +60,7 @@ pub fn update_automation( if keys.just_pressed(KeyCode::AltRight) { automation.smooth(); commands.spawn((ViewRedrawRequest)); - + } if keys.just_pressed(KeyCode::KeyS) { @@ -226,7 +226,7 @@ pub fn setup_hex_matrix( let sprite = view_query.iter().next().unwrap(); - images.remove(sprite.image.id()); + // images.remove(sprite.image.id()); images.insert(sprite.image.id(), image).unwrap(); commands.spawn( (HexMatrixBuild { @@ -254,7 +254,7 @@ pub fn update_hex_matrix_view( let sprite = view_query.iter().next().unwrap(); - images.remove(sprite.image.id()); + // images.remove(sprite.image.id()); images.insert(sprite.image.id(), image).unwrap(); } #[derive(Component)]