A bit more reorganizing, almost no sim code in main
This commit is contained in:
parent
eaa595b963
commit
b4b9dc1d55
23
src/main.rs
23
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<WrappedState>) -> Template {
|
||||
@ -23,24 +23,17 @@ fn index(sim: RocketState<WrappedState>) -> 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();
|
||||
}
|
||||
|
@ -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<Object> {
|
||||
let mut result: Vec<Object> = vec![];
|
||||
fn render(&mut self) {
|
||||
let mut state = self.state.lock().unwrap();
|
||||
|
||||
state.objects = 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 });
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user