pause simulation, instead of resetting
@@ -24,15 +24,17 @@ pub struct Props {
pub struct Simulation {
boids: Vec<Boid>,
interval: Interval,
+ settings: Settings,
+ generation: usize,
}
impl Component for Simulation {
type Message = Msg;
type Properties = Props;
fn create(ctx: &Context<Self>) -> Self {
- let settings = &ctx.props().settings;
+ let settings = ctx.props().settings.clone();
let boids = (0..settings.boids)
- .map(|_| Boid::new_random(settings))
+ .map(|_| Boid::new_random(&settings))
.collect();
let interval = {
@@ -42,7 +44,13 @@ impl Component for Simulation {
})
};
- Self { boids, interval }
+ let generation = ctx.props().generation;
+ Self {
+ boids,
+ interval,
+ settings,
+ generation,
+ }
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
@@ -65,9 +73,14 @@ impl Component for Simulation {
}
fn changed(&mut self, ctx: &Context<Self>) -> bool {
+ let props = ctx.props();
+ let should_reset = self.settings != props.settings || self.generation != props.generation;
+ self.settings = props.settings.clone();
+ self.generation = props.generation;
+ if should_reset {
self.boids.clear();
- let settings = &ctx.props().settings;
+ let settings = &props.settings;
self.boids
.resize_with(settings.boids, || Boid::new_random(settings));
@@ -81,6 +94,9 @@ impl Component for Simulation {
};
true
+ } else {
+ false
+ }
}
fn view(&self, _ctx: &Context<Self>) -> Html {
