add frequency control for sim - once per second

This commit is contained in:
Oresavna
2021-05-22 11:18:59 +02:00
committed by Ludvig Böklin
parent bcb62df809
commit 0338b67527
3 changed files with 21 additions and 11 deletions

View File

@ -1,13 +1,13 @@
#![allow(dead_code)]
use crate::windsim::WindSim;
use crate::windsim::{WindSim, MS_BETWEEN_TICKS};
use common::{
comp::{Pos, Vel},
resources::DeltaTime,
};
use common_ecs::{Job, Origin, Phase, System};
use rand::Rng;
use specs::{Read, Write};
use vek::*;
use rand::Rng;
/// This system updates the wind grid for the entire map
#[derive(Default)]
@ -22,25 +22,31 @@ impl<'a> System<'a> for Sys {
fn run(_job: &mut Job<Self>, (dt, mut windsim): Self::SystemData) {
let mut rng = rand::thread_rng();
// 1000 chunks
let wind_sources: Vec<(Pos, Vel)> = (0..1000)
.map(|y| {
.map(|vertical_chunk| {
let r1 = rng.gen_range(0.0..std::f32::consts::PI / 2.0);
let r2 = rng.gen_range(0.0..std::f32::consts::PI / 2.0);
let r = (r1 + r2) / 2.0;
(
Pos(Vec3 {
x: inline_tweak::tweak!(5000.0),
y: y as f32 * 32.0,
x: inline_tweak::tweak!(1000.0),
y: vertical_chunk as f32 * 32.0,
z: inline_tweak::tweak!(200.0),
}),
Vel(Vec3 {
x: inline_tweak::tweak!(30.0) * r.sin(),
y: inline_tweak::tweak!(30.0) * r.cos(),
z: inline_tweak::tweak!(30.0) * rng.gen_range(-0.25..0.25),
x: inline_tweak::tweak!(300.0) * r.sin(),
y: inline_tweak::tweak!(300.0) * r.cos(),
z: inline_tweak::tweak!(10.0) * rng.gen_range(-0.25..0.25),
}),
)
})
.collect::<Vec<_>>();
windsim.tick(wind_sources, &dt);
// If MS_BETWEEN_TICKS is 1000 it runs the sim once per second
if windsim.ms_since_update >= MS_BETWEEN_TICKS {
windsim.tick(wind_sources, &DeltaTime((MS_BETWEEN_TICKS / 1000) as f32));
} else {
windsim.ms_since_update += (dt.0 * 1000.0) as u32;
}
}
}

View File

@ -18,11 +18,13 @@ pub const GRID_SIZE: Vec3<usize> = Vec3 {
y: Y_SIZE,
z: Z_SIZE,
};
pub const MS_BETWEEN_TICKS: u32 = 1000;
#[derive(Default)]
pub struct WindSim {
grid: WindGrid,
blocks_per_cell: Vec3<u32>,
pub ms_since_update: u32,
}
impl WindSim {
@ -30,6 +32,7 @@ impl WindSim {
Self {
grid: WindGrid::default(),
blocks_per_cell: cell_size_in_blocks(world_size),
ms_since_update: MS_BETWEEN_TICKS,
}
}
@ -75,6 +78,7 @@ impl WindSim {
0.1,
true,
);
self.ms_since_update = 0;
}
}

View File

@ -1,8 +1,8 @@
#![allow(dead_code)]
use vek::*;
pub const X_SIZE: usize = 96;
pub const Y_SIZE: usize = 96;
pub const X_SIZE: usize = 256;
pub const Y_SIZE: usize = 256;
pub const Z_SIZE: usize = 4;
pub const SIZE_1D: usize = (X_SIZE + 2) * (Y_SIZE + 2) * (Z_SIZE + 2);