diff --git a/Cargo.lock b/Cargo.lock index 1eeca14..97c67ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -131,6 +131,15 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "approx" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cab112f0a86d568ea0e627cc1d6be74a1e9cd55214684db5561995f6dad897c6" +dependencies = [ + "num-traits", +] + [[package]] name = "arrayref" version = "0.3.8" @@ -570,6 +579,7 @@ dependencies = [ "console_log", "env_logger", "log", + "nalgebra", "pollster", "rand", "wasm-bindgen", @@ -875,6 +885,16 @@ dependencies = [ "libc", ] +[[package]] +name = "matrixmultiply" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9380b911e3e96d10c1f415da0876389aaf1b56759054eeb0de7df940c456ba1a" +dependencies = [ + "autocfg", + "rawpointer", +] + [[package]] name = "memchr" version = "2.7.4" @@ -926,6 +946,33 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "nalgebra" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c4b5f057b303842cf3262c27e465f4c303572e7f6b0648f60e16248ac3397f4" +dependencies = [ + "approx", + "matrixmultiply", + "nalgebra-macros", + "num-complex", + "num-rational", + "num-traits", + "simba", + "typenum", +] + +[[package]] +name = "nalgebra-macros" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "254a5372af8fc138e36684761d3c0cdb758a4410e938babcff1c860ce14ddbfc" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.75", +] + [[package]] name = "ndk" version = "0.8.0" @@ -957,6 +1004,54 @@ dependencies = [ "jni-sys", ] +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "num_enum" version = "0.7.3" @@ -1206,6 +1301,12 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539" +[[package]] +name = "rawpointer" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" + [[package]] name = "redox_syscall" version = "0.3.5" @@ -1287,6 +1388,15 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "safe_arch" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3460605018fdc9612bce72735cba0d27efbcd9904780d44c7e3a9948f96148a" +dependencies = [ + "bytemuck", +] + [[package]] name = "same-file" version = "1.0.6" @@ -1347,6 +1457,19 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "simba" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3a386a501cd104797982c15ae17aafe8b9261315b5d07e3ec803f2ea26be0fa" +dependencies = [ + "approx", + "num-complex", + "num-traits", + "paste", + "wide", +] + [[package]] name = "slab" version = "0.4.9" @@ -1541,6 +1664,12 @@ version = "0.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5be21190ff5d38e8b4a2d3b6a3ae57f612cc39c96e83cedeaf7abc338a8bac4a" +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + [[package]] name = "unicode-ident" version = "1.0.12" @@ -1895,6 +2024,16 @@ dependencies = [ "web-sys", ] +[[package]] +name = "wide" +version = "0.7.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b828f995bf1e9622031f8009f8481a85406ce1f4d4588ff746d872043e855690" +dependencies = [ + "bytemuck", + "safe_arch", +] + [[package]] name = "widestring" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index 5f6c946..a05c3ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ cfg-if = "1" pollster = "0.3" bytemuck = { version = "1.17", features = [ "derive" ] } rand = "0.8" +nalgebra = "0.33" [target.'cfg(target_arch = "wasm32")'.dependencies] console_error_panic_hook = "0.1.6" diff --git a/src/state.rs b/src/state.rs index e6e2d98..38cf218 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,4 +1,5 @@ use std::time::Instant; +use nalgebra::Point2; use rand::prelude::*; use winit::event::{DeviceEvent, ElementState, Event, MouseButton, MouseScrollDelta, WindowEvent}; @@ -14,7 +15,7 @@ enum CellKind { Grass, } impl CellKind { - const VALID_CHARS: [char; 3] = ['v', 's', 'g']; + const VARIANTS: u8 = 3; fn color(&self) -> [f32; 4] { match self { Self::Void => [0.; 4], @@ -23,16 +24,6 @@ impl CellKind { } } } -impl From for CellKind { - fn from(value: char) -> Self { - match value { - 'v' => Self::Void, - 's' => Self::Sea, - 'g' => Self::Grass, - _ => panic!("Invalid cell kind") - } - } -} impl From for CellKind { fn from(value: u8) -> Self { match value { @@ -45,12 +36,14 @@ impl From for CellKind { } struct Cell { + pos: Point2, kind: CellKind } impl Cell { const RADIUS: f32 = 1.; - fn new(kind: CellKind) -> Self { + fn new(pos: Point2, kind: CellKind) -> Self { Self { + pos, kind } } @@ -62,30 +55,24 @@ struct Map { impl Map { const HEIGHT: usize = 10; const WIDTH: usize = 10; - const SIZE: usize = Self::HEIGHT*Self::WIDTH; + const SIZE: usize = 10; fn new() -> Self { - std::array::from_fn(|_| thread_rng().gen_range(1..=2)).into() + Self { + cells: std::array::from_fn(|_| { + let mut rng = thread_rng(); + Cell::new( + Point2::new(rng.gen_range(-1.0..1.), rng.gen_range(-1.0..1.)), + rng.gen_range(0..CellKind::VARIANTS).into() + ) + }) + } + // "sgssv // ggsvg // gsvvs // vgsgs // ssggs".into() } - fn enumerate<'a>(&'a self) -> std::iter::Map>, fn((usize, &Cell)) -> ([usize; 2], &Cell)> { - self.cells.iter().enumerate().map(|(i, c)| ([i % Self::HEIGHT, i / Self::WIDTH], c)) - } -} -impl From<&str> for Map { - fn from(value: &str) -> Self { - let mut chars = value.chars().filter(|c| CellKind::VALID_CHARS.contains(c)); - let cells = std::array::from_fn(|_| Cell::new(chars.next().expect("Invalid map size").into())); - Self { cells } - } -} -impl From<[u8; Map::SIZE]> for Map { - fn from(value: [u8; Map::SIZE]) -> Self { - Self { cells: value.map(|c| Cell::new(c.into())) } - } } pub struct State { @@ -137,7 +124,7 @@ impl State { self.vertices = Vec::with_capacity(self.map.cells.len()*6); self.indices = Vec::with_capacity(self.map.cells.len()*12); - for ([x, y], c) in self.map.enumerate() { + for c in self.map.cells.iter() { let x = x as f32; let y = y as f32; let i = self.vertices.len();