Move the simulation out of the main file
This commit is contained in:
parent
8e5eaa6fc8
commit
ec238ff969
62
src/main.rs
62
src/main.rs
@ -3,41 +3,21 @@
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
|
||||
mod simulation;
|
||||
|
||||
use std::fmt::Write as FmtWrite;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use legion::prelude::*;
|
||||
use rand::prelude::*;
|
||||
use rocket::State;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
struct Name {
|
||||
name: String,
|
||||
}
|
||||
use simulation::{Object, Simulation};
|
||||
|
||||
#[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 {
|
||||
pub iteration: u64,
|
||||
pub objects: Vec<ObjectOutput>
|
||||
pub objects: Vec<Object>
|
||||
}
|
||||
|
||||
type WrappedState = Arc<Mutex<SimState>>;
|
||||
@ -59,41 +39,15 @@ fn index(sim: State<WrappedState>) -> String {
|
||||
|
||||
fn simulation(s: WrappedState) {
|
||||
std::thread::spawn(move || {
|
||||
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),
|
||||
},
|
||||
)
|
||||
}),
|
||||
);
|
||||
|
||||
let mut simulation = Simulation::new();
|
||||
|
||||
loop {
|
||||
let update_query = <(Write<Position>, Read<Velocity>)>::query();
|
||||
for (mut pos, vel) in update_query.iter(&mut world) {
|
||||
pos.x += vel.dx;
|
||||
pos.y += vel.dy;
|
||||
}
|
||||
|
||||
simulation.update();
|
||||
{
|
||||
let mut state = s.lock().unwrap();
|
||||
(*state).iteration += 1;
|
||||
let mut result: Vec<ObjectOutput> = vec![];
|
||||
|
||||
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 = simulation.render();
|
||||
}
|
||||
(*state).objects = result;
|
||||
|
||||
thread::sleep(Duration::from_secs(1))
|
||||
}
|
||||
});
|
||||
|
71
src/simulation.rs
Normal file
71
src/simulation.rs
Normal 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
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user