From 7ae3b240fcbf2f20cf49e78def1a4a582cd51c81 Mon Sep 17 00:00:00 2001 From: Kai Siren Date: Sat, 25 Nov 2023 01:14:57 -0500 Subject: [PATCH] Adds D3 graphing --- src/js/lib/application.tsx | 94 ++++++++++++------- src/js/lib/dataviz.tsx | 94 ++++++++++++------- src/js/lib/galaxy.ts | 29 +++--- src/rust/cell.rs | 77 --------------- src/rust/galaxy.rs | 187 ++++++++++++++++++++++++++----------- src/rust/galaxyecs.rs | 84 ----------------- src/rust/lib.rs | 10 -- 7 files changed, 269 insertions(+), 306 deletions(-) delete mode 100644 src/rust/cell.rs delete mode 100644 src/rust/galaxyecs.rs diff --git a/src/js/lib/application.tsx b/src/js/lib/application.tsx index 16ac899..8f0936b 100644 --- a/src/js/lib/application.tsx +++ b/src/js/lib/application.tsx @@ -1,70 +1,95 @@ -import React from 'react'; -import "./styles.css" +import React from "react"; +import "./styles.css"; import * as dataviz from "./dataviz"; import * as galaxy from "./galaxy"; const wasm = import("galaxy_gen_backend/galaxy_gen_backend"); -export function Interface() { +export function Interface() { const [galaxySize, setGalaxySize] = React.useState(100); - const [galaxySeedMass, setGalaxySeedMass] = React.useState(3); - const [galaxyGravityReach, setGalaxyGravityReach] = React.useState(10); + const [galaxySeedMass, setGalaxySeedMass] = React.useState(5); + const [minStarMass, setMinStarMass] = React.useState(1000); let wasmModule: any = null; let galaxyFrontend: galaxy.Frontend = null; wasm.then((module) => { - console.log("wasm module loaded: ", module) + console.log("wasm module loaded: ", module); wasmModule = module; }); - const handleGalaxySizeChange = (event: React.ChangeEvent) => { - setGalaxySize(parseInt(event.target.value)); + const handleGalaxySizeChange = ( + event: React.ChangeEvent + ) => { + const value = parseInt(event.target.value); + setGalaxySize(Number.isNaN(value) ? 0 : value); }; const galaxySizeInput = (
- Galaxy Size: + + Galaxy Size: +
- ) + ); - const handleGalaxySeedMassChange = (event: React.ChangeEvent) => { - setGalaxySeedMass(parseInt(event.target.value)); + const handleGalaxySeedMassChange = ( + event: React.ChangeEvent + ) => { + const value = parseInt(event.target.value); + setGalaxySeedMass(Number.isNaN(value) ? 0 : value); }; const galaxySeedMassInput = (
- Seed Mass: + + Seed Mass: +
- ) + ); - const handleGalaxyGravityReachChange = (event: React.ChangeEvent) => { - setGalaxySeedMass(parseInt(event.target.value)); + const handleMinStarMassChange = ( + event: React.ChangeEvent + ) => { + const value = parseInt(event.target.value); + setMinStarMass(Number.isNaN(value) ? 0 : value); }; - const galaxyGravityReachInput = ( + const minStarMassInput = (
- Gravity Reach: + + Min Star Mass: +
- ) + ); const handleInitClick = () => { if (wasmModule === null) { console.error("wasm not yet loaded"); } else { console.log("initializing galaxy"); - galaxyFrontend = new galaxy.Frontend(galaxySize); + galaxyFrontend = new galaxy.Frontend(galaxySize, minStarMass); + dataviz.initViz(galaxyFrontend); } }; @@ -72,7 +97,7 @@ export function Interface() { - ) + ); const handleSeedClick = () => { if (galaxyFrontend === null) { @@ -80,6 +105,7 @@ export function Interface() { } else { console.log("seeding galaxy"); galaxyFrontend.seed(galaxySeedMass); + dataviz.initData(galaxyFrontend); } }; @@ -87,25 +113,29 @@ export function Interface() { - ) + ); const handleTickClick = () => { - galaxyFrontend.tick(galaxyGravityReach); + galaxyFrontend.tick(minStarMass); }; const tickButton = ( - ) + ); return (

Galaxy Generator

-

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

+

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

{galaxySizeInput} {galaxySeedMassInput} - {galaxyGravityReachInput} + {minStarMassInput}
{initButton} {seedButton} diff --git a/src/js/lib/dataviz.tsx b/src/js/lib/dataviz.tsx index 2dd08a5..e3f70b4 100644 --- a/src/js/lib/dataviz.tsx +++ b/src/js/lib/dataviz.tsx @@ -2,47 +2,71 @@ import React from "react"; import * as d3 from "d3"; import * as galaxy from "./galaxy"; -export function DataViz(data: galaxy.Frontend) { - // set the dimensions and margins of the graph - const margin = { top: 10, right: 30, bottom: 30, left: 60 }, - width = 460 - margin.left - margin.right, - height = 400 - margin.top - margin.bottom; +const margin = { top: 40, right: 40, bottom: 40, left: 40 }; + +function getSizeModifier(galaxyFrontend: galaxy.Frontend) { + return Math.sqrt(galaxyFrontend.galaxySize); +} + +export function initViz(galaxyFrontend: galaxy.Frontend) { + const sizeModifier = getSizeModifier(galaxyFrontend); + const width = galaxyFrontend.galaxySize + margin.left + margin.right; + const height = galaxyFrontend.galaxySize + margin.top + margin.bottom; + + // remove old svg + d3.select("#dataviz svg").remove(); // append the svg object to the body of the page const svg = d3 .select("#dataviz") .append("svg") - .attr("width", width + margin.left + margin.right) - .attr("height", height + margin.top + margin.bottom) + .attr("width", width * sizeModifier + margin.left + margin.right) + .attr("height", height * sizeModifier + margin.top + margin.bottom) .append("g") .attr("transform", `translate(${margin.left}, ${margin.top})`); - //Read the data - d3.csv("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/2_TwoNum.csv").then( function(data) { - - // Add X axis - const x = d3.scaleLinear() - .domain([0, 4000]) - .range([ 0, width ]); - svg.append("g") - .attr("transform", `translate(0, ${height})`) - .call(d3.axisBottom(x)); - - // Add Y axis - const y = d3.scaleLinear() - .domain([0, 500000]) - .range([ height, 0]); - svg.append("g") - .call(d3.axisLeft(y)); - - // Add dots - svg.append('g') - .selectAll("dot") - .data(data) - .join("circle") - .attr("cx", function (d) { return x(d.GrLivArea); } ) - .attr("cy", function (d) { return y(d.SalePrice); } ) - .attr("r", 1.5) - .style("fill", "#69b3a2") - }); + // Add X axis + const x = d3 + .scaleLinear() + .domain([0, galaxyFrontend.galaxySize]) + .range([0, galaxyFrontend.galaxySize * sizeModifier]); + svg + .append("g") + .attr( + "transform", + `translate(0, ${galaxyFrontend.galaxySize * sizeModifier})` + ) + .call(d3.axisBottom(x)); + + // Add Y axis + const y = d3 + .scaleLinear() + .domain([0, galaxyFrontend.galaxySize]) + .range([galaxyFrontend.galaxySize * sizeModifier, 0]); + svg.append("g").call(d3.axisLeft(y)); +} + +export function initData(galaxyFrontend: galaxy.Frontend) { + const sizeModifier = getSizeModifier(galaxyFrontend); + + // remove old data + d3.select("#dataviz svg circle").remove(); + + // append the svg object to the body of the page + const svg = d3.select("#dataviz svg"); + svg + .append("g") + .selectAll("dot") + .data(galaxyFrontend.cells()) + .join("circle") + .attr("cx", function (c: galaxy.Cell) { + return c.x * sizeModifier + margin.left; + }) + .attr("cy", function (c: galaxy.Cell) { + return c.y * sizeModifier + margin.top; + }) + .attr("r", function (c: galaxy.Cell) { + return c.mass; + }) + .style("fill", "#69b3a2"); } diff --git a/src/js/lib/galaxy.ts b/src/js/lib/galaxy.ts index 85e7ceb..b3f3866 100644 --- a/src/js/lib/galaxy.ts +++ b/src/js/lib/galaxy.ts @@ -1,6 +1,6 @@ import * as wasm from "galaxy_gen_backend/galaxy_gen_backend"; -interface Cell { +export interface Cell { mass: number; x: number; y: number; @@ -12,32 +12,33 @@ interface Cell { */ export class Frontend { private galaxy: wasm.Galaxy; // pointer to galaxy - private galaxySize: number; + public galaxySize: number; - constructor(galaxySize: number) { - this.galaxy = new wasm.Galaxy(galaxySize, 0); + constructor(galaxySize: number, minStarMass: number) { + this.galaxy = new wasm.Galaxy(galaxySize, 0, minStarMass); this.galaxySize = galaxySize; } public seed(additionalMass: number): void { - this.galaxy.seed(additionalMass); + this.galaxy = this.galaxy.seed(additionalMass); } public tick(gravityReach: number): void { - this.galaxy.tick(gravityReach); + this.galaxy = this.galaxy.tick(gravityReach); } public cells(): Cell[] { - // Uint16Array to list of numbers - let cells: Cell[] = []; - const mass = Array.from(this.galaxy.mass()); - mass.forEach((element, index) => { + const mass = this.galaxy.mass(); + const x = this.galaxy.x(); + const y = this.galaxy.y(); + const cells: Cell[] = []; + for (let i = 0; i < this.galaxySize ** 2; i++) { cells.push({ - mass: element, - x: index % this.galaxySize, - y: Math.floor(index / this.galaxySize), + mass: mass[i], + x: x[i], + y: y[i], }); - }); + } return cells; } } diff --git a/src/rust/cell.rs b/src/rust/cell.rs deleted file mode 100644 index 0496f96..0000000 --- a/src/rust/cell.rs +++ /dev/null @@ -1,77 +0,0 @@ -use crate::galaxy::Galaxy; - -// internal constants -impl Cell { - pub const TYPE_INDEX_GAS: u8 = 0; - pub const TYPE_INDEX_STAR: u8 = 2; -} - -// types -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct Cell { - pub mass: u16, - pub accel_mangitude: u16, - pub accel_degree: u16, -} - -// defaults -impl Default for Cell { - fn default() -> Cell { - Cell { - mass: 0, - accel_mangitude: 0, - accel_degree: 0, - } - } -} - -// public methods -impl Cell { - pub fn get_type(&self) -> u8 { - if self.mass < Galaxy::MIN_MASS_STAR { - return Cell::TYPE_INDEX_GAS; - } else { - return Cell::TYPE_INDEX_STAR; - } - } - pub fn is_gas(&self) -> bool { - return self.check_if_type(Cell::TYPE_INDEX_GAS); - } - pub fn is_star(&self) -> bool { - return self.check_if_type(Cell::TYPE_INDEX_STAR); - } -} - -// internal methods -impl Cell { - pub fn check_if_type(&self, type_index: u8) -> bool { - 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 >= Galaxy::MIN_MASS_STAR) { - return true; - } else { - return false; - } - } -} - -#[cfg(test)] -mod tests_cell_types { - use super::*; - #[test] - fn test_gas_cell() { - let cell = Cell { - mass: 1, - ..Default::default() - }; - assert_eq!(cell.is_gas(), true); - } - #[test] - fn test_star_cell() { - let cell = Cell { - 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 b16f0f9..af0cc8d 100644 --- a/src/rust/galaxy.rs +++ b/src/rust/galaxy.rs @@ -1,27 +1,56 @@ -use cell::*; use rand::Rng; use wasm_bindgen::prelude::*; +// types +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Cell { + pub mass: u16, + pub accel_mangitude: u16, + pub accel_degree: u16, + // Cells have an x and y position that is not stored here. + // It is generated by looking that the cell's index in the galaxy. +} + +// defaults +impl Default for Cell { + fn default() -> Cell { + Cell { + mass: 0, + accel_mangitude: 0, + accel_degree: 0, + } + } +} + // types #[wasm_bindgen] pub struct Galaxy { size: u16, cells: Vec, + min_star_mass: u16, +} + +// defaults +impl Galaxy { + pub const MAX_STAR_MASS: u16 = u16::MAX; + pub const TYPE_INDEX_GAS: u8 = 0; + pub const TYPE_INDEX_STAR: u8 = 2; } // public methods #[wasm_bindgen] impl Galaxy { #[wasm_bindgen(constructor)] - pub fn new(size: u16, mass: u16) -> Galaxy { + pub fn new(size: u16, cell_initial_mass: u16, min_star_mass: u16) -> Galaxy { // create a new galaxy // https://github.com/rustwasm/console_error_panic_hook#readme console_error_panic_hook::set_once(); return Galaxy { size, + min_star_mass, cells: vec![ Cell { - mass: mass, + mass: cell_initial_mass, ..Default::default() }; (size as u32).pow(2) as usize @@ -30,59 +59,78 @@ impl Galaxy { } pub fn seed(&self, additional: u16) -> Galaxy { // add mass to the galaxy - let mut next = Vec::with_capacity((self.size as usize).pow(2)); let mut rng = rand::thread_rng(); - for cell_index in 0..self.size.pow(2) { - let mass = self.cells[cell_index as usize].mass; - next.push(Cell { - mass: mass + rng.gen_range(0..additional + 1), - ..Default::default() - }); - } + let next: Vec = (0..self.size.pow(2)) + .map(|index| { + let mass = self.cells[index as usize].mass; + Cell { + mass: mass + rng.gen_range(0..additional + 1), + ..Default::default() + } + }) + .collect(); return Galaxy { size: self.size, cells: next, + min_star_mass: self.min_star_mass, }; } 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, reach); - } + let next: Vec = (0..self.size.pow(2)) + .map(|index| { + let _cell = self.cells[index as usize]; + let _neighbours = self.neighbours(index, reach); + Cell { + ..Default::default() + } + }) + .collect(); return Galaxy { size: self.size, cells: next, + min_star_mass: self.min_star_mass, }; } pub fn mass(&self) -> Vec { - // for every cell, get the mass - let mut mass = Vec::new(); - for cell in self.cells.iter() { - mass.push(cell.mass); - } - return mass; + return self.cells.iter().map(|cell| cell.mass).collect(); + } + pub fn x(&self) -> Vec { + return (0..self.size.pow(2)) + .map(|index| self.index_to_row_col(index).0) + .collect(); + } + pub fn y(&self) -> Vec { + return (0..self.size.pow(2)) + .map(|index| self.index_to_row_col(index).1) + .collect(); } } // private methods impl Galaxy { + fn star_reach_range(&self) -> u16 { + return self.size; + } + fn gas_reach_range(&self) -> u16 { + return (self.size as f64).sqrt() as u16; + } fn reach_of_type(&self, type_index: u8) -> u16 { match type_index { - Cell::TYPE_INDEX_GAS => self.size / Galaxy::GAS_REACH_MODIFIER + 1, - Cell::TYPE_INDEX_STAR => self.size, + Galaxy::TYPE_INDEX_STAR => self.star_reach_range(), + Galaxy::TYPE_INDEX_GAS => self.gas_reach_range(), _ => unreachable!(), } } fn neighbours_of_my_type(&self, index: u16) -> Vec<(u16, u16)> { - return self.neighbours_of_type(index, self.cells[index as usize].get_type()); + let type_index = self.get_type_index(self.cells[index as usize]); + return self.neighbours_of_type(index, type_index); } fn neighbours_of_type(&self, index: u16, type_index: u8) -> Vec<(u16, u16)> { let mut neighbours_of_type = Vec::new(); for neighbour in self.neighbours(index, self.reach_of_type(type_index)) { - if self.cells[index as usize].check_if_type(type_index) { + if self.check_if_type(self.cells[index as usize], type_index) { neighbours_of_type.push(neighbour); } } @@ -102,6 +150,22 @@ impl Galaxy { } return neighbours; } + fn get_type_index(&self, cell: Cell) -> u8 { + if cell.mass < self.min_star_mass { + return Galaxy::TYPE_INDEX_GAS; + } else { + return Galaxy::TYPE_INDEX_STAR; + } + } + fn check_if_type(&self, cell: Cell, type_index: u8) -> bool { + if (type_index == Galaxy::TYPE_INDEX_GAS) & (cell.mass < self.min_star_mass) { + return true; + } else if (type_index == Galaxy::TYPE_INDEX_STAR) & (cell.mass >= self.min_star_mass) { + return true; + } else { + return false; + } + } fn col_row_to_index(&self, col: u16, row: u16) -> u16 { return row * self.size + col; } @@ -139,19 +203,19 @@ mod tests_intial_generation { use super::*; #[test] fn test_inital_generation_no_panic() { - Galaxy::new(10, 0); + Galaxy::new(10, 0, 1000); } #[test] fn test_seed_no_panic() { - Galaxy::new(10, 0).seed(1); + Galaxy::new(10, 0, 1000).seed(1); } #[test] fn test_seed_tick_no_panic() { - Galaxy::new(10, 1).seed(1).tick(1); + Galaxy::new(10, 1, 1000).seed(1).tick(1); } #[test] fn test_seed_alters_data() { - let mut galaxy = Galaxy::new(10, 0); + let mut galaxy = Galaxy::new(10, 0, 1000); let cells_before = galaxy.cells.clone(); galaxy = galaxy.seed(1); let cells_after = galaxy.cells.clone(); @@ -159,7 +223,7 @@ mod tests_intial_generation { } #[test] fn test_seed_doesnt_alter_when_zero() { - let mut galaxy = Galaxy::new(10, 0); + let mut galaxy = Galaxy::new(10, 0, 1000); let cells_before = galaxy.cells.clone(); galaxy = galaxy.seed(0); let cells_after = galaxy.cells.clone(); @@ -167,7 +231,7 @@ mod tests_intial_generation { } #[test] fn test_seed_alters_data_twice() { - let mut galaxy = Galaxy::new(10, 0); + let mut galaxy = Galaxy::new(10, 0, 1000); let cells_first = galaxy.cells.clone(); galaxy = galaxy.seed(1); @@ -187,37 +251,37 @@ mod tests_indexing { use super::*; #[test] fn test_index_to_row_col_start() { - let galaxy = Galaxy::new(3, 0); + let galaxy = Galaxy::new(3, 0, 1000); assert_eq!(galaxy.index_to_row_col(0), (0, 0)); } #[test] fn test_col_row_to_index_start() { - let galaxy = Galaxy::new(3, 0); + let galaxy = Galaxy::new(3, 0, 1000); assert_eq!(galaxy.col_row_to_index(0, 0), 0); } #[test] fn test_index_to_row_col_center() { - let galaxy = Galaxy::new(3, 0); + let galaxy = Galaxy::new(3, 0, 1000); assert_eq!(galaxy.index_to_row_col(4), (1, 1)); } #[test] fn test_col_row_to_index_center() { - let galaxy = Galaxy::new(3, 0); + let galaxy = Galaxy::new(3, 0, 1000); assert_eq!(galaxy.col_row_to_index(1, 1), 4); } #[test] fn test_index_to_row_col_end() { - let galaxy = Galaxy::new(3, 0); + let galaxy = Galaxy::new(3, 0, 1000); assert_eq!(galaxy.index_to_row_col(8), (2, 2)); } #[test] fn test_col_row_to_index_end() { - let galaxy = Galaxy::new(3, 0); + let galaxy = Galaxy::new(3, 0, 1000); assert_eq!(galaxy.col_row_to_index(2, 2), 8); } #[test] fn index_to_row_col_first() { - let galaxy = Galaxy::new(3, 0); + let galaxy = Galaxy::new(3, 0, 1000); let index = 1; let (x, y) = galaxy.index_to_row_col(index); assert_eq!(x, 1); @@ -225,7 +289,7 @@ mod tests_indexing { } #[test] fn index_to_row_col_second() { - let galaxy = Galaxy::new(3, 0); + let galaxy = Galaxy::new(3, 0, 1000); let index = 2; let (x, y) = galaxy.index_to_row_col(index); assert_eq!(x, 2); @@ -233,7 +297,7 @@ mod tests_indexing { } #[test] fn test_index_edge_transform_top_right() { - let galaxy = Galaxy::new(3, 0); + let galaxy = Galaxy::new(3, 0, 1000); let index = 2; let (x, y) = galaxy.index_to_row_col(index); assert_eq!(galaxy.col_row_to_index(x, y), index); @@ -242,7 +306,7 @@ mod tests_indexing { } #[test] fn test_index_edge_transform_bottom_left() { - let galaxy = Galaxy::new(3, 0); + let galaxy = Galaxy::new(3, 0, 1000); let index = 6; let (x, y) = galaxy.index_to_row_col(index); assert_eq!(galaxy.col_row_to_index(x, y), index); @@ -255,53 +319,68 @@ mod tests_indexing { mod tests_neighbors_and_reach { use super::*; #[test] + fn test_mass() { + let galaxy = Galaxy::new(3, 0, 1000); + assert_eq!(galaxy.mass(), vec![0, 0, 0, 0, 0, 0, 0, 0, 0]); + } + #[test] + fn test_x() { + let galaxy = Galaxy::new(3, 0, 1000); + assert_eq!(galaxy.x(), vec![0, 1, 2, 0, 1, 2, 0, 1, 2]); + } + #[test] + fn test_y() { + let galaxy = Galaxy::new(3, 0, 1000); + assert_eq!(galaxy.y(), vec![0, 0, 0, 1, 1, 1, 2, 2, 2]); + } + #[test] fn test_reach_range_start_edge() { - let galaxy = Galaxy::new(3, 0); + let galaxy = Galaxy::new(3, 0, 1000); assert_eq!(galaxy.reach_range_start(0, 99), 0); } #[test] fn test_reach_range_start_overflow() { - let galaxy = Galaxy::new(3, 0); + let galaxy = Galaxy::new(3, 0, 1000); assert_eq!(galaxy.reach_range_start(1, 99), 0); } #[test] fn test_reach_range_start_contained() { - let galaxy = Galaxy::new(10, 0); + let galaxy = Galaxy::new(10, 0, 1000); assert_eq!(galaxy.reach_range_start(4, 2), 2); } #[test] fn test_reach_range_end_edge() { - let galaxy = Galaxy::new(3, 0); + let galaxy = Galaxy::new(3, 0, 1000); assert_eq!(galaxy.reach_range_end(2, 99), 2); } #[test] fn test_reach_range_end_overflow() { - let galaxy = Galaxy::new(3, 0); + let galaxy = Galaxy::new(3, 0, 1000); assert_eq!(galaxy.reach_range_end(0, 99), 2); } #[test] fn test_reach_range_end_contained() { - let galaxy = Galaxy::new(10, 0); + let galaxy = Galaxy::new(10, 0, 1000); assert_eq!(galaxy.reach_range_end(2, 2), 4); } #[test] fn test_neighbor_size() { - let galaxy = Galaxy::new(10, 0); + let galaxy = Galaxy::new(10, 0, 1000); assert_eq!(galaxy.neighbours(0, 1).len(), 3); } #[test] fn test_neighbor_size_larger() { - let galaxy = Galaxy::new(10, 0); + let galaxy = Galaxy::new(10, 0, 1000); assert_eq!(galaxy.neighbours(0, 2).len(), 8); } #[test] fn test_neighbor_size_center() { - let galaxy = Galaxy::new(3, 0); + let galaxy = Galaxy::new(3, 0, 1000); assert_eq!(galaxy.neighbours(4, 1).len(), 8); } #[test] fn test_neighbor_size_differs_for_large_galaxy() { - let mut galaxy = Galaxy::new(100, 0); + let mut galaxy = Galaxy::new(100, 0, 1000); let index = 0 as usize; // gas galaxy.cells[index] = Cell { @@ -311,7 +390,7 @@ mod tests_neighbors_and_reach { let gas_neighbours = galaxy.neighbours_of_my_type(0).len(); // star galaxy.cells[index] = Cell { - mass: 59999, + mass: 65535, ..Default::default() }; let star_neighbours = galaxy.neighbours_of_my_type(0).len(); @@ -319,7 +398,7 @@ mod tests_neighbors_and_reach { } #[test] fn test_neighbor_size_same_for_small_galaxy() { - let mut galaxy = Galaxy::new(1, 0); + let mut galaxy = Galaxy::new(1, 0, 1000); let index = 0 as usize; // gas galaxy.cells[index] = Cell { diff --git a/src/rust/galaxyecs.rs b/src/rust/galaxyecs.rs deleted file mode 100644 index 4dedba7..0000000 --- a/src/rust/galaxyecs.rs +++ /dev/null @@ -1,84 +0,0 @@ -use specs::VecStorage; - -pub struct StarMass(u32); - -#[derive(Component, Debug)] -#[storage(VecStorage)] -pub struct Position { - pub x: u16, - pub y: u16, -} - -#[derive(Component, Debug)] -#[storage(VecStorage)] -pub struct Mass { - pub value: u32, -} - -use specs::{Join, ReadStorage, System, WriteStorage}; - -struct Emmission; - -impl<'a> System<'a> for Emmission { - type SystemData = ReadStorage<'a, Position>; - - fn run(&mut self, position: Self::SystemData) { - for position in position.join() { - println!("at position ({}, {})", position.x, position.y); - } - } -} - -struct Movement; - -impl<'a> System<'a> for Movement { - type SystemData = (ReadStorage<'a, Mass>, WriteStorage<'a, Position>); - - fn run(&mut self, (velocity, mut position): Self::SystemData) { - for (velocity, position) in (&velocity, &mut position).join() { - println!("at position ({}, {})", position.x, position.y); - println!("with velocity {}", velocity.value); - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use specs::{Builder, DispatcherBuilder, RunNow, World}; - - #[test] - fn test_world() { - let mut world = World::new(); - world.register::(); - world.register::(); - world - .create_entity() - .with(Position { x: 50, y: 50 }) - .with(Mass { value: 100 }) - .build(); - let mut dispatcher = DispatcherBuilder::new() - .with(Emmission, "emmission", &[]) - .with(Movement, "movement", &["emmission"]) - .with(Emmission, "emmission_secondary", &["emmission"]) - .build(); - dispatcher.dispatch(&mut world.res); - world.maintain(); - } - #[test] - fn test_resource_modification() { - let mut world = World::new(); - world.add_resource(StarMass(1000)); - let mut delta = world.write_resource::(); - *delta = StarMass(500); - } - #[test] - fn test_system() { - let mut world = World::new(); - world.register::(); - world.register::(); - let mut emmission = Emmission; - emmission.run_now(&world.res); - world.maintain(); - } -} diff --git a/src/rust/lib.rs b/src/rust/lib.rs index 80e4f01..bda9cf8 100644 --- a/src/rust/lib.rs +++ b/src/rust/lib.rs @@ -5,14 +5,4 @@ extern crate rand; extern crate specs; extern crate wasm_bindgen; -pub mod cell; pub mod galaxy; -pub mod galaxyecs; - -use galaxy::Galaxy; - -// public constants -impl Galaxy { - pub const MIN_MASS_STAR: u16 = 10000; - pub const GAS_REACH_MODIFIER: u16 = 10; -}