From b4b9dc1d550c64c27a765058863f5d27dd3dc94e Mon Sep 17 00:00:00 2001 From: ModZero Date: Sun, 31 May 2020 20:56:55 +0200 Subject: [PATCH] A bit more reorganizing, almost no sim code in main --- src/main.rs | 23 ++++++++--------------- src/simulation.rs | 33 ++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/main.rs b/src/main.rs index b8ff4d5..354f3b3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ extern crate rocket; extern crate serde; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use rocket::State as RocketState; use rocket_contrib::templates::Template; @@ -13,7 +13,7 @@ mod simulation; mod state; use simulation::Simulation; -use state::{State, WrappedState}; +use state::WrappedState; #[get("/")] fn index(sim: RocketState) -> Template { @@ -23,24 +23,17 @@ fn index(sim: RocketState) -> Template { Template::render("stuff", state.clone()) } -fn simulation(s: WrappedState) { - std::thread::spawn(move || { - let mut simulation = Simulation::new(); - - simulation.run(s); - }); -} - fn main() { - let state = Arc::new(Mutex::new(State { - iteration: 0, objects: vec![] - })); + let mut simulation = Simulation::new(); + let state = Arc::clone(&simulation.state); - simulation(Arc::clone(&state)); + std::thread::spawn(move || { + simulation.run(); + }); rocket::ignite() .attach(Template::fairing()) - .manage(Arc::clone(&state)) + .manage(state) .mount("/", routes![index]) .launch(); } diff --git a/src/simulation.rs b/src/simulation.rs index b78bce9..51dac1a 100644 --- a/src/simulation.rs +++ b/src/simulation.rs @@ -1,10 +1,12 @@ +use std::sync::{Arc, Mutex}; use std::thread; use std::time::Duration; + use legion::prelude::*; use rand::prelude::*; -use crate::state::{WrappedState, Object}; +use crate::state::{State, WrappedState, Object}; #[derive(Clone, Debug, PartialEq)] struct Name { @@ -25,6 +27,7 @@ struct Velocity { pub struct Simulation { world: World, + pub state: WrappedState, } impl Simulation { @@ -47,7 +50,13 @@ impl Simulation { }), ); - Self {world} + let mut result = Self {world, state: Arc::new(Mutex::new(State { + iteration: 0, objects: vec![] + }))}; + + result.render(); + + result } pub fn update(&mut self) { @@ -56,26 +65,24 @@ impl Simulation { pos.x += vel.dx; pos.y += vel.dy; } + self.state.lock().unwrap().iteration += 1; } - fn render(&mut self) -> Vec { - let mut result: Vec = vec![]; + fn render(&mut self) { + let mut state = self.state.lock().unwrap(); + + state.objects = vec![]; for (name, pos) in <(Read, Read)>::query().iter(&mut self.world) { - result.push(Object{ name: name.name.clone(), x: pos.x, y: pos.y }); + state.objects.push(Object{ name: name.name.clone(), x: pos.x, y: pos.y }); } - - result } - pub fn run(&mut self, wrapped_state: WrappedState) { + pub fn run(&mut self) { loop { self.update(); - { - let mut state = wrapped_state.lock().unwrap(); - (*state).iteration += 1; - (*state).objects = self.render(); - } + self.render(); + thread::sleep(Duration::from_secs(1)) } }