Move the simulation out of the main file

This commit is contained in:
Gender Shrapnel 2020-05-31 03:12:25 +02:00
parent 8e5eaa6fc8
commit ec238ff969
2 changed files with 82 additions and 57 deletions

View File

@ -3,41 +3,21 @@
#[macro_use] #[macro_use]
extern crate rocket; extern crate rocket;
mod simulation;
use std::fmt::Write as FmtWrite; use std::fmt::Write as FmtWrite;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
use legion::prelude::*;
use rand::prelude::*;
use rocket::State; use rocket::State;
#[derive(Clone, Debug, PartialEq)] use simulation::{Object, Simulation};
struct Name {
name: String,
}
#[derive(Clone, Copy, Debug, PartialEq)]
struct Position {
x: f64,
y: f64,
}
#[derive(Clone, Copy, Debug, PartialEq)]
struct Velocity {
dx: f64,
dy: f64,
}
struct ObjectOutput {
pub name: String,
pub x: f64,
pub y: f64,
}
struct SimState { struct SimState {
pub iteration: u64, pub iteration: u64,
pub objects: Vec<ObjectOutput> pub objects: Vec<Object>
} }
type WrappedState = Arc<Mutex<SimState>>; type WrappedState = Arc<Mutex<SimState>>;
@ -59,41 +39,15 @@ fn index(sim: State<WrappedState>) -> String {
fn simulation(s: WrappedState) { fn simulation(s: WrappedState) {
std::thread::spawn(move || { std::thread::spawn(move || {
let universe = Universe::new(); let mut simulation = Simulation::new();
let mut world = universe.create_world();
let mut rng = thread_rng();
world.insert(
(),
(0..999).map(|n| {
(
Name { name: format!("Entity {}", n)},
Position { x: 0.0, y: 0.0 },
Velocity {
dx: rng.gen_range(0.0, 1.0),
dy: rng.gen_range(0.0, 1.0),
},
)
}),
);
loop { loop {
let update_query = <(Write<Position>, Read<Velocity>)>::query(); simulation.update();
for (mut pos, vel) in update_query.iter(&mut world) { {
pos.x += vel.dx;
pos.y += vel.dy;
}
let mut state = s.lock().unwrap(); let mut state = s.lock().unwrap();
(*state).iteration += 1; (*state).iteration += 1;
let mut result: Vec<ObjectOutput> = vec![]; (*state).objects = simulation.render();
for (name, pos) in <(Read<Name>, Read<Position>)>::query().iter(&mut world) {
result.push(ObjectOutput{ name: name.name.clone(), x: pos.x, y: pos.y });
} }
(*state).objects = result;
thread::sleep(Duration::from_secs(1)) thread::sleep(Duration::from_secs(1))
} }
}); });

71
src/simulation.rs Normal file
View File

@ -0,0 +1,71 @@
use legion::prelude::*;
use rand::prelude::*;
#[derive(Clone, Debug, PartialEq)]
struct Name {
name: String,
}
#[derive(Clone, Copy, Debug, PartialEq)]
struct Position {
x: f64,
y: f64,
}
#[derive(Clone, Copy, Debug, PartialEq)]
struct Velocity {
dx: f64,
dy: f64,
}
pub struct Simulation {
world: World
}
pub struct Object {
pub name: String,
pub x: f64,
pub y: f64,
}
impl Simulation {
pub fn new() -> Simulation {
let universe = Universe::new();
let mut world = universe.create_world();
let mut rng = thread_rng();
world.insert(
(),
(0..999).map(|n| {
(
Name { name: format!("Entity {}", n)},
Position { x: 0.0, y: 0.0 },
Velocity {
dx: rng.gen_range(0.0, 1.0),
dy: rng.gen_range(0.0, 1.0),
},
)
}),
);
Simulation {world}
}
pub fn update(&mut self) {
let update_query = <(Write<Position>, Read<Velocity>)>::query();
for (mut pos, vel) in update_query.iter(&mut self.world) {
pos.x += vel.dx;
pos.y += vel.dy;
}
}
pub fn render(&mut self) -> Vec<Object> {
let mut result: Vec<Object> = vec![];
for (name, pos) in <(Read<Name>, Read<Position>)>::query().iter(&mut self.world) {
result.push(Object{ name: name.name.clone(), x: pos.x, y: pos.y });
}
result
}
}