Organize things a bit

This commit is contained in:
Gender Shrapnel 2020-05-31 18:05:26 +02:00
parent ceb26b40b1
commit eaa595b963
2 changed files with 19 additions and 14 deletions

View File

@ -5,8 +5,6 @@ extern crate rocket;
extern crate serde; extern crate serde;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
use rocket::State as RocketState; use rocket::State as RocketState;
use rocket_contrib::templates::Template; use rocket_contrib::templates::Template;
@ -29,15 +27,7 @@ fn simulation(s: WrappedState) {
std::thread::spawn(move || { std::thread::spawn(move || {
let mut simulation = Simulation::new(); let mut simulation = Simulation::new();
loop { simulation.run(s);
simulation.update();
{
let mut state = s.lock().unwrap();
(*state).iteration += 1;
(*state).objects = simulation.render();
}
thread::sleep(Duration::from_secs(1))
}
}); });
} }

View File

@ -1,7 +1,10 @@
use std::thread;
use std::time::Duration;
use legion::prelude::*; use legion::prelude::*;
use rand::prelude::*; use rand::prelude::*;
use crate::state::Object; use crate::state::{WrappedState, Object};
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
struct Name { struct Name {
@ -21,7 +24,7 @@ struct Velocity {
} }
pub struct Simulation { pub struct Simulation {
world: World world: World,
} }
impl Simulation { impl Simulation {
@ -55,7 +58,7 @@ impl Simulation {
} }
} }
pub fn render(&mut self) -> Vec<Object> { fn render(&mut self) -> Vec<Object> {
let mut result: Vec<Object> = vec![]; let mut result: Vec<Object> = vec![];
for (name, pos) in <(Read<Name>, Read<Position>)>::query().iter(&mut self.world) { for (name, pos) in <(Read<Name>, Read<Position>)>::query().iter(&mut self.world) {
@ -64,4 +67,16 @@ impl Simulation {
result result
} }
pub fn run(&mut self, wrapped_state: WrappedState) {
loop {
self.update();
{
let mut state = wrapped_state.lock().unwrap();
(*state).iteration += 1;
(*state).objects = self.render();
}
thread::sleep(Duration::from_secs(1))
}
}
} }