dae_parser/physics/
scene.rs1use crate::*;
2
3#[derive(Clone, Default, Debug)]
5pub struct PhysicsScene {
6 pub id: Option<String>,
8 pub name: Option<String>,
10 pub asset: Option<Box<Asset>>,
12 pub instance_force_field: Vec<Instance<ForceField>>,
14 pub instance_physics_model: Vec<Instance<PhysicsModel>>,
17 pub common: PhysicsSceneCommon,
20 pub technique: Vec<Technique>,
22 pub extra: Vec<Extra>,
24}
25
26impl XNode for PhysicsScene {
27 const NAME: &'static str = "physics_scene";
28 fn parse(element: &Element) -> Result<Self> {
29 debug_assert_eq!(element.name(), Self::NAME);
30 let mut it = element.children().peekable();
31 Ok(PhysicsScene {
32 id: element.attr("id").map(Into::into),
33 name: element.attr("name").map(Into::into),
34 asset: Asset::parse_opt_box(&mut it)?,
35 instance_force_field: Instance::parse_list(&mut it)?,
36 instance_physics_model: Instance::parse_list(&mut it)?,
37 common: parse_one(Technique::COMMON, &mut it, PhysicsSceneCommon::parse)?,
38 technique: Technique::parse_list(&mut it)?,
39 extra: Extra::parse_many(it)?,
40 })
41 }
42}
43
44impl XNodeWrite for PhysicsScene {
45 fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
46 let mut e = Self::elem();
47 e.opt_attr("id", &self.id);
48 e.opt_attr("name", &self.name);
49 let e = e.start(w)?;
50 self.asset.write_to(w)?;
51 self.instance_force_field.write_to(w)?;
52 self.instance_physics_model.write_to(w)?;
53 let common = ElemBuilder::new(Technique::COMMON).start(w)?;
54 self.common.write_to(w)?;
55 common.end(w)?;
56 self.technique.write_to(w)?;
57 self.extra.write_to(w)?;
58 e.end(w)
59 }
60}
61
62#[derive(Clone, Default, Debug)]
65pub struct PhysicsSceneCommon {
66 pub gravity: Option<Box<[f32; 3]>>,
71 pub time_step: Option<f32>,
74}
75
76impl PhysicsSceneCommon {
77 pub fn parse(e: &Element) -> Result<Self> {
80 let mut it = e.children().peekable();
81 let res = Self {
82 gravity: parse_opt("gravity", &mut it, parse_array_n)?,
83 time_step: parse_opt("time_step", &mut it, parse_elem)?,
84 };
85 finish(res, it)
86 }
87}
88
89impl XNodeWrite for PhysicsSceneCommon {
90 fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
91 opt(&self.gravity, |e| {
92 ElemBuilder::print_arr("gravity", &**e, w)
93 })?;
94 ElemBuilder::opt_print("time_step", &self.time_step, w)
95 }
96}
97
98#[derive(Clone, Default, Debug)]
102pub struct ForceField {
103 pub id: Option<String>,
105 pub name: Option<String>,
107 pub asset: Option<Box<Asset>>,
109 pub technique: Vec<Technique>,
113 pub extra: Vec<Extra>,
115}
116
117impl XNode for ForceField {
118 const NAME: &'static str = "force_field";
119 fn parse(element: &Element) -> Result<Self> {
120 debug_assert_eq!(element.name(), Self::NAME);
121 let mut it = element.children().peekable();
122 Ok(ForceField {
123 id: element.attr("id").map(Into::into),
124 name: element.attr("name").map(Into::into),
125 asset: Asset::parse_opt_box(&mut it)?,
126 technique: Technique::parse_list_n::<1>(&mut it)?,
127 extra: Extra::parse_many(it)?,
128 })
129 }
130}
131
132impl XNodeWrite for ForceField {
133 fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
134 let mut e = Self::elem();
135 e.opt_attr("id", &self.id);
136 e.opt_attr("name", &self.name);
137 let e = e.start(w)?;
138 self.asset.write_to(w)?;
139 self.technique.write_to(w)?;
140 self.extra.write_to(w)?;
141 e.end(w)
142 }
143}
144
145#[derive(Clone, Debug)]
147pub struct Attachment {
148 pub rigid_body: Url,
151 pub transform: Vec<RigidTransform>,
153 pub extra: Vec<Extra>,
155}
156
157impl Attachment {
158 pub fn new(rigid_body: Url) -> Self {
160 Self {
161 rigid_body,
162 transform: vec![],
163 extra: vec![],
164 }
165 }
166
167 pub const REF: &'static str = "ref_attachment";
169
170 pub(crate) fn write_inner<W: Write>(
171 &self,
172 mut e: ElemBuilder,
173 w: &mut XWriter<W>,
174 ) -> Result<()> {
175 e.print_attr("rigid_body", &self.rigid_body);
176 let e = e.start(w)?;
177 self.transform.write_to(w)?;
178 self.extra.write_to(w)?;
179 e.end(w)
180 }
181}
182
183impl XNode for Attachment {
184 const NAME: &'static str = "attachment";
185 fn parse(element: &Element) -> Result<Self> {
186 debug_assert!(element.name() == Self::NAME || element.name() == Self::REF);
187 let mut it = element.children().peekable();
188 Ok(Attachment {
189 rigid_body: parse_attr(element.attr("rigid_body"))?.ok_or("missing rigid_body attr")?,
190 transform: parse_list_many(&mut it, RigidTransform::parse)?,
191 extra: Extra::parse_many(it)?,
192 })
193 }
194}
195
196impl XNodeWrite for Attachment {
197 fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
198 self.write_inner(Self::elem(), w)
199 }
200}