Hex table

This commit is contained in:
2026-06-22 11:37:35 +03:00
parent eda216b85d
commit 74f01cf895
6 changed files with 162 additions and 29 deletions
+5 -5
View File
@@ -111,7 +111,7 @@ impl IntoImage for Table<bool> {
data[idx] = value;
data[idx + 1] = value;
data[idx + 2] = value;
data[idx + 3] = 255;
data[idx + 3] = value;
});
data
}
@@ -125,10 +125,10 @@ impl IntoImage for Table<Color> {
.for_each(|(color, idx)| {
let idx = idx * 4;
let color = color.to_linear();
data[idx] = (color.red * 255.0) as u8;
data[idx + 1] = (color.green * 255.0) as u8;
data[idx + 2] = (color.blue * 255.0) as u8;
data[idx + 3] = 255;
data[idx] = (color.red * 256.0) as u8;
data[idx + 1] = (color.green * 256.0) as u8;
data[idx + 2] = (color.blue * 256.0) as u8;
data[idx + 3] = (color.alpha * 256.0) as u8;
});
data
}
+31
View File
@@ -0,0 +1,31 @@
pub struct HexTable {
len: usize,
}
const VERTICAL_OFFSET: f32 = 0.866025;
impl HexTable {
pub fn new(len: usize) -> Self{
Self{
len
}
}
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, y_point));
index += 1;
}
vec
}
}
+25 -1
View File
@@ -2,9 +2,11 @@ mod basic;
mod seed_automation;
mod plate_automation;
mod subplate_automation;
mod hex_table;
use crate::basic::{IntoImage, Table};
use bevy::asset::io::embedded::GetAssetServer;
use bevy::image::TextureFormatPixelInfo;
use bevy::input::keyboard::keyboard_input_system;
use bevy::prelude::*;
use bevy::window::{ExitCondition, WindowResolution};
@@ -12,9 +14,12 @@ use rand::{Rng, RngExt};
use rand_chacha::ChaCha8Rng;
use rand_chacha::rand_core::SeedableRng;
use seed_automation::SeedAutomation;
use crate::subplate_automation::HexMatrixView;
const RECTANGLE_SIDE: f32 = 1250.0;
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
@@ -33,25 +38,44 @@ fn main() {
.add_systems(Update, plate_automation::update_automation_view)
.add_systems(Update, subplate_automation::update_automation)
.add_systems(Update, subplate_automation::update_automation_view)
.add_systems(Update, subplate_automation::setup_hex_matrix)
.run();
}
fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
commands.spawn(Camera2d);
let handle = images.add(Image::default());
let mut shape = Sprite::from_image(handle);
shape.custom_size = Some(Vec2::new(RECTANGLE_SIDE, RECTANGLE_SIDE));
let automation = SeedAutomation {
world: Table::new(false, 16),
world: Table::new(false, 64),
};
commands.spawn((shape, automation));
let seeded_rng = ChaCha8Rng::seed_from_u64(rand::random());
commands.insert_resource(SeededRng(seeded_rng));
let mut hex_image = Image::default();
hex_image.data = Some(vec![
0;
hex_image
.texture_descriptor
.format
.pixel_size()
.unwrap_or(0)
]);
let handle = images.add(hex_image);
let mut shape = Sprite::from_image(handle);
shape.custom_size = Some(Vec2::new(RECTANGLE_SIDE, RECTANGLE_SIDE));
commands.spawn((shape, HexMatrixView, Transform::from_xyz(0.0, 0.0, 1.0)));
}
#[derive(Resource)]
+4 -3
View File
@@ -11,7 +11,7 @@ use rand::RngExt;
use rand_chacha::ChaCha8Rng;
use rayon::prelude::IntoParallelIterator;
use crate::seed_automation::SeedAutomation;
use crate::subplate_automation::SubPlateAutomation;
use crate::subplate_automation::{HexMatrixRequest, SubPlateAutomation};
use rayon::iter::ParallelIterator;
pub fn update_automation_view(
@@ -64,11 +64,12 @@ pub fn update_automation(
if keys.just_pressed(KeyCode::Enter) {
println!("Switching to SubPlateAutomation");
let mut new_table = Table::<Color>::new(Color::BLACK, automation.world.side);
automation.world.convert_copy(&mut new_table, |value| { if value { Color::WHITE } else { Color::BLACK } });
let mut new_table = Table::<Color>::new(Color::NONE, automation.world.side);
automation.world.convert_copy(&mut new_table, |value| { if value { Color::WHITE } else { Color::NONE} });
commands.entity(entity).remove::<PlateAutomation>().insert(SubPlateAutomation{
world: new_table
});
commands.spawn((HexMatrixRequest));
}
}
+2 -2
View File
@@ -69,8 +69,8 @@ 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(2..14);
let y = rng.random_range(2..14);
let x = rng.random_range(16..48);
let y = rng.random_range(16..48);
if *self.world.get_dim(x, y) {
continue;
+95 -18
View File
@@ -1,16 +1,18 @@
use rayon::iter::ParallelIterator;
use std::time::Instant;
use crate::SeededRng;
use crate::basic::{IntoImage, Table};
use crate::hex_table::HexTable;
use crate::{RECTANGLE_SIDE, SeededRng};
use bevy::asset::{Assets, RenderAssetUsages};
use bevy::image::{BevyDefault, Image};
use bevy::input::ButtonInput;
use bevy::prelude::{Color, Commands, Component, Entity, KeyCode, Query, Res, ResMut, Sprite};
use bevy::math::Vec2;
use bevy::prelude::{Color, Commands, Component, Entity, KeyCode, Query, Res, ResMut, Sprite, With};
use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat};
use bevy::tasks::futures_lite::StreamExt;
use rand::{ RngExt};
use rand::RngExt;
use rand_chacha::ChaCha8Rng;
use rayon::iter::IntoParallelIterator;
use rayon::iter::ParallelIterator;
use std::time::Instant;
pub fn update_automation_view(
query: Query<(&Sprite, &SubPlateAutomation)>,
@@ -43,7 +45,6 @@ pub fn update_automation(
mut seeded_rng: ResMut<SeededRng>,
keys: Res<ButtonInput<KeyCode>>,
commands: Commands,
) {
if query.is_empty() {
return;
@@ -78,15 +79,21 @@ impl SubPlateAutomation {
for index in 0..self.world.side * self.world.side {
let current_color = *self.world.get(index);
if current_color == Color::BLACK || current_color != Color::WHITE {
if current_color == Color::NONE || current_color != Color::WHITE {
continue;
}
if rng.random::<f32>() < 0.7 {
if rng.random::<f32>() < 0.8 {
continue;
}
let around = self.world.around_line(index).iter().filter(|v| ***v != Color::WHITE && ***v != Color::BLACK).map(|color| (*color).clone()).collect::<Vec<_>>();
let around = self
.world
.around_line(index)
.iter()
.filter(|v| ***v != Color::WHITE && ***v != Color::NONE)
.map(|color| (*color).clone())
.collect::<Vec<_>>();
if around.len() == 0 {
continue;
@@ -97,38 +104,108 @@ impl SubPlateAutomation {
}
println!("Time elapsed: {:?} ms", time.elapsed().as_millis());
}
fn smooth(&mut self) {
let range = 0..self.world.side * self.world.side;
let update = range.into_iter().map(|index| {
for index in range {
let around = self.world.around_line(index);
let mut counts: Vec<(Color, u8)> = vec![];
for p in around {
for count_index in 0..counts.len() {
if counts[count_index].0 == *p {
counts[count_index].1 += 1;
continue
continue;
}
}
counts.push((p.clone(), 1));
}
counts.iter().max_by_key(|(_, c)| {c}).unwrap().0
}).collect::<Vec<Color>>();
self.world.data = update;
self.world
.set(index, counts.iter().max_by_key(|(_, c)| c).unwrap().0);
}
}
fn seed(&mut self, rng: &mut ChaCha8Rng){
fn seed(&mut self, rng: &mut ChaCha8Rng) {
let len = (self.world.side * self.world.side) as f32;
loop {
let index = (rng.random::<f32>() * len) as usize;
if *self.world.get(index) == Color::BLACK {
if *self.world.get(index) == Color::NONE {
continue;
}
self.world.set(index, Color::hsv(rng.random::<f32>() * 360.0, 1.0, 1.0));
self.world
.set(index, Color::hsv(rng.random::<f32>() * 360.0, 1.0, 1.0));
break;
}
}
}
pub fn setup_hex_matrix(
mut request_query: Query<(&HexMatrixRequest, Entity)>,
mut view_query: Query<&Sprite, With<HexMatrixView>>,
mut automation_query: Query<&SubPlateAutomation>,
mut commands: Commands,
mut images: ResMut<Assets<Image>>,
) {
if request_query.is_empty() {
return;
}
let entity = request_query.iter().next().unwrap().1;
let automation = automation_query.iter_mut().next().unwrap();
commands.entity(entity).despawn();
println!("Seed hex matrix");
let resolution = automation.world.side / 4;
let generator = HexTable::new(resolution);
let mut table = Table::new(false, automation.world.side);
generator.calculate().iter().for_each(|(x, y)| {
let x = (x * 4.0) as usize;
let y = (y * 4.0) as usize;
if x + y * table.side >= table.data.len() {
return;
}
if *automation.world.get_dim(x, y) == Color::NONE {
return;
}
table.set_dim(x, y, true);
});
let data = table.get_image_data();
let image = Image::new(
Extent3d {
width: table.side as u32,
height: table.side as u32,
depth_or_array_layers: 1,
},
TextureDimension::D2,
data,
TextureFormat::bevy_default(),
RenderAssetUsages::default(),
);
let sprite = view_query.iter().next().unwrap();
images.remove(sprite.image.id());
images.insert(sprite.image.id(), image).unwrap();
commands.spawn((HexMatrixBuild{
points: table
},));
}
#[derive(Component)]
pub struct HexMatrixRequest;
#[derive(Component)]
pub struct HexMatrixView;
#[derive(Component)]
pub struct HexMatrixBuild{
points: Table<bool>
}