Update rendering

This commit is contained in:
2026-06-29 01:29:58 +03:00
parent bfbc22ca89
commit 8d2ed11e73
4 changed files with 104 additions and 43 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2024"
[dependencies]
bevy = { version = "0.19.0", features = ["keyboard"] }
bevy = { version = "0.19.0", features = ["keyboard", "debug"] }
rand = "0.10.1"
rand_chacha = "0.10.0"
rayon = "1.12.0"
+53 -14
View File
@@ -1,9 +1,10 @@
mod table;
mod seed_automation;
mod plate_automation;
mod subplate_automation;
mod hex_table;
mod plate_automation;
mod seed_automation;
mod subplate_automation;
mod table;
use crate::subplate_automation::HexMatrixView;
use crate::table::{IntoImage, Table};
use bevy::asset::io::embedded::GetAssetServer;
use bevy::image::TextureFormatPixelInfo;
@@ -14,13 +15,10 @@ use rand::{Rng, RngExt};
use rand_chacha::ChaCha8Rng;
use rand_chacha::rand_core::SeedableRng;
use seed_automation::SeedAutomation;
use crate::subplate_automation::HexMatrixView;
extern crate bevy;
const RECTANGLE_SIDE: f32 = 1000.0;
const WINDOW_SIDE: u32 = 1200;
const RECTANGLE_SIDE: f32 = 1500.0;
const WINDOW_SIDE: u32 = 1700;
fn main() {
App::new()
@@ -42,14 +40,13 @@ fn main() {
.add_systems(Update, subplate_automation::update_automation_view)
.add_systems(Update, subplate_automation::setup_hex_matrix)
.add_systems(Update, subplate_automation::update_hex_matrix_view)
.add_systems(Update, moving_system)
.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);
@@ -59,7 +56,7 @@ fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
world: Table::new(false, 64),
};
commands.spawn((shape, automation));
commands.spawn((shape, automation, Moving));
let seeded_rng = ChaCha8Rng::seed_from_u64(rand::random());
commands.insert_resource(SeededRng(seeded_rng));
@@ -78,16 +75,58 @@ fn setup(mut commands: Commands, mut images: ResMut<Assets<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)));
commands.spawn((
shape,
HexMatrixView,
Transform::from_xyz(0.0, 0.0, 1.0),
Moving,
));
}
fn moving_system(mut 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;
}
if keys.pressed(KeyCode::ArrowUp) {
transform.translation.y -= 3.0;
}
if keys.pressed(KeyCode::ArrowLeft) {
transform.translation.x += 3.0;
}
if keys.pressed(KeyCode::ArrowRight) {
transform.translation.x -= 3.0;
}
if keys.pressed(KeyCode::PageUp) {
transform.scale -= 0.05;
}
if keys.pressed(KeyCode::PageDown) {
transform.scale += 0.05;
}
if keys.just_pressed(KeyCode::Home) {
transform.translation.x = 0.0;
transform.translation.y = 0.0;
transform.scale = Vec3::ONE;
}
}
}
#[derive(Resource)]
struct SeededRng(ChaCha8Rng);
#[derive(Component)]
pub struct Moving;
#[cfg(test)]
mod tests {
use crate::table::Table;
use crate::hex_table::HexTable;
use crate::table::Table;
use bevy::render::render_resource::encase::private::RuntimeSizedArray;
#[test]
fn around_1() {
+16 -10
View File
@@ -5,27 +5,29 @@ use crate::table::{IntoImage, Table};
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::prelude::{Color, Commands, Component, Entity, KeyCode, Query, Res, ResMut, Single, Sprite};
use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat};
use rand::RngExt;
use rand_chacha::ChaCha8Rng;
use rayon::prelude::IntoParallelIterator;
use crate::seed_automation::SeedAutomation;
use crate::subplate_automation::{HexMatrixRequest, SubPlateAutomation};
use crate::subplate_automation::{HexMatrixRequest, SubPlateAutomation, ViewRedrawRequest};
use rayon::iter::ParallelIterator;
pub fn update_automation_view(
query: Query<(&Sprite, &PlateAutomation)>,
query: Single<(&Sprite, &PlateAutomation)>,
request: Single<(&ViewRedrawRequest, Entity)>,
mut images: ResMut<Assets<Image>>,
mut commands: Commands,
) {
if query.is_empty() {
return;
}
let (sprite, automation) = query.iter().next().unwrap();
let (sprite, automation) = query.into_inner();
let image = automation.world.get_image_data();
images.remove(sprite.image.id());
images.insert(sprite.image.id(), image).unwrap();
commands.entity(request.into_inner().1).despawn();
}
pub fn update_automation(
@@ -42,12 +44,16 @@ pub fn update_automation(
if keys.just_pressed(KeyCode::Space) || keys.all_pressed([KeyCode::Space, KeyCode::ShiftLeft]) {
let random = &mut seeded_rng.0;
automation.next(random)
automation.next(random);
commands.spawn((ViewRedrawRequest));
}
if keys.just_pressed(KeyCode::AltLeft) {
automation.world.grow();
println!("Map size {}", automation.world.side)
commands.spawn((ViewRedrawRequest));
println!("Map size {}", automation.world.side);
}
if keys.just_pressed(KeyCode::Enter) {
@@ -82,7 +88,7 @@ impl PlateAutomation {
return false;
}
let chance = (around as f32 * 0.2).powi(2);
let chance = (around as f32 * 0.25);
if random > chance {
return false;
}
+25 -9
View File
@@ -13,19 +13,21 @@ use rand::RngExt;
use rand_chacha::ChaCha8Rng;
use std::collections::HashMap;
use std::time::Instant;
use crate::plate_automation::PlateAutomation;
pub fn update_automation_view(
query: Query<(&Sprite, &SubPlateAutomation)>,
query: Single<(&Sprite, &SubPlateAutomation)>,
request: Single<(&ViewRedrawRequest, Entity)>,
mut images: ResMut<Assets<Image>>,
mut commands: Commands,
) {
if query.is_empty() {
return;
}
let (sprite, automation) = query.iter().next().unwrap();
let (sprite, automation) = query.into_inner();
let image = automation.world.get_image_data();
images.remove(sprite.image.id());
images.insert(sprite.image.id(), image).unwrap();
commands.entity(request.into_inner().1).despawn();
}
pub fn update_automation(
@@ -39,7 +41,9 @@ pub fn update_automation(
if keys.just_pressed(KeyCode::Space) || keys.all_pressed([KeyCode::Space, KeyCode::ShiftLeft]) {
let random = &mut seeded_rng.0;
automation.next(random)
automation.next(random);
commands.spawn((ViewRedrawRequest));
}
if keys.just_pressed(KeyCode::AltLeft) {
@@ -48,11 +52,16 @@ pub fn update_automation(
for _ in 0..16 {
automation.seed(random);
}
commands.spawn((ViewRedrawRequest));
}
// if keys.just_pressed(KeyCode::AltRight) {
// automation.smooth();
// }
if keys.just_pressed(KeyCode::AltRight) {
automation.smooth();
commands.spawn((ViewRedrawRequest));
}
if keys.just_pressed(KeyCode::KeyS) {
automation.calculate_subplates(&mut commands);
@@ -60,6 +69,7 @@ pub fn update_automation(
if keys.just_pressed(KeyCode::KeyF) {
automation.calculate_front_lines(&mut commands, hex_query, &automation);
}
}
@@ -72,6 +82,7 @@ impl SubPlateAutomation {
fn next(&mut self, rng: &mut ChaCha8Rng) {
let time = Instant::now();
for index in 0..self.world.side * self.world.side {
let current_color = *self.world.get(index);
if current_color == 0 || current_color != u8::MAX {
@@ -97,6 +108,8 @@ impl SubPlateAutomation {
self.world.set(index, color);
}
println!("Done in {} mcs", time.elapsed().as_micros());
}
fn smooth(&mut self) {
@@ -250,6 +263,9 @@ pub struct HexMatrixRequest;
#[derive(Component)]
pub struct HexMatrixRedrawRequest;
#[derive(Component)]
pub struct ViewRedrawRequest;
#[derive(Component)]
pub struct HexMatrixView;