diff --git a/Cargo.toml b/Cargo.toml index a7fe9d6..f9dcfde 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,16 +12,13 @@ license = "AGPL" crate-type = ["cdylib", "rlib"] path = "src/rust/lib.rs" -[features] -default = ["console_error_panic_hook"] - [dependencies] -console_error_panic_hook = { version = "^0.1", optional = true } +console_error_panic_hook = "^0.1" specs = "^0.12" specs-derive = "^0.2" rand = "^0.5" wasm-bindgen = "^0.2" -getrandom = { version = "^0.2", features = ["js"] } +getrandom = { version = "^0.2", features = ["js", "wasm-bindgen"] } [dev-dependencies] cargo-watch = "^7" diff --git a/src/js/lib/application.tsx b/src/js/lib/application.tsx index 7e9d969..16ac899 100644 --- a/src/js/lib/application.tsx +++ b/src/js/lib/application.tsx @@ -7,15 +7,17 @@ const wasm = import("galaxy_gen_backend/galaxy_gen_backend"); export function Interface() { const [galaxySize, setGalaxySize] = React.useState(100); - const [wasmModule, setWasmModule] = React.useState(null); + const [galaxySeedMass, setGalaxySeedMass] = React.useState(3); + const [galaxyGravityReach, setGalaxyGravityReach] = React.useState(10); + let wasmModule: any = null; let galaxyFrontend: galaxy.Frontend = null; wasm.then((module) => { - console.log("module", module) - setWasmModule(module); + console.log("wasm module loaded: ", module) + wasmModule = module; }); - const handleChange = (event: React.ChangeEvent) => { + const handleGalaxySizeChange = (event: React.ChangeEvent) => { setGalaxySize(parseInt(event.target.value)); }; @@ -24,7 +26,35 @@ export function Interface() { Galaxy Size: + + ) + + const handleGalaxySeedMassChange = (event: React.ChangeEvent) => { + setGalaxySeedMass(parseInt(event.target.value)); + }; + + const galaxySeedMassInput = ( +
+ Seed Mass: + +
+ ) + + const handleGalaxyGravityReachChange = (event: React.ChangeEvent) => { + setGalaxySeedMass(parseInt(event.target.value)); + }; + + const galaxyGravityReachInput = ( +
+ Gravity Reach: +
) @@ -33,6 +63,7 @@ export function Interface() { if (wasmModule === null) { console.error("wasm not yet loaded"); } else { + console.log("initializing galaxy"); galaxyFrontend = new galaxy.Frontend(galaxySize); } }; @@ -44,10 +75,11 @@ export function Interface() { ) const handleSeedClick = () => { - if (wasmModule === null) { - console.error("wasm not yet loaded"); + if (galaxyFrontend === null) { + console.error("galaxy not yet initialized"); } else { - galaxyFrontend.seed(); + console.log("seeding galaxy"); + galaxyFrontend.seed(galaxySeedMass); } }; @@ -58,7 +90,7 @@ export function Interface() { ) const handleTickClick = () => { - galaxyFrontend.tick(); + galaxyFrontend.tick(galaxyGravityReach); }; const tickButton = ( @@ -72,6 +104,8 @@ export function Interface() {

Galaxy Generator

( rust => wasm => js ) galaxy generation simulation

{galaxySizeInput} + {galaxySeedMassInput} + {galaxyGravityReachInput}
{initButton} {seedButton} diff --git a/src/js/lib/galaxy.ts b/src/js/lib/galaxy.ts index a2a9c90..85e7ceb 100644 --- a/src/js/lib/galaxy.ts +++ b/src/js/lib/galaxy.ts @@ -19,18 +19,18 @@ export class Frontend { this.galaxySize = galaxySize; } - public seed(): void { - this.galaxy.seed(); + public seed(additionalMass: number): void { + this.galaxy.seed(additionalMass); } - public tick(): void { - this.galaxy.tick(); + public tick(gravityReach: number): void { + this.galaxy.tick(gravityReach); } public cells(): Cell[] { // Uint16Array to list of numbers let cells: Cell[] = []; - const mass = Array.from(this.galaxy.cell_mass()); + const mass = Array.from(this.galaxy.mass()); mass.forEach((element, index) => { cells.push({ mass: element, diff --git a/src/rust/cell.rs b/src/rust/cell.rs index 3a44f44..0496f96 100644 --- a/src/rust/cell.rs +++ b/src/rust/cell.rs @@ -1,4 +1,4 @@ -use wasm_bindgen::prelude::*; +use crate::galaxy::Galaxy; // internal constants impl Cell { @@ -28,7 +28,7 @@ impl Default for Cell { // public methods impl Cell { pub fn get_type(&self) -> u8 { - if self.mass < Cell::MIN_MASS_STAR { + if self.mass < Galaxy::MIN_MASS_STAR { return Cell::TYPE_INDEX_GAS; } else { return Cell::TYPE_INDEX_STAR; @@ -45,9 +45,9 @@ impl Cell { // internal methods impl Cell { pub fn check_if_type(&self, type_index: u8) -> bool { - if (type_index == Cell::TYPE_INDEX_GAS) & (self.mass < Cell::MIN_MASS_STAR) { + if (type_index == Cell::TYPE_INDEX_GAS) & (self.mass < Galaxy::MIN_MASS_STAR) { return true; - } else if (type_index == Cell::TYPE_INDEX_STAR) & (self.mass >= Cell::MIN_MASS_STAR) { + } else if (type_index == Cell::TYPE_INDEX_STAR) & (self.mass >= Galaxy::MIN_MASS_STAR) { return true; } else { return false; @@ -69,7 +69,7 @@ mod tests_cell_types { #[test] fn test_star_cell() { let cell = Cell { - mass: Cell::MIN_MASS_STAR * 2, + mass: Galaxy::MIN_MASS_STAR * 2, ..Default::default() }; assert_eq!(cell.is_star(), true); diff --git a/src/rust/galaxy.rs b/src/rust/galaxy.rs index 4828e14..3c3f063 100644 --- a/src/rust/galaxy.rs +++ b/src/rust/galaxy.rs @@ -2,8 +2,6 @@ use cell::*; use rand::Rng; use wasm_bindgen::prelude::*; -use crate::utils; - // types #[wasm_bindgen] pub struct Galaxy { @@ -16,7 +14,9 @@ pub struct Galaxy { impl Galaxy { #[wasm_bindgen(constructor)] pub fn new(size: u16, mass: u16) -> Galaxy { - utils::set_panic_hook(); + // create a new galaxy + // https://github.com/rustwasm/console_error_panic_hook#readme + console_error_panic_hook::set_once(); return Galaxy { size, cells: vec![ @@ -28,23 +28,35 @@ impl Galaxy { ], }; } - pub fn seed(&mut self) { + pub fn seed(&self, additional: u16) -> Galaxy { + // add mass to the galaxy + let mut next = Vec::with_capacity((self.size as usize).pow(2)); for cell_index in 0..self.size.pow(2) { - self.cells[cell_index as usize] = Cell { - mass: rand::thread_rng().gen_range(0, self.size), + let mass = self.cells[cell_index as usize].mass; + next.push(Cell { + mass: mass + rand::thread_rng().gen_range(0, additional + 1), ..Default::default() - }; + }); } + return Galaxy { + size: self.size, + cells: next, + }; } - pub fn tick(&mut self) { - let next = self.cells.clone(); + pub fn tick(&self, reach: u16) -> Galaxy { + // advance the galaxy one tick + // TODO: not yet implemented + let next = Vec::with_capacity((self.size as usize).pow(2)); for index in 0..(self.size - 1) { let _cell = self.cells[index as usize]; - let _neighbours = self.neighbours(index, 1); + let _neighbours = self.neighbours(index, reach); } - self.cells = next; + return Galaxy { + size: self.size, + cells: next, + }; } - pub fn cell_mass(&self) -> Vec { + pub fn mass(&self) -> Vec { // for every cell, get the mass let mut mass = Vec::new(); for cell in self.cells.iter() { @@ -130,22 +142,43 @@ mod tests_intial_generation { } #[test] fn test_seed_no_panic() { - Galaxy::new(10, 0).seed(); + Galaxy::new(10, 0).seed(1); } #[test] fn test_seed_tick_no_panic() { - let mut galaxy = Galaxy::new(10, 1); - galaxy.seed(); - galaxy.tick(); + Galaxy::new(10, 1).seed(1).tick(1); } #[test] fn test_seed_alters_data() { let mut galaxy = Galaxy::new(10, 0); let cells_before = galaxy.cells.clone(); - galaxy.seed(); + galaxy = galaxy.seed(1); let cells_after = galaxy.cells.clone(); assert_ne!(cells_before, cells_after); } + #[test] + fn test_seed_doesnt_alter_when_zero() { + let mut galaxy = Galaxy::new(10, 0); + let cells_before = galaxy.cells.clone(); + galaxy = galaxy.seed(0); + let cells_after = galaxy.cells.clone(); + assert_eq!(cells_before, cells_after); + } + #[test] + fn test_seed_alters_data_twice() { + let mut galaxy = Galaxy::new(10, 0); + let cells_first = galaxy.cells.clone(); + + galaxy = galaxy.seed(1); + let cells_second = galaxy.cells.clone(); + assert_ne!(cells_first, cells_second); + + galaxy = galaxy.seed(1); + let cells_third = galaxy.cells.clone(); + assert_ne!(cells_first, cells_second); + assert_ne!(cells_first, cells_third); + assert_ne!(cells_second, cells_third); + } } #[cfg(test)] diff --git a/src/rust/lib.rs b/src/rust/lib.rs index 8a83487..80e4f01 100644 --- a/src/rust/lib.rs +++ b/src/rust/lib.rs @@ -8,15 +8,11 @@ extern crate wasm_bindgen; pub mod cell; pub mod galaxy; pub mod galaxyecs; -mod utils; -use cell::Cell; use galaxy::Galaxy; // public constants -impl Cell { - pub const MIN_MASS_STAR: u16 = 10000; -} impl Galaxy { + pub const MIN_MASS_STAR: u16 = 10000; pub const GAS_REACH_MODIFIER: u16 = 10; } diff --git a/src/rust/utils.rs b/src/rust/utils.rs deleted file mode 100644 index b1d7929..0000000 --- a/src/rust/utils.rs +++ /dev/null @@ -1,10 +0,0 @@ -pub fn set_panic_hook() { - // When the `console_error_panic_hook` feature is enabled, we can call the - // `set_panic_hook` function at least once during initialization, and then - // we will get better error messages if our code ever panics. - // - // For more details see - // https://github.com/rustwasm/console_error_panic_hook#readme - #[cfg(feature = "console_error_panic_hook")] - console_error_panic_hook::set_once(); -}