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
93
94
95
96
97
98
99
100
101
102
use crate::*;
#[derive(Clone, Default, Debug)]
pub struct PhysicsMaterial {
pub id: Option<String>,
pub name: Option<String>,
pub asset: Option<Box<Asset>>,
pub common: PhysicsMaterialCommon,
pub technique: Vec<Technique>,
pub extra: Vec<Extra>,
}
impl PhysicsMaterial {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: Some(id.into()),
name: None,
asset: None,
common: Default::default(),
technique: vec![],
extra: vec![],
}
}
}
impl XNode for PhysicsMaterial {
const NAME: &'static str = "physics_material";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
Ok(PhysicsMaterial {
id: element.attr("id").map(Into::into),
name: element.attr("name").map(Into::into),
asset: Asset::parse_opt_box(&mut it)?,
common: parse_one(Technique::COMMON, &mut it, PhysicsMaterialCommon::parse)?,
technique: Technique::parse_list(&mut it)?,
extra: Extra::parse_many(it)?,
})
}
}
impl XNodeWrite for PhysicsMaterial {
fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
let mut e = Self::elem();
e.opt_attr("id", &self.id);
e.opt_attr("name", &self.name);
let e = e.start(w)?;
self.asset.write_to(w)?;
let common = ElemBuilder::new(Technique::COMMON).start(w)?;
self.common.write_to(w)?;
common.end(w)?;
self.technique.write_to(w)?;
self.extra.write_to(w)?;
e.end(w)
}
}
#[derive(Clone, Copy, Default, Debug)]
pub struct PhysicsMaterialCommon {
pub dynamic_friction: f32,
pub restitution: f32,
pub static_friction: f32,
}
impl PhysicsMaterialCommon {
fn parse(e: &Element) -> Result<Self> {
let mut it = e.children().peekable();
let res = PhysicsMaterialCommon {
dynamic_friction: parse_opt("dynamic_friction", &mut it, parse_elem)?.unwrap_or(0.),
restitution: parse_opt("restitution", &mut it, parse_elem)?.unwrap_or(0.),
static_friction: parse_opt("static_friction", &mut it, parse_elem)?.unwrap_or(0.),
};
finish(res, it)
}
}
impl XNodeWrite for PhysicsMaterialCommon {
fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
ElemBuilder::def_print("dynamic_friction", self.dynamic_friction, 0., w)?;
ElemBuilder::def_print("restitution", self.restitution, 0., w)?;
ElemBuilder::def_print("static_friction", self.static_friction, 0., w)
}
}