Collision base

This commit is contained in:
2026-06-29 13:07:49 +03:00
parent 8d2ed11e73
commit 93408306ea
6 changed files with 64 additions and 39 deletions
+29
View File
@@ -0,0 +1,29 @@
use bevy::prelude::*;
#[derive(Clone)]
pub struct CollisionWorld<T: Clone> {
bodies: Vec<CollisionBody<T>>
}
#[derive(Clone)]
pub struct CollisionBody<T: Clone> {
value: T,
offset: Vec2,
border: Vec<Vec2>,
forces: Vec<Vec2>,
resistance: f32,
}
impl<T: Clone> CollisionWorld<T> {
pub fn new(parts: Vec<(Vec<Vec2>, T)>) -> CollisionWorld<T> {
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(),
}
}
}
+1 -2
View File
@@ -1,9 +1,8 @@
use crate::table::{IntoImage, Table}; use crate::table::{IntoImage, Table};
use bevy::asset::RenderAssetUsages; use bevy::asset::RenderAssetUsages;
use bevy::image::{BevyDefault, Image}; use bevy::image::Image;
use bevy::prelude::Color; use bevy::prelude::Color;
use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat}; use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat};
use std::slice::Iter;
#[derive(Clone)] #[derive(Clone)]
pub struct HexTable<T: Clone> { pub struct HexTable<T: Clone> {
+14 -17
View File
@@ -1,22 +1,20 @@
extern crate bevy;
mod hex_table; mod hex_table;
mod plate_automation; mod plate_automation;
mod seed_automation; mod seed_automation;
mod subplate_automation; mod subplate_automation;
mod table; mod table;
mod collision_world;
use crate::subplate_automation::HexMatrixView; use crate::subplate_automation::HexMatrixView;
use crate::table::{IntoImage, Table}; use crate::table::{Table};
use bevy::asset::io::embedded::GetAssetServer;
use bevy::image::TextureFormatPixelInfo; use bevy::image::TextureFormatPixelInfo;
use bevy::input::keyboard::keyboard_input_system;
use bevy::prelude::*; use bevy::prelude::*;
use bevy::window::{ExitCondition, WindowResolution}; use bevy::window::WindowResolution;
use rand::{Rng, RngExt};
use rand_chacha::ChaCha8Rng;
use rand_chacha::rand_core::SeedableRng; use rand_chacha::rand_core::SeedableRng;
use rand_chacha::ChaCha8Rng;
use seed_automation::SeedAutomation; use seed_automation::SeedAutomation;
extern crate bevy;
const RECTANGLE_SIDE: f32 = 1500.0; const RECTANGLE_SIDE: f32 = 1500.0;
const WINDOW_SIDE: u32 = 1700; const WINDOW_SIDE: u32 = 1700;
@@ -83,7 +81,7 @@ fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
)); ));
} }
fn moving_system(mut hex: Query<&mut Transform, With<Moving>>, keys: Res<ButtonInput<KeyCode>>) { fn moving_system(hex: Query<&mut Transform, With<Moving>>, keys: Res<ButtonInput<KeyCode>>) {
for mut transform in hex { for mut transform in hex {
if keys.pressed(KeyCode::ArrowDown) { if keys.pressed(KeyCode::ArrowDown) {
transform.translation.y += 3.0; transform.translation.y += 3.0;
@@ -126,59 +124,58 @@ pub struct Moving;
mod tests { mod tests {
use crate::hex_table::HexTable; use crate::hex_table::HexTable;
use crate::table::Table; use crate::table::Table;
use bevy::render::render_resource::encase::private::RuntimeSizedArray;
#[test] #[test]
fn around_1() { fn around_1() {
let mut table = Table::new(false, 16); let table = Table::new(false, 16);
let around = table.around(0, 0); let around = table.around(0, 0);
assert_eq!(around.len(), 2); assert_eq!(around.len(), 2);
} }
#[test] #[test]
fn around_2() { fn around_2() {
let mut table = Table::new(false, 16); let table = Table::new(false, 16);
let around = table.around(15, 15); let around = table.around(15, 15);
assert_eq!(around.len(), 2); assert_eq!(around.len(), 2);
} }
#[test] #[test]
fn around_3() { fn around_3() {
let mut table = Table::new(false, 16); let table = Table::new(false, 16);
let around = table.around(5, 5); let around = table.around(5, 5);
assert_eq!(around.len(), 4); assert_eq!(around.len(), 4);
} }
#[test] #[test]
fn around_1_line() { fn around_1_line() {
let mut table = Table::new(false, 16); let table = Table::new(false, 16);
let around = table.around_line(0); let around = table.around_line(0);
assert_eq!(around.len(), 2); assert_eq!(around.len(), 2);
} }
#[test] #[test]
fn around_2_line() { 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); let around = table.around_line(16 * 16 - 1);
assert_eq!(around.len(), 2); assert_eq!(around.len(), 2);
} }
#[test] #[test]
fn around_3_line() { fn around_3_line() {
let mut table = Table::new(false, 16); let table = Table::new(false, 16);
let around = table.around_line(24); let around = table.around_line(24);
assert_eq!(around.len(), 4); assert_eq!(around.len(), 4);
} }
#[test] #[test]
fn hex_table_1() { 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); let coord = table.get_offset_of(9, 0);
assert_eq!(coord, (36.0, 0.0)); assert_eq!(coord, (36.0, 0.0));
} }
#[test] #[test]
fn hex_table_around_1() { 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); let around = table.around(9, 0);
assert_eq!(around.len(), 3); assert_eq!(around.len(), 3);
} }
+15 -15
View File
@@ -1,18 +1,16 @@
use rayon::iter::IndexedParallelIterator;
use std::time::Instant; use std::time::Instant;
use crate::SeededRng; use crate::subplate_automation::{HexMatrixRequest, SubPlateAutomation, ViewRedrawRequest};
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::{Color, Commands, Component, Entity, KeyCode, Query, Res, ResMut, Single, Sprite}; use bevy::prelude::{Commands, Component, Entity, KeyCode, Query, Res, ResMut, Single, Sprite};
use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat};
use rand::RngExt; use rand::RngExt;
use rand_chacha::ChaCha8Rng; use rand_chacha::ChaCha8Rng;
use rayon::prelude::IntoParallelIterator; use rayon::iter::IndexedParallelIterator;
use crate::seed_automation::SeedAutomation;
use crate::subplate_automation::{HexMatrixRequest, SubPlateAutomation, ViewRedrawRequest};
use rayon::iter::ParallelIterator; use rayon::iter::ParallelIterator;
use rayon::prelude::IntoParallelIterator;
pub fn update_automation_view( pub fn update_automation_view(
query: Single<(&Sprite, &PlateAutomation)>, query: Single<(&Sprite, &PlateAutomation)>,
@@ -24,7 +22,7 @@ pub fn update_automation_view(
let (sprite, automation) = query.into_inner(); let (sprite, automation) = query.into_inner();
let image = automation.world.get_image_data(); let image = automation.world.get_image_data();
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.entity(request.into_inner().1).despawn(); commands.entity(request.into_inner().1).despawn();
@@ -45,13 +43,13 @@ 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);
} }
if keys.just_pressed(KeyCode::AltLeft) { if keys.just_pressed(KeyCode::AltLeft) {
automation.world.grow(); automation.world.grow();
commands.spawn((ViewRedrawRequest)); commands.spawn(ViewRedrawRequest);
println!("Map size {}", automation.world.side); println!("Map size {}", automation.world.side);
} }
@@ -63,7 +61,7 @@ pub fn update_automation(
commands.entity(entity).remove::<PlateAutomation>().insert(SubPlateAutomation{ commands.entity(entity).remove::<PlateAutomation>().insert(SubPlateAutomation{
world: new_table world: new_table
}); });
commands.spawn((HexMatrixRequest)); commands.spawn(HexMatrixRequest);
} }
} }
@@ -76,7 +74,7 @@ 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(|x| 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)| {
if *self.world.get(index) { if *self.world.get(index) {
return true; return true;
@@ -88,7 +86,7 @@ impl PlateAutomation {
return false; return false;
} }
let chance = (around as f32 * 0.25); let chance = (around as f32 * 0.25).powi(2);
if random > chance { if random > chance {
return false; return false;
} }
@@ -98,5 +96,7 @@ impl PlateAutomation {
self.world.data = updated; self.world.data = updated;
// println!("Done in {} mcs", time.elapsed().as_micros());
} }
} }
+1 -1
View File
@@ -21,7 +21,7 @@ pub fn update_automation_view(
let image = automation.world.get_image_data(); let image = automation.world.get_image_data();
images.remove(sprite.image.id()); // images.remove(sprite.image.id());
images.insert(sprite.image.id(), image).unwrap(); images.insert(sprite.image.id(), image).unwrap();
} }
+4 -4
View File
@@ -24,7 +24,7 @@ pub fn update_automation_view(
let (sprite, automation) = query.into_inner(); let (sprite, automation) = query.into_inner();
let image = automation.world.get_image_data(); let image = automation.world.get_image_data();
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.entity(request.into_inner().1).despawn(); commands.entity(request.into_inner().1).despawn();
@@ -60,7 +60,7 @@ pub fn update_automation(
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) {
@@ -226,7 +226,7 @@ pub fn setup_hex_matrix(
let sprite = view_query.iter().next().unwrap(); let sprite = view_query.iter().next().unwrap();
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 {
@@ -254,7 +254,7 @@ pub fn update_hex_matrix_view(
let sprite = view_query.iter().next().unwrap(); let sprite = view_query.iter().next().unwrap();
images.remove(sprite.image.id()); // images.remove(sprite.image.id());
images.insert(sprite.image.id(), image).unwrap(); images.insert(sprite.image.id(), image).unwrap();
} }
#[derive(Component)] #[derive(Component)]