Collision base
This commit is contained in:
@@ -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
@@ -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<T: Clone> {
|
||||
|
||||
+14
-17
@@ -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<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 {
|
||||
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);
|
||||
}
|
||||
|
||||
+15
-15
@@ -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::<PlateAutomation>().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::<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)| {
|
||||
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());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
@@ -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)]
|
||||
|
||||
Reference in New Issue
Block a user