Update rendering
This commit is contained in:
+62
-23
@@ -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,69 +75,111 @@ 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(){
|
||||
fn around_1() {
|
||||
let mut table = Table::new(false, 16);
|
||||
let around = table.around(0, 0);
|
||||
assert_eq!(around.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn around_2(){
|
||||
fn around_2() {
|
||||
let mut table = Table::new(false, 16);
|
||||
let around = table.around(15, 15);
|
||||
assert_eq!(around.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn around_3(){
|
||||
fn around_3() {
|
||||
let mut table = Table::new(false, 16);
|
||||
let around = table.around(5, 5);
|
||||
assert_eq!(around.len(), 4);
|
||||
}
|
||||
#[test]
|
||||
fn around_1_line(){
|
||||
fn around_1_line() {
|
||||
let mut table = Table::new(false, 16);
|
||||
let around = table.around_line(0);
|
||||
assert_eq!(around.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn around_2_line(){
|
||||
fn around_2_line() {
|
||||
let mut table = Table::new(false, 16);
|
||||
let around = table.around_line(16 * 16 - 1);
|
||||
assert_eq!(around.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn around_3_line(){
|
||||
fn around_3_line() {
|
||||
let mut table = Table::new(false, 16);
|
||||
let around = table.around_line(24);
|
||||
assert_eq!(around.len(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hex_table_1(){
|
||||
fn hex_table_1() {
|
||||
let mut 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(){
|
||||
fn hex_table_around_1() {
|
||||
let mut table = HexTable::new(10, false, 4.0);
|
||||
let around = table.around(9, 0);
|
||||
assert_eq!(around.len(), 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+16
-10
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user