Files
tect_gen/src/seed_automation.rs
T

69 lines
1.8 KiB
Rust
Raw Normal View History

2026-06-29 13:13:17 +03:00
use crate::plate_automation::PlateAutomation;
2026-06-28 13:57:55 +03:00
use crate::table::{IntoImage, Table};
2026-06-29 13:13:17 +03:00
use crate::SeededRng;
use bevy::asset::Assets;
use bevy::image::Image;
2026-06-18 12:04:53 +03:00
use bevy::input::ButtonInput;
2026-06-18 12:38:59 +03:00
use bevy::prelude::{Commands, Component, Entity, KeyCode, Query, Res, ResMut, Sprite};
2026-06-18 12:04:53 +03:00
use rand::RngExt;
use rand_chacha::ChaCha8Rng;
2026-06-18 20:51:09 +03:00
pub fn update_automation_view(
2026-06-18 12:04:53 +03:00
query: Query<(&Sprite, &SeedAutomation)>,
mut images: ResMut<Assets<Image>>,
) {
2026-06-18 12:38:59 +03:00
if query.is_empty() {
return;
}
2026-06-18 12:04:53 +03:00
let (sprite, automation) = query.iter().next().unwrap();
2026-06-28 22:33:01 +03:00
let image = automation.world.get_image_data();
2026-06-18 12:04:53 +03:00
2026-06-29 13:07:49 +03:00
// images.remove(sprite.image.id());
2026-06-18 12:04:53 +03:00
images.insert(sprite.image.id(), image).unwrap();
}
2026-06-18 20:51:09 +03:00
pub fn update_automation(
2026-06-18 12:04:53 +03:00
mut query: Query<(&mut SeedAutomation, Entity)>,
mut seeded_rng: ResMut<SeededRng>,
2026-06-18 12:38:59 +03:00
keys: Res<ButtonInput<KeyCode>>,
2026-06-18 12:04:53 +03:00
mut commands: Commands) {
2026-06-18 12:38:59 +03:00
if query.is_empty() {
return;
}
2026-06-18 12:04:53 +03:00
let (mut automation, entity) = query.iter_mut().next().unwrap();
2026-06-18 12:38:59 +03:00
2026-06-18 12:04:53 +03:00
if keys.just_pressed(KeyCode::Space){
let random = &mut seeded_rng.0;
automation.next(random)
}
2026-06-18 21:46:47 +03:00
if keys.just_pressed(KeyCode::Enter){
2026-06-18 12:38:59 +03:00
println!("Switching to PlateAutomation");
commands.entity(entity).remove::<SeedAutomation>().insert(PlateAutomation{
world: automation.world.clone()
});
2026-06-18 12:04:53 +03:00
}
}
#[derive(Component)]
pub struct SeedAutomation {
pub(crate) world: Table<bool>,
}
impl SeedAutomation {
fn next(&mut self, rng: &mut ChaCha8Rng) {
loop {
2026-06-22 11:37:35 +03:00
let x = rng.random_range(16..48);
let y = rng.random_range(16..48);
2026-06-19 06:53:28 +03:00
if *self.world.get_dim(x, y) {
2026-06-18 12:04:53 +03:00
continue;
}
2026-06-19 06:53:28 +03:00
self.world.set_dim(x, y, true);
2026-06-18 12:04:53 +03:00
break;
}
}
}