A bit more reorganizing, almost no sim code in main

This commit is contained in:
Gender Shrapnel 2020-05-31 20:56:55 +02:00
parent eaa595b963
commit b4b9dc1d55
2 changed files with 28 additions and 28 deletions

View File

@ -4,7 +4,7 @@
extern crate rocket; extern crate rocket;
extern crate serde; extern crate serde;
use std::sync::{Arc, Mutex}; use std::sync::Arc;
use rocket::State as RocketState; use rocket::State as RocketState;
use rocket_contrib::templates::Template; use rocket_contrib::templates::Template;
@ -13,7 +13,7 @@ mod simulation;
mod state; mod state;
use simulation::Simulation; use simulation::Simulation;
use state::{State, WrappedState}; use state::WrappedState;
#[get("/")] #[get("/")]
fn index(sim: RocketState<WrappedState>) -> Template { fn index(sim: RocketState<WrappedState>) -> Template {
@ -23,24 +23,17 @@ fn index(sim: RocketState<WrappedState>) -> Template {
Template::render("stuff", state.clone()) Template::render("stuff", state.clone())
} }
fn simulation(s: WrappedState) {
std::thread::spawn(move || {
let mut simulation = Simulation::new();
simulation.run(s);
});
}
fn main() { fn main() {
let state = Arc::new(Mutex::new(State { let mut simulation = Simulation::new();
iteration: 0, objects: vec![] let state = Arc::clone(&simulation.state);
}));
simulation(Arc::clone(&state)); std::thread::spawn(move || {
simulation.run();
});
rocket::ignite() rocket::ignite()
.attach(Template::fairing()) .attach(Template::fairing())
.manage(Arc::clone(&state)) .manage(state)
.mount("/", routes![index]) .mount("/", routes![index])
.launch(); .launch();
} }

View File

@ -1,10 +1,12 @@
use std::sync::{Arc, Mutex};
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
use legion::prelude::*; use legion::prelude::*;
use rand::prelude::*; use rand::prelude::*;
use crate::state::{WrappedState, Object}; use crate::state::{State, WrappedState, Object};
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
struct Name { struct Name {
@ -25,6 +27,7 @@ struct Velocity {
pub struct Simulation { pub struct Simulation {
world: World, world: World,
pub state: WrappedState,
} }
impl Simulation { 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) { pub fn update(&mut self) {
@ -56,26 +65,24 @@ impl Simulation {
pos.x += vel.dx; pos.x += vel.dx;
pos.y += vel.dy; pos.y += vel.dy;
} }
self.state.lock().unwrap().iteration += 1;
} }
fn render(&mut self) -> Vec<Object> { fn render(&mut self) {
let mut result: Vec<Object> = vec![]; let mut state = self.state.lock().unwrap();
state.objects = 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) {
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 { loop {
self.update(); self.update();
{ self.render();
let mut state = wrapped_state.lock().unwrap();
(*state).iteration += 1;
(*state).objects = self.render();
}
thread::sleep(Duration::from_secs(1)) thread::sleep(Duration::from_secs(1))
} }
} }