From eaa595b963fb5584a64cbafc1c25946dbb8033fc Mon Sep 17 00:00:00 2001 From: ModZero Date: Sun, 31 May 2020 18:05:26 +0200 Subject: [PATCH] Organize things a bit --- src/main.rs | 12 +----------- src/simulation.rs | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 66b9ac8..b8ff4d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,6 @@ extern crate rocket; extern crate serde; use std::sync::{Arc, Mutex}; -use std::thread; -use std::time::Duration; use rocket::State as RocketState; use rocket_contrib::templates::Template; @@ -29,15 +27,7 @@ fn simulation(s: WrappedState) { std::thread::spawn(move || { let mut simulation = Simulation::new(); - loop { - simulation.update(); - { - let mut state = s.lock().unwrap(); - (*state).iteration += 1; - (*state).objects = simulation.render(); - } - thread::sleep(Duration::from_secs(1)) - } + simulation.run(s); }); } diff --git a/src/simulation.rs b/src/simulation.rs index 2c58524..b78bce9 100644 --- a/src/simulation.rs +++ b/src/simulation.rs @@ -1,7 +1,10 @@ +use std::thread; +use std::time::Duration; + use legion::prelude::*; use rand::prelude::*; -use crate::state::Object; +use crate::state::{WrappedState, Object}; #[derive(Clone, Debug, PartialEq)] struct Name { @@ -21,7 +24,7 @@ struct Velocity { } pub struct Simulation { - world: World + world: World, } impl Simulation { @@ -55,7 +58,7 @@ impl Simulation { } } - pub fn render(&mut self) -> Vec { + fn render(&mut self) -> Vec { let mut result: Vec = vec![]; for (name, pos) in <(Read, Read)>::query().iter(&mut self.world) { @@ -64,4 +67,16 @@ impl Simulation { 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)) + } + } }