1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use super::time::InstantWrapperImpl;
use super::{InstantWrapperFactoryFn, World, WorldInteractorFactoryFn};
use crate::simulation::simulation_impl::world::builder::NphysicsWorldBuilder;
use crate::simulation::simulation_impl::SimulationImpl;
use crate::simulation::Simulation;
use crate::world_interactor::WorldInteractorImpl;
use nameof::name_of_type;
use std::fmt;
use std::fmt::Debug;
use std::time::Instant;
#[derive(Default)]
pub struct SimulationBuilder {
world: Option<Box<dyn World>>,
world_interactor_factory_fn: Option<Box<WorldInteractorFactoryFn>>,
instant_wrapper_factory_fn: Option<Box<InstantWrapperFactoryFn>>,
}
impl Debug for SimulationBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct(name_of_type!(SimulationBuilder))
.field("world", &self.world)
.finish()
}
}
impl SimulationBuilder {
pub fn new() -> SimulationBuilder {
SimulationBuilder::default()
}
pub fn world(&mut self, world: Box<dyn World>) {
self.world = Some(world)
}
pub fn world_interactor_factory_fn(
&mut self,
world_interactor_factory_fn: Box<WorldInteractorFactoryFn>,
) {
self.world_interactor_factory_fn = Some(world_interactor_factory_fn)
}
pub fn instant_wrapper_factory_fn(
&mut self,
instant_wrapper_factory_fn: Box<InstantWrapperFactoryFn>,
) {
self.instant_wrapper_factory_fn = Some(instant_wrapper_factory_fn)
}
pub fn build(self) -> Box<dyn Simulation> {
box SimulationImpl::new(
self.world
.unwrap_or_else(|| box NphysicsWorldBuilder::default().build()),
self.world_interactor_factory_fn
.unwrap_or_else(|| box |simulation| box WorldInteractorImpl::new(simulation)),
self.instant_wrapper_factory_fn
.unwrap_or_else(|| box || box InstantWrapperImpl::new(Instant::now())),
)
}
}