2020-05-31 18:05:26 +02:00
|
|
|
use std::thread;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2020-05-31 03:12:25 +02:00
|
|
|
use legion::prelude::*;
|
|
|
|
use rand::prelude::*;
|
2020-05-31 05:29:13 +02:00
|
|
|
|
2020-05-31 18:05:26 +02:00
|
|
|
use crate::state::{WrappedState, Object};
|
2020-05-31 03:12:25 +02:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
struct Name {
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
struct Position {
|
|
|
|
x: f64,
|
|
|
|
y: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
struct Velocity {
|
|
|
|
dx: f64,
|
|
|
|
dy: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Simulation {
|
2020-05-31 18:05:26 +02:00
|
|
|
world: World,
|
2020-05-31 03:12:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Simulation {
|
2020-05-31 03:18:00 +02:00
|
|
|
pub fn new() -> Self {
|
2020-05-31 03:12:25 +02:00
|
|
|
let universe = Universe::new();
|
|
|
|
let mut world = universe.create_world();
|
|
|
|
let mut rng = thread_rng();
|
|
|
|
|
|
|
|
world.insert(
|
|
|
|
(),
|
|
|
|
(0..999).map(|n| {
|
|
|
|
(
|
|
|
|
Name { name: format!("Entity {}", n)},
|
|
|
|
Position { x: 0.0, y: 0.0 },
|
|
|
|
Velocity {
|
|
|
|
dx: rng.gen_range(0.0, 1.0),
|
|
|
|
dy: rng.gen_range(0.0, 1.0),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2020-05-31 03:18:00 +02:00
|
|
|
Self {world}
|
2020-05-31 03:12:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update(&mut self) {
|
|
|
|
let update_query = <(Write<Position>, Read<Velocity>)>::query();
|
|
|
|
for (mut pos, vel) in update_query.iter(&mut self.world) {
|
|
|
|
pos.x += vel.dx;
|
|
|
|
pos.y += vel.dy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 18:05:26 +02:00
|
|
|
fn render(&mut self) -> Vec<Object> {
|
2020-05-31 03:12:25 +02:00
|
|
|
let mut result: Vec<Object> = 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 });
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
2020-05-31 18:05:26 +02:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
2020-05-31 03:12:25 +02:00
|
|
|
}
|