Smoooooth version of previous (and rust-fmt)
This commit is contained in:
parent
a000624a7e
commit
af5a529d48
2 changed files with 76 additions and 44 deletions
10
README.md
10
README.md
|
@ -22,3 +22,13 @@ npm run start
|
||||||
This command automatically watches the files, so you can build
|
This command automatically watches the files, so you can build
|
||||||
rust stuff and it will automatically appear.
|
rust stuff and it will automatically appear.
|
||||||
|
|
||||||
|
|
||||||
|
## Deploy
|
||||||
|
|
||||||
|
```
|
||||||
|
cd www
|
||||||
|
npm run build
|
||||||
|
```
|
||||||
|
|
||||||
|
Then copy the dist directory to a server.
|
||||||
|
|
||||||
|
|
110
src/lib.rs
110
src/lib.rs
|
@ -2,6 +2,7 @@ mod utils;
|
||||||
|
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
extern crate js_sys;
|
extern crate js_sys;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
|
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
|
||||||
// allocator.
|
// allocator.
|
||||||
|
@ -9,7 +10,6 @@ extern crate js_sys;
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
||||||
|
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub struct Universe {
|
pub struct Universe {
|
||||||
width: i32,
|
width: i32,
|
||||||
|
@ -18,6 +18,7 @@ pub struct Universe {
|
||||||
|
|
||||||
cells: Vec<u8>,
|
cells: Vec<u8>,
|
||||||
base: Vec<u8>,
|
base: Vec<u8>,
|
||||||
|
queue: HashSet<(i32, i32)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Universe {
|
impl Universe {
|
||||||
|
@ -26,59 +27,72 @@ impl Universe {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn in_bounds(&self, row: i32, col: i32) -> bool {
|
fn in_bounds(&self, row: i32, col: i32) -> bool {
|
||||||
0 <= row && row < self.height && 0 <= col && col < self.width
|
0 <= row && row < self.height && 0 <= col && col < self.width
|
||||||
}
|
}
|
||||||
|
|
||||||
fn incr_cell(&mut self, row: i32, col: i32) {
|
fn incr_cell(&mut self, row: i32, col: i32) {
|
||||||
if !self.in_bounds(row, col) { return; }
|
if !self.in_bounds(row, col) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let idx = self.get_index(row, col);
|
let idx = self.get_index(row, col);
|
||||||
self.base[idx] += 1;
|
self.base[idx] += 1;
|
||||||
|
|
||||||
if self.base[idx] >= 4 {
|
if self.base[idx] >= 4 {
|
||||||
self.cells[idx] = 100;
|
self.queue.insert((row, col));
|
||||||
self.base[idx] = 0;
|
}
|
||||||
for dc in [-1, 1].iter() {
|
|
||||||
let col2 = col + dc;
|
|
||||||
self.incr_cell(row, col2);
|
|
||||||
}
|
|
||||||
for dr in [-1, 1].iter() {
|
|
||||||
let row2 = row + dr;
|
|
||||||
self.incr_cell(row2, col);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn incr_random_cell(&mut self) {
|
fn incr_random_cell(&mut self) {
|
||||||
let row = (js_sys::Math::random() * self.height as f64) as i32;
|
let row = (js_sys::Math::random() * self.height as f64) as i32;
|
||||||
let col = (js_sys::Math::random() * self.width as f64) as i32;
|
let col = (js_sys::Math::random() * self.width as f64) as i32;
|
||||||
let idx = self.get_index(row, col);
|
let idx = self.get_index(row, col);
|
||||||
let count = self.base[idx];
|
let count = self.base[idx];
|
||||||
|
|
||||||
if js_sys::Math::random() < 1.0 / (count + 1) as f64 {
|
if js_sys::Math::random() < 1.0 / (count + 1) as f64 {
|
||||||
self.incr_cell(row, col);
|
self.incr_cell(row, col);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
impl Universe {
|
impl Universe {
|
||||||
pub fn tick(&mut self) {
|
pub fn tick(&mut self) {
|
||||||
self.stress = 0;
|
self.stress = 0;
|
||||||
|
|
||||||
for _i in 1..2 {
|
for _i in 1..2 {
|
||||||
self.incr_random_cell();
|
self.incr_random_cell();
|
||||||
}
|
}
|
||||||
|
|
||||||
for row in 0..self.height {
|
let queue_clone = self.queue.clone();
|
||||||
|
self.queue.clear();
|
||||||
|
for (row, col) in queue_clone {
|
||||||
|
let idx = self.get_index(row, col);
|
||||||
|
if self.base[idx] < 4 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.cells[idx] = 100;
|
||||||
|
self.base[idx] -= 4;
|
||||||
|
for dc in [-1, 1].iter() {
|
||||||
|
let col2 = col + dc;
|
||||||
|
self.incr_cell(row, col2);
|
||||||
|
}
|
||||||
|
for dr in [-1, 1].iter() {
|
||||||
|
let row2 = row + dr;
|
||||||
|
self.incr_cell(row2, col);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for row in 0..self.height {
|
||||||
for col in 0..self.width {
|
for col in 0..self.width {
|
||||||
let idx = self.get_index(row, col);
|
let idx = self.get_index(row, col);
|
||||||
|
|
||||||
self.stress += self.base[idx] as i32;
|
self.stress += self.base[idx] as i32;
|
||||||
|
|
||||||
if self.cells[idx] > 0 {
|
if self.cells[idx] > 0 {
|
||||||
self.cells[idx] -= 1;
|
self.cells[idx] -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -88,21 +102,29 @@ impl Universe {
|
||||||
let height = 81;
|
let height = 81;
|
||||||
let stress = 0;
|
let stress = 0;
|
||||||
|
|
||||||
let cells = (0..width * height)
|
let cells = (0..width * height).map(|_i| 0).collect();
|
||||||
.map(|_i| 0)
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let base = (0..width * height)
|
let base = (0..width * height).map(|_i| 0).collect();
|
||||||
.map(|_i| 0)
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
Universe {width, height, stress, cells, base}
|
let queue = HashSet::new();
|
||||||
|
|
||||||
|
Universe {
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
stress,
|
||||||
|
cells,
|
||||||
|
base,
|
||||||
|
queue,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(&mut self) {
|
pub fn init(&mut self) {
|
||||||
for _i in 1..50000 {
|
for _i in 1..50000 {
|
||||||
self.incr_random_cell();
|
self.incr_random_cell();
|
||||||
}
|
}
|
||||||
|
for _i in 1..1000 {
|
||||||
|
self.tick();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn width(&self) -> i32 {
|
pub fn width(&self) -> i32 {
|
||||||
|
|
Loading…
Add table
Reference in a new issue