From d333c9c9d60891e19c7982ecc87c8af173ca6988 Mon Sep 17 00:00:00 2001 From: annieversary Date: Fri, 3 Sep 2021 18:59:19 +0200 Subject: [PATCH] subtitled #13: goop eating ants --- Cargo.lock | 8 +++ crates/subtitled13/Cargo.toml | 8 +++ crates/subtitled13/src/main.rs | 89 ++++++++++++++++++++++++++++++++++ crates/utils/src/drawing.rs | 12 +++++ crates/utils/src/lib.rs | 4 ++ 5 files changed, 121 insertions(+) create mode 100644 crates/subtitled13/Cargo.toml create mode 100644 crates/subtitled13/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 086a7cf..16b22d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2390,6 +2390,14 @@ dependencies = [ "utils", ] +[[package]] +name = "subtitled13" +version = "0.1.0" +dependencies = [ + "nannou", + "utils", +] + [[package]] name = "subtitled2" version = "0.1.0" diff --git a/crates/subtitled13/Cargo.toml b/crates/subtitled13/Cargo.toml new file mode 100644 index 0000000..6491441 --- /dev/null +++ b/crates/subtitled13/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "subtitled13" +version = "0.1.0" +edition = "2018" + +[dependencies] +nannou = "0.17" +utils = { path = "../utils" } diff --git a/crates/subtitled13/src/main.rs b/crates/subtitled13/src/main.rs new file mode 100644 index 0000000..2d084f5 --- /dev/null +++ b/crates/subtitled13/src/main.rs @@ -0,0 +1,89 @@ +use nannou::prelude::*; +use nannou::rand::seq::SliceRandom; +use utils::*; + +fn main() { + nannou::app(model).update(update).simple_window(view).run(); +} + +struct Model { + walkers: Vec, + clustered: Vec, +} + +fn model(_app: &App) -> Model { + Model { + walkers: (0..5000).map(|_| vec2_range(-1.0, 1.0) * 300.0).collect(), + clustered: vec![Vec2::ZERO], + } +} + +fn update(app: &App, model: &mut Model, _update: Update) { + let _t = app.elapsed_frames() as f32 / 60.0; + + let mut clusters = model.clustered.clone(); + clusters.sort_by(|a, b| a.x.partial_cmp(&b.x).unwrap()); + + let mut to_remove = vec![]; + for (i, p) in &mut model.walkers.iter_mut().enumerate() { + *p += vec2_range(-1.0, 1.0) * 3.5; + + const RAD: f32 = 6.0; + for &c in &clusters { + if c.x > p.x + RAD { + break; + } + if c.x < p.x - RAD { + continue; + } + if p.distance_squared(c) < RAD * RAD { + model.clustered.push(*p); + to_remove.push(i); + break; + } + } + } + + for i in to_remove.into_iter().rev() { + model.walkers.remove(i); + } +} + +fn view(app: &App, model: &Model, frame: Frame) { + let t = frame.nth() as f32 / 60.0; + + let draw = app.draw(); + + drawing::draw_soft_bg(&draw, app, SNOW, 0.03); + + for &p in &model.walkers { + draw.ellipse().xy(p).radius(1.0).color(BLACK); + } + for &p in &model.clustered { + let v = map_sin(t + (p.x.powi(2) + p.y.powi(2) + t) * 0.00003, -20.0, 20.0); + let h = (100.0 + v) / 360.0; + draw.ellipse() + .xy(p) + .radius(4.0) + .color(hsla(h, 0.5, 0.5, 0.3)); + } + + let mut rng = nannou::rand::thread_rng(); + let n = (frame.nth() as f32 * 0.7) as usize; + for _ in 0..n { + let a = model.clustered.choose(&mut rng); + let b = model.clustered.choose(&mut rng); + if let (Some(&a), Some(&b)) = (a, b) { + let v = map_sin(t * 2.0 + a.x * a.y, -20.0, 20.0); + let h = (100.0 + v) / 360.0; + + draw.line() + .stroke_weight(3.0) + .points(a, b) + .color(hsla(h, 0.5, 0.5, 0.04)); + } + } + + draw.to_frame(app, &frame).unwrap(); + utils::record::record(app, &frame); +} diff --git a/crates/utils/src/drawing.rs b/crates/utils/src/drawing.rs index 2f8b270..97ef3a7 100644 --- a/crates/utils/src/drawing.rs +++ b/crates/utils/src/drawing.rs @@ -1,5 +1,17 @@ use nannou::{color::IntoLinSrgba, draw::properties::ColorScalar, prelude::*}; +pub fn draw_soft_bg(draw: &Draw, app: &App, color: impl IntoLinSrgba, alpha: f32) { + if app.elapsed_frames() <= 1 { + draw.background().color(color); + } else { + let mut color = color.into_lin_srgba(); + color.alpha = alpha; + + let win = app.window_rect(); + draw.rect().wh(win.wh()).color(color); + } +} + /// Draws the opposite of a box pub fn draw_exterior(draw: &Draw, size: f32, color: impl IntoLinSrgba + Clone) { draw.quad() diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs index 45e8a87..b9b06fa 100644 --- a/crates/utils/src/lib.rs +++ b/crates/utils/src/lib.rs @@ -42,3 +42,7 @@ impl Tup2Extension for (f32, f32) { self.into() } } + +pub fn vec2_range(min: f32, max: f32) -> Vec2 { + vec2(random_range(min, max), random_range(min, max)) +}