This commit is contained in:
Arkitu 2024-08-23 10:00:33 +02:00
parent db1f756544
commit 549bd261b8
3 changed files with 157 additions and 30 deletions

139
Cargo.lock generated
View File

@ -131,6 +131,15 @@ dependencies = [
"windows-sys 0.52.0", "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]] [[package]]
name = "arrayref" name = "arrayref"
version = "0.3.8" version = "0.3.8"
@ -570,6 +579,7 @@ dependencies = [
"console_log", "console_log",
"env_logger", "env_logger",
"log", "log",
"nalgebra",
"pollster", "pollster",
"rand", "rand",
"wasm-bindgen", "wasm-bindgen",
@ -875,6 +885,16 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "matrixmultiply"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9380b911e3e96d10c1f415da0876389aaf1b56759054eeb0de7df940c456ba1a"
dependencies = [
"autocfg",
"rawpointer",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.7.4" version = "2.7.4"
@ -926,6 +946,33 @@ dependencies = [
"unicode-xid", "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]] [[package]]
name = "ndk" name = "ndk"
version = "0.8.0" version = "0.8.0"
@ -957,6 +1004,54 @@ dependencies = [
"jni-sys", "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]] [[package]]
name = "num_enum" name = "num_enum"
version = "0.7.3" version = "0.7.3"
@ -1206,6 +1301,12 @@ version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539" checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539"
[[package]]
name = "rawpointer"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.3.5" version = "0.3.5"
@ -1287,6 +1388,15 @@ dependencies = [
"windows-sys 0.52.0", "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]] [[package]]
name = "same-file" name = "same-file"
version = "1.0.6" version = "1.0.6"
@ -1347,6 +1457,19 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" 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]] [[package]]
name = "slab" name = "slab"
version = "0.4.9" version = "0.4.9"
@ -1541,6 +1664,12 @@ version = "0.24.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5be21190ff5d38e8b4a2d3b6a3ae57f612cc39c96e83cedeaf7abc338a8bac4a" checksum = "5be21190ff5d38e8b4a2d3b6a3ae57f612cc39c96e83cedeaf7abc338a8bac4a"
[[package]]
name = "typenum"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.12" version = "1.0.12"
@ -1895,6 +2024,16 @@ dependencies = [
"web-sys", "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]] [[package]]
name = "widestring" name = "widestring"
version = "1.1.0" version = "1.1.0"

View File

@ -12,6 +12,7 @@ cfg-if = "1"
pollster = "0.3" pollster = "0.3"
bytemuck = { version = "1.17", features = [ "derive" ] } bytemuck = { version = "1.17", features = [ "derive" ] }
rand = "0.8" rand = "0.8"
nalgebra = "0.33"
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = "0.1.6" console_error_panic_hook = "0.1.6"

View File

@ -1,4 +1,5 @@
use std::time::Instant; use std::time::Instant;
use nalgebra::Point2;
use rand::prelude::*; use rand::prelude::*;
use winit::event::{DeviceEvent, ElementState, Event, MouseButton, MouseScrollDelta, WindowEvent}; use winit::event::{DeviceEvent, ElementState, Event, MouseButton, MouseScrollDelta, WindowEvent};
@ -14,7 +15,7 @@ enum CellKind {
Grass, Grass,
} }
impl CellKind { impl CellKind {
const VALID_CHARS: [char; 3] = ['v', 's', 'g']; const VARIANTS: u8 = 3;
fn color(&self) -> [f32; 4] { fn color(&self) -> [f32; 4] {
match self { match self {
Self::Void => [0.; 4], Self::Void => [0.; 4],
@ -23,16 +24,6 @@ impl CellKind {
} }
} }
} }
impl From<char> for CellKind {
fn from(value: char) -> Self {
match value {
'v' => Self::Void,
's' => Self::Sea,
'g' => Self::Grass,
_ => panic!("Invalid cell kind")
}
}
}
impl From<u8> for CellKind { impl From<u8> for CellKind {
fn from(value: u8) -> Self { fn from(value: u8) -> Self {
match value { match value {
@ -45,12 +36,14 @@ impl From<u8> for CellKind {
} }
struct Cell { struct Cell {
pos: Point2<f32>,
kind: CellKind kind: CellKind
} }
impl Cell { impl Cell {
const RADIUS: f32 = 1.; const RADIUS: f32 = 1.;
fn new(kind: CellKind) -> Self { fn new(pos: Point2<f32>, kind: CellKind) -> Self {
Self { Self {
pos,
kind kind
} }
} }
@ -62,30 +55,24 @@ struct Map {
impl Map { impl Map {
const HEIGHT: usize = 10; const HEIGHT: usize = 10;
const WIDTH: usize = 10; const WIDTH: usize = 10;
const SIZE: usize = Self::HEIGHT*Self::WIDTH; const SIZE: usize = 10;
fn new() -> Self { 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 // "sgssv
// ggsvg // ggsvg
// gsvvs // gsvvs
// vgsgs // vgsgs
// ssggs".into() // ssggs".into()
} }
fn enumerate<'a>(&'a self) -> std::iter::Map<std::iter::Enumerate<std::slice::Iter<'a, Cell>>, 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 { pub struct State {
@ -137,7 +124,7 @@ impl State {
self.vertices = Vec::with_capacity(self.map.cells.len()*6); self.vertices = Vec::with_capacity(self.map.cells.len()*6);
self.indices = Vec::with_capacity(self.map.cells.len()*12); 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 x = x as f32;
let y = y as f32; let y = y as f32;
let i = self.vertices.len(); let i = self.vertices.len();