chore: redo iteration inc/dec keybind behaviour

This commit is contained in:
Rekai Nyangadzayi Musuka 2021-03-29 21:07:10 -05:00
parent 0c6dae0d04
commit 9222835f5d
1 changed files with 25 additions and 26 deletions

View File

@ -7,62 +7,61 @@ fn main() {
App::build() App::build()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_startup_system(startup.system()) .add_startup_system(startup.system())
.add_resource(SpriteScale::default()) .add_resource(TextureOptions::default())
.add_system(mandelbrot_render_system.system()) .add_system(mandelbrot_render_system.system())
.add_system(transform_mandelbrot_system.system()) .add_system(transform_mandelbrot_system.system())
.run(); .run();
} }
struct SpriteScale { struct TextureOptions {
zoom: f64, zoom: f64,
horizontal: f64, horizontal: f64,
vertical: f64, vertical: f64,
step: f64, step: f64,
iterations: u32, iteration_limit: u32,
} }
impl Default for SpriteScale { impl Default for TextureOptions {
fn default() -> Self { fn default() -> Self {
Self { Self {
zoom: 1.0, zoom: 1.0,
horizontal: 0.0, horizontal: 0.0,
vertical: 0.0, vertical: 0.0,
step: 0.01, step: 0.01,
iterations: 64, iteration_limit: 64,
} }
} }
} }
fn transform_mandelbrot_system( fn transform_mandelbrot_system(input: Res<Input<KeyCode>>, mut scale: ResMut<TextureOptions>) {
input: Res<Input<KeyCode>>, // Zoom: E to Zoom In, Q to Zoom Out
mut scale: ResMut<SpriteScale>, // Horizontal Movement: D to move right, A to move left
// camera: Res<Camera>, // Vertical Movement: W to move up, S to move Down
) { // R to increase the steps taken by zoom, horizontal movement, and vertical movement
// T to increase the Iteration Limit, G to decrease the iteration limit
let zoom = input.pressed(KeyCode::Q) as i8 - input.pressed(KeyCode::E) as i8; let zoom = input.pressed(KeyCode::Q) as i8 - input.pressed(KeyCode::E) as i8;
let horizontal = input.pressed(KeyCode::D) as i8 - input.pressed(KeyCode::A) as i8; let horizontal = input.pressed(KeyCode::D) as i8 - input.pressed(KeyCode::A) as i8;
let vertical = input.pressed(KeyCode::W) as i8 - input.pressed(KeyCode::S) as i8; let vertical = input.pressed(KeyCode::W) as i8 - input.pressed(KeyCode::S) as i8;
let step_mod = input.pressed(KeyCode::R) as i8 - input.pressed(KeyCode::F) as i8; let step_change = input.pressed(KeyCode::R) as i8 - input.pressed(KeyCode::F) as i8;
let iter_mod = input.pressed(KeyCode::T) as i8 - input.pressed(KeyCode::G) as i8; let limit_change = input.just_pressed(KeyCode::T) as i8 - input.just_pressed(KeyCode::G) as i8;
scale.step += (scale.step / 10.0) * step_mod as f64; scale.step += (scale.step / 10.0) * step_change as f64;
scale.vertical += scale.step * vertical as f64; scale.vertical += scale.step * vertical as f64;
scale.horizontal += scale.step * horizontal as f64; scale.horizontal += scale.step * horizontal as f64;
scale.zoom += scale.step * zoom as f64; scale.zoom += scale.step * zoom as f64;
if iter_mod == 1 { if limit_change == 1 {
scale.iterations += (scale.step * 100.0) as u32; scale.iteration_limit *= 2;
if scale.iterations > 512 { if scale.iteration_limit > 512 {
scale.iterations = 512; scale.iteration_limit = 512;
} }
} else if iter_mod == -1 { } else if limit_change == -1 {
let step = (scale.step * 100.0) as u32; scale.iteration_limit /= 2;
let diff = scale.iterations as i64 - step as i64;
if diff < 32 { if scale.iteration_limit < 32 {
scale.iterations = 32; scale.iteration_limit = 32;
} else {
scale.iterations -= step;
} }
} }
@ -78,7 +77,7 @@ fn transform_mandelbrot_system(
fn mandelbrot_render_system( fn mandelbrot_render_system(
materials: Res<Assets<ColorMaterial>>, materials: Res<Assets<ColorMaterial>>,
mut textures: ResMut<Assets<Texture>>, mut textures: ResMut<Assets<Texture>>,
scale: Res<SpriteScale>, scale: Res<TextureOptions>,
mut query: Query<(&mut Mandelbrot, &Handle<ColorMaterial>)>, mut query: Query<(&mut Mandelbrot, &Handle<ColorMaterial>)>,
) { ) {
for (mut fractal, handle) in query.iter_mut() { for (mut fractal, handle) in query.iter_mut() {
@ -88,7 +87,7 @@ fn mandelbrot_render_system(
let z = scale.zoom; let z = scale.zoom;
let h = scale.horizontal; let h = scale.horizontal;
let v = scale.vertical; let v = scale.vertical;
let limit = scale.iterations; let limit = scale.iteration_limit;
let x = ((-2.5 * z) + h, (1.0 * z) + h); let x = ((-2.5 * z) + h, (1.0 * z) + h);
let y = ((-1.0 * z) - v, (1.0 * z) - v); let y = ((-1.0 * z) - v, (1.0 * z) - v);